xref: /petsc/src/ts/interface/ts.c (revision cc9c3a59a6070fbcbd700c84769ddeb741478fac)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6d763cef2SBarry Smith 
7d5ba7fb7SMatthew Knepley /* Logging support */
8d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
9a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
10d405a339SMatthew Knepley 
11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1249354f04SShri Abhyankar 
13fde5950dSBarry Smith /*@C
14fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
15fde5950dSBarry Smith 
16fde5950dSBarry Smith    Collective on TS
17fde5950dSBarry Smith 
18fde5950dSBarry Smith    Input Parameters:
19fde5950dSBarry Smith +  ts - TS object you wish to monitor
20fde5950dSBarry Smith .  name - the monitor type one is seeking
21fde5950dSBarry Smith .  help - message indicating what monitoring is done
22fde5950dSBarry Smith .  manual - manual page for the monitor
23fde5950dSBarry Smith .  monitor - the monitor function
24fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
25fde5950dSBarry Smith 
26fde5950dSBarry Smith    Level: developer
27fde5950dSBarry Smith 
28fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
29fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
30fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
31fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
32fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
33fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
34fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
35fde5950dSBarry Smith @*/
36721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
37fde5950dSBarry Smith {
38fde5950dSBarry Smith   PetscErrorCode    ierr;
39fde5950dSBarry Smith   PetscViewer       viewer;
40fde5950dSBarry Smith   PetscViewerFormat format;
41fde5950dSBarry Smith   PetscBool         flg;
42fde5950dSBarry Smith 
43fde5950dSBarry Smith   PetscFunctionBegin;
44fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
45fde5950dSBarry Smith   if (flg) {
46721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
47721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
48721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
49fde5950dSBarry Smith     if (monitorsetup) {
50721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
51fde5950dSBarry Smith     }
52721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
53fde5950dSBarry Smith   }
54fde5950dSBarry Smith   PetscFunctionReturn(0);
55fde5950dSBarry Smith }
56fde5950dSBarry Smith 
572ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
582ffb9264SLisandro Dalcin {
592ffb9264SLisandro Dalcin   PetscErrorCode ierr;
602ffb9264SLisandro Dalcin 
612ffb9264SLisandro Dalcin   PetscFunctionBegin;
62b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
63b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
642ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
652ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
662ffb9264SLisandro Dalcin   }
672ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
682ffb9264SLisandro Dalcin }
692ffb9264SLisandro Dalcin 
70bdad233fSMatthew Knepley /*@
71bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
72bdad233fSMatthew Knepley 
73bdad233fSMatthew Knepley    Collective on TS
74bdad233fSMatthew Knepley 
75bdad233fSMatthew Knepley    Input Parameter:
76bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Options Database Keys:
79b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
80ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
81ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
82ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
83ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
84ef85077eSLisandro Dalcin .  -ts_final_time <time> - final time to compute to
853e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
86a3cdaa26SBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
87a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
88a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
89a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
90a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
91a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
92f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
93f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
94ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
95847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
96bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
97de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
98de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
997cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1006934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1018b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
102de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
103de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
104de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
105de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1063e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1073e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
108fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
109e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
110110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11153ea634cSHong Zhang 
11291b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley    Level: beginner
115bdad233fSMatthew Knepley 
116bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
117bdad233fSMatthew Knepley 
118a313700dSBarry Smith .seealso: TSGetType()
119bdad233fSMatthew Knepley @*/
1207087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
121bdad233fSMatthew Knepley {
122bc952696SBarry Smith   PetscBool              opt,flg,tflg;
123dfbe8321SBarry Smith   PetscErrorCode         ierr;
124eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12531748224SBarry Smith   PetscReal              time_step;
12649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
127d1212d36SBarry Smith   char                   dir[16];
128cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1296991f827SBarry Smith   const char             *defaultType;
1306991f827SBarry Smith   char                   typeName[256];
131bdad233fSMatthew Knepley 
132bdad233fSMatthew Knepley   PetscFunctionBegin;
1330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1346991f827SBarry Smith 
1356991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
136cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
137cd11d68dSLisandro Dalcin 
138cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
139cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
140cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
141cd11d68dSLisandro Dalcin   else
142cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
1436991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1446991f827SBarry Smith   if (opt) {
1456991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1466991f827SBarry Smith   } else {
1476991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1486991f827SBarry Smith   }
149bdad233fSMatthew Knepley 
150bdad233fSMatthew Knepley   /* Handle generic TS options */
151ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15219eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1530298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
154ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15531748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
156cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15749354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
15849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1590298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1600298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1610298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1630298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
164bdad233fSMatthew Knepley 
165f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);CHKERRQ(ierr);
166f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose","Test the RHS Jacobian transpose for consistency with RHS at each solve ","None",ts->testjacobiantranspose,&ts->testjacobiantranspose,NULL);CHKERRQ(ierr);
16756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
16856f85f32SBarry Smith   {
16956f85f32SBarry Smith   PetscBool set;
17056f85f32SBarry Smith   flg  = PETSC_FALSE;
17156f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
17256f85f32SBarry Smith   if (set) {
17356f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
17456f85f32SBarry Smith   }
17556f85f32SBarry Smith   }
17656f85f32SBarry Smith #endif
17756f85f32SBarry Smith 
178bdad233fSMatthew Knepley   /* Monitor options */
179cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
180*cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
181fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
182fde5950dSBarry Smith 
1835180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1845180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1855180491cSLisandro Dalcin 
1864f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
187b3603a34SBarry Smith   if (opt) {
1880b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1893923b477SBarry Smith     PetscInt       howoften = 1;
190b3603a34SBarry Smith 
1910298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
1926ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1934f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
194bdad233fSMatthew Knepley   }
1956ba87a44SLisandro Dalcin 
1964f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
197ef20d060SBarry Smith   if (opt) {
1980b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1993923b477SBarry Smith     PetscInt       howoften = 1;
200ef20d060SBarry Smith 
2010298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2026ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2034f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
204ef20d060SBarry Smith   }
205edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2067cf37e64SBarry Smith 
2076934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2086934998bSLisandro Dalcin   if (opt) {
2096934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2106934998bSLisandro Dalcin     PetscInt       howoften = 1;
2116934998bSLisandro Dalcin 
2126934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2136ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2146934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2156934998bSLisandro Dalcin   }
2168b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2178b668821SLisandro Dalcin   if (opt) {
2188b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2198b668821SLisandro Dalcin     PetscInt       howoften = 1;
2208b668821SLisandro Dalcin 
2218b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2228b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2238b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2248b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2258b668821SLisandro Dalcin   }
2268b668821SLisandro Dalcin 
227201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
228201da799SBarry Smith   if (opt) {
229201da799SBarry Smith     TSMonitorLGCtx ctx;
230201da799SBarry Smith     PetscInt       howoften = 1;
231201da799SBarry Smith 
2320298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2336ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
234201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
235201da799SBarry Smith   }
236201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
237201da799SBarry Smith   if (opt) {
238201da799SBarry Smith     TSMonitorLGCtx ctx;
239201da799SBarry Smith     PetscInt       howoften = 1;
240201da799SBarry Smith 
2410298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2426ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
243201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
244201da799SBarry Smith   }
2458189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2468189c53fSBarry Smith   if (opt) {
2478189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2488189c53fSBarry Smith     PetscInt          howoften = 1;
2498189c53fSBarry Smith 
2500298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2516934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2528189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2538189c53fSBarry Smith   }
254ef20d060SBarry Smith   opt  = PETSC_FALSE;
2550dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
256a7cc72afSBarry Smith   if (opt) {
25783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
25883a4ac43SBarry Smith     PetscInt         howoften = 1;
259a80ad3e0SBarry Smith 
2600298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2610ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
26283a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
263bdad233fSMatthew Knepley   }
264fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2652d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2662d5ee99bSBarry Smith   if (opt) {
2672d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2682d5ee99bSBarry Smith     PetscReal        bounds[4];
2692d5ee99bSBarry Smith     PetscInt         n = 4;
2702d5ee99bSBarry Smith     PetscDraw        draw;
2716934998bSLisandro Dalcin     PetscDrawAxis    axis;
2722d5ee99bSBarry Smith 
2732d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2742d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2756934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2762d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2776934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2786934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2796934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2802d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2812d5ee99bSBarry Smith   }
2822d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2830dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2843a471f94SBarry Smith   if (opt) {
28583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28683a4ac43SBarry Smith     PetscInt         howoften = 1;
2873a471f94SBarry Smith 
2880298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
2890ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2913a471f94SBarry Smith   }
2920ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
2930ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
2940ed3bfb6SBarry Smith   if (opt) {
2950ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
2960ed3bfb6SBarry Smith     PetscInt         howoften = 1;
2970ed3bfb6SBarry Smith 
2980ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
2990ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3000ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3010ed3bfb6SBarry Smith   }
302fde5950dSBarry Smith 
303ed81e22dSJed Brown   opt  = PETSC_FALSE;
30491b97e58SBarry 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);
305ed81e22dSJed Brown   if (flg) {
306ed81e22dSJed Brown     const char *ptr,*ptr2;
307ed81e22dSJed Brown     char       *filetemplate;
308ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
309ed81e22dSJed Brown     /* Do some cursory validation of the input. */
310ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
311ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
312ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
313ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
314ce94432eSBarry 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");
315ed81e22dSJed Brown       if (ptr2) break;
316ed81e22dSJed Brown     }
317ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
318ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
319ed81e22dSJed Brown   }
320bdad233fSMatthew Knepley 
321d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
322d1212d36SBarry Smith   if (flg) {
323d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
324d1212d36SBarry Smith     int                  ray = 0;
325d1212d36SBarry Smith     DMDADirection        ddir;
326d1212d36SBarry Smith     DM                   da;
327d1212d36SBarry Smith     PetscMPIInt          rank;
328d1212d36SBarry Smith 
329ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
330d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
331d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
332ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
333d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
334d1212d36SBarry Smith 
335d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
336b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
337d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
338d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
339ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
340d1212d36SBarry Smith     if (!rank) {
341d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
342d1212d36SBarry Smith     }
34351b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
344d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
345d1212d36SBarry Smith   }
34651b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
34751b4a12fSMatthew G. Knepley   if (flg) {
34851b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
34951b4a12fSMatthew G. Knepley     int                 ray = 0;
35051b4a12fSMatthew G. Knepley     DMDADirection       ddir;
35151b4a12fSMatthew G. Knepley     DM                  da;
35251b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
353d1212d36SBarry Smith 
35451b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
35551b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
35651b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
35751b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
35851b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3591c3436cfSJed Brown 
36051b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
361b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
36251b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
36351b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
36451b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
36551b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
36651b4a12fSMatthew G. Knepley   }
367a7a1495cSBarry Smith 
368b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
369b3d3934dSBarry Smith   if (opt) {
370b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
371b3d3934dSBarry Smith 
372b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
373b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
374b3d3934dSBarry Smith   }
375b3d3934dSBarry Smith 
376847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
377847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
378847ff0e1SMatthew G. Knepley   if (flg) {
379847ff0e1SMatthew G. Knepley     DM   dm;
380847ff0e1SMatthew G. Knepley     DMTS tdm;
381847ff0e1SMatthew G. Knepley 
382847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
383847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
384847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
385847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
386847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
387847ff0e1SMatthew G. Knepley   }
388847ff0e1SMatthew G. Knepley 
389d763cef2SBarry Smith   /* Handle specific TS options */
390d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3916991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
392d763cef2SBarry Smith   }
393fbc52257SHong Zhang 
394a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
395a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
396a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
397a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
398a7bdc993SLisandro Dalcin 
39968bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4004f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
40168bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
40268bece0bSHong Zhang   if (tflg) {
40368bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
40468bece0bSHong Zhang   }
405a05bf03eSHong Zhang 
406a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
407d763cef2SBarry Smith 
408d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4090633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
410fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
411d763cef2SBarry Smith 
4124f122a70SLisandro Dalcin   if (ts->trajectory) {
4134f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
414d763cef2SBarry Smith   }
41568bece0bSHong Zhang 
4164f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4174f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4184f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
419d763cef2SBarry Smith   PetscFunctionReturn(0);
420d763cef2SBarry Smith }
421d763cef2SBarry Smith 
422d2daff3dSHong Zhang /*@
42378fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
42478fbdcc8SBarry Smith 
42578fbdcc8SBarry Smith    Collective on TS
42678fbdcc8SBarry Smith 
42778fbdcc8SBarry Smith    Input Parameters:
42878fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
42978fbdcc8SBarry Smith 
43078fbdcc8SBarry Smith    Output Parameters;
43178fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
43278fbdcc8SBarry Smith 
43378fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
43478fbdcc8SBarry Smith 
43578fbdcc8SBarry Smith    Level: advanced
43678fbdcc8SBarry Smith 
43778fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
43878fbdcc8SBarry Smith 
43978fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
44078fbdcc8SBarry Smith @*/
44178fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
44278fbdcc8SBarry Smith {
44378fbdcc8SBarry Smith   PetscFunctionBegin;
44478fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44578fbdcc8SBarry Smith   *tr = ts->trajectory;
44678fbdcc8SBarry Smith   PetscFunctionReturn(0);
44778fbdcc8SBarry Smith }
44878fbdcc8SBarry Smith 
44978fbdcc8SBarry Smith /*@
450bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
451d2daff3dSHong Zhang 
452d2daff3dSHong Zhang    Collective on TS
453d2daff3dSHong Zhang 
454d2daff3dSHong Zhang    Input Parameters:
455bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
456bc952696SBarry Smith 
45778fbdcc8SBarry Smith    Options Database:
45878fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
45978fbdcc8SBarry Smith -  -ts_trajectory_type type
46078fbdcc8SBarry Smith 
46168bece0bSHong Zhang Note: This routine should be called after all TS options have been set
462d2daff3dSHong Zhang 
463c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
46478fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
46578fbdcc8SBarry Smith 
466d2daff3dSHong Zhang    Level: intermediate
467d2daff3dSHong Zhang 
46878fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
469d2daff3dSHong Zhang 
470bc952696SBarry Smith .keywords: TS, set, checkpoint,
471d2daff3dSHong Zhang @*/
472bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
473d2daff3dSHong Zhang {
474bc952696SBarry Smith   PetscErrorCode ierr;
475bc952696SBarry Smith 
476d2daff3dSHong Zhang   PetscFunctionBegin;
477d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
478bc952696SBarry Smith   if (!ts->trajectory) {
479bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
480bc952696SBarry Smith   }
481d2daff3dSHong Zhang   PetscFunctionReturn(0);
482d2daff3dSHong Zhang }
483d2daff3dSHong Zhang 
484a7a1495cSBarry Smith /*@
485a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
486a7a1495cSBarry Smith       set with TSSetRHSJacobian().
487a7a1495cSBarry Smith 
488a7a1495cSBarry Smith    Collective on TS and Vec
489a7a1495cSBarry Smith 
490a7a1495cSBarry Smith    Input Parameters:
491316643e7SJed Brown +  ts - the TS context
492a7a1495cSBarry Smith .  t - current timestep
4930910c330SBarry Smith -  U - input vector
494a7a1495cSBarry Smith 
495a7a1495cSBarry Smith    Output Parameters:
496a7a1495cSBarry Smith +  A - Jacobian matrix
497a7a1495cSBarry Smith .  B - optional preconditioning matrix
498a7a1495cSBarry Smith -  flag - flag indicating matrix structure
499a7a1495cSBarry Smith 
500a7a1495cSBarry Smith    Notes:
501a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
502a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
503a7a1495cSBarry Smith 
50494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
505a7a1495cSBarry Smith    flag parameter.
506a7a1495cSBarry Smith 
507a7a1495cSBarry Smith    Level: developer
508a7a1495cSBarry Smith 
509a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
510a7a1495cSBarry Smith 
51194b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
512a7a1495cSBarry Smith @*/
513d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
514a7a1495cSBarry Smith {
515dfbe8321SBarry Smith   PetscErrorCode   ierr;
516270bf2e7SJed Brown   PetscObjectState Ustate;
5176c1e1eecSBarry Smith   PetscObjectId    Uid;
51824989b8cSPeter Brune   DM               dm;
519942e3340SBarry Smith   DMTS             tsdm;
52024989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
52124989b8cSPeter Brune   void             *ctx;
52224989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
523b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
524a7a1495cSBarry Smith 
525a7a1495cSBarry Smith   PetscFunctionBegin;
5260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5280910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
52924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
530942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
53124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5320298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
533b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53459e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5356c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
5366c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5370e4ef248SJed Brown     PetscFunctionReturn(0);
538f8ede8e7SJed Brown   }
539d90be118SSean Farley 
540ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
541d90be118SSean Farley 
542e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
54394ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54494ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
545303f9b21SStefano Zampini     if (B && A != B) {
54694ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54794ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
548e1244c69SJed Brown     }
549e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
550e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
551e1244c69SJed Brown   }
552e1244c69SJed Brown 
55324989b8cSPeter Brune   if (rhsjacobianfunc) {
5546cd88445SBarry Smith     PetscBool missing;
55594ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
556a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
557d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
558a7a1495cSBarry Smith     PetscStackPop;
55994ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5606cd88445SBarry Smith     if (A) {
5616cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5626cd88445SBarry 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");
5636cd88445SBarry Smith     }
5646cd88445SBarry Smith     if (B && B != A) {
5656cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5666cd88445SBarry 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");
5676cd88445SBarry Smith     }
568ef66eb69SBarry Smith   } else {
56994ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
57094ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
571ef66eb69SBarry Smith   }
5720e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5737f79407eSBarry Smith   ierr                       = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
57459e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
575a7a1495cSBarry Smith   PetscFunctionReturn(0);
576a7a1495cSBarry Smith }
577a7a1495cSBarry Smith 
578316643e7SJed Brown /*@
579d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
580d763cef2SBarry Smith 
581316643e7SJed Brown    Collective on TS and Vec
582316643e7SJed Brown 
583316643e7SJed Brown    Input Parameters:
584316643e7SJed Brown +  ts - the TS context
585316643e7SJed Brown .  t - current time
5860910c330SBarry Smith -  U - state vector
587316643e7SJed Brown 
588316643e7SJed Brown    Output Parameter:
589316643e7SJed Brown .  y - right hand side
590316643e7SJed Brown 
591316643e7SJed Brown    Note:
592316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
593316643e7SJed Brown    is used internally within the nonlinear solvers.
594316643e7SJed Brown 
595316643e7SJed Brown    Level: developer
596316643e7SJed Brown 
597316643e7SJed Brown .keywords: TS, compute
598316643e7SJed Brown 
599316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
600316643e7SJed Brown @*/
6010910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
602d763cef2SBarry Smith {
603dfbe8321SBarry Smith   PetscErrorCode ierr;
60424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60524989b8cSPeter Brune   TSIFunction    ifunction;
60624989b8cSPeter Brune   void           *ctx;
60724989b8cSPeter Brune   DM             dm;
60824989b8cSPeter Brune 
609d763cef2SBarry Smith   PetscFunctionBegin;
6100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6110910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6120700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6150298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
616d763cef2SBarry Smith 
617ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
618d763cef2SBarry Smith 
6190910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
62024989b8cSPeter Brune   if (rhsfunction) {
621d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6220910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
623d763cef2SBarry Smith     PetscStackPop;
624214bc6a2SJed Brown   } else {
625214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
626b2cd27e8SJed Brown   }
62744a41b28SSean Farley 
6280910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
629d763cef2SBarry Smith   PetscFunctionReturn(0);
630d763cef2SBarry Smith }
631d763cef2SBarry Smith 
632ef20d060SBarry Smith /*@
633ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
634ef20d060SBarry Smith 
635ef20d060SBarry Smith    Collective on TS and Vec
636ef20d060SBarry Smith 
637ef20d060SBarry Smith    Input Parameters:
638ef20d060SBarry Smith +  ts - the TS context
639ef20d060SBarry Smith -  t - current time
640ef20d060SBarry Smith 
641ef20d060SBarry Smith    Output Parameter:
6420910c330SBarry Smith .  U - the solution
643ef20d060SBarry Smith 
644ef20d060SBarry Smith    Note:
645ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
646ef20d060SBarry Smith    is used internally within the nonlinear solvers.
647ef20d060SBarry Smith 
648ef20d060SBarry Smith    Level: developer
649ef20d060SBarry Smith 
650ef20d060SBarry Smith .keywords: TS, compute
651ef20d060SBarry Smith 
652abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
653ef20d060SBarry Smith @*/
6540910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
655ef20d060SBarry Smith {
656ef20d060SBarry Smith   PetscErrorCode     ierr;
657ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
658ef20d060SBarry Smith   void               *ctx;
659ef20d060SBarry Smith   DM                 dm;
660ef20d060SBarry Smith 
661ef20d060SBarry Smith   PetscFunctionBegin;
662ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
664ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
665ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
666ef20d060SBarry Smith 
667ef20d060SBarry Smith   if (solutionfunction) {
6689b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6690910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
670ef20d060SBarry Smith     PetscStackPop;
671ef20d060SBarry Smith   }
672ef20d060SBarry Smith   PetscFunctionReturn(0);
673ef20d060SBarry Smith }
6749b7cd975SBarry Smith /*@
6759b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6769b7cd975SBarry Smith 
6779b7cd975SBarry Smith    Collective on TS and Vec
6789b7cd975SBarry Smith 
6799b7cd975SBarry Smith    Input Parameters:
6809b7cd975SBarry Smith +  ts - the TS context
6819b7cd975SBarry Smith -  t - current time
6829b7cd975SBarry Smith 
6839b7cd975SBarry Smith    Output Parameter:
6849b7cd975SBarry Smith .  U - the function value
6859b7cd975SBarry Smith 
6869b7cd975SBarry Smith    Note:
6879b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6889b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6899b7cd975SBarry Smith 
6909b7cd975SBarry Smith    Level: developer
6919b7cd975SBarry Smith 
6929b7cd975SBarry Smith .keywords: TS, compute
6939b7cd975SBarry Smith 
6949b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6959b7cd975SBarry Smith @*/
6969b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6979b7cd975SBarry Smith {
6989b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6999b7cd975SBarry Smith   void               *ctx;
7009b7cd975SBarry Smith   DM                 dm;
7019b7cd975SBarry Smith 
7029b7cd975SBarry Smith   PetscFunctionBegin;
7039b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7049b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7059b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7069b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7079b7cd975SBarry Smith 
7089b7cd975SBarry Smith   if (forcing) {
7099b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7109b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7119b7cd975SBarry Smith     PetscStackPop;
7129b7cd975SBarry Smith   }
7139b7cd975SBarry Smith   PetscFunctionReturn(0);
7149b7cd975SBarry Smith }
715ef20d060SBarry Smith 
716214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
717214bc6a2SJed Brown {
7182dd45cf8SJed Brown   Vec            F;
719214bc6a2SJed Brown   PetscErrorCode ierr;
720214bc6a2SJed Brown 
721214bc6a2SJed Brown   PetscFunctionBegin;
7220298fd71SBarry Smith   *Frhs = NULL;
7230298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
724214bc6a2SJed Brown   if (!ts->Frhs) {
7252dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
726214bc6a2SJed Brown   }
727214bc6a2SJed Brown   *Frhs = ts->Frhs;
728214bc6a2SJed Brown   PetscFunctionReturn(0);
729214bc6a2SJed Brown }
730214bc6a2SJed Brown 
731214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
732214bc6a2SJed Brown {
733214bc6a2SJed Brown   Mat            A,B;
7342dd45cf8SJed Brown   PetscErrorCode ierr;
73541a1d4d2SBarry Smith   TSIJacobian    ijacobian;
736214bc6a2SJed Brown 
737214bc6a2SJed Brown   PetscFunctionBegin;
738c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
739c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
74041a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
741214bc6a2SJed Brown   if (Arhs) {
742214bc6a2SJed Brown     if (!ts->Arhs) {
74341a1d4d2SBarry Smith       if (ijacobian) {
744214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
74541a1d4d2SBarry Smith       } else {
74641a1d4d2SBarry Smith         ts->Arhs = A;
74741a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
74841a1d4d2SBarry Smith       }
7493565c898SBarry Smith     } else {
7503565c898SBarry Smith       PetscBool flg;
7513565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7523565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7533565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7543565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7553565c898SBarry Smith         ts->Arhs = A;
7563565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7573565c898SBarry Smith       }
758214bc6a2SJed Brown     }
759214bc6a2SJed Brown     *Arhs = ts->Arhs;
760214bc6a2SJed Brown   }
761214bc6a2SJed Brown   if (Brhs) {
762214bc6a2SJed Brown     if (!ts->Brhs) {
763bdb70873SJed Brown       if (A != B) {
76441a1d4d2SBarry Smith         if (ijacobian) {
765214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
766bdb70873SJed Brown         } else {
76741a1d4d2SBarry Smith           ts->Brhs = B;
76841a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
76941a1d4d2SBarry Smith         }
77041a1d4d2SBarry Smith       } else {
771bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
77251699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
773bdb70873SJed Brown       }
774214bc6a2SJed Brown     }
775214bc6a2SJed Brown     *Brhs = ts->Brhs;
776214bc6a2SJed Brown   }
777214bc6a2SJed Brown   PetscFunctionReturn(0);
778214bc6a2SJed Brown }
779214bc6a2SJed Brown 
780316643e7SJed Brown /*@
7810910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
782316643e7SJed Brown 
783316643e7SJed Brown    Collective on TS and Vec
784316643e7SJed Brown 
785316643e7SJed Brown    Input Parameters:
786316643e7SJed Brown +  ts - the TS context
787316643e7SJed Brown .  t - current time
7880910c330SBarry Smith .  U - state vector
7890910c330SBarry Smith .  Udot - time derivative of state vector
790214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
791316643e7SJed Brown 
792316643e7SJed Brown    Output Parameter:
793316643e7SJed Brown .  Y - right hand side
794316643e7SJed Brown 
795316643e7SJed Brown    Note:
796316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
797316643e7SJed Brown    is used internally within the nonlinear solvers.
798316643e7SJed Brown 
799316643e7SJed Brown    If the user did did not write their equations in implicit form, this
800316643e7SJed Brown    function recasts them in implicit form.
801316643e7SJed Brown 
802316643e7SJed Brown    Level: developer
803316643e7SJed Brown 
804316643e7SJed Brown .keywords: TS, compute
805316643e7SJed Brown 
806316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
807316643e7SJed Brown @*/
8080910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
809316643e7SJed Brown {
810316643e7SJed Brown   PetscErrorCode ierr;
81124989b8cSPeter Brune   TSIFunction    ifunction;
81224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
81324989b8cSPeter Brune   void           *ctx;
81424989b8cSPeter Brune   DM             dm;
815316643e7SJed Brown 
816316643e7SJed Brown   PetscFunctionBegin;
8170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8180910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8190910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8200700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
821316643e7SJed Brown 
82224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8240298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
82524989b8cSPeter Brune 
826ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
827d90be118SSean Farley 
8280910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
82924989b8cSPeter Brune   if (ifunction) {
830316643e7SJed Brown     PetscStackPush("TS user implicit function");
8310910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
832316643e7SJed Brown     PetscStackPop;
833214bc6a2SJed Brown   }
834214bc6a2SJed Brown   if (imex) {
83524989b8cSPeter Brune     if (!ifunction) {
8360910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8372dd45cf8SJed Brown     }
83824989b8cSPeter Brune   } else if (rhsfunction) {
83924989b8cSPeter Brune     if (ifunction) {
840214bc6a2SJed Brown       Vec Frhs;
841214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8420910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
843214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8442dd45cf8SJed Brown     } else {
8450910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8460910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
847316643e7SJed Brown     }
8484a6899ffSJed Brown   }
8490910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
850316643e7SJed Brown   PetscFunctionReturn(0);
851316643e7SJed Brown }
852316643e7SJed Brown 
853316643e7SJed Brown /*@
854316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
855316643e7SJed Brown 
856316643e7SJed Brown    Collective on TS and Vec
857316643e7SJed Brown 
858316643e7SJed Brown    Input
859316643e7SJed Brown       Input Parameters:
860316643e7SJed Brown +  ts - the TS context
861316643e7SJed Brown .  t - current timestep
8620910c330SBarry Smith .  U - state vector
8630910c330SBarry Smith .  Udot - time derivative of state vector
864214bc6a2SJed Brown .  shift - shift to apply, see note below
865214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
866316643e7SJed Brown 
867316643e7SJed Brown    Output Parameters:
868316643e7SJed Brown +  A - Jacobian matrix
8693565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
870316643e7SJed Brown 
871316643e7SJed Brown    Notes:
8720910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
873316643e7SJed Brown 
8740910c330SBarry Smith    dF/dU + shift*dF/dUdot
875316643e7SJed Brown 
876316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
877316643e7SJed Brown    is used internally within the nonlinear solvers.
878316643e7SJed Brown 
879316643e7SJed Brown    Level: developer
880316643e7SJed Brown 
881316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
882316643e7SJed Brown 
883316643e7SJed Brown .seealso:  TSSetIJacobian()
88463495f91SJed Brown @*/
885d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
886316643e7SJed Brown {
887316643e7SJed Brown   PetscErrorCode ierr;
88824989b8cSPeter Brune   TSIJacobian    ijacobian;
88924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
89024989b8cSPeter Brune   DM             dm;
89124989b8cSPeter Brune   void           *ctx;
892316643e7SJed Brown 
893316643e7SJed Brown   PetscFunctionBegin;
8940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8950910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8960910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
897316643e7SJed Brown   PetscValidPointer(A,6);
89894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
899316643e7SJed Brown   PetscValidPointer(B,7);
90094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
90124989b8cSPeter Brune 
90224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
90324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9040298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
90524989b8cSPeter Brune 
906ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
907316643e7SJed Brown 
90894ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
90924989b8cSPeter Brune   if (ijacobian) {
9106cd88445SBarry Smith     PetscBool missing;
911316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
912d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
913316643e7SJed Brown     PetscStackPop;
9146cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9156cd88445SBarry 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");
9163565c898SBarry Smith     if (B != A) {
9176cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9186cd88445SBarry 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");
9196cd88445SBarry Smith     }
9204a6899ffSJed Brown   }
921214bc6a2SJed Brown   if (imex) {
922b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9234c26be97Sstefano_zampini       PetscBool assembled;
92494ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9254c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9264c26be97Sstefano_zampini       if (!assembled) {
9274c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9284c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9294c26be97Sstefano_zampini       }
93094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93194ab13aaSBarry Smith       if (A != B) {
93294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9334c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9344c26be97Sstefano_zampini         if (!assembled) {
9354c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9364c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9374c26be97Sstefano_zampini         }
93894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
939214bc6a2SJed Brown       }
940214bc6a2SJed Brown     }
941214bc6a2SJed Brown   } else {
942e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
943e1244c69SJed Brown     if (rhsjacobian) {
944214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
945d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
946e1244c69SJed Brown     }
94794ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
9483565c898SBarry Smith       PetscBool flg;
949e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
950e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
9513565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
9523565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
9533565c898SBarry Smith       if (!flg) {
95494ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
95594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
9563565c898SBarry Smith       }
95794ab13aaSBarry Smith       if (A != B) {
95894ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
95994ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
960316643e7SJed Brown       }
961e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
962e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
963e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
96494ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
96594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
96694ab13aaSBarry Smith         if (A != B) {
96794ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
96894ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
969214bc6a2SJed Brown         }
970316643e7SJed Brown       }
97194ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
97294ab13aaSBarry Smith       if (A != B) {
97394ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
974316643e7SJed Brown       }
975316643e7SJed Brown     }
976316643e7SJed Brown   }
97794ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
978316643e7SJed Brown   PetscFunctionReturn(0);
979316643e7SJed Brown }
980316643e7SJed Brown 
981d763cef2SBarry Smith /*@C
982d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
983b5abc632SBarry Smith     where U_t = G(t,u).
984d763cef2SBarry Smith 
9853f9fe445SBarry Smith     Logically Collective on TS
986d763cef2SBarry Smith 
987d763cef2SBarry Smith     Input Parameters:
988d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9890298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
990d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
991d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9920298fd71SBarry Smith           function evaluation routine (may be NULL)
993d763cef2SBarry Smith 
994d763cef2SBarry Smith     Calling sequence of func:
99587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
996d763cef2SBarry Smith 
997d763cef2SBarry Smith +   t - current timestep
998d763cef2SBarry Smith .   u - input vector
999d763cef2SBarry Smith .   F - function vector
1000d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1001d763cef2SBarry Smith 
1002d763cef2SBarry Smith     Level: beginner
1003d763cef2SBarry Smith 
100495452b02SPatrick Sanan     Notes:
100595452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10062bbac0d3SBarry Smith 
1007d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1008d763cef2SBarry Smith 
1009ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1010d763cef2SBarry Smith @*/
1011089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1012d763cef2SBarry Smith {
1013089b2837SJed Brown   PetscErrorCode ierr;
1014089b2837SJed Brown   SNES           snes;
10150298fd71SBarry Smith   Vec            ralloc = NULL;
101624989b8cSPeter Brune   DM             dm;
1017d763cef2SBarry Smith 
1018089b2837SJed Brown   PetscFunctionBegin;
10190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1020ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
102124989b8cSPeter Brune 
102224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
102324989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1024089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1025e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1026e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1027e856ceecSJed Brown     r = ralloc;
1028e856ceecSJed Brown   }
1029089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1030e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1031d763cef2SBarry Smith   PetscFunctionReturn(0);
1032d763cef2SBarry Smith }
1033d763cef2SBarry Smith 
1034ef20d060SBarry Smith /*@C
1035abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1036ef20d060SBarry Smith 
1037ef20d060SBarry Smith     Logically Collective on TS
1038ef20d060SBarry Smith 
1039ef20d060SBarry Smith     Input Parameters:
1040ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1041ef20d060SBarry Smith .   f - routine for evaluating the solution
1042ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10430298fd71SBarry Smith           function evaluation routine (may be NULL)
1044ef20d060SBarry Smith 
1045ef20d060SBarry Smith     Calling sequence of func:
1046ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1047ef20d060SBarry Smith 
1048ef20d060SBarry Smith +   t - current timestep
1049ef20d060SBarry Smith .   u - output vector
1050ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1051ef20d060SBarry Smith 
10520ed3bfb6SBarry Smith     Options Database:
10530ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
10540ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
10550ed3bfb6SBarry Smith 
1056abd5a294SJed Brown     Notes:
1057abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1058abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1059abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1060abd5a294SJed Brown 
10614f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1062abd5a294SJed Brown 
1063ef20d060SBarry Smith     Level: beginner
1064ef20d060SBarry Smith 
1065ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1066ef20d060SBarry Smith 
10670ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1068ef20d060SBarry Smith @*/
1069ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1070ef20d060SBarry Smith {
1071ef20d060SBarry Smith   PetscErrorCode ierr;
1072ef20d060SBarry Smith   DM             dm;
1073ef20d060SBarry Smith 
1074ef20d060SBarry Smith   PetscFunctionBegin;
1075ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1076ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1077ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1078ef20d060SBarry Smith   PetscFunctionReturn(0);
1079ef20d060SBarry Smith }
1080ef20d060SBarry Smith 
10819b7cd975SBarry Smith /*@C
10829b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10839b7cd975SBarry Smith 
10849b7cd975SBarry Smith     Logically Collective on TS
10859b7cd975SBarry Smith 
10869b7cd975SBarry Smith     Input Parameters:
10879b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1088e162b725SBarry Smith .   func - routine for evaluating the forcing function
10899b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10900298fd71SBarry Smith           function evaluation routine (may be NULL)
10919b7cd975SBarry Smith 
10929b7cd975SBarry Smith     Calling sequence of func:
1093e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
10949b7cd975SBarry Smith 
10959b7cd975SBarry Smith +   t - current timestep
1096e162b725SBarry Smith .   f - output vector
10979b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10989b7cd975SBarry Smith 
10999b7cd975SBarry Smith     Notes:
11009b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1101e162b725SBarry 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
1102e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1103e162b725SBarry Smith 
1104e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1105e162b725SBarry Smith 
1106e162b725SBarry 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
1107e162b725SBarry Smith     parameters can be passed in the ctx variable.
11089b7cd975SBarry Smith 
11099b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11109b7cd975SBarry Smith 
11119b7cd975SBarry Smith     Level: beginner
11129b7cd975SBarry Smith 
11139b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11149b7cd975SBarry Smith 
11159b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11169b7cd975SBarry Smith @*/
1117e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11189b7cd975SBarry Smith {
11199b7cd975SBarry Smith   PetscErrorCode ierr;
11209b7cd975SBarry Smith   DM             dm;
11219b7cd975SBarry Smith 
11229b7cd975SBarry Smith   PetscFunctionBegin;
11239b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11249b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1125e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11269b7cd975SBarry Smith   PetscFunctionReturn(0);
11279b7cd975SBarry Smith }
11289b7cd975SBarry Smith 
1129d763cef2SBarry Smith /*@C
1130f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1131b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1132d763cef2SBarry Smith 
11333f9fe445SBarry Smith    Logically Collective on TS
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith    Input Parameters:
1136d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1137e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1138e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1139d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1140d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11410298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1142d763cef2SBarry Smith 
1143f7ab8db6SBarry Smith    Calling sequence of f:
1144ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1145d763cef2SBarry Smith 
1146d763cef2SBarry Smith +  t - current timestep
1147d763cef2SBarry Smith .  u - input vector
1148e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1149e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1150d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1151d763cef2SBarry Smith 
11526cd88445SBarry Smith    Notes:
11536cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11546cd88445SBarry Smith 
11556cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1156ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1157d763cef2SBarry Smith 
1158d763cef2SBarry Smith    Level: beginner
1159d763cef2SBarry Smith 
1160d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1161d763cef2SBarry Smith 
1162ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1163d763cef2SBarry Smith 
1164d763cef2SBarry Smith @*/
1165e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1166d763cef2SBarry Smith {
1167277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1168089b2837SJed Brown   SNES           snes;
116924989b8cSPeter Brune   DM             dm;
117024989b8cSPeter Brune   TSIJacobian    ijacobian;
1171277b19d0SLisandro Dalcin 
1172d763cef2SBarry Smith   PetscFunctionBegin;
11730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1174e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1175e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1176e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1177e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1178d763cef2SBarry Smith 
117924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1181e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1182e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1183e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1184e1244c69SJed Brown   }
11850298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1186089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11875f659677SPeter Brune   if (!ijacobian) {
1188e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11890e4ef248SJed Brown   }
1190e5d3d808SBarry Smith   if (Amat) {
1191e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11920e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1193e5d3d808SBarry Smith     ts->Arhs = Amat;
11940e4ef248SJed Brown   }
1195e5d3d808SBarry Smith   if (Pmat) {
1196e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11970e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1198e5d3d808SBarry Smith     ts->Brhs = Pmat;
11990e4ef248SJed Brown   }
1200d763cef2SBarry Smith   PetscFunctionReturn(0);
1201d763cef2SBarry Smith }
1202d763cef2SBarry Smith 
1203316643e7SJed Brown /*@C
1204b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1205316643e7SJed Brown 
12063f9fe445SBarry Smith    Logically Collective on TS
1207316643e7SJed Brown 
1208316643e7SJed Brown    Input Parameters:
1209316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12100298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1211316643e7SJed Brown .  f   - the function evaluation routine
12120298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1213316643e7SJed Brown 
1214316643e7SJed Brown    Calling sequence of f:
1215316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1216316643e7SJed Brown 
1217316643e7SJed Brown +  t   - time at step/stage being solved
1218316643e7SJed Brown .  u   - state vector
1219316643e7SJed Brown .  u_t - time derivative of state vector
1220316643e7SJed Brown .  F   - function vector
1221316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1222316643e7SJed Brown 
1223316643e7SJed Brown    Important:
12242bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1225316643e7SJed Brown 
1226316643e7SJed Brown    Level: beginner
1227316643e7SJed Brown 
1228316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1229316643e7SJed Brown 
1230d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1231316643e7SJed Brown @*/
123251699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1233316643e7SJed Brown {
1234089b2837SJed Brown   PetscErrorCode ierr;
1235089b2837SJed Brown   SNES           snes;
123651699248SLisandro Dalcin   Vec            ralloc = NULL;
123724989b8cSPeter Brune   DM             dm;
1238316643e7SJed Brown 
1239316643e7SJed Brown   PetscFunctionBegin;
12400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
124151699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
124224989b8cSPeter Brune 
124324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
124524989b8cSPeter Brune 
1246089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
124751699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
124851699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
124951699248SLisandro Dalcin     r  = ralloc;
1250e856ceecSJed Brown   }
125151699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
125251699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1253089b2837SJed Brown   PetscFunctionReturn(0);
1254089b2837SJed Brown }
1255089b2837SJed Brown 
1256089b2837SJed Brown /*@C
1257089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1258089b2837SJed Brown 
1259089b2837SJed Brown    Not Collective
1260089b2837SJed Brown 
1261089b2837SJed Brown    Input Parameter:
1262089b2837SJed Brown .  ts - the TS context
1263089b2837SJed Brown 
1264089b2837SJed Brown    Output Parameter:
12650298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12660298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12670298fd71SBarry Smith -  ctx - the function context (or NULL)
1268089b2837SJed Brown 
1269089b2837SJed Brown    Level: advanced
1270089b2837SJed Brown 
1271089b2837SJed Brown .keywords: TS, nonlinear, get, function
1272089b2837SJed Brown 
1273089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1274089b2837SJed Brown @*/
1275089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1276089b2837SJed Brown {
1277089b2837SJed Brown   PetscErrorCode ierr;
1278089b2837SJed Brown   SNES           snes;
127924989b8cSPeter Brune   DM             dm;
1280089b2837SJed Brown 
1281089b2837SJed Brown   PetscFunctionBegin;
1282089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1283089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12840298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
128524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1287089b2837SJed Brown   PetscFunctionReturn(0);
1288089b2837SJed Brown }
1289089b2837SJed Brown 
1290089b2837SJed Brown /*@C
1291089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1292089b2837SJed Brown 
1293089b2837SJed Brown    Not Collective
1294089b2837SJed Brown 
1295089b2837SJed Brown    Input Parameter:
1296089b2837SJed Brown .  ts - the TS context
1297089b2837SJed Brown 
1298089b2837SJed Brown    Output Parameter:
12990298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13000298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13010298fd71SBarry Smith -  ctx - the function context (or NULL)
1302089b2837SJed Brown 
1303089b2837SJed Brown    Level: advanced
1304089b2837SJed Brown 
1305089b2837SJed Brown .keywords: TS, nonlinear, get, function
1306089b2837SJed Brown 
13072bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1308089b2837SJed Brown @*/
1309089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1310089b2837SJed Brown {
1311089b2837SJed Brown   PetscErrorCode ierr;
1312089b2837SJed Brown   SNES           snes;
131324989b8cSPeter Brune   DM             dm;
1314089b2837SJed Brown 
1315089b2837SJed Brown   PetscFunctionBegin;
1316089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1317089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13180298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
131924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1321316643e7SJed Brown   PetscFunctionReturn(0);
1322316643e7SJed Brown }
1323316643e7SJed Brown 
1324316643e7SJed Brown /*@C
1325a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1326ae8867d6SBarry Smith         provided with TSSetIFunction().
1327316643e7SJed Brown 
13283f9fe445SBarry Smith    Logically Collective on TS
1329316643e7SJed Brown 
1330316643e7SJed Brown    Input Parameters:
1331316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1332e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1333e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1334316643e7SJed Brown .  f   - the Jacobian evaluation routine
13350298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1336316643e7SJed Brown 
1337316643e7SJed Brown    Calling sequence of f:
1338ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1339316643e7SJed Brown 
1340316643e7SJed Brown +  t    - time at step/stage being solved
13411b4a444bSJed Brown .  U    - state vector
13421b4a444bSJed Brown .  U_t  - time derivative of state vector
1343316643e7SJed Brown .  a    - shift
1344e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1345e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1346316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1347316643e7SJed Brown 
1348316643e7SJed Brown    Notes:
1349e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1350316643e7SJed Brown 
1351895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1352895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1353895c21f2SBarry Smith 
1354a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1355b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1356a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1357a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1358a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1359a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1360a4f0a591SBarry Smith 
13616cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13626cd88445SBarry Smith 
13636cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1364ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1365ca5f011dSBarry Smith 
1366316643e7SJed Brown    Level: beginner
1367316643e7SJed Brown 
1368316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1369316643e7SJed Brown 
1370ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1371316643e7SJed Brown 
1372316643e7SJed Brown @*/
1373e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1374316643e7SJed Brown {
1375316643e7SJed Brown   PetscErrorCode ierr;
1376089b2837SJed Brown   SNES           snes;
137724989b8cSPeter Brune   DM             dm;
1378316643e7SJed Brown 
1379316643e7SJed Brown   PetscFunctionBegin;
13800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1381e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1382e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1383e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1384e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
138524989b8cSPeter Brune 
138624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
138724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
138824989b8cSPeter Brune 
1389089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1390e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1391316643e7SJed Brown   PetscFunctionReturn(0);
1392316643e7SJed Brown }
1393316643e7SJed Brown 
1394e1244c69SJed Brown /*@
1395e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1396e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1397e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1398e1244c69SJed Brown    not been changed by the TS.
1399e1244c69SJed Brown 
1400e1244c69SJed Brown    Logically Collective
1401e1244c69SJed Brown 
1402e1244c69SJed Brown    Input Arguments:
1403e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1404e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1405e1244c69SJed Brown 
1406e1244c69SJed Brown    Level: intermediate
1407e1244c69SJed Brown 
1408e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1409e1244c69SJed Brown @*/
1410e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1411e1244c69SJed Brown {
1412e1244c69SJed Brown   PetscFunctionBegin;
1413e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1414e1244c69SJed Brown   PetscFunctionReturn(0);
1415e1244c69SJed Brown }
1416e1244c69SJed Brown 
1417efe9872eSLisandro Dalcin /*@C
1418efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1419efe9872eSLisandro Dalcin 
1420efe9872eSLisandro Dalcin    Logically Collective on TS
1421efe9872eSLisandro Dalcin 
1422efe9872eSLisandro Dalcin    Input Parameters:
1423efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1424efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1425efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1426efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1427efe9872eSLisandro Dalcin 
1428efe9872eSLisandro Dalcin    Calling sequence of fun:
1429efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1430efe9872eSLisandro Dalcin 
1431efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1432efe9872eSLisandro Dalcin .  U    - state vector
1433efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1434efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1435efe9872eSLisandro Dalcin .  F    - function vector
1436efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1437efe9872eSLisandro Dalcin 
1438efe9872eSLisandro Dalcin    Level: beginner
1439efe9872eSLisandro Dalcin 
1440efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1441efe9872eSLisandro Dalcin 
1442efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1443efe9872eSLisandro Dalcin @*/
1444efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1445efe9872eSLisandro Dalcin {
1446efe9872eSLisandro Dalcin   DM             dm;
1447efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1448efe9872eSLisandro Dalcin 
1449efe9872eSLisandro Dalcin   PetscFunctionBegin;
1450efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1451efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1452efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1453efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1454efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1455efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1456efe9872eSLisandro Dalcin }
1457efe9872eSLisandro Dalcin 
1458efe9872eSLisandro Dalcin /*@C
1459efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1460efe9872eSLisandro Dalcin 
1461efe9872eSLisandro Dalcin   Not Collective
1462efe9872eSLisandro Dalcin 
1463efe9872eSLisandro Dalcin   Input Parameter:
1464efe9872eSLisandro Dalcin . ts - the TS context
1465efe9872eSLisandro Dalcin 
1466efe9872eSLisandro Dalcin   Output Parameter:
1467efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1468efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1469efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1470efe9872eSLisandro Dalcin 
1471efe9872eSLisandro Dalcin   Level: advanced
1472efe9872eSLisandro Dalcin 
1473efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1474efe9872eSLisandro Dalcin 
1475efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1476efe9872eSLisandro Dalcin @*/
1477efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1478efe9872eSLisandro Dalcin {
1479efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1480efe9872eSLisandro Dalcin   SNES           snes;
1481efe9872eSLisandro Dalcin   DM             dm;
1482efe9872eSLisandro Dalcin 
1483efe9872eSLisandro Dalcin   PetscFunctionBegin;
1484efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1485efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1486efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1487efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1488efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1489efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1490efe9872eSLisandro Dalcin }
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin /*@C
1493bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1494efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1495efe9872eSLisandro Dalcin 
1496efe9872eSLisandro Dalcin    Logically Collective on TS
1497efe9872eSLisandro Dalcin 
1498efe9872eSLisandro Dalcin    Input Parameters:
1499efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1500efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1501efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1502efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1503efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1504efe9872eSLisandro Dalcin 
1505efe9872eSLisandro Dalcin    Calling sequence of jac:
1506bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1509efe9872eSLisandro Dalcin .  U    - state vector
1510efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1511efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1512efe9872eSLisandro Dalcin .  v    - shift for U_t
1513efe9872eSLisandro Dalcin .  a    - shift for U_tt
1514efe9872eSLisandro 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
1515efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1516efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1517efe9872eSLisandro Dalcin 
1518efe9872eSLisandro Dalcin    Notes:
1519efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1520efe9872eSLisandro Dalcin 
1521efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1522efe9872eSLisandro 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.
1523efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1524bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin    Level: beginner
1527efe9872eSLisandro Dalcin 
1528efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1531efe9872eSLisandro Dalcin @*/
1532efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1533efe9872eSLisandro Dalcin {
1534efe9872eSLisandro Dalcin   DM             dm;
1535efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1536efe9872eSLisandro Dalcin 
1537efe9872eSLisandro Dalcin   PetscFunctionBegin;
1538efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1539efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1540efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1541efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1542efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1543efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1544efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1545efe9872eSLisandro Dalcin }
1546efe9872eSLisandro Dalcin 
1547efe9872eSLisandro Dalcin /*@C
1548efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1549efe9872eSLisandro Dalcin 
1550efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1551efe9872eSLisandro Dalcin 
1552efe9872eSLisandro Dalcin   Input Parameter:
1553efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1554efe9872eSLisandro Dalcin 
1555efe9872eSLisandro Dalcin   Output Parameters:
1556efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1557efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1558efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1559efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1560efe9872eSLisandro Dalcin 
156195452b02SPatrick Sanan   Notes:
156295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin   Level: advanced
1565efe9872eSLisandro Dalcin 
156680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1567efe9872eSLisandro Dalcin 
1568efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1569efe9872eSLisandro Dalcin @*/
1570efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1571efe9872eSLisandro Dalcin {
1572efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1573efe9872eSLisandro Dalcin   SNES           snes;
1574efe9872eSLisandro Dalcin   DM             dm;
1575efe9872eSLisandro Dalcin 
1576efe9872eSLisandro Dalcin   PetscFunctionBegin;
1577efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1578efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1579efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1580efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1581efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1582efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1583efe9872eSLisandro Dalcin }
1584efe9872eSLisandro Dalcin 
1585efe9872eSLisandro Dalcin /*@
1586efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1587efe9872eSLisandro Dalcin 
1588efe9872eSLisandro Dalcin   Collective on TS and Vec
1589efe9872eSLisandro Dalcin 
1590efe9872eSLisandro Dalcin   Input Parameters:
1591efe9872eSLisandro Dalcin + ts - the TS context
1592efe9872eSLisandro Dalcin . t - current time
1593efe9872eSLisandro Dalcin . U - state vector
1594efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1595efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1596efe9872eSLisandro Dalcin 
1597efe9872eSLisandro Dalcin   Output Parameter:
1598efe9872eSLisandro Dalcin . F - the residual vector
1599efe9872eSLisandro Dalcin 
1600efe9872eSLisandro Dalcin   Note:
1601efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1602efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1603efe9872eSLisandro Dalcin 
1604efe9872eSLisandro Dalcin   Level: developer
1605efe9872eSLisandro Dalcin 
1606efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1607efe9872eSLisandro Dalcin 
1608efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1609efe9872eSLisandro Dalcin @*/
1610efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1611efe9872eSLisandro Dalcin {
1612efe9872eSLisandro Dalcin   DM             dm;
1613efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1614efe9872eSLisandro Dalcin   void           *ctx;
1615efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1616efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1617efe9872eSLisandro Dalcin 
1618efe9872eSLisandro Dalcin   PetscFunctionBegin;
1619efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1620efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1621efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1622efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1623efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1624efe9872eSLisandro Dalcin 
1625efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1626efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1627efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1628efe9872eSLisandro Dalcin 
1629efe9872eSLisandro Dalcin   if (!I2Function) {
1630efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1631efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1632efe9872eSLisandro Dalcin   }
1633efe9872eSLisandro Dalcin 
1634efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1635efe9872eSLisandro Dalcin 
1636efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1637efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1638efe9872eSLisandro Dalcin   PetscStackPop;
1639efe9872eSLisandro Dalcin 
1640efe9872eSLisandro Dalcin   if (rhsfunction) {
1641efe9872eSLisandro Dalcin     Vec Frhs;
1642efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1643efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1644efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1645efe9872eSLisandro Dalcin   }
1646efe9872eSLisandro Dalcin 
1647efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1648efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1649efe9872eSLisandro Dalcin }
1650efe9872eSLisandro Dalcin 
1651efe9872eSLisandro Dalcin /*@
1652efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1653efe9872eSLisandro Dalcin 
1654efe9872eSLisandro Dalcin   Collective on TS and Vec
1655efe9872eSLisandro Dalcin 
1656efe9872eSLisandro Dalcin   Input Parameters:
1657efe9872eSLisandro Dalcin + ts - the TS context
1658efe9872eSLisandro Dalcin . t - current timestep
1659efe9872eSLisandro Dalcin . U - state vector
1660efe9872eSLisandro Dalcin . V - time derivative of state vector
1661efe9872eSLisandro Dalcin . A - second time derivative of state vector
1662efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1663efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1664efe9872eSLisandro Dalcin 
1665efe9872eSLisandro Dalcin   Output Parameters:
1666efe9872eSLisandro Dalcin + J - Jacobian matrix
1667efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1668efe9872eSLisandro Dalcin 
1669efe9872eSLisandro Dalcin   Notes:
1670efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1673efe9872eSLisandro Dalcin 
1674efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1675efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1676efe9872eSLisandro Dalcin 
1677efe9872eSLisandro Dalcin   Level: developer
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1680efe9872eSLisandro Dalcin 
1681efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1682efe9872eSLisandro Dalcin @*/
1683efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1684efe9872eSLisandro Dalcin {
1685efe9872eSLisandro Dalcin   DM             dm;
1686efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1687efe9872eSLisandro Dalcin   void           *ctx;
1688efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1689efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1690efe9872eSLisandro Dalcin 
1691efe9872eSLisandro Dalcin   PetscFunctionBegin;
1692efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1693efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1694efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1695efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1696efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1697efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1700efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1702efe9872eSLisandro Dalcin 
1703efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1704efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1705efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1706efe9872eSLisandro Dalcin   }
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1711efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1712efe9872eSLisandro Dalcin   PetscStackPop;
1713efe9872eSLisandro Dalcin 
1714efe9872eSLisandro Dalcin   if (rhsjacobian) {
1715efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1716efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1717efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1718efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1719efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1720efe9872eSLisandro Dalcin   }
1721efe9872eSLisandro Dalcin 
1722efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1723efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1724efe9872eSLisandro Dalcin }
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin /*@
1727efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1728efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1729efe9872eSLisandro Dalcin 
1730efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin    Input Parameters:
1733efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1734efe9872eSLisandro Dalcin .  u - the solution vector
1735efe9872eSLisandro Dalcin -  v - the time derivative vector
1736efe9872eSLisandro Dalcin 
1737efe9872eSLisandro Dalcin    Level: beginner
1738efe9872eSLisandro Dalcin 
1739efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1740efe9872eSLisandro Dalcin @*/
1741efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1742efe9872eSLisandro Dalcin {
1743efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1744efe9872eSLisandro Dalcin 
1745efe9872eSLisandro Dalcin   PetscFunctionBegin;
1746efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1747efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1748efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1749efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1750efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin   ts->vec_dot = v;
1753efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1754efe9872eSLisandro Dalcin }
1755efe9872eSLisandro Dalcin 
1756efe9872eSLisandro Dalcin /*@
1757efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1758efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1759efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1760efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1761efe9872eSLisandro Dalcin 
1762efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1763efe9872eSLisandro Dalcin 
1764efe9872eSLisandro Dalcin    Input Parameter:
1765efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1766efe9872eSLisandro Dalcin 
1767efe9872eSLisandro Dalcin    Output Parameter:
1768efe9872eSLisandro Dalcin +  u - the vector containing the solution
1769efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1770efe9872eSLisandro Dalcin 
1771efe9872eSLisandro Dalcin    Level: intermediate
1772efe9872eSLisandro Dalcin 
1773efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1774efe9872eSLisandro Dalcin 
1775efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1776efe9872eSLisandro Dalcin @*/
1777efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1778efe9872eSLisandro Dalcin {
1779efe9872eSLisandro Dalcin   PetscFunctionBegin;
1780efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1781efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1782efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1783efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1784efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1785efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1786efe9872eSLisandro Dalcin }
1787efe9872eSLisandro Dalcin 
178855849f57SBarry Smith /*@C
178955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
179055849f57SBarry Smith 
179155849f57SBarry Smith   Collective on PetscViewer
179255849f57SBarry Smith 
179355849f57SBarry Smith   Input Parameters:
179455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
179555849f57SBarry Smith            some related function before a call to TSLoad().
179655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
179755849f57SBarry Smith 
179855849f57SBarry Smith    Level: intermediate
179955849f57SBarry Smith 
180055849f57SBarry Smith   Notes:
180155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
180255849f57SBarry Smith 
180355849f57SBarry Smith   Notes for advanced users:
180455849f57SBarry Smith   Most users should not need to know the details of the binary storage
180555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
180655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
180755849f57SBarry Smith   format is
180855849f57SBarry Smith .vb
180955849f57SBarry Smith      has not yet been determined
181055849f57SBarry Smith .ve
181155849f57SBarry Smith 
181255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
181355849f57SBarry Smith @*/
1814f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
181555849f57SBarry Smith {
181655849f57SBarry Smith   PetscErrorCode ierr;
181755849f57SBarry Smith   PetscBool      isbinary;
181855849f57SBarry Smith   PetscInt       classid;
181955849f57SBarry Smith   char           type[256];
18202d53ad75SBarry Smith   DMTS           sdm;
1821ad6bc421SBarry Smith   DM             dm;
182255849f57SBarry Smith 
182355849f57SBarry Smith   PetscFunctionBegin;
1824f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
182555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
182655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
182755849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
182855849f57SBarry Smith 
1829060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1830ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1831060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1832f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1833f2c2a1b9SBarry Smith   if (ts->ops->load) {
1834f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1835f2c2a1b9SBarry Smith   }
1836ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1837ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1838ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1839f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1840f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18412d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18422d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
184355849f57SBarry Smith   PetscFunctionReturn(0);
184455849f57SBarry Smith }
184555849f57SBarry Smith 
18469804daf3SBarry Smith #include <petscdraw.h>
1847e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1848e04113cfSBarry Smith #include <petscviewersaws.h>
1849f05ece33SBarry Smith #endif
18507e2c5f70SBarry Smith /*@C
1851d763cef2SBarry Smith     TSView - Prints the TS data structure.
1852d763cef2SBarry Smith 
18534c49b128SBarry Smith     Collective on TS
1854d763cef2SBarry Smith 
1855d763cef2SBarry Smith     Input Parameters:
1856d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1857d763cef2SBarry Smith -   viewer - visualization context
1858d763cef2SBarry Smith 
1859d763cef2SBarry Smith     Options Database Key:
1860d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1861d763cef2SBarry Smith 
1862d763cef2SBarry Smith     Notes:
1863d763cef2SBarry Smith     The available visualization contexts include
1864b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1865b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1866d763cef2SBarry Smith          output where only the first processor opens
1867d763cef2SBarry Smith          the file.  All other processors send their
1868d763cef2SBarry Smith          data to the first processor to print.
1869d763cef2SBarry Smith 
1870d763cef2SBarry Smith     The user can open an alternative visualization context with
1871b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1872d763cef2SBarry Smith 
1873d763cef2SBarry Smith     Level: beginner
1874d763cef2SBarry Smith 
1875d763cef2SBarry Smith .keywords: TS, timestep, view
1876d763cef2SBarry Smith 
1877b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1878d763cef2SBarry Smith @*/
18797087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1880d763cef2SBarry Smith {
1881dfbe8321SBarry Smith   PetscErrorCode ierr;
188219fd82e9SBarry Smith   TSType         type;
18832b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18842d53ad75SBarry Smith   DMTS           sdm;
1885e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1886536b137fSBarry Smith   PetscBool      issaws;
1887f05ece33SBarry Smith #endif
1888d763cef2SBarry Smith 
1889d763cef2SBarry Smith   PetscFunctionBegin;
18900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18913050cee2SBarry Smith   if (!viewer) {
1892ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
18933050cee2SBarry Smith   }
18940700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1895c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1896fd16b177SBarry Smith 
1897251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1898251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
189955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19002b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1901e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1902536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1903f05ece33SBarry Smith #endif
190432077d6dSBarry Smith   if (iascii) {
1905dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1906efd4aadfSBarry Smith     if (ts->ops->view) {
1907efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1908efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1909efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1910efd4aadfSBarry Smith     }
1911ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
191277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1913ef85077eSLisandro Dalcin     }
1914ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19157c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1916ef85077eSLisandro Dalcin     }
1917efd4aadfSBarry Smith     if (ts->usessnes) {
1918efd4aadfSBarry Smith       PetscBool lin;
1919d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19205ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1921d763cef2SBarry Smith       }
19225ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1923efd4aadfSBarry Smith       ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr);
1924efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1925efd4aadfSBarry Smith     }
1926193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1927a0af407cSBarry Smith     if (ts->vrtol) {
1928a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1929a0af407cSBarry Smith     } else {
1930a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1931a0af407cSBarry Smith     }
1932a0af407cSBarry Smith     if (ts->vatol) {
1933a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1934a0af407cSBarry Smith     } else {
1935a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1936a0af407cSBarry Smith     }
1937825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1938efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1939825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1940825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
1941825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1942825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
1943825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1944825ab935SBarry Smith     }
19452d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19462d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19470f5bd95cSBarry Smith   } else if (isstring) {
1948a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1949b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
195055849f57SBarry Smith   } else if (isbinary) {
195155849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
195255849f57SBarry Smith     MPI_Comm    comm;
195355849f57SBarry Smith     PetscMPIInt rank;
195455849f57SBarry Smith     char        type[256];
195555849f57SBarry Smith 
195655849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
195755849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
195855849f57SBarry Smith     if (!rank) {
195955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
196055849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
196155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
196255849f57SBarry Smith     }
196355849f57SBarry Smith     if (ts->ops->view) {
196455849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
196555849f57SBarry Smith     }
1966efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1967f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1968f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19692d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19702d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19712b0a91c0SBarry Smith   } else if (isdraw) {
19722b0a91c0SBarry Smith     PetscDraw draw;
19732b0a91c0SBarry Smith     char      str[36];
197489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19752b0a91c0SBarry Smith 
19762b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19772b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19782b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19792b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
198051fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
198189fd9fafSBarry Smith     bottom = y - h;
19822b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19832b0a91c0SBarry Smith     if (ts->ops->view) {
19842b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19852b0a91c0SBarry Smith     }
1986efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1987efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
19882b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1989e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1990536b137fSBarry Smith   } else if (issaws) {
1991d45a07a7SBarry Smith     PetscMPIInt rank;
19922657e9d9SBarry Smith     const char  *name;
19932657e9d9SBarry Smith 
19942657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1995d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1996d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1997d45a07a7SBarry Smith       char       dir[1024];
1998d45a07a7SBarry Smith 
1999e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2000a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20012657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20022657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20032657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2004d763cef2SBarry Smith     }
20050acecf5bSBarry Smith     if (ts->ops->view) {
20060acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20070acecf5bSBarry Smith     }
2008f05ece33SBarry Smith #endif
2009f05ece33SBarry Smith   }
2010f05ece33SBarry Smith 
2011b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2012251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2013b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2014d763cef2SBarry Smith   PetscFunctionReturn(0);
2015d763cef2SBarry Smith }
2016d763cef2SBarry Smith 
2017b07ff414SBarry Smith /*@
2018d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2019d763cef2SBarry Smith    the timesteppers.
2020d763cef2SBarry Smith 
20213f9fe445SBarry Smith    Logically Collective on TS
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith    Input Parameters:
2024d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2025d763cef2SBarry Smith -  usrP - optional user context
2026d763cef2SBarry Smith 
202795452b02SPatrick Sanan    Fortran Notes:
202895452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2029daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2030daf670e6SBarry Smith 
2031d763cef2SBarry Smith    Level: intermediate
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2034d763cef2SBarry Smith 
2035d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2036d763cef2SBarry Smith @*/
20377087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2038d763cef2SBarry Smith {
2039d763cef2SBarry Smith   PetscFunctionBegin;
20400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2041d763cef2SBarry Smith   ts->user = usrP;
2042d763cef2SBarry Smith   PetscFunctionReturn(0);
2043d763cef2SBarry Smith }
2044d763cef2SBarry Smith 
2045b07ff414SBarry Smith /*@
2046d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2047d763cef2SBarry Smith     timestepper.
2048d763cef2SBarry Smith 
2049d763cef2SBarry Smith     Not Collective
2050d763cef2SBarry Smith 
2051d763cef2SBarry Smith     Input Parameter:
2052d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith     Output Parameter:
2055d763cef2SBarry Smith .   usrP - user context
2056d763cef2SBarry Smith 
205795452b02SPatrick Sanan    Fortran Notes:
205895452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2059daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2060daf670e6SBarry Smith 
2061d763cef2SBarry Smith     Level: intermediate
2062d763cef2SBarry Smith 
2063d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2064d763cef2SBarry Smith 
2065d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2066d763cef2SBarry Smith @*/
2067e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2068d763cef2SBarry Smith {
2069d763cef2SBarry Smith   PetscFunctionBegin;
20700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2071e71120c6SJed Brown   *(void**)usrP = ts->user;
2072d763cef2SBarry Smith   PetscFunctionReturn(0);
2073d763cef2SBarry Smith }
2074d763cef2SBarry Smith 
2075d763cef2SBarry Smith /*@
207680275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith    Not Collective
2079d763cef2SBarry Smith 
2080d763cef2SBarry Smith    Input Parameter:
2081d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2082d763cef2SBarry Smith 
2083d763cef2SBarry Smith    Output Parameter:
208480275a0aSLisandro Dalcin .  steps - number of steps completed so far
2085d763cef2SBarry Smith 
2086d763cef2SBarry Smith    Level: intermediate
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20899be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2090d763cef2SBarry Smith @*/
209180275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2092d763cef2SBarry Smith {
2093d763cef2SBarry Smith   PetscFunctionBegin;
20940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
209580275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
209680275a0aSLisandro Dalcin   *steps = ts->steps;
209780275a0aSLisandro Dalcin   PetscFunctionReturn(0);
209880275a0aSLisandro Dalcin }
209980275a0aSLisandro Dalcin 
210080275a0aSLisandro Dalcin /*@
210180275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
210280275a0aSLisandro Dalcin 
210380275a0aSLisandro Dalcin    Logically Collective on TS
210480275a0aSLisandro Dalcin 
210580275a0aSLisandro Dalcin    Input Parameters:
210680275a0aSLisandro Dalcin +  ts - the TS context
210780275a0aSLisandro Dalcin -  steps - number of steps completed so far
210880275a0aSLisandro Dalcin 
210980275a0aSLisandro Dalcin    Notes:
211080275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
211180275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
211280275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
211380275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
211480275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
211580275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
211680275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
211780275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
211880275a0aSLisandro Dalcin 
211980275a0aSLisandro Dalcin    Level: advanced
212080275a0aSLisandro Dalcin 
212180275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
212280275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
212380275a0aSLisandro Dalcin @*/
212480275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
212580275a0aSLisandro Dalcin {
212680275a0aSLisandro Dalcin   PetscFunctionBegin;
212780275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
212880275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
212980275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
213080275a0aSLisandro Dalcin   ts->steps = steps;
2131d763cef2SBarry Smith   PetscFunctionReturn(0);
2132d763cef2SBarry Smith }
2133d763cef2SBarry Smith 
2134d763cef2SBarry Smith /*@
2135d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2136d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2137d763cef2SBarry Smith 
21383f9fe445SBarry Smith    Logically Collective on TS
2139d763cef2SBarry Smith 
2140d763cef2SBarry Smith    Input Parameters:
2141d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2142d763cef2SBarry Smith -  time_step - the size of the timestep
2143d763cef2SBarry Smith 
2144d763cef2SBarry Smith    Level: intermediate
2145d763cef2SBarry Smith 
2146aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2147d763cef2SBarry Smith 
2148d763cef2SBarry Smith .keywords: TS, set, timestep
2149d763cef2SBarry Smith @*/
21507087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2151d763cef2SBarry Smith {
2152d763cef2SBarry Smith   PetscFunctionBegin;
21530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2154c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2155d763cef2SBarry Smith   ts->time_step = time_step;
2156d763cef2SBarry Smith   PetscFunctionReturn(0);
2157d763cef2SBarry Smith }
2158d763cef2SBarry Smith 
2159a43b19c4SJed Brown /*@
216049354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
216149354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
216249354f04SShri Abhyankar      or just return at the final time TS computed.
2163a43b19c4SJed Brown 
2164a43b19c4SJed Brown   Logically Collective on TS
2165a43b19c4SJed Brown 
2166a43b19c4SJed Brown    Input Parameter:
2167a43b19c4SJed Brown +   ts - the time-step context
216849354f04SShri Abhyankar -   eftopt - exact final time option
2169a43b19c4SJed Brown 
2170feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2171feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2172feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2173feed9e9dSBarry Smith 
2174feed9e9dSBarry Smith    Options Database:
2175feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2176feed9e9dSBarry Smith 
2177ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2178ee346746SBarry Smith     then the final time you selected.
2179ee346746SBarry Smith 
2180a43b19c4SJed Brown    Level: beginner
2181a43b19c4SJed Brown 
2182f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2183a43b19c4SJed Brown @*/
218449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2185a43b19c4SJed Brown {
2186a43b19c4SJed Brown   PetscFunctionBegin;
2187a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
218849354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
218949354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2190a43b19c4SJed Brown   PetscFunctionReturn(0);
2191a43b19c4SJed Brown }
2192a43b19c4SJed Brown 
2193d763cef2SBarry Smith /*@
2194f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2195f6953c82SLisandro Dalcin 
2196f6953c82SLisandro Dalcin    Not Collective
2197f6953c82SLisandro Dalcin 
2198f6953c82SLisandro Dalcin    Input Parameter:
2199f6953c82SLisandro Dalcin .  ts - the TS context
2200f6953c82SLisandro Dalcin 
2201f6953c82SLisandro Dalcin    Output Parameter:
2202f6953c82SLisandro Dalcin .  eftopt - exact final time option
2203f6953c82SLisandro Dalcin 
2204f6953c82SLisandro Dalcin    Level: beginner
2205f6953c82SLisandro Dalcin 
2206f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2207f6953c82SLisandro Dalcin @*/
2208f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2209f6953c82SLisandro Dalcin {
2210f6953c82SLisandro Dalcin   PetscFunctionBegin;
2211f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2212f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2213f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2214f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2215f6953c82SLisandro Dalcin }
2216f6953c82SLisandro Dalcin 
2217f6953c82SLisandro Dalcin /*@
2218d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2219d763cef2SBarry Smith 
2220d763cef2SBarry Smith    Not Collective
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Input Parameter:
2223d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2224d763cef2SBarry Smith 
2225d763cef2SBarry Smith    Output Parameter:
2226d763cef2SBarry Smith .  dt - the current timestep size
2227d763cef2SBarry Smith 
2228d763cef2SBarry Smith    Level: intermediate
2229d763cef2SBarry Smith 
2230aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith .keywords: TS, get, timestep
2233d763cef2SBarry Smith @*/
22347087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2235d763cef2SBarry Smith {
2236d763cef2SBarry Smith   PetscFunctionBegin;
22370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2238f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2239d763cef2SBarry Smith   *dt = ts->time_step;
2240d763cef2SBarry Smith   PetscFunctionReturn(0);
2241d763cef2SBarry Smith }
2242d763cef2SBarry Smith 
2243d8e5e3e6SSatish Balay /*@
2244d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2245d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2246d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2247d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2248d763cef2SBarry Smith 
2249d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith    Input Parameter:
2252d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2253d763cef2SBarry Smith 
2254d763cef2SBarry Smith    Output Parameter:
2255d763cef2SBarry Smith .  v - the vector containing the solution
2256d763cef2SBarry Smith 
225763e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
225863e21af5SBarry Smith    final time. It returns the solution at the next timestep.
225963e21af5SBarry Smith 
2260d763cef2SBarry Smith    Level: intermediate
2261d763cef2SBarry Smith 
22620ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2263d763cef2SBarry Smith 
2264d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2265d763cef2SBarry Smith @*/
22667087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2267d763cef2SBarry Smith {
2268d763cef2SBarry Smith   PetscFunctionBegin;
22690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22704482741eSBarry Smith   PetscValidPointer(v,2);
22718737fe31SLisandro Dalcin   *v = ts->vec_sol;
2272d763cef2SBarry Smith   PetscFunctionReturn(0);
2273d763cef2SBarry Smith }
2274d763cef2SBarry Smith 
227503fe5f5eSDebojyoti Ghosh /*@
2276b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
227703fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2278b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
227903fe5f5eSDebojyoti Ghosh    structure as the solution vector.
228003fe5f5eSDebojyoti Ghosh 
228103fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
228203fe5f5eSDebojyoti Ghosh 
228303fe5f5eSDebojyoti Ghosh    Parameters :
228403fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2285b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2286b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
228703fe5f5eSDebojyoti Ghosh        returned in v.
2288b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
228903fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2290b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
229103fe5f5eSDebojyoti Ghosh 
22924cdd57e5SDebojyoti Ghosh    Level: advanced
229303fe5f5eSDebojyoti Ghosh 
229403fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
229503fe5f5eSDebojyoti Ghosh 
229603fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
229703fe5f5eSDebojyoti Ghosh @*/
2298b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
229903fe5f5eSDebojyoti Ghosh {
230003fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
230103fe5f5eSDebojyoti Ghosh 
230203fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
230303fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2304b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
230503fe5f5eSDebojyoti Ghosh   else {
2306b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
230703fe5f5eSDebojyoti Ghosh   }
230803fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
230903fe5f5eSDebojyoti Ghosh }
231003fe5f5eSDebojyoti Ghosh 
23114cdd57e5SDebojyoti Ghosh /*@
23124cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23134cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23144cdd57e5SDebojyoti Ghosh 
23154cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23164cdd57e5SDebojyoti Ghosh 
23174cdd57e5SDebojyoti Ghosh    Parameters :
23184cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23194cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23204cdd57e5SDebojyoti Ghosh 
23214cdd57e5SDebojyoti Ghosh    Level: intermediate
23224cdd57e5SDebojyoti Ghosh 
23234cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23244cdd57e5SDebojyoti Ghosh 
23254cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23264cdd57e5SDebojyoti Ghosh @*/
23274cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23284cdd57e5SDebojyoti Ghosh {
23294cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23304cdd57e5SDebojyoti Ghosh 
23314cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23324cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2333f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23344cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2335f6356ec7SDebojyoti Ghosh   } else {
2336f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2337f6356ec7SDebojyoti Ghosh   }
23384cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23394cdd57e5SDebojyoti Ghosh }
23404cdd57e5SDebojyoti Ghosh 
23414cdd57e5SDebojyoti Ghosh /*@
23424cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23434cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23444cdd57e5SDebojyoti Ghosh 
23454cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23464cdd57e5SDebojyoti Ghosh 
23479657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23489657682dSDebojyoti Ghosh 
23494cdd57e5SDebojyoti Ghosh    Parameters :
23504cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2351657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23524cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23534cdd57e5SDebojyoti Ghosh 
23544cdd57e5SDebojyoti Ghosh    Level: intermediate
23554cdd57e5SDebojyoti Ghosh 
235657df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23574cdd57e5SDebojyoti Ghosh 
23584cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
23594cdd57e5SDebojyoti Ghosh @*/
23600a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23614cdd57e5SDebojyoti Ghosh {
23624cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23634cdd57e5SDebojyoti Ghosh 
23644cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23654cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2366f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23670a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2368f6356ec7SDebojyoti Ghosh   } else {
2369f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2370f6356ec7SDebojyoti Ghosh   }
23714cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23724cdd57e5SDebojyoti Ghosh }
23734cdd57e5SDebojyoti Ghosh 
237457df6a1bSDebojyoti Ghosh /*@
237557df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
237657df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
237757df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
237857df6a1bSDebojyoti Ghosh 
237957df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
238057df6a1bSDebojyoti Ghosh 
238157df6a1bSDebojyoti Ghosh    Parameters :
238257df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
238357df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
238457df6a1bSDebojyoti Ghosh 
238557df6a1bSDebojyoti Ghosh    Level: intermediate
238657df6a1bSDebojyoti Ghosh 
238757df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
238857df6a1bSDebojyoti Ghosh 
238957df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
239057df6a1bSDebojyoti Ghosh @*/
239157df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
239257df6a1bSDebojyoti Ghosh {
239357df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
239457df6a1bSDebojyoti Ghosh 
239557df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
239657df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23979657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
239857df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
239957df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
240057df6a1bSDebojyoti Ghosh   }
240157df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
240257df6a1bSDebojyoti Ghosh }
240357df6a1bSDebojyoti Ghosh 
2404bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2405d8e5e3e6SSatish Balay /*@
2406bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2407d763cef2SBarry Smith 
2408bdad233fSMatthew Knepley   Not collective
2409d763cef2SBarry Smith 
2410bdad233fSMatthew Knepley   Input Parameters:
2411bdad233fSMatthew Knepley + ts   - The TS
2412bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2413d763cef2SBarry Smith .vb
24140910c330SBarry Smith          U_t - A U = 0      (linear)
24150910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24160910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2417d763cef2SBarry Smith .ve
2418d763cef2SBarry Smith 
2419d763cef2SBarry Smith    Level: beginner
2420d763cef2SBarry Smith 
2421bdad233fSMatthew Knepley .keywords: TS, problem type
2422bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2423d763cef2SBarry Smith @*/
24247087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2425a7cc72afSBarry Smith {
24269e2a6581SJed Brown   PetscErrorCode ierr;
24279e2a6581SJed Brown 
2428d763cef2SBarry Smith   PetscFunctionBegin;
24290700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2430bdad233fSMatthew Knepley   ts->problem_type = type;
24319e2a6581SJed Brown   if (type == TS_LINEAR) {
24329e2a6581SJed Brown     SNES snes;
24339e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24349e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24359e2a6581SJed Brown   }
2436d763cef2SBarry Smith   PetscFunctionReturn(0);
2437d763cef2SBarry Smith }
2438d763cef2SBarry Smith 
2439bdad233fSMatthew Knepley /*@C
2440bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2441bdad233fSMatthew Knepley 
2442bdad233fSMatthew Knepley   Not collective
2443bdad233fSMatthew Knepley 
2444bdad233fSMatthew Knepley   Input Parameter:
2445bdad233fSMatthew Knepley . ts   - The TS
2446bdad233fSMatthew Knepley 
2447bdad233fSMatthew Knepley   Output Parameter:
2448bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2449bdad233fSMatthew Knepley .vb
2450089b2837SJed Brown          M U_t = A U
2451089b2837SJed Brown          M(t) U_t = A(t) U
2452b5abc632SBarry Smith          F(t,U,U_t)
2453bdad233fSMatthew Knepley .ve
2454bdad233fSMatthew Knepley 
2455bdad233fSMatthew Knepley    Level: beginner
2456bdad233fSMatthew Knepley 
2457bdad233fSMatthew Knepley .keywords: TS, problem type
2458bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2459bdad233fSMatthew Knepley @*/
24607087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2461a7cc72afSBarry Smith {
2462bdad233fSMatthew Knepley   PetscFunctionBegin;
24630700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24644482741eSBarry Smith   PetscValidIntPointer(type,2);
2465bdad233fSMatthew Knepley   *type = ts->problem_type;
2466bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2467bdad233fSMatthew Knepley }
2468d763cef2SBarry Smith 
2469d763cef2SBarry Smith /*@
2470d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2471d763cef2SBarry Smith    of a timestepper.
2472d763cef2SBarry Smith 
2473d763cef2SBarry Smith    Collective on TS
2474d763cef2SBarry Smith 
2475d763cef2SBarry Smith    Input Parameter:
2476d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2477d763cef2SBarry Smith 
2478d763cef2SBarry Smith    Notes:
2479d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2480d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2481141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2482d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2483141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2484d763cef2SBarry Smith 
2485d763cef2SBarry Smith    Level: advanced
2486d763cef2SBarry Smith 
2487d763cef2SBarry Smith .keywords: TS, timestep, setup
2488d763cef2SBarry Smith 
2489141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2490d763cef2SBarry Smith @*/
24917087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2492d763cef2SBarry Smith {
2493dfbe8321SBarry Smith   PetscErrorCode ierr;
24946c6b9e74SPeter Brune   DM             dm;
24956c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2496d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2497cd11d68dSLisandro Dalcin   TSIFunction    ifun;
24986c6b9e74SPeter Brune   TSIJacobian    ijac;
2499efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25006c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25012ffb9264SLisandro Dalcin   PetscBool      isnone;
2502d763cef2SBarry Smith 
2503d763cef2SBarry Smith   PetscFunctionBegin;
25040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2505277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2506277b19d0SLisandro Dalcin 
25077adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2508cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2509cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2510d763cef2SBarry Smith   }
2511277b19d0SLisandro Dalcin 
2512277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2513277b19d0SLisandro Dalcin 
2514e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2515e1244c69SJed Brown     Mat Amat,Pmat;
2516e1244c69SJed Brown     SNES snes;
2517e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2518e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2519e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2520e1244c69SJed Brown      * have displaced the RHS matrix */
2521e1244c69SJed Brown     if (Amat == ts->Arhs) {
2522abc0d4abSBarry 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 */
2523abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2524e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2525e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2526e1244c69SJed Brown     }
2527e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2528abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2529e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2530e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2531e1244c69SJed Brown     }
2532e1244c69SJed Brown   }
25332ffb9264SLisandro Dalcin 
25342ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25352ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25362ffb9264SLisandro Dalcin 
2537277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2538000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2539277b19d0SLisandro Dalcin   }
2540277b19d0SLisandro Dalcin 
25412ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25422ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25432ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25442ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25452ffb9264SLisandro Dalcin 
2546a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25476c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25486c6b9e74SPeter Brune    */
25496c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25500298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25516c6b9e74SPeter Brune   if (!func) {
25526c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25536c6b9e74SPeter Brune   }
2554a6772fa2SLisandro 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.
25556c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25566c6b9e74SPeter Brune    */
25570298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25580298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2559efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25600298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2561efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25626c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25636c6b9e74SPeter Brune   }
2564c0517034SDebojyoti Ghosh 
2565c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2566c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2567c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2568c0517034SDebojyoti Ghosh   }
2569c0517034SDebojyoti Ghosh 
2570277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2571277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2572277b19d0SLisandro Dalcin }
2573277b19d0SLisandro Dalcin 
2574f6a906c0SBarry Smith /*@
2575277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2576277b19d0SLisandro Dalcin 
2577277b19d0SLisandro Dalcin    Collective on TS
2578277b19d0SLisandro Dalcin 
2579277b19d0SLisandro Dalcin    Input Parameter:
2580277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2581277b19d0SLisandro Dalcin 
2582277b19d0SLisandro Dalcin    Level: beginner
2583277b19d0SLisandro Dalcin 
2584277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2585277b19d0SLisandro Dalcin 
2586277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2587277b19d0SLisandro Dalcin @*/
2588277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2589277b19d0SLisandro Dalcin {
2590277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2591277b19d0SLisandro Dalcin 
2592277b19d0SLisandro Dalcin   PetscFunctionBegin;
2593277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2594b18ea86cSHong Zhang 
2595277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2596277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2597277b19d0SLisandro Dalcin   }
2598277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2599e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2600bbd56ea5SKarl Rupp 
26014e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26024e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2603214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26046bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2605efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2606e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2607e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
260838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2609bbd56ea5SKarl Rupp 
2610d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2611d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2612715f1b00SHong Zhang 
2613ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26142c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
261536eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
261613b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2617022c081aSHong Zhang 
2618277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2619d763cef2SBarry Smith   PetscFunctionReturn(0);
2620d763cef2SBarry Smith }
2621d763cef2SBarry Smith 
2622d8e5e3e6SSatish Balay /*@
2623d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2624d763cef2SBarry Smith    with TSCreate().
2625d763cef2SBarry Smith 
2626d763cef2SBarry Smith    Collective on TS
2627d763cef2SBarry Smith 
2628d763cef2SBarry Smith    Input Parameter:
2629d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2630d763cef2SBarry Smith 
2631d763cef2SBarry Smith    Level: beginner
2632d763cef2SBarry Smith 
2633d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2634d763cef2SBarry Smith 
2635d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2636d763cef2SBarry Smith @*/
26376bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2638d763cef2SBarry Smith {
26396849ba73SBarry Smith   PetscErrorCode ierr;
2640d763cef2SBarry Smith 
2641d763cef2SBarry Smith   PetscFunctionBegin;
26426bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26436bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26446bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2645d763cef2SBarry Smith 
26466bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2647277b19d0SLisandro Dalcin 
2648e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2649e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26506bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26516d4c513bSLisandro Dalcin 
2652bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2653bc952696SBarry Smith 
265484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26556427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26566427ac75SLisandro Dalcin 
26576bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26586bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26596bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
26600dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
26616d4c513bSLisandro Dalcin 
2662a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2663d763cef2SBarry Smith   PetscFunctionReturn(0);
2664d763cef2SBarry Smith }
2665d763cef2SBarry Smith 
2666d8e5e3e6SSatish Balay /*@
2667d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2668d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2669d763cef2SBarry Smith 
2670d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2671d763cef2SBarry Smith 
2672d763cef2SBarry Smith    Input Parameter:
2673d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2674d763cef2SBarry Smith 
2675d763cef2SBarry Smith    Output Parameter:
2676d763cef2SBarry Smith .  snes - the nonlinear solver context
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith    Notes:
2679d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2680d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
268194b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2682d763cef2SBarry Smith 
2683d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
26840298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2685d763cef2SBarry Smith 
2686d763cef2SBarry Smith    Level: beginner
2687d763cef2SBarry Smith 
2688d763cef2SBarry Smith .keywords: timestep, get, SNES
2689d763cef2SBarry Smith @*/
26907087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2691d763cef2SBarry Smith {
2692d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2693d372ba47SLisandro Dalcin 
2694d763cef2SBarry Smith   PetscFunctionBegin;
26950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26964482741eSBarry Smith   PetscValidPointer(snes,2);
2697d372ba47SLisandro Dalcin   if (!ts->snes) {
2698ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
26990298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27003bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2701d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2702496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27039e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27049e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27059e2a6581SJed Brown     }
2706d372ba47SLisandro Dalcin   }
2707d763cef2SBarry Smith   *snes = ts->snes;
2708d763cef2SBarry Smith   PetscFunctionReturn(0);
2709d763cef2SBarry Smith }
2710d763cef2SBarry Smith 
2711deb2cd25SJed Brown /*@
2712deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2713deb2cd25SJed Brown 
2714deb2cd25SJed Brown    Collective
2715deb2cd25SJed Brown 
2716deb2cd25SJed Brown    Input Parameter:
2717deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2718deb2cd25SJed Brown -  snes - the nonlinear solver context
2719deb2cd25SJed Brown 
2720deb2cd25SJed Brown    Notes:
2721deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2722deb2cd25SJed Brown 
2723deb2cd25SJed Brown    Level: developer
2724deb2cd25SJed Brown 
2725deb2cd25SJed Brown .keywords: timestep, set, SNES
2726deb2cd25SJed Brown @*/
2727deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2728deb2cd25SJed Brown {
2729deb2cd25SJed Brown   PetscErrorCode ierr;
2730d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2731deb2cd25SJed Brown 
2732deb2cd25SJed Brown   PetscFunctionBegin;
2733deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2734deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2735deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2736deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2737bbd56ea5SKarl Rupp 
2738deb2cd25SJed Brown   ts->snes = snes;
2739bbd56ea5SKarl Rupp 
27400298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27410298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2742740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27430298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2744740132f1SEmil Constantinescu   }
2745deb2cd25SJed Brown   PetscFunctionReturn(0);
2746deb2cd25SJed Brown }
2747deb2cd25SJed Brown 
2748d8e5e3e6SSatish Balay /*@
274994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2750d763cef2SBarry Smith    a TS (timestepper) context.
2751d763cef2SBarry Smith 
275294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2753d763cef2SBarry Smith 
2754d763cef2SBarry Smith    Input Parameter:
2755d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2756d763cef2SBarry Smith 
2757d763cef2SBarry Smith    Output Parameter:
275894b7f48cSBarry Smith .  ksp - the nonlinear solver context
2759d763cef2SBarry Smith 
2760d763cef2SBarry Smith    Notes:
276194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2762d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2763d763cef2SBarry Smith    KSP and PC contexts as well.
2764d763cef2SBarry Smith 
276594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
27660298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2767d763cef2SBarry Smith 
2768d763cef2SBarry Smith    Level: beginner
2769d763cef2SBarry Smith 
277094b7f48cSBarry Smith .keywords: timestep, get, KSP
2771d763cef2SBarry Smith @*/
27727087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2773d763cef2SBarry Smith {
2774d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2775089b2837SJed Brown   SNES           snes;
2776d372ba47SLisandro Dalcin 
2777d763cef2SBarry Smith   PetscFunctionBegin;
27780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27794482741eSBarry Smith   PetscValidPointer(ksp,2);
278017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2781e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2782089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2783089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2784d763cef2SBarry Smith   PetscFunctionReturn(0);
2785d763cef2SBarry Smith }
2786d763cef2SBarry Smith 
2787d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2788d763cef2SBarry Smith 
2789adb62b0dSMatthew Knepley /*@
2790618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2791618ce8baSLisandro Dalcin 
2792618ce8baSLisandro Dalcin    Logically Collective on TS
2793618ce8baSLisandro Dalcin 
2794618ce8baSLisandro Dalcin    Input Parameters:
2795618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2796618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2797618ce8baSLisandro Dalcin 
2798618ce8baSLisandro Dalcin    Options Database Keys:
2799618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2800618ce8baSLisandro Dalcin 
2801618ce8baSLisandro Dalcin    Notes:
2802618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2803618ce8baSLisandro Dalcin 
2804618ce8baSLisandro Dalcin    Level: intermediate
2805618ce8baSLisandro Dalcin 
2806618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2807618ce8baSLisandro Dalcin 
2808618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2809618ce8baSLisandro Dalcin @*/
2810618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2811618ce8baSLisandro Dalcin {
2812618ce8baSLisandro Dalcin   PetscFunctionBegin;
2813618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2814618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2815618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2816618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2817618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2818618ce8baSLisandro Dalcin }
2819618ce8baSLisandro Dalcin 
2820618ce8baSLisandro Dalcin /*@
2821618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2822618ce8baSLisandro Dalcin 
2823618ce8baSLisandro Dalcin    Not Collective
2824618ce8baSLisandro Dalcin 
2825618ce8baSLisandro Dalcin    Input Parameters:
2826618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2827618ce8baSLisandro Dalcin 
2828618ce8baSLisandro Dalcin    Output Parameter:
2829618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2830618ce8baSLisandro Dalcin 
2831618ce8baSLisandro Dalcin    Level: advanced
2832618ce8baSLisandro Dalcin 
2833618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2834618ce8baSLisandro Dalcin 
2835618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2836618ce8baSLisandro Dalcin @*/
2837618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2838618ce8baSLisandro Dalcin {
2839618ce8baSLisandro Dalcin   PetscFunctionBegin;
2840618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2841618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2842618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2843618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2844618ce8baSLisandro Dalcin }
2845618ce8baSLisandro Dalcin 
2846618ce8baSLisandro Dalcin /*@
2847618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2848618ce8baSLisandro Dalcin 
2849618ce8baSLisandro Dalcin    Logically Collective on TS
2850618ce8baSLisandro Dalcin 
2851618ce8baSLisandro Dalcin    Input Parameters:
2852618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2853618ce8baSLisandro Dalcin -  maxtime - final time to step to
2854618ce8baSLisandro Dalcin 
2855618ce8baSLisandro Dalcin    Options Database Keys:
2856ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2857618ce8baSLisandro Dalcin 
2858618ce8baSLisandro Dalcin    Notes:
2859618ce8baSLisandro Dalcin    The default maximum time is 5.0
2860618ce8baSLisandro Dalcin 
2861618ce8baSLisandro Dalcin    Level: intermediate
2862618ce8baSLisandro Dalcin 
2863618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
2864618ce8baSLisandro Dalcin 
2865618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2866618ce8baSLisandro Dalcin @*/
2867618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2868618ce8baSLisandro Dalcin {
2869618ce8baSLisandro Dalcin   PetscFunctionBegin;
2870618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2871618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2872618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2873618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2874618ce8baSLisandro Dalcin }
2875618ce8baSLisandro Dalcin 
2876618ce8baSLisandro Dalcin /*@
2877618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2878618ce8baSLisandro Dalcin 
2879618ce8baSLisandro Dalcin    Not Collective
2880618ce8baSLisandro Dalcin 
2881618ce8baSLisandro Dalcin    Input Parameters:
2882618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2883618ce8baSLisandro Dalcin 
2884618ce8baSLisandro Dalcin    Output Parameter:
2885618ce8baSLisandro Dalcin .  maxtime - final time to step to
2886618ce8baSLisandro Dalcin 
2887618ce8baSLisandro Dalcin    Level: advanced
2888618ce8baSLisandro Dalcin 
2889618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
2890618ce8baSLisandro Dalcin 
2891618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2892618ce8baSLisandro Dalcin @*/
2893618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2894618ce8baSLisandro Dalcin {
2895618ce8baSLisandro Dalcin   PetscFunctionBegin;
2896618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2897618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2898618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2899618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2900618ce8baSLisandro Dalcin }
2901618ce8baSLisandro Dalcin 
2902618ce8baSLisandro Dalcin /*@
2903aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2904edc382c3SSatish Balay 
2905edc382c3SSatish Balay    Level: deprecated
2906edc382c3SSatish Balay 
2907aaa6c58dSLisandro Dalcin @*/
2908aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2909aaa6c58dSLisandro Dalcin {
2910aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2911aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2912aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2913aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2914aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2915aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2916aaa6c58dSLisandro Dalcin }
2917aaa6c58dSLisandro Dalcin 
2918aaa6c58dSLisandro Dalcin /*@
291919eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
2920edc382c3SSatish Balay 
2921edc382c3SSatish Balay    Level: deprecated
2922edc382c3SSatish Balay 
2923adb62b0dSMatthew Knepley @*/
29247087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2925adb62b0dSMatthew Knepley {
2926adb62b0dSMatthew Knepley   PetscFunctionBegin;
29270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2928abc0a331SBarry Smith   if (maxsteps) {
29294482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2930adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2931adb62b0dSMatthew Knepley   }
2932abc0a331SBarry Smith   if (maxtime) {
29334482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2934adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2935adb62b0dSMatthew Knepley   }
2936adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2937adb62b0dSMatthew Knepley }
2938adb62b0dSMatthew Knepley 
2939d763cef2SBarry Smith /*@
294019eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
2941edc382c3SSatish Balay 
2942edc382c3SSatish Balay    Level: deprecated
2943edc382c3SSatish Balay 
2944d763cef2SBarry Smith @*/
29457087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2946d763cef2SBarry Smith {
2947d763cef2SBarry Smith   PetscFunctionBegin;
29480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2949c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2950c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
295139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
295239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2953d763cef2SBarry Smith   PetscFunctionReturn(0);
2954d763cef2SBarry Smith }
2955d763cef2SBarry Smith 
2956d763cef2SBarry Smith /*@
29575c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
2958edc382c3SSatish Balay 
2959edc382c3SSatish Balay    Level: deprecated
2960edc382c3SSatish Balay 
29615c5f5948SLisandro Dalcin @*/
2962e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
29635c5f5948SLisandro Dalcin 
296419eac22cSLisandro Dalcin /*@
29654f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
2966edc382c3SSatish Balay 
2967edc382c3SSatish Balay    Level: deprecated
2968edc382c3SSatish Balay 
29694f4e0956SLisandro Dalcin @*/
29704f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
29714f4e0956SLisandro Dalcin 
29724f4e0956SLisandro Dalcin /*@
2973d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2974d763cef2SBarry Smith    for use by the TS routines.
2975d763cef2SBarry Smith 
29763f9fe445SBarry Smith    Logically Collective on TS and Vec
2977d763cef2SBarry Smith 
2978d763cef2SBarry Smith    Input Parameters:
2979d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
29800910c330SBarry Smith -  u - the solution vector
2981d763cef2SBarry Smith 
2982d763cef2SBarry Smith    Level: beginner
2983d763cef2SBarry Smith 
2984715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
29850ed3bfb6SBarry Smith 
29860ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
2987d763cef2SBarry Smith @*/
29880910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2989d763cef2SBarry Smith {
29908737fe31SLisandro Dalcin   PetscErrorCode ierr;
2991496e6a7aSJed Brown   DM             dm;
29928737fe31SLisandro Dalcin 
2993d763cef2SBarry Smith   PetscFunctionBegin;
29940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29950910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
29960910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
29976bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
29980910c330SBarry Smith   ts->vec_sol = u;
2999bbd56ea5SKarl Rupp 
3000496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30010910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3002d763cef2SBarry Smith   PetscFunctionReturn(0);
3003d763cef2SBarry Smith }
3004d763cef2SBarry Smith 
3005ac226902SBarry Smith /*@C
3006000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30073f2090d5SJed Brown   called once at the beginning of each time step.
3008000e7ae3SMatthew Knepley 
30093f9fe445SBarry Smith   Logically Collective on TS
3010000e7ae3SMatthew Knepley 
3011000e7ae3SMatthew Knepley   Input Parameters:
3012000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3013000e7ae3SMatthew Knepley - func - The function
3014000e7ae3SMatthew Knepley 
3015000e7ae3SMatthew Knepley   Calling sequence of func:
3016000e7ae3SMatthew Knepley . func (TS ts);
3017000e7ae3SMatthew Knepley 
3018000e7ae3SMatthew Knepley   Level: intermediate
3019000e7ae3SMatthew Knepley 
3020000e7ae3SMatthew Knepley .keywords: TS, timestep
3021dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3022000e7ae3SMatthew Knepley @*/
30237087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3024000e7ae3SMatthew Knepley {
3025000e7ae3SMatthew Knepley   PetscFunctionBegin;
30260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3027ae60f76fSBarry Smith   ts->prestep = func;
3028000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3029000e7ae3SMatthew Knepley }
3030000e7ae3SMatthew Knepley 
303109ee8438SJed Brown /*@
30323f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
30333f2090d5SJed Brown 
30343f2090d5SJed Brown   Collective on TS
30353f2090d5SJed Brown 
30363f2090d5SJed Brown   Input Parameters:
30373f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
30383f2090d5SJed Brown 
30393f2090d5SJed Brown   Notes:
30403f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
30413f2090d5SJed Brown   so most users would not generally call this routine themselves.
30423f2090d5SJed Brown 
30433f2090d5SJed Brown   Level: developer
30443f2090d5SJed Brown 
30453f2090d5SJed Brown .keywords: TS, timestep
30469be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
30473f2090d5SJed Brown @*/
30487087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
30493f2090d5SJed Brown {
30503f2090d5SJed Brown   PetscErrorCode ierr;
30513f2090d5SJed Brown 
30523f2090d5SJed Brown   PetscFunctionBegin;
30530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3054ae60f76fSBarry Smith   if (ts->prestep) {
30555efd42a4SStefano Zampini     Vec              U;
30565efd42a4SStefano Zampini     PetscObjectState sprev,spost;
30575efd42a4SStefano Zampini 
30585efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
30595efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3060ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
30615efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3062dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3063312ce896SJed Brown   }
30643f2090d5SJed Brown   PetscFunctionReturn(0);
30653f2090d5SJed Brown }
30663f2090d5SJed Brown 
3067b8123daeSJed Brown /*@C
3068b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3069b8123daeSJed Brown   called once at the beginning of each stage.
3070b8123daeSJed Brown 
3071b8123daeSJed Brown   Logically Collective on TS
3072b8123daeSJed Brown 
3073b8123daeSJed Brown   Input Parameters:
3074b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3075b8123daeSJed Brown - func - The function
3076b8123daeSJed Brown 
3077b8123daeSJed Brown   Calling sequence of func:
3078b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3079b8123daeSJed Brown 
3080b8123daeSJed Brown   Level: intermediate
3081b8123daeSJed Brown 
3082b8123daeSJed Brown   Note:
3083b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
308480275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3085b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3086b8123daeSJed Brown 
3087b8123daeSJed Brown .keywords: TS, timestep
30889be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3089b8123daeSJed Brown @*/
3090b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3091b8123daeSJed Brown {
3092b8123daeSJed Brown   PetscFunctionBegin;
3093b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3094ae60f76fSBarry Smith   ts->prestage = func;
3095b8123daeSJed Brown   PetscFunctionReturn(0);
3096b8123daeSJed Brown }
3097b8123daeSJed Brown 
30989be3e283SDebojyoti Ghosh /*@C
30999be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31009be3e283SDebojyoti Ghosh   called once at the end of each stage.
31019be3e283SDebojyoti Ghosh 
31029be3e283SDebojyoti Ghosh   Logically Collective on TS
31039be3e283SDebojyoti Ghosh 
31049be3e283SDebojyoti Ghosh   Input Parameters:
31059be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31069be3e283SDebojyoti Ghosh - func - The function
31079be3e283SDebojyoti Ghosh 
31089be3e283SDebojyoti Ghosh   Calling sequence of func:
31099be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31109be3e283SDebojyoti Ghosh 
31119be3e283SDebojyoti Ghosh   Level: intermediate
31129be3e283SDebojyoti Ghosh 
31139be3e283SDebojyoti Ghosh   Note:
31149be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
311580275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31169be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31179be3e283SDebojyoti Ghosh 
31189be3e283SDebojyoti Ghosh .keywords: TS, timestep
31199be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
31209be3e283SDebojyoti Ghosh @*/
31219be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
31229be3e283SDebojyoti Ghosh {
31239be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31249be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
31259be3e283SDebojyoti Ghosh   ts->poststage = func;
31269be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
31279be3e283SDebojyoti Ghosh }
31289be3e283SDebojyoti Ghosh 
3129c688d042SShri Abhyankar /*@C
3130c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3131c688d042SShri Abhyankar   called once at the end of each step evaluation.
3132c688d042SShri Abhyankar 
3133c688d042SShri Abhyankar   Logically Collective on TS
3134c688d042SShri Abhyankar 
3135c688d042SShri Abhyankar   Input Parameters:
3136c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3137c688d042SShri Abhyankar - func - The function
3138c688d042SShri Abhyankar 
3139c688d042SShri Abhyankar   Calling sequence of func:
3140c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3141c688d042SShri Abhyankar 
3142c688d042SShri Abhyankar   Level: intermediate
3143c688d042SShri Abhyankar 
3144c688d042SShri Abhyankar   Note:
31451785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
31461785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3147e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3148e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3149e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3150c688d042SShri Abhyankar 
3151c688d042SShri Abhyankar .keywords: TS, timestep
3152c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3153c688d042SShri Abhyankar @*/
3154c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3155c688d042SShri Abhyankar {
3156c688d042SShri Abhyankar   PetscFunctionBegin;
3157c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3158c688d042SShri Abhyankar   ts->postevaluate = func;
3159c688d042SShri Abhyankar   PetscFunctionReturn(0);
3160c688d042SShri Abhyankar }
3161c688d042SShri Abhyankar 
3162b8123daeSJed Brown /*@
3163b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3164b8123daeSJed Brown 
3165b8123daeSJed Brown   Collective on TS
3166b8123daeSJed Brown 
3167b8123daeSJed Brown   Input Parameters:
3168b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
31699be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3170b8123daeSJed Brown 
3171b8123daeSJed Brown   Notes:
3172b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3173b8123daeSJed Brown   most users would not generally call this routine themselves.
3174b8123daeSJed Brown 
3175b8123daeSJed Brown   Level: developer
3176b8123daeSJed Brown 
3177b8123daeSJed Brown .keywords: TS, timestep
31789be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3179b8123daeSJed Brown @*/
3180b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3181b8123daeSJed Brown {
3182b8123daeSJed Brown   PetscErrorCode ierr;
3183b8123daeSJed Brown 
3184b8123daeSJed Brown   PetscFunctionBegin;
3185b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3186ae60f76fSBarry Smith   if (ts->prestage) {
3187ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3188b8123daeSJed Brown   }
3189b8123daeSJed Brown   PetscFunctionReturn(0);
3190b8123daeSJed Brown }
3191b8123daeSJed Brown 
31929be3e283SDebojyoti Ghosh /*@
31939be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
31949be3e283SDebojyoti Ghosh 
31959be3e283SDebojyoti Ghosh   Collective on TS
31969be3e283SDebojyoti Ghosh 
31979be3e283SDebojyoti Ghosh   Input Parameters:
31989be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
31999be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32009be3e283SDebojyoti Ghosh   stageindex  - Stage number
32019be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32029be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32039be3e283SDebojyoti Ghosh 
32049be3e283SDebojyoti Ghosh   Notes:
32059be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32069be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32079be3e283SDebojyoti Ghosh 
32089be3e283SDebojyoti Ghosh   Level: developer
32099be3e283SDebojyoti Ghosh 
32109be3e283SDebojyoti Ghosh .keywords: TS, timestep
32119be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32129be3e283SDebojyoti Ghosh @*/
32139be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32149be3e283SDebojyoti Ghosh {
32159be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
32169be3e283SDebojyoti Ghosh 
32179be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32189be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32194beae5d8SLisandro Dalcin   if (ts->poststage) {
32209be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
32219be3e283SDebojyoti Ghosh   }
32229be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32239be3e283SDebojyoti Ghosh }
32249be3e283SDebojyoti Ghosh 
3225c688d042SShri Abhyankar /*@
3226c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3227c688d042SShri Abhyankar 
3228c688d042SShri Abhyankar   Collective on TS
3229c688d042SShri Abhyankar 
3230c688d042SShri Abhyankar   Input Parameters:
3231c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3232c688d042SShri Abhyankar 
3233c688d042SShri Abhyankar   Notes:
3234c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3235c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3236c688d042SShri Abhyankar 
3237c688d042SShri Abhyankar   Level: developer
3238c688d042SShri Abhyankar 
3239c688d042SShri Abhyankar .keywords: TS, timestep
3240c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3241c688d042SShri Abhyankar @*/
3242c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3243c688d042SShri Abhyankar {
3244c688d042SShri Abhyankar   PetscErrorCode ierr;
3245c688d042SShri Abhyankar 
3246c688d042SShri Abhyankar   PetscFunctionBegin;
3247c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3248c688d042SShri Abhyankar   if (ts->postevaluate) {
3249dcb233daSLisandro Dalcin     Vec              U;
3250dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3251dcb233daSLisandro Dalcin 
3252dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3253dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3254c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3255dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3256dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3257c688d042SShri Abhyankar   }
3258c688d042SShri Abhyankar   PetscFunctionReturn(0);
3259c688d042SShri Abhyankar }
3260c688d042SShri Abhyankar 
3261ac226902SBarry Smith /*@C
3262000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
32633f2090d5SJed Brown   called once at the end of each time step.
3264000e7ae3SMatthew Knepley 
32653f9fe445SBarry Smith   Logically Collective on TS
3266000e7ae3SMatthew Knepley 
3267000e7ae3SMatthew Knepley   Input Parameters:
3268000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3269000e7ae3SMatthew Knepley - func - The function
3270000e7ae3SMatthew Knepley 
3271000e7ae3SMatthew Knepley   Calling sequence of func:
3272b8123daeSJed Brown $ func (TS ts);
3273000e7ae3SMatthew Knepley 
32741785ff2aSShri Abhyankar   Notes:
32751785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
32761785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
32771785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
32781785ff2aSShri Abhyankar 
3279000e7ae3SMatthew Knepley   Level: intermediate
3280000e7ae3SMatthew Knepley 
3281000e7ae3SMatthew Knepley .keywords: TS, timestep
3282dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3283000e7ae3SMatthew Knepley @*/
32847087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3285000e7ae3SMatthew Knepley {
3286000e7ae3SMatthew Knepley   PetscFunctionBegin;
32870700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3288ae60f76fSBarry Smith   ts->poststep = func;
3289000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3290000e7ae3SMatthew Knepley }
3291000e7ae3SMatthew Knepley 
329209ee8438SJed Brown /*@
32933f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
32943f2090d5SJed Brown 
32953f2090d5SJed Brown   Collective on TS
32963f2090d5SJed Brown 
32973f2090d5SJed Brown   Input Parameters:
32983f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32993f2090d5SJed Brown 
33003f2090d5SJed Brown   Notes:
33013f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33023f2090d5SJed Brown   so most users would not generally call this routine themselves.
33033f2090d5SJed Brown 
33043f2090d5SJed Brown   Level: developer
33053f2090d5SJed Brown 
33063f2090d5SJed Brown .keywords: TS, timestep
33073f2090d5SJed Brown @*/
33087087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33093f2090d5SJed Brown {
33103f2090d5SJed Brown   PetscErrorCode ierr;
33113f2090d5SJed Brown 
33123f2090d5SJed Brown   PetscFunctionBegin;
33130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3314ae60f76fSBarry Smith   if (ts->poststep) {
33155efd42a4SStefano Zampini     Vec              U;
33165efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33175efd42a4SStefano Zampini 
33185efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
33195efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3320ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
33215efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3322dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
332372ac3e02SJed Brown   }
33243f2090d5SJed Brown   PetscFunctionReturn(0);
33253f2090d5SJed Brown }
33263f2090d5SJed Brown 
3327d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3328d763cef2SBarry Smith 
3329d763cef2SBarry Smith /*@C
3330a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3331d763cef2SBarry Smith    timestep to display the iteration's  progress.
3332d763cef2SBarry Smith 
33333f9fe445SBarry Smith    Logically Collective on TS
3334d763cef2SBarry Smith 
3335d763cef2SBarry Smith    Input Parameters:
3336d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3337e213d8f1SJed Brown .  monitor - monitoring routine
3338329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
33390298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3340b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
33410298fd71SBarry Smith           (may be NULL)
3342d763cef2SBarry Smith 
3343e213d8f1SJed Brown    Calling sequence of monitor:
3344dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3345d763cef2SBarry Smith 
3346d763cef2SBarry Smith +    ts - the TS context
334763e21af5SBarry 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)
33481f06c33eSBarry Smith .    time - current time
33490910c330SBarry Smith .    u - current iterate
3350d763cef2SBarry Smith -    mctx - [optional] monitoring context
3351d763cef2SBarry Smith 
3352d763cef2SBarry Smith    Notes:
3353d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3354d763cef2SBarry Smith    already has been loaded.
3355d763cef2SBarry Smith 
335695452b02SPatrick Sanan    Fortran Notes:
335795452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3358025f1a04SBarry Smith 
3359d763cef2SBarry Smith    Level: intermediate
3360d763cef2SBarry Smith 
3361d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3362d763cef2SBarry Smith 
3363a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3364d763cef2SBarry Smith @*/
3365c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3366d763cef2SBarry Smith {
336778064530SBarry Smith   PetscErrorCode ierr;
336878064530SBarry Smith   PetscInt       i;
336978064530SBarry Smith   PetscBool      identical;
337078064530SBarry Smith 
3371d763cef2SBarry Smith   PetscFunctionBegin;
33720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
337378064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
337478064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
337578064530SBarry Smith     if (identical) PetscFunctionReturn(0);
337678064530SBarry Smith   }
337717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3378d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
33798704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3380d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3381d763cef2SBarry Smith   PetscFunctionReturn(0);
3382d763cef2SBarry Smith }
3383d763cef2SBarry Smith 
3384d763cef2SBarry Smith /*@C
3385a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3386d763cef2SBarry Smith 
33873f9fe445SBarry Smith    Logically Collective on TS
3388d763cef2SBarry Smith 
3389d763cef2SBarry Smith    Input Parameters:
3390d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3391d763cef2SBarry Smith 
3392d763cef2SBarry Smith    Notes:
3393d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3394d763cef2SBarry Smith 
3395d763cef2SBarry Smith    Level: intermediate
3396d763cef2SBarry Smith 
3397d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3398d763cef2SBarry Smith 
3399a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3400d763cef2SBarry Smith @*/
34017087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3402d763cef2SBarry Smith {
3403d952e501SBarry Smith   PetscErrorCode ierr;
3404d952e501SBarry Smith   PetscInt       i;
3405d952e501SBarry Smith 
3406d763cef2SBarry Smith   PetscFunctionBegin;
34070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3408d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34098704b422SBarry Smith     if (ts->monitordestroy[i]) {
34108704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3411d952e501SBarry Smith     }
3412d952e501SBarry Smith   }
3413d763cef2SBarry Smith   ts->numbermonitors = 0;
3414d763cef2SBarry Smith   PetscFunctionReturn(0);
3415d763cef2SBarry Smith }
3416d763cef2SBarry Smith 
3417721cd6eeSBarry Smith /*@C
3418721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
34195516499fSSatish Balay 
34205516499fSSatish Balay    Level: intermediate
342141251cbbSSatish Balay 
34225516499fSSatish Balay .keywords: TS, set, monitor
34235516499fSSatish Balay 
342463e21af5SBarry Smith .seealso:  TSMonitorSet()
342541251cbbSSatish Balay @*/
3426721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3427d763cef2SBarry Smith {
3428dfbe8321SBarry Smith   PetscErrorCode ierr;
3429721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
343041aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3431d132466eSBarry Smith 
3432d763cef2SBarry Smith   PetscFunctionBegin;
34334d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
343441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
343541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3436721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
343741aca3d6SBarry Smith   if (iascii) {
3438649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
343963e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
344063e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
344163e21af5SBarry Smith     } else {
34428392e04aSShri 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);
344363e21af5SBarry Smith     }
3444649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
344541aca3d6SBarry Smith   } else if (ibinary) {
344641aca3d6SBarry Smith     PetscMPIInt rank;
344741aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
344841aca3d6SBarry Smith     if (!rank) {
3449450a797fSBarry Smith       PetscBool skipHeader;
3450450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3451450a797fSBarry Smith 
3452450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3453450a797fSBarry Smith       if (!skipHeader) {
3454450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3455450a797fSBarry Smith        }
345641aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
345741aca3d6SBarry Smith     } else {
345841aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
345941aca3d6SBarry Smith     }
346041aca3d6SBarry Smith   }
3461721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3462d763cef2SBarry Smith   PetscFunctionReturn(0);
3463d763cef2SBarry Smith }
3464d763cef2SBarry Smith 
3465*cc9c3a59SBarry Smith /*@C
3466*cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3467*cc9c3a59SBarry Smith 
3468*cc9c3a59SBarry Smith    Level: intermediate
3469*cc9c3a59SBarry Smith 
3470*cc9c3a59SBarry Smith .keywords: TS, set, monitor
3471*cc9c3a59SBarry Smith 
3472*cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3473*cc9c3a59SBarry Smith @*/
3474*cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3475*cc9c3a59SBarry Smith {
3476*cc9c3a59SBarry Smith   PetscErrorCode ierr;
3477*cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3478*cc9c3a59SBarry Smith   PetscBool      iascii;
3479*cc9c3a59SBarry Smith   PetscReal      max,min;
3480*cc9c3a59SBarry Smith 
3481*cc9c3a59SBarry Smith 
3482*cc9c3a59SBarry Smith   PetscFunctionBegin;
3483*cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3484*cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3485*cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3486*cc9c3a59SBarry Smith   if (iascii) {
3487*cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3488*cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3489*cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3490*cc9c3a59SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s max %g min %g",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)" : "",(double)max,(double)min);CHKERRQ(ierr);
3491*cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3492*cc9c3a59SBarry Smith   }
3493*cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3494*cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3495*cc9c3a59SBarry Smith }
3496*cc9c3a59SBarry Smith 
3497cd652676SJed Brown /*@
3498cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3499cd652676SJed Brown 
3500cd652676SJed Brown    Collective on TS
3501cd652676SJed Brown 
3502cd652676SJed Brown    Input Argument:
3503cd652676SJed Brown +  ts - time stepping context
3504cd652676SJed Brown -  t - time to interpolate to
3505cd652676SJed Brown 
3506cd652676SJed Brown    Output Argument:
35070910c330SBarry Smith .  U - state at given time
3508cd652676SJed Brown 
3509cd652676SJed Brown    Level: intermediate
3510cd652676SJed Brown 
3511cd652676SJed Brown    Developer Notes:
3512cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3513cd652676SJed Brown 
3514cd652676SJed Brown .keywords: TS, set
3515cd652676SJed Brown 
3516874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3517cd652676SJed Brown @*/
35180910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3519cd652676SJed Brown {
3520cd652676SJed Brown   PetscErrorCode ierr;
3521cd652676SJed Brown 
3522cd652676SJed Brown   PetscFunctionBegin;
3523cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3524b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3525be5899b3SLisandro 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);
3526ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35270910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3528cd652676SJed Brown   PetscFunctionReturn(0);
3529cd652676SJed Brown }
3530cd652676SJed Brown 
3531d763cef2SBarry Smith /*@
35326d9e5789SSean Farley    TSStep - Steps one time step
3533d763cef2SBarry Smith 
3534d763cef2SBarry Smith    Collective on TS
3535d763cef2SBarry Smith 
3536d763cef2SBarry Smith    Input Parameter:
3537d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3538d763cef2SBarry Smith 
353927829d71SBarry Smith    Level: developer
3540d763cef2SBarry Smith 
3541b8123daeSJed Brown    Notes:
354227829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
354327829d71SBarry Smith 
3544b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3545b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3546b8123daeSJed Brown 
354719eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
354819eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
354925cb2221SBarry Smith 
3550d763cef2SBarry Smith .keywords: TS, timestep, solve
3551d763cef2SBarry Smith 
35529be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3553d763cef2SBarry Smith @*/
3554193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3555d763cef2SBarry Smith {
3556dfbe8321SBarry Smith   PetscErrorCode   ierr;
3557fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3558be5899b3SLisandro Dalcin   PetscReal        ptime;
3559d763cef2SBarry Smith 
3560d763cef2SBarry Smith   PetscFunctionBegin;
35610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3562fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3563fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3564fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3565fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3566fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3567fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3568302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3569fffbeea8SBarry Smith 
3570d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
357168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3572d405a339SMatthew Knepley 
3573ef85077eSLisandro 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>");
3574a6772fa2SLisandro 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()");
3575be5899b3SLisandro 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");
3576a6772fa2SLisandro Dalcin 
3577be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3578be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
35792808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3580e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3581d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3582193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3583d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3584be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
35852808aa04SLisandro Dalcin   ts->steps++;
3586be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
358728d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3588362cd11cSLisandro Dalcin 
3589362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3590cef5090cSJed Brown     if (ts->errorifstepfailed) {
359108c7845fSBarry 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]);
359208c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3593d2daff3dSHong Zhang     }
359408c7845fSBarry Smith   } else if (!ts->reason) {
359508c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
359608c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
359708c7845fSBarry Smith   }
359808c7845fSBarry Smith   PetscFunctionReturn(0);
359908c7845fSBarry Smith }
360008c7845fSBarry Smith 
360108c7845fSBarry Smith /*@
36027cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36037cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36047cbde773SLisandro Dalcin 
36057cbde773SLisandro Dalcin    Collective on TS
36067cbde773SLisandro Dalcin 
36077cbde773SLisandro Dalcin    Input Arguments:
36087cbde773SLisandro Dalcin +  ts - time stepping context
36097cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36107cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36117cbde773SLisandro Dalcin 
36127cbde773SLisandro Dalcin    Output Arguments:
36137cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36147cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36157cbde773SLisandro Dalcin 
36167cbde773SLisandro Dalcin    Level: advanced
36177cbde773SLisandro Dalcin 
36187cbde773SLisandro Dalcin    Notes:
36197cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36207cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36217cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36227cbde773SLisandro Dalcin 
36237cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36247cbde773SLisandro Dalcin @*/
36257cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36267cbde773SLisandro Dalcin {
36277cbde773SLisandro Dalcin   PetscErrorCode ierr;
36287cbde773SLisandro Dalcin 
36297cbde773SLisandro Dalcin   PetscFunctionBegin;
36307cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36317cbde773SLisandro Dalcin   PetscValidType(ts,1);
36327cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
36337cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36347cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36357cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36367cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36377cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36387cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36397cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36407cbde773SLisandro Dalcin }
36417cbde773SLisandro Dalcin 
364205175c85SJed Brown /*@
364305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
364405175c85SJed Brown 
36451c3436cfSJed Brown    Collective on TS
364605175c85SJed Brown 
364705175c85SJed Brown    Input Arguments:
36481c3436cfSJed Brown +  ts - time stepping context
36491c3436cfSJed Brown .  order - desired order of accuracy
36500298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
365105175c85SJed Brown 
365205175c85SJed Brown    Output Arguments:
36530910c330SBarry Smith .  U - state at the end of the current step
365405175c85SJed Brown 
365505175c85SJed Brown    Level: advanced
365605175c85SJed Brown 
3657108c343cSJed Brown    Notes:
3658108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3659108c343cSJed 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.
3660108c343cSJed Brown 
36611c3436cfSJed Brown .seealso: TSStep(), TSAdapt
366205175c85SJed Brown @*/
36630910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
366405175c85SJed Brown {
366505175c85SJed Brown   PetscErrorCode ierr;
366605175c85SJed Brown 
366705175c85SJed Brown   PetscFunctionBegin;
366805175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
366905175c85SJed Brown   PetscValidType(ts,1);
36700910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3671ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36720910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
367305175c85SJed Brown   PetscFunctionReturn(0);
367405175c85SJed Brown }
367505175c85SJed Brown 
3676b1cb36f3SHong Zhang /*@
36776a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
36786a4d4014SLisandro Dalcin 
36796a4d4014SLisandro Dalcin    Collective on TS
36806a4d4014SLisandro Dalcin 
36816a4d4014SLisandro Dalcin    Input Parameter:
36826a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
368363e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
368463e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
36855a3a76d0SJed Brown 
36866a4d4014SLisandro Dalcin    Level: beginner
36876a4d4014SLisandro Dalcin 
36885a3a76d0SJed Brown    Notes:
36895a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
36905a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
36915a3a76d0SJed Brown    stepped over the final time.
36925a3a76d0SJed Brown 
36936a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
36946a4d4014SLisandro Dalcin 
369563e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
36966a4d4014SLisandro Dalcin @*/
3697cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
36986a4d4014SLisandro Dalcin {
3699b06615a5SLisandro Dalcin   Vec               solution;
37006a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3701f22f69f0SBarry Smith 
37026a4d4014SLisandro Dalcin   PetscFunctionBegin;
37030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3704f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3705feed9e9dSBarry Smith 
3706ee41a567SStefano 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 */
37070910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3708b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3709b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3710b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
37115a3a76d0SJed Brown     }
37120910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3713715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3714bbd56ea5SKarl Rupp   } else if (u) {
37150910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
37165a3a76d0SJed Brown   }
3717b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
371868bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3719a6772fa2SLisandro Dalcin 
3720ef85077eSLisandro 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>");
3721a6772fa2SLisandro 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()");
3722a6772fa2SLisandro 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");
3723a6772fa2SLisandro Dalcin 
3724715f1b00SHong Zhang   if (ts->forward_solve) {
3725715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3726715f1b00SHong Zhang   }
3727715f1b00SHong Zhang 
3728e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3729715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3730e7069c78SShri      TSTrajectory to incorrectly save the output files
3731e7069c78SShri   */
3732715f1b00SHong Zhang   /* reset time step and iteration counters */
37332808aa04SLisandro Dalcin   if (!ts->steps) {
37345ef26d82SJed Brown     ts->ksp_its           = 0;
37355ef26d82SJed Brown     ts->snes_its          = 0;
3736c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3737c610991cSLisandro Dalcin     ts->reject            = 0;
37382808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
37392808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
37402808aa04SLisandro Dalcin   }
374110bc0e4dSStefano 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;
3742193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3743193ac0bcSJed Brown 
3744ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3745f05ece33SBarry Smith 
3746193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3747193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3748a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3749cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3750a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3751be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3752db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3753db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3754e7069c78SShri 
37552808aa04SLisandro Dalcin     if (!ts->steps) {
37562808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37576427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37582808aa04SLisandro Dalcin     }
37596427ac75SLisandro Dalcin 
3760e1a7a14fSJed Brown     while (!ts->reason) {
37612808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37629687d888SLisandro Dalcin       if (!ts->steprollback) {
37639687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
37649687d888SLisandro Dalcin       }
3765193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3766f3b1f45cSBarry Smith       if (ts->testjacobian) {
3767f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3768f3b1f45cSBarry Smith       }
3769f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3770f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3771f3b1f45cSBarry Smith       }
3772e783b05fSHong 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. */
3773b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3774b1cb36f3SHong Zhang       }
377558818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3776715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3777715f1b00SHong Zhang       }
377810b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3779e783b05fSHong 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. */
378058818c2dSLisandro Dalcin       if (ts->steprollback) {
378158818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
378258818c2dSLisandro Dalcin       }
3783e783b05fSHong Zhang       if (!ts->steprollback) {
37842808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37851eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3786aeb4809dSShri Abhyankar       }
3787193ac0bcSJed Brown     }
37882808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37896427ac75SLisandro Dalcin 
379049354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
37910910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3792cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3793b06615a5SLisandro Dalcin       solution = u;
379463e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
37950574a7fbSJed Brown     } else {
3796ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3797cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3798b06615a5SLisandro Dalcin       solution = ts->vec_sol;
37990574a7fbSJed Brown     }
3800193ac0bcSJed Brown   }
3801d2daff3dSHong Zhang 
3802ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
380363e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
380456f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3805ef222394SBarry Smith   if (ts->adjoint_solve) {
3806ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38072b0a91c0SBarry Smith   }
38086a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38096a4d4014SLisandro Dalcin }
38106a4d4014SLisandro Dalcin 
38117db568b7SBarry Smith /*@C
3812228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3813228d79bcSJed Brown 
3814228d79bcSJed Brown    Collective on TS
3815228d79bcSJed Brown 
3816228d79bcSJed Brown    Input Parameters:
3817228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3818228d79bcSJed Brown .  step - step number that has just completed
3819228d79bcSJed Brown .  ptime - model time of the state
38200910c330SBarry Smith -  u - state at the current model time
3821228d79bcSJed Brown 
3822228d79bcSJed Brown    Notes:
38237db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
38247db568b7SBarry Smith    Users would almost never call this routine directly.
3825228d79bcSJed Brown 
382663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
382763e21af5SBarry Smith 
38287db568b7SBarry Smith    Level: developer
3829228d79bcSJed Brown 
3830228d79bcSJed Brown .keywords: TS, timestep
3831228d79bcSJed Brown @*/
38320910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3833d763cef2SBarry Smith {
3834d6152f81SLisandro Dalcin   DM             dm;
3835a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3836d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3837d763cef2SBarry Smith 
3838d763cef2SBarry Smith   PetscFunctionBegin;
3839b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3840b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3841d6152f81SLisandro Dalcin 
3842d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3843d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3844d6152f81SLisandro Dalcin 
38455edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3846d763cef2SBarry Smith   for (i=0; i<n; i++) {
38470910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3848d763cef2SBarry Smith   }
38495edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3850d763cef2SBarry Smith   PetscFunctionReturn(0);
3851d763cef2SBarry Smith }
3852d763cef2SBarry Smith 
3853d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3854d763cef2SBarry Smith /*@C
38557db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3856a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3857d763cef2SBarry Smith 
3858d763cef2SBarry Smith    Collective on TS
3859d763cef2SBarry Smith 
3860d763cef2SBarry Smith    Input Parameters:
3861d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3862d763cef2SBarry Smith .  label - the title to put in the title bar
38637c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3864a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3865a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3866d763cef2SBarry Smith 
3867d763cef2SBarry Smith    Output Parameter:
38680b039ecaSBarry Smith .  ctx - the context
3869d763cef2SBarry Smith 
3870d763cef2SBarry Smith    Options Database Key:
38714f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
38728b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
38737db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
38747db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
38757db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
38767db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3877b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3878d763cef2SBarry Smith 
3879d763cef2SBarry Smith    Notes:
3880a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3881d763cef2SBarry Smith 
38827db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
38837db568b7SBarry Smith 
38847db568b7SBarry 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
38857db568b7SBarry 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
38867db568b7SBarry Smith    as the first argument.
38877db568b7SBarry Smith 
38887db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
38897db568b7SBarry Smith 
3890d763cef2SBarry Smith    Level: intermediate
3891d763cef2SBarry Smith 
38927db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3893d763cef2SBarry Smith 
38947db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
38957db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
38967db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
38977db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
38987db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
38997c922b88SBarry Smith 
3900d763cef2SBarry Smith @*/
3901a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3902d763cef2SBarry Smith {
39037f52daa2SLisandro Dalcin   PetscDraw      draw;
3904dfbe8321SBarry Smith   PetscErrorCode ierr;
3905d763cef2SBarry Smith 
3906d763cef2SBarry Smith   PetscFunctionBegin;
3907b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39087f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39097f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39107f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3911287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
39127f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3913a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3914d763cef2SBarry Smith   PetscFunctionReturn(0);
3915d763cef2SBarry Smith }
3916d763cef2SBarry Smith 
3917b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3918d763cef2SBarry Smith {
39190b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3920c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3921dfbe8321SBarry Smith   PetscErrorCode ierr;
3922d763cef2SBarry Smith 
3923d763cef2SBarry Smith   PetscFunctionBegin;
392463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3925b06615a5SLisandro Dalcin   if (!step) {
3926a9f9c1f6SBarry Smith     PetscDrawAxis axis;
39278b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
3928a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
39298b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
3930a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3931a9f9c1f6SBarry Smith   }
3932c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
39338b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
39340b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3935b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
39360b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
39376934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
39383923b477SBarry Smith   }
3939d763cef2SBarry Smith   PetscFunctionReturn(0);
3940d763cef2SBarry Smith }
3941d763cef2SBarry Smith 
3942d763cef2SBarry Smith /*@C
3943a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3944a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3945d763cef2SBarry Smith 
39460b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3947d763cef2SBarry Smith 
3948d763cef2SBarry Smith    Input Parameter:
39490b039ecaSBarry Smith .  ctx - the monitor context
3950d763cef2SBarry Smith 
3951d763cef2SBarry Smith    Level: intermediate
3952d763cef2SBarry Smith 
3953d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3954d763cef2SBarry Smith 
39554f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3956d763cef2SBarry Smith @*/
3957a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3958d763cef2SBarry Smith {
3959dfbe8321SBarry Smith   PetscErrorCode ierr;
3960d763cef2SBarry Smith 
3961d763cef2SBarry Smith   PetscFunctionBegin;
39627684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
39637684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
39647684fa3eSBarry Smith   }
39650b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
396631152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3967387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3968387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3969387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
39700b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3971d763cef2SBarry Smith   PetscFunctionReturn(0);
3972d763cef2SBarry Smith }
3973d763cef2SBarry Smith 
3974d763cef2SBarry Smith /*@
3975b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3976d763cef2SBarry Smith 
3977d763cef2SBarry Smith    Not Collective
3978d763cef2SBarry Smith 
3979d763cef2SBarry Smith    Input Parameter:
3980d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3981d763cef2SBarry Smith 
3982d763cef2SBarry Smith    Output Parameter:
398319eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
3984d763cef2SBarry Smith 
3985d763cef2SBarry Smith    Level: beginner
3986d763cef2SBarry Smith 
3987b8123daeSJed Brown    Note:
3988b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
39899be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3990b8123daeSJed Brown 
3991aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
3992d763cef2SBarry Smith 
3993d763cef2SBarry Smith .keywords: TS, get, time
3994d763cef2SBarry Smith @*/
39957087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3996d763cef2SBarry Smith {
3997d763cef2SBarry Smith   PetscFunctionBegin;
39980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3999f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4000d763cef2SBarry Smith   *t = ts->ptime;
4001d763cef2SBarry Smith   PetscFunctionReturn(0);
4002d763cef2SBarry Smith }
4003d763cef2SBarry Smith 
4004e5e524a1SHong Zhang /*@
4005e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4006e5e524a1SHong Zhang 
4007e5e524a1SHong Zhang    Not Collective
4008e5e524a1SHong Zhang 
4009e5e524a1SHong Zhang    Input Parameter:
4010e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4011e5e524a1SHong Zhang 
4012e5e524a1SHong Zhang    Output Parameter:
4013e5e524a1SHong Zhang .  t  - the previous time
4014e5e524a1SHong Zhang 
4015e5e524a1SHong Zhang    Level: beginner
4016e5e524a1SHong Zhang 
4017aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4018e5e524a1SHong Zhang 
4019e5e524a1SHong Zhang .keywords: TS, get, time
4020e5e524a1SHong Zhang @*/
4021e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4022e5e524a1SHong Zhang {
4023e5e524a1SHong Zhang   PetscFunctionBegin;
4024e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4025e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4026e5e524a1SHong Zhang   *t = ts->ptime_prev;
4027e5e524a1SHong Zhang   PetscFunctionReturn(0);
4028e5e524a1SHong Zhang }
4029e5e524a1SHong Zhang 
40306a4d4014SLisandro Dalcin /*@
40316a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40326a4d4014SLisandro Dalcin 
40333f9fe445SBarry Smith    Logically Collective on TS
40346a4d4014SLisandro Dalcin 
40356a4d4014SLisandro Dalcin    Input Parameters:
40366a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40376a4d4014SLisandro Dalcin -  time - the time
40386a4d4014SLisandro Dalcin 
40396a4d4014SLisandro Dalcin    Level: intermediate
40406a4d4014SLisandro Dalcin 
404119eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40426a4d4014SLisandro Dalcin 
40436a4d4014SLisandro Dalcin .keywords: TS, set, time
40446a4d4014SLisandro Dalcin @*/
40457087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40466a4d4014SLisandro Dalcin {
40476a4d4014SLisandro Dalcin   PetscFunctionBegin;
40480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4049c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40506a4d4014SLisandro Dalcin   ts->ptime = t;
40516a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40526a4d4014SLisandro Dalcin }
40536a4d4014SLisandro Dalcin 
4054d763cef2SBarry Smith /*@C
4055d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4056d763cef2SBarry Smith    TS options in the database.
4057d763cef2SBarry Smith 
40583f9fe445SBarry Smith    Logically Collective on TS
4059d763cef2SBarry Smith 
4060d763cef2SBarry Smith    Input Parameter:
4061d763cef2SBarry Smith +  ts     - The TS context
4062d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4063d763cef2SBarry Smith 
4064d763cef2SBarry Smith    Notes:
4065d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4066d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4067d763cef2SBarry Smith    hyphen.
4068d763cef2SBarry Smith 
4069d763cef2SBarry Smith    Level: advanced
4070d763cef2SBarry Smith 
4071d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4072d763cef2SBarry Smith 
4073d763cef2SBarry Smith .seealso: TSSetFromOptions()
4074d763cef2SBarry Smith 
4075d763cef2SBarry Smith @*/
40767087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4077d763cef2SBarry Smith {
4078dfbe8321SBarry Smith   PetscErrorCode ierr;
4079089b2837SJed Brown   SNES           snes;
4080d763cef2SBarry Smith 
4081d763cef2SBarry Smith   PetscFunctionBegin;
40820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4083d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4084089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4085089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4086d763cef2SBarry Smith   PetscFunctionReturn(0);
4087d763cef2SBarry Smith }
4088d763cef2SBarry Smith 
4089d763cef2SBarry Smith /*@C
4090d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4091d763cef2SBarry Smith    TS options in the database.
4092d763cef2SBarry Smith 
40933f9fe445SBarry Smith    Logically Collective on TS
4094d763cef2SBarry Smith 
4095d763cef2SBarry Smith    Input Parameter:
4096d763cef2SBarry Smith +  ts     - The TS context
4097d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4098d763cef2SBarry Smith 
4099d763cef2SBarry Smith    Notes:
4100d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4101d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4102d763cef2SBarry Smith    hyphen.
4103d763cef2SBarry Smith 
4104d763cef2SBarry Smith    Level: advanced
4105d763cef2SBarry Smith 
4106d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4107d763cef2SBarry Smith 
4108d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4109d763cef2SBarry Smith 
4110d763cef2SBarry Smith @*/
41117087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4112d763cef2SBarry Smith {
4113dfbe8321SBarry Smith   PetscErrorCode ierr;
4114089b2837SJed Brown   SNES           snes;
4115d763cef2SBarry Smith 
4116d763cef2SBarry Smith   PetscFunctionBegin;
41170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4118d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4119089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4120089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4121d763cef2SBarry Smith   PetscFunctionReturn(0);
4122d763cef2SBarry Smith }
4123d763cef2SBarry Smith 
4124d763cef2SBarry Smith /*@C
4125d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4126d763cef2SBarry Smith    TS options in the database.
4127d763cef2SBarry Smith 
4128d763cef2SBarry Smith    Not Collective
4129d763cef2SBarry Smith 
4130d763cef2SBarry Smith    Input Parameter:
4131d763cef2SBarry Smith .  ts - The TS context
4132d763cef2SBarry Smith 
4133d763cef2SBarry Smith    Output Parameter:
4134d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4135d763cef2SBarry Smith 
413695452b02SPatrick Sanan    Notes:
413795452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4138d763cef2SBarry Smith    sufficient length to hold the prefix.
4139d763cef2SBarry Smith 
4140d763cef2SBarry Smith    Level: intermediate
4141d763cef2SBarry Smith 
4142d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4143d763cef2SBarry Smith 
4144d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4145d763cef2SBarry Smith @*/
41467087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4147d763cef2SBarry Smith {
4148dfbe8321SBarry Smith   PetscErrorCode ierr;
4149d763cef2SBarry Smith 
4150d763cef2SBarry Smith   PetscFunctionBegin;
41510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41524482741eSBarry Smith   PetscValidPointer(prefix,2);
4153d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4154d763cef2SBarry Smith   PetscFunctionReturn(0);
4155d763cef2SBarry Smith }
4156d763cef2SBarry Smith 
4157d763cef2SBarry Smith /*@C
4158d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4159d763cef2SBarry Smith 
4160d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4161d763cef2SBarry Smith 
4162d763cef2SBarry Smith    Input Parameter:
4163d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4164d763cef2SBarry Smith 
4165d763cef2SBarry Smith    Output Parameters:
4166e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4167e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4168e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4169e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4170d763cef2SBarry Smith 
417195452b02SPatrick Sanan    Notes:
417295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4173d763cef2SBarry Smith 
4174d763cef2SBarry Smith    Level: intermediate
4175d763cef2SBarry Smith 
417680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4177d763cef2SBarry Smith 
4178d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4179d763cef2SBarry Smith @*/
4180e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4181d763cef2SBarry Smith {
4182089b2837SJed Brown   PetscErrorCode ierr;
418324989b8cSPeter Brune   DM             dm;
4184089b2837SJed Brown 
4185d763cef2SBarry Smith   PetscFunctionBegin;
418623a57915SBarry Smith   if (Amat || Pmat) {
418723a57915SBarry Smith     SNES snes;
4188089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
418923a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4190e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
419123a57915SBarry Smith   }
419224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
419324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4194d763cef2SBarry Smith   PetscFunctionReturn(0);
4195d763cef2SBarry Smith }
4196d763cef2SBarry Smith 
41972eca1d9cSJed Brown /*@C
41982eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
41992eca1d9cSJed Brown 
42002eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42012eca1d9cSJed Brown 
42022eca1d9cSJed Brown    Input Parameter:
42032eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42042eca1d9cSJed Brown 
42052eca1d9cSJed Brown    Output Parameters:
4206e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4207e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42082eca1d9cSJed Brown .  f   - The function to compute the matrices
42092eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42102eca1d9cSJed Brown 
421195452b02SPatrick Sanan    Notes:
421295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42132eca1d9cSJed Brown 
42142eca1d9cSJed Brown    Level: advanced
42152eca1d9cSJed Brown 
421680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42172eca1d9cSJed Brown 
42182eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
42192eca1d9cSJed Brown @*/
4220e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42212eca1d9cSJed Brown {
4222089b2837SJed Brown   PetscErrorCode ierr;
422324989b8cSPeter Brune   DM             dm;
42240910c330SBarry Smith 
42252eca1d9cSJed Brown   PetscFunctionBegin;
4226c0aab802Sstefano_zampini   if (Amat || Pmat) {
4227c0aab802Sstefano_zampini     SNES snes;
4228089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4229f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4230e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4231c0aab802Sstefano_zampini   }
423224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
423324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
42342eca1d9cSJed Brown   PetscFunctionReturn(0);
42352eca1d9cSJed Brown }
42362eca1d9cSJed Brown 
42371713a123SBarry Smith /*@C
423883a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
42391713a123SBarry Smith    VecView() for the solution at each timestep
42401713a123SBarry Smith 
42411713a123SBarry Smith    Collective on TS
42421713a123SBarry Smith 
42431713a123SBarry Smith    Input Parameters:
42441713a123SBarry Smith +  ts - the TS context
42451713a123SBarry Smith .  step - current time-step
4246142b95e3SSatish Balay .  ptime - current time
42470298fd71SBarry Smith -  dummy - either a viewer or NULL
42481713a123SBarry Smith 
424999fdda47SBarry Smith    Options Database:
425099fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
425199fdda47SBarry Smith 
425295452b02SPatrick Sanan    Notes:
425395452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
425499fdda47SBarry Smith        will look bad
425599fdda47SBarry Smith 
42561713a123SBarry Smith    Level: intermediate
42571713a123SBarry Smith 
42581713a123SBarry Smith .keywords: TS,  vector, monitor, view
42591713a123SBarry Smith 
4260a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42611713a123SBarry Smith @*/
42620910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42631713a123SBarry Smith {
4264dfbe8321SBarry Smith   PetscErrorCode   ierr;
426583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4266473a3ab2SBarry Smith   PetscDraw        draw;
42671713a123SBarry Smith 
42681713a123SBarry Smith   PetscFunctionBegin;
42696083293cSBarry Smith   if (!step && ictx->showinitial) {
42706083293cSBarry Smith     if (!ictx->initialsolution) {
42710910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
42721713a123SBarry Smith     }
42730910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
42746083293cSBarry Smith   }
4275b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42760dcf80beSBarry Smith 
42776083293cSBarry Smith   if (ictx->showinitial) {
42786083293cSBarry Smith     PetscReal pause;
42796083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
42806083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
42816083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
42826083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
42836083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
42846083293cSBarry Smith   }
42850910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4286473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
428751fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4288473a3ab2SBarry Smith     char      time[32];
4289473a3ab2SBarry Smith 
4290473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
429185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4292473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4293473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
429451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4295473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4296473a3ab2SBarry Smith   }
4297473a3ab2SBarry Smith 
42986083293cSBarry Smith   if (ictx->showinitial) {
42996083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
43006083293cSBarry Smith   }
43011713a123SBarry Smith   PetscFunctionReturn(0);
43021713a123SBarry Smith }
43031713a123SBarry Smith 
43049110b2e7SHong Zhang /*@C
43052d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43062d5ee99bSBarry Smith 
43072d5ee99bSBarry Smith    Collective on TS
43082d5ee99bSBarry Smith 
43092d5ee99bSBarry Smith    Input Parameters:
43102d5ee99bSBarry Smith +  ts - the TS context
43112d5ee99bSBarry Smith .  step - current time-step
43122d5ee99bSBarry Smith .  ptime - current time
43132d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43142d5ee99bSBarry Smith 
43152d5ee99bSBarry Smith    Level: intermediate
43162d5ee99bSBarry Smith 
43172d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
43182d5ee99bSBarry Smith 
43192d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43202d5ee99bSBarry Smith @*/
43212d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43222d5ee99bSBarry Smith {
43232d5ee99bSBarry Smith   PetscErrorCode    ierr;
43242d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43252d5ee99bSBarry Smith   PetscDraw         draw;
43266934998bSLisandro Dalcin   PetscDrawAxis     axis;
43272d5ee99bSBarry Smith   PetscInt          n;
43282d5ee99bSBarry Smith   PetscMPIInt       size;
43296934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43302d5ee99bSBarry Smith   char              time[32];
43312d5ee99bSBarry Smith   const PetscScalar *U;
43322d5ee99bSBarry Smith 
43332d5ee99bSBarry Smith   PetscFunctionBegin;
43346934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43356934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43362d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43376934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43382d5ee99bSBarry Smith 
43392d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43406934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43416934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43426934998bSLisandro Dalcin   if (!step) {
43436934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43446934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
43456934998bSLisandro Dalcin   }
43462d5ee99bSBarry Smith 
43472d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
43486934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
43496934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4350a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43516934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
43522d5ee99bSBarry Smith 
43536934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
43546934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
43552d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43564b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
435785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43582d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
435951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43602d5ee99bSBarry Smith   }
43616934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
43622d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
436356d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
43646934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
43652d5ee99bSBarry Smith   PetscFunctionReturn(0);
43662d5ee99bSBarry Smith }
43672d5ee99bSBarry Smith 
43686083293cSBarry Smith /*@C
436983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43706083293cSBarry Smith 
43716083293cSBarry Smith    Collective on TS
43726083293cSBarry Smith 
43736083293cSBarry Smith    Input Parameters:
43746083293cSBarry Smith .    ctx - the monitor context
43756083293cSBarry Smith 
43766083293cSBarry Smith    Level: intermediate
43776083293cSBarry Smith 
43786083293cSBarry Smith .keywords: TS,  vector, monitor, view
43796083293cSBarry Smith 
438083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
43816083293cSBarry Smith @*/
438283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
43836083293cSBarry Smith {
43846083293cSBarry Smith   PetscErrorCode ierr;
43856083293cSBarry Smith 
43866083293cSBarry Smith   PetscFunctionBegin;
438783a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
438883a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
438983a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
43906083293cSBarry Smith   PetscFunctionReturn(0);
43916083293cSBarry Smith }
43926083293cSBarry Smith 
43936083293cSBarry Smith /*@C
439483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
43956083293cSBarry Smith 
43966083293cSBarry Smith    Collective on TS
43976083293cSBarry Smith 
43986083293cSBarry Smith    Input Parameter:
43996083293cSBarry Smith .    ts - time-step context
44006083293cSBarry Smith 
44016083293cSBarry Smith    Output Patameter:
44026083293cSBarry Smith .    ctx - the monitor context
44036083293cSBarry Smith 
440499fdda47SBarry Smith    Options Database:
440599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
440699fdda47SBarry Smith 
44076083293cSBarry Smith    Level: intermediate
44086083293cSBarry Smith 
44096083293cSBarry Smith .keywords: TS,  vector, monitor, view
44106083293cSBarry Smith 
441183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44126083293cSBarry Smith @*/
441383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44146083293cSBarry Smith {
44156083293cSBarry Smith   PetscErrorCode   ierr;
44166083293cSBarry Smith 
44176083293cSBarry Smith   PetscFunctionBegin;
4418b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
441983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4420e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4421bbd56ea5SKarl Rupp 
442283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4423473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4424c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4425473a3ab2SBarry Smith 
4426473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4427c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44286083293cSBarry Smith   PetscFunctionReturn(0);
44296083293cSBarry Smith }
44306083293cSBarry Smith 
44313a471f94SBarry Smith /*@C
44320ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
44330ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
44340ed3bfb6SBarry Smith 
44350ed3bfb6SBarry Smith    Collective on TS
44360ed3bfb6SBarry Smith 
44370ed3bfb6SBarry Smith    Input Parameters:
44380ed3bfb6SBarry Smith +  ts - the TS context
44390ed3bfb6SBarry Smith .  step - current time-step
44400ed3bfb6SBarry Smith .  ptime - current time
44410ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
44420ed3bfb6SBarry Smith 
44430ed3bfb6SBarry Smith    Options Database:
44440ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44450ed3bfb6SBarry Smith 
44460ed3bfb6SBarry Smith    Level: intermediate
44470ed3bfb6SBarry Smith 
44480ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
44490ed3bfb6SBarry Smith 
44500ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44510ed3bfb6SBarry Smith @*/
44520ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44530ed3bfb6SBarry Smith {
44540ed3bfb6SBarry Smith   PetscErrorCode   ierr;
44550ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
44560ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
44570ed3bfb6SBarry Smith   Vec              work;
44580ed3bfb6SBarry Smith 
44590ed3bfb6SBarry Smith   PetscFunctionBegin;
44600ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44610ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44620ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44630ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44640ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44650ed3bfb6SBarry Smith   PetscFunctionReturn(0);
44660ed3bfb6SBarry Smith }
44670ed3bfb6SBarry Smith 
44680ed3bfb6SBarry Smith /*@C
446983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
44703a471f94SBarry Smith    VecView() for the error at each timestep
44713a471f94SBarry Smith 
44723a471f94SBarry Smith    Collective on TS
44733a471f94SBarry Smith 
44743a471f94SBarry Smith    Input Parameters:
44753a471f94SBarry Smith +  ts - the TS context
44763a471f94SBarry Smith .  step - current time-step
44773a471f94SBarry Smith .  ptime - current time
44780298fd71SBarry Smith -  dummy - either a viewer or NULL
44793a471f94SBarry Smith 
44800ed3bfb6SBarry Smith    Options Database:
44810ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44820ed3bfb6SBarry Smith 
44833a471f94SBarry Smith    Level: intermediate
44843a471f94SBarry Smith 
44853a471f94SBarry Smith .keywords: TS,  vector, monitor, view
44863a471f94SBarry Smith 
44870ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44883a471f94SBarry Smith @*/
44890910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44903a471f94SBarry Smith {
44913a471f94SBarry Smith   PetscErrorCode   ierr;
449283a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
449383a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
44943a471f94SBarry Smith   Vec              work;
44953a471f94SBarry Smith 
44963a471f94SBarry Smith   PetscFunctionBegin;
4497b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44980910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44993a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45000910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
45013a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45023a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45033a471f94SBarry Smith   PetscFunctionReturn(0);
45043a471f94SBarry Smith }
45053a471f94SBarry Smith 
4506af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
45076c699258SBarry Smith /*@
45082a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
45096c699258SBarry Smith 
45103f9fe445SBarry Smith    Logically Collective on TS and DM
45116c699258SBarry Smith 
45126c699258SBarry Smith    Input Parameters:
45132a808120SBarry Smith +  ts - the ODE integrator object
45142a808120SBarry Smith -  dm - the dm, cannot be NULL
45156c699258SBarry Smith 
45166c699258SBarry Smith    Level: intermediate
45176c699258SBarry Smith 
45186c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
45196c699258SBarry Smith @*/
45207087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
45216c699258SBarry Smith {
45226c699258SBarry Smith   PetscErrorCode ierr;
4523089b2837SJed Brown   SNES           snes;
4524942e3340SBarry Smith   DMTS           tsdm;
45256c699258SBarry Smith 
45266c699258SBarry Smith   PetscFunctionBegin;
45270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45282a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
452970663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4530942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
45312a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4532942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4533942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
453424989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
453524989b8cSPeter Brune         tsdm->originaldm = dm;
453624989b8cSPeter Brune       }
453724989b8cSPeter Brune     }
45386bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
453924989b8cSPeter Brune   }
45406c699258SBarry Smith   ts->dm = dm;
4541bbd56ea5SKarl Rupp 
4542089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4543089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
45446c699258SBarry Smith   PetscFunctionReturn(0);
45456c699258SBarry Smith }
45466c699258SBarry Smith 
45476c699258SBarry Smith /*@
45486c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
45496c699258SBarry Smith 
45503f9fe445SBarry Smith    Not Collective
45516c699258SBarry Smith 
45526c699258SBarry Smith    Input Parameter:
45536c699258SBarry Smith . ts - the preconditioner context
45546c699258SBarry Smith 
45556c699258SBarry Smith    Output Parameter:
45566c699258SBarry Smith .  dm - the dm
45576c699258SBarry Smith 
45586c699258SBarry Smith    Level: intermediate
45596c699258SBarry Smith 
45606c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
45616c699258SBarry Smith @*/
45627087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
45636c699258SBarry Smith {
4564496e6a7aSJed Brown   PetscErrorCode ierr;
4565496e6a7aSJed Brown 
45666c699258SBarry Smith   PetscFunctionBegin;
45670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4568496e6a7aSJed Brown   if (!ts->dm) {
4569ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4570496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4571496e6a7aSJed Brown   }
45726c699258SBarry Smith   *dm = ts->dm;
45736c699258SBarry Smith   PetscFunctionReturn(0);
45746c699258SBarry Smith }
45751713a123SBarry Smith 
45760f5c6efeSJed Brown /*@
45770f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45780f5c6efeSJed Brown 
45793f9fe445SBarry Smith    Logically Collective on SNES
45800f5c6efeSJed Brown 
45810f5c6efeSJed Brown    Input Parameter:
4582d42a1c89SJed Brown + snes - nonlinear solver
45830910c330SBarry Smith . U - the current state at which to evaluate the residual
4584d42a1c89SJed Brown - ctx - user context, must be a TS
45850f5c6efeSJed Brown 
45860f5c6efeSJed Brown    Output Parameter:
45870f5c6efeSJed Brown . F - the nonlinear residual
45880f5c6efeSJed Brown 
45890f5c6efeSJed Brown    Notes:
45900f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
45910f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
45920f5c6efeSJed Brown 
45930f5c6efeSJed Brown    Level: advanced
45940f5c6efeSJed Brown 
45950f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
45960f5c6efeSJed Brown @*/
45970910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
45980f5c6efeSJed Brown {
45990f5c6efeSJed Brown   TS             ts = (TS)ctx;
46000f5c6efeSJed Brown   PetscErrorCode ierr;
46010f5c6efeSJed Brown 
46020f5c6efeSJed Brown   PetscFunctionBegin;
46030f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46040910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46050f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46060f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46070910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46080f5c6efeSJed Brown   PetscFunctionReturn(0);
46090f5c6efeSJed Brown }
46100f5c6efeSJed Brown 
46110f5c6efeSJed Brown /*@
46120f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
46130f5c6efeSJed Brown 
46140f5c6efeSJed Brown    Collective on SNES
46150f5c6efeSJed Brown 
46160f5c6efeSJed Brown    Input Parameter:
46170f5c6efeSJed Brown + snes - nonlinear solver
46180910c330SBarry Smith . U - the current state at which to evaluate the residual
46190f5c6efeSJed Brown - ctx - user context, must be a TS
46200f5c6efeSJed Brown 
46210f5c6efeSJed Brown    Output Parameter:
46220f5c6efeSJed Brown + A - the Jacobian
46230f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
46240f5c6efeSJed Brown - flag - indicates any structure change in the matrix
46250f5c6efeSJed Brown 
46260f5c6efeSJed Brown    Notes:
46270f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46280f5c6efeSJed Brown 
46290f5c6efeSJed Brown    Level: developer
46300f5c6efeSJed Brown 
46310f5c6efeSJed Brown .seealso: SNESSetJacobian()
46320f5c6efeSJed Brown @*/
4633d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46340f5c6efeSJed Brown {
46350f5c6efeSJed Brown   TS             ts = (TS)ctx;
46360f5c6efeSJed Brown   PetscErrorCode ierr;
46370f5c6efeSJed Brown 
46380f5c6efeSJed Brown   PetscFunctionBegin;
46390f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46400910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46410f5c6efeSJed Brown   PetscValidPointer(A,3);
464294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
46430f5c6efeSJed Brown   PetscValidPointer(B,4);
464494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
46450f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4646d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
46470f5c6efeSJed Brown   PetscFunctionReturn(0);
46480f5c6efeSJed Brown }
4649325fc9f4SBarry Smith 
46500e4ef248SJed Brown /*@C
46519ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
46520e4ef248SJed Brown 
46530e4ef248SJed Brown    Collective on TS
46540e4ef248SJed Brown 
46550e4ef248SJed Brown    Input Arguments:
46560e4ef248SJed Brown +  ts - time stepping context
46570e4ef248SJed Brown .  t - time at which to evaluate
46580910c330SBarry Smith .  U - state at which to evaluate
46590e4ef248SJed Brown -  ctx - context
46600e4ef248SJed Brown 
46610e4ef248SJed Brown    Output Arguments:
46620e4ef248SJed Brown .  F - right hand side
46630e4ef248SJed Brown 
46640e4ef248SJed Brown    Level: intermediate
46650e4ef248SJed Brown 
46660e4ef248SJed Brown    Notes:
46670e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
46680e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
46690e4ef248SJed Brown 
46700e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
46710e4ef248SJed Brown @*/
46720910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46730e4ef248SJed Brown {
46740e4ef248SJed Brown   PetscErrorCode ierr;
46750e4ef248SJed Brown   Mat            Arhs,Brhs;
46760e4ef248SJed Brown 
46770e4ef248SJed Brown   PetscFunctionBegin;
46780e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4679d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46800910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46810e4ef248SJed Brown   PetscFunctionReturn(0);
46820e4ef248SJed Brown }
46830e4ef248SJed Brown 
46840e4ef248SJed Brown /*@C
46850e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
46860e4ef248SJed Brown 
46870e4ef248SJed Brown    Collective on TS
46880e4ef248SJed Brown 
46890e4ef248SJed Brown    Input Arguments:
46900e4ef248SJed Brown +  ts - time stepping context
46910e4ef248SJed Brown .  t - time at which to evaluate
46920910c330SBarry Smith .  U - state at which to evaluate
46930e4ef248SJed Brown -  ctx - context
46940e4ef248SJed Brown 
46950e4ef248SJed Brown    Output Arguments:
46960e4ef248SJed Brown +  A - pointer to operator
46970e4ef248SJed Brown .  B - pointer to preconditioning matrix
46980e4ef248SJed Brown -  flg - matrix structure flag
46990e4ef248SJed Brown 
47000e4ef248SJed Brown    Level: intermediate
47010e4ef248SJed Brown 
47020e4ef248SJed Brown    Notes:
47030e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47040e4ef248SJed Brown 
47050e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47060e4ef248SJed Brown @*/
4707d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47080e4ef248SJed Brown {
47090e4ef248SJed Brown   PetscFunctionBegin;
47100e4ef248SJed Brown   PetscFunctionReturn(0);
47110e4ef248SJed Brown }
47120e4ef248SJed Brown 
47130026cea9SSean Farley /*@C
47140026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
47150026cea9SSean Farley 
47160026cea9SSean Farley    Collective on TS
47170026cea9SSean Farley 
47180026cea9SSean Farley    Input Arguments:
47190026cea9SSean Farley +  ts - time stepping context
47200026cea9SSean Farley .  t - time at which to evaluate
47210910c330SBarry Smith .  U - state at which to evaluate
47220910c330SBarry Smith .  Udot - time derivative of state vector
47230026cea9SSean Farley -  ctx - context
47240026cea9SSean Farley 
47250026cea9SSean Farley    Output Arguments:
47260026cea9SSean Farley .  F - left hand side
47270026cea9SSean Farley 
47280026cea9SSean Farley    Level: intermediate
47290026cea9SSean Farley 
47300026cea9SSean Farley    Notes:
47310910c330SBarry 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
47320026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47330026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47340026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47350026cea9SSean Farley 
47369ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47379ae8fd06SBarry Smith 
47389ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47390026cea9SSean Farley @*/
47400910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
47410026cea9SSean Farley {
47420026cea9SSean Farley   PetscErrorCode ierr;
47430026cea9SSean Farley   Mat            A,B;
47440026cea9SSean Farley 
47450026cea9SSean Farley   PetscFunctionBegin;
47460298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4747d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
47480910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
47490026cea9SSean Farley   PetscFunctionReturn(0);
47500026cea9SSean Farley }
47510026cea9SSean Farley 
47520026cea9SSean Farley /*@C
4753b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
47540026cea9SSean Farley 
47550026cea9SSean Farley    Collective on TS
47560026cea9SSean Farley 
47570026cea9SSean Farley    Input Arguments:
47580026cea9SSean Farley +  ts - time stepping context
47590026cea9SSean Farley .  t - time at which to evaluate
47600910c330SBarry Smith .  U - state at which to evaluate
47610910c330SBarry Smith .  Udot - time derivative of state vector
47620026cea9SSean Farley .  shift - shift to apply
47630026cea9SSean Farley -  ctx - context
47640026cea9SSean Farley 
47650026cea9SSean Farley    Output Arguments:
47660026cea9SSean Farley +  A - pointer to operator
47670026cea9SSean Farley .  B - pointer to preconditioning matrix
47680026cea9SSean Farley -  flg - matrix structure flag
47690026cea9SSean Farley 
4770b41af12eSJed Brown    Level: advanced
47710026cea9SSean Farley 
47720026cea9SSean Farley    Notes:
47730026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47740026cea9SSean Farley 
4775b41af12eSJed Brown    It is only appropriate for problems of the form
4776b41af12eSJed Brown 
4777b41af12eSJed Brown $     M Udot = F(U,t)
4778b41af12eSJed Brown 
4779b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4780b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4781b41af12eSJed Brown   an implicit operator of the form
4782b41af12eSJed Brown 
4783b41af12eSJed Brown $    shift*M + J
4784b41af12eSJed Brown 
4785b41af12eSJed 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
4786b41af12eSJed Brown   a copy of M or reassemble it when requested.
4787b41af12eSJed Brown 
47880026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
47890026cea9SSean Farley @*/
4790d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
47910026cea9SSean Farley {
4792b41af12eSJed Brown   PetscErrorCode ierr;
4793b41af12eSJed Brown 
47940026cea9SSean Farley   PetscFunctionBegin;
479594ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4796b41af12eSJed Brown   ts->ijacobian.shift = shift;
47970026cea9SSean Farley   PetscFunctionReturn(0);
47980026cea9SSean Farley }
4799b41af12eSJed Brown 
4800e817cc15SEmil Constantinescu /*@
4801e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4802e817cc15SEmil Constantinescu 
4803e817cc15SEmil Constantinescu    Not Collective
4804e817cc15SEmil Constantinescu 
4805e817cc15SEmil Constantinescu    Input Parameter:
4806e817cc15SEmil Constantinescu .  ts - the TS context
4807e817cc15SEmil Constantinescu 
4808e817cc15SEmil Constantinescu    Output Parameter:
48094e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4810e817cc15SEmil Constantinescu 
4811e817cc15SEmil Constantinescu    Level: beginner
4812e817cc15SEmil Constantinescu 
4813e817cc15SEmil Constantinescu .keywords: TS, equation type
4814e817cc15SEmil Constantinescu 
4815e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4816e817cc15SEmil Constantinescu @*/
4817e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4818e817cc15SEmil Constantinescu {
4819e817cc15SEmil Constantinescu   PetscFunctionBegin;
4820e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4821e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4822e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4823e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4824e817cc15SEmil Constantinescu }
4825e817cc15SEmil Constantinescu 
4826e817cc15SEmil Constantinescu /*@
4827e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4828e817cc15SEmil Constantinescu 
4829e817cc15SEmil Constantinescu    Not Collective
4830e817cc15SEmil Constantinescu 
4831e817cc15SEmil Constantinescu    Input Parameter:
4832e817cc15SEmil Constantinescu +  ts - the TS context
48331297b224SEmil Constantinescu -  equation_type - see TSEquationType
4834e817cc15SEmil Constantinescu 
4835e817cc15SEmil Constantinescu    Level: advanced
4836e817cc15SEmil Constantinescu 
4837e817cc15SEmil Constantinescu .keywords: TS, equation type
4838e817cc15SEmil Constantinescu 
4839e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4840e817cc15SEmil Constantinescu @*/
4841e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4842e817cc15SEmil Constantinescu {
4843e817cc15SEmil Constantinescu   PetscFunctionBegin;
4844e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4845e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4846e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4847e817cc15SEmil Constantinescu }
48480026cea9SSean Farley 
48494af1b03aSJed Brown /*@
48504af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
48514af1b03aSJed Brown 
48524af1b03aSJed Brown    Not Collective
48534af1b03aSJed Brown 
48544af1b03aSJed Brown    Input Parameter:
48554af1b03aSJed Brown .  ts - the TS context
48564af1b03aSJed Brown 
48574af1b03aSJed Brown    Output Parameter:
48584af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
48594af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
48604af1b03aSJed Brown 
4861487e0bb9SJed Brown    Level: beginner
48624af1b03aSJed Brown 
4863cd652676SJed Brown    Notes:
4864cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48654af1b03aSJed Brown 
48664af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
48674af1b03aSJed Brown 
48684af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48694af1b03aSJed Brown @*/
48704af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48714af1b03aSJed Brown {
48724af1b03aSJed Brown   PetscFunctionBegin;
48734af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48744af1b03aSJed Brown   PetscValidPointer(reason,2);
48754af1b03aSJed Brown   *reason = ts->reason;
48764af1b03aSJed Brown   PetscFunctionReturn(0);
48774af1b03aSJed Brown }
48784af1b03aSJed Brown 
4879d6ad946cSShri Abhyankar /*@
4880d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4881d6ad946cSShri Abhyankar 
4882d6ad946cSShri Abhyankar    Not Collective
4883d6ad946cSShri Abhyankar 
4884d6ad946cSShri Abhyankar    Input Parameter:
4885d6ad946cSShri Abhyankar +  ts - the TS context
4886d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4887d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4888d6ad946cSShri Abhyankar 
4889f5abba47SShri Abhyankar    Level: advanced
4890d6ad946cSShri Abhyankar 
4891d6ad946cSShri Abhyankar    Notes:
4892d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4893d6ad946cSShri Abhyankar 
4894d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4895d6ad946cSShri Abhyankar 
4896d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4897d6ad946cSShri Abhyankar @*/
4898d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4899d6ad946cSShri Abhyankar {
4900d6ad946cSShri Abhyankar   PetscFunctionBegin;
4901d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4902d6ad946cSShri Abhyankar   ts->reason = reason;
4903d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4904d6ad946cSShri Abhyankar }
4905d6ad946cSShri Abhyankar 
4906cc708dedSBarry Smith /*@
4907cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4908cc708dedSBarry Smith 
4909cc708dedSBarry Smith    Not Collective
4910cc708dedSBarry Smith 
4911cc708dedSBarry Smith    Input Parameter:
4912cc708dedSBarry Smith .  ts - the TS context
4913cc708dedSBarry Smith 
4914cc708dedSBarry Smith    Output Parameter:
491519eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4916cc708dedSBarry Smith 
4917487e0bb9SJed Brown    Level: beginner
4918cc708dedSBarry Smith 
4919cc708dedSBarry Smith    Notes:
4920cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4921cc708dedSBarry Smith 
4922cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4923cc708dedSBarry Smith 
4924cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4925cc708dedSBarry Smith @*/
4926cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4927cc708dedSBarry Smith {
4928cc708dedSBarry Smith   PetscFunctionBegin;
4929cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4930cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4931cc708dedSBarry Smith   *ftime = ts->solvetime;
4932cc708dedSBarry Smith   PetscFunctionReturn(0);
4933cc708dedSBarry Smith }
4934cc708dedSBarry Smith 
49352c18e0fdSBarry Smith /*@
49365ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49379f67acb7SJed Brown    used by the time integrator.
49389f67acb7SJed Brown 
49399f67acb7SJed Brown    Not Collective
49409f67acb7SJed Brown 
49419f67acb7SJed Brown    Input Parameter:
49429f67acb7SJed Brown .  ts - TS context
49439f67acb7SJed Brown 
49449f67acb7SJed Brown    Output Parameter:
49459f67acb7SJed Brown .  nits - number of nonlinear iterations
49469f67acb7SJed Brown 
49479f67acb7SJed Brown    Notes:
49489f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49499f67acb7SJed Brown 
49509f67acb7SJed Brown    Level: intermediate
49519f67acb7SJed Brown 
49529f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
49539f67acb7SJed Brown 
49545ef26d82SJed Brown .seealso:  TSGetKSPIterations()
49559f67acb7SJed Brown @*/
49565ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
49579f67acb7SJed Brown {
49589f67acb7SJed Brown   PetscFunctionBegin;
49599f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49609f67acb7SJed Brown   PetscValidIntPointer(nits,2);
49615ef26d82SJed Brown   *nits = ts->snes_its;
49629f67acb7SJed Brown   PetscFunctionReturn(0);
49639f67acb7SJed Brown }
49649f67acb7SJed Brown 
49659f67acb7SJed Brown /*@
49665ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
49679f67acb7SJed Brown    used by the time integrator.
49689f67acb7SJed Brown 
49699f67acb7SJed Brown    Not Collective
49709f67acb7SJed Brown 
49719f67acb7SJed Brown    Input Parameter:
49729f67acb7SJed Brown .  ts - TS context
49739f67acb7SJed Brown 
49749f67acb7SJed Brown    Output Parameter:
49759f67acb7SJed Brown .  lits - number of linear iterations
49769f67acb7SJed Brown 
49779f67acb7SJed Brown    Notes:
49789f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49799f67acb7SJed Brown 
49809f67acb7SJed Brown    Level: intermediate
49819f67acb7SJed Brown 
49829f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
49839f67acb7SJed Brown 
49845ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
49859f67acb7SJed Brown @*/
49865ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
49879f67acb7SJed Brown {
49889f67acb7SJed Brown   PetscFunctionBegin;
49899f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49909f67acb7SJed Brown   PetscValidIntPointer(lits,2);
49915ef26d82SJed Brown   *lits = ts->ksp_its;
49929f67acb7SJed Brown   PetscFunctionReturn(0);
49939f67acb7SJed Brown }
49949f67acb7SJed Brown 
4995cef5090cSJed Brown /*@
4996cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4997cef5090cSJed Brown 
4998cef5090cSJed Brown    Not Collective
4999cef5090cSJed Brown 
5000cef5090cSJed Brown    Input Parameter:
5001cef5090cSJed Brown .  ts - TS context
5002cef5090cSJed Brown 
5003cef5090cSJed Brown    Output Parameter:
5004cef5090cSJed Brown .  rejects - number of steps rejected
5005cef5090cSJed Brown 
5006cef5090cSJed Brown    Notes:
5007cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5008cef5090cSJed Brown 
5009cef5090cSJed Brown    Level: intermediate
5010cef5090cSJed Brown 
5011cef5090cSJed Brown .keywords: TS, get, number
5012cef5090cSJed Brown 
50135ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5014cef5090cSJed Brown @*/
5015cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5016cef5090cSJed Brown {
5017cef5090cSJed Brown   PetscFunctionBegin;
5018cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5019cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5020cef5090cSJed Brown   *rejects = ts->reject;
5021cef5090cSJed Brown   PetscFunctionReturn(0);
5022cef5090cSJed Brown }
5023cef5090cSJed Brown 
5024cef5090cSJed Brown /*@
5025cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5026cef5090cSJed Brown 
5027cef5090cSJed Brown    Not Collective
5028cef5090cSJed Brown 
5029cef5090cSJed Brown    Input Parameter:
5030cef5090cSJed Brown .  ts - TS context
5031cef5090cSJed Brown 
5032cef5090cSJed Brown    Output Parameter:
5033cef5090cSJed Brown .  fails - number of failed nonlinear solves
5034cef5090cSJed Brown 
5035cef5090cSJed Brown    Notes:
5036cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5037cef5090cSJed Brown 
5038cef5090cSJed Brown    Level: intermediate
5039cef5090cSJed Brown 
5040cef5090cSJed Brown .keywords: TS, get, number
5041cef5090cSJed Brown 
50425ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5043cef5090cSJed Brown @*/
5044cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5045cef5090cSJed Brown {
5046cef5090cSJed Brown   PetscFunctionBegin;
5047cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5048cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5049cef5090cSJed Brown   *fails = ts->num_snes_failures;
5050cef5090cSJed Brown   PetscFunctionReturn(0);
5051cef5090cSJed Brown }
5052cef5090cSJed Brown 
5053cef5090cSJed Brown /*@
5054cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5055cef5090cSJed Brown 
5056cef5090cSJed Brown    Not Collective
5057cef5090cSJed Brown 
5058cef5090cSJed Brown    Input Parameter:
5059cef5090cSJed Brown +  ts - TS context
5060cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5061cef5090cSJed Brown 
5062cef5090cSJed Brown    Notes:
5063cef5090cSJed Brown    The counter is reset to zero for each step
5064cef5090cSJed Brown 
5065cef5090cSJed Brown    Options Database Key:
5066cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5067cef5090cSJed Brown 
5068cef5090cSJed Brown    Level: intermediate
5069cef5090cSJed Brown 
5070cef5090cSJed Brown .keywords: TS, set, maximum, number
5071cef5090cSJed Brown 
50725ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5073cef5090cSJed Brown @*/
5074cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5075cef5090cSJed Brown {
5076cef5090cSJed Brown   PetscFunctionBegin;
5077cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5078cef5090cSJed Brown   ts->max_reject = rejects;
5079cef5090cSJed Brown   PetscFunctionReturn(0);
5080cef5090cSJed Brown }
5081cef5090cSJed Brown 
5082cef5090cSJed Brown /*@
5083cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5084cef5090cSJed Brown 
5085cef5090cSJed Brown    Not Collective
5086cef5090cSJed Brown 
5087cef5090cSJed Brown    Input Parameter:
5088cef5090cSJed Brown +  ts - TS context
5089cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5090cef5090cSJed Brown 
5091cef5090cSJed Brown    Notes:
5092cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5093cef5090cSJed Brown 
5094cef5090cSJed Brown    Options Database Key:
5095cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5096cef5090cSJed Brown 
5097cef5090cSJed Brown    Level: intermediate
5098cef5090cSJed Brown 
5099cef5090cSJed Brown .keywords: TS, set, maximum, number
5100cef5090cSJed Brown 
51015ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5102cef5090cSJed Brown @*/
5103cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5104cef5090cSJed Brown {
5105cef5090cSJed Brown   PetscFunctionBegin;
5106cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5107cef5090cSJed Brown   ts->max_snes_failures = fails;
5108cef5090cSJed Brown   PetscFunctionReturn(0);
5109cef5090cSJed Brown }
5110cef5090cSJed Brown 
5111cef5090cSJed Brown /*@
5112cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5113cef5090cSJed Brown 
5114cef5090cSJed Brown    Not Collective
5115cef5090cSJed Brown 
5116cef5090cSJed Brown    Input Parameter:
5117cef5090cSJed Brown +  ts - TS context
5118cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5119cef5090cSJed Brown 
5120cef5090cSJed Brown    Options Database Key:
5121cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5122cef5090cSJed Brown 
5123cef5090cSJed Brown    Level: intermediate
5124cef5090cSJed Brown 
5125cef5090cSJed Brown .keywords: TS, set, error
5126cef5090cSJed Brown 
51275ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5128cef5090cSJed Brown @*/
5129cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5130cef5090cSJed Brown {
5131cef5090cSJed Brown   PetscFunctionBegin;
5132cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5133cef5090cSJed Brown   ts->errorifstepfailed = err;
5134cef5090cSJed Brown   PetscFunctionReturn(0);
5135cef5090cSJed Brown }
5136cef5090cSJed Brown 
5137fb1732b5SBarry Smith /*@C
5138fde5950dSBarry 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
5139fb1732b5SBarry Smith 
5140fb1732b5SBarry Smith    Collective on TS
5141fb1732b5SBarry Smith 
5142fb1732b5SBarry Smith    Input Parameters:
5143fb1732b5SBarry Smith +  ts - the TS context
5144fb1732b5SBarry Smith .  step - current time-step
5145fb1732b5SBarry Smith .  ptime - current time
51460910c330SBarry Smith .  u - current state
5147721cd6eeSBarry Smith -  vf - viewer and its format
5148fb1732b5SBarry Smith 
5149fb1732b5SBarry Smith    Level: intermediate
5150fb1732b5SBarry Smith 
5151fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5152fb1732b5SBarry Smith 
5153fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5154fb1732b5SBarry Smith @*/
5155721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5156fb1732b5SBarry Smith {
5157fb1732b5SBarry Smith   PetscErrorCode ierr;
5158fb1732b5SBarry Smith 
5159fb1732b5SBarry Smith   PetscFunctionBegin;
5160721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5161721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5162721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5163ed81e22dSJed Brown   PetscFunctionReturn(0);
5164ed81e22dSJed Brown }
5165ed81e22dSJed Brown 
5166ed81e22dSJed Brown /*@C
5167ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5168ed81e22dSJed Brown 
5169ed81e22dSJed Brown    Collective on TS
5170ed81e22dSJed Brown 
5171ed81e22dSJed Brown    Input Parameters:
5172ed81e22dSJed Brown +  ts - the TS context
5173ed81e22dSJed Brown .  step - current time-step
5174ed81e22dSJed Brown .  ptime - current time
51750910c330SBarry Smith .  u - current state
5176ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5177ed81e22dSJed Brown 
5178ed81e22dSJed Brown    Level: intermediate
5179ed81e22dSJed Brown 
5180ed81e22dSJed Brown    Notes:
5181ed81e22dSJed 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.
5182ed81e22dSJed Brown    These are named according to the file name template.
5183ed81e22dSJed Brown 
5184ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5185ed81e22dSJed Brown 
5186ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5187ed81e22dSJed Brown 
5188ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5189ed81e22dSJed Brown @*/
51900910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5191ed81e22dSJed Brown {
5192ed81e22dSJed Brown   PetscErrorCode ierr;
5193ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5194ed81e22dSJed Brown   PetscViewer    viewer;
5195ed81e22dSJed Brown 
5196ed81e22dSJed Brown   PetscFunctionBegin;
519763e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
51988caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5199ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
52000910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5201ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5202ed81e22dSJed Brown   PetscFunctionReturn(0);
5203ed81e22dSJed Brown }
5204ed81e22dSJed Brown 
5205ed81e22dSJed Brown /*@C
5206ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5207ed81e22dSJed Brown 
5208ed81e22dSJed Brown    Collective on TS
5209ed81e22dSJed Brown 
5210ed81e22dSJed Brown    Input Parameters:
5211ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5212ed81e22dSJed Brown 
5213ed81e22dSJed Brown    Level: intermediate
5214ed81e22dSJed Brown 
5215ed81e22dSJed Brown    Note:
5216ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5217ed81e22dSJed Brown 
5218ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5219ed81e22dSJed Brown 
5220ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5221ed81e22dSJed Brown @*/
5222ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5223ed81e22dSJed Brown {
5224ed81e22dSJed Brown   PetscErrorCode ierr;
5225ed81e22dSJed Brown 
5226ed81e22dSJed Brown   PetscFunctionBegin;
5227ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5228fb1732b5SBarry Smith   PetscFunctionReturn(0);
5229fb1732b5SBarry Smith }
5230fb1732b5SBarry Smith 
523184df9cb4SJed Brown /*@
5232552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
523384df9cb4SJed Brown 
5234ed81e22dSJed Brown    Collective on TS if controller has not been created yet
523584df9cb4SJed Brown 
523684df9cb4SJed Brown    Input Arguments:
5237ed81e22dSJed Brown .  ts - time stepping context
523884df9cb4SJed Brown 
523984df9cb4SJed Brown    Output Arguments:
5240ed81e22dSJed Brown .  adapt - adaptive controller
524184df9cb4SJed Brown 
524284df9cb4SJed Brown    Level: intermediate
524384df9cb4SJed Brown 
5244ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
524584df9cb4SJed Brown @*/
5246552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
524784df9cb4SJed Brown {
524884df9cb4SJed Brown   PetscErrorCode ierr;
524984df9cb4SJed Brown 
525084df9cb4SJed Brown   PetscFunctionBegin;
525184df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5252bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
525384df9cb4SJed Brown   if (!ts->adapt) {
5254ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
52553bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
52561c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
525784df9cb4SJed Brown   }
5258bec58848SLisandro Dalcin   *adapt = ts->adapt;
525984df9cb4SJed Brown   PetscFunctionReturn(0);
526084df9cb4SJed Brown }
5261d6ebe24aSShri Abhyankar 
52621c3436cfSJed Brown /*@
52631c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
52641c3436cfSJed Brown 
52651c3436cfSJed Brown    Logically Collective
52661c3436cfSJed Brown 
52671c3436cfSJed Brown    Input Arguments:
52681c3436cfSJed Brown +  ts - time integration context
52691c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
52700298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
52711c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
52720298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
52731c3436cfSJed Brown 
5274a3cdaa26SBarry Smith    Options Database keys:
5275a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5276a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5277a3cdaa26SBarry Smith 
52783ff766beSShri Abhyankar    Notes:
52793ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
52803ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
52813ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
52823ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
52833ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
52843ff766beSShri Abhyankar    differential variables.
52853ff766beSShri Abhyankar 
52861c3436cfSJed Brown    Level: beginner
52871c3436cfSJed Brown 
5288c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
52891c3436cfSJed Brown @*/
52901c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
52911c3436cfSJed Brown {
52921c3436cfSJed Brown   PetscErrorCode ierr;
52931c3436cfSJed Brown 
52941c3436cfSJed Brown   PetscFunctionBegin;
5295c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
52961c3436cfSJed Brown   if (vatol) {
52971c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
52981c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
52991c3436cfSJed Brown     ts->vatol = vatol;
53001c3436cfSJed Brown   }
5301c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
53021c3436cfSJed Brown   if (vrtol) {
53031c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53041c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
53051c3436cfSJed Brown     ts->vrtol = vrtol;
53061c3436cfSJed Brown   }
53071c3436cfSJed Brown   PetscFunctionReturn(0);
53081c3436cfSJed Brown }
53091c3436cfSJed Brown 
5310c5033834SJed Brown /*@
5311c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5312c5033834SJed Brown 
5313c5033834SJed Brown    Logically Collective
5314c5033834SJed Brown 
5315c5033834SJed Brown    Input Arguments:
5316c5033834SJed Brown .  ts - time integration context
5317c5033834SJed Brown 
5318c5033834SJed Brown    Output Arguments:
53190298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53200298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53210298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53220298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5323c5033834SJed Brown 
5324c5033834SJed Brown    Level: beginner
5325c5033834SJed Brown 
5326c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5327c5033834SJed Brown @*/
5328c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5329c5033834SJed Brown {
5330c5033834SJed Brown   PetscFunctionBegin;
5331c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5332c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5333c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5334c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5335c5033834SJed Brown   PetscFunctionReturn(0);
5336c5033834SJed Brown }
5337c5033834SJed Brown 
53389c6b16b5SShri Abhyankar /*@
5339a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
53409c6b16b5SShri Abhyankar 
53419c6b16b5SShri Abhyankar    Collective on TS
53429c6b16b5SShri Abhyankar 
53439c6b16b5SShri Abhyankar    Input Arguments:
53449c6b16b5SShri Abhyankar +  ts - time stepping context
5345a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5346a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
53479c6b16b5SShri Abhyankar 
53489c6b16b5SShri Abhyankar    Output Arguments:
53497453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
53507453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
53517453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
53529c6b16b5SShri Abhyankar 
53539c6b16b5SShri Abhyankar    Level: developer
53549c6b16b5SShri Abhyankar 
5355deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
53569c6b16b5SShri Abhyankar @*/
53577453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53589c6b16b5SShri Abhyankar {
53599c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
53609c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
53617453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
53627453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
53639c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
53647453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
53657453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
53667453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
53679c6b16b5SShri Abhyankar 
53689c6b16b5SShri Abhyankar   PetscFunctionBegin;
53699c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5370a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5371a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5372a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5373a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5374a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5375a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
53768a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
53778a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5378a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
53799c6b16b5SShri Abhyankar 
53809c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
53819c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
53829c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
53839c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
53849c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
53857453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
53867453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
53877453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
53889c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
53899c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
53909c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53919c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53929c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
53937453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
53947453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
53957453f775SEmil Constantinescu       if(tola>0.){
53967453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
53977453f775SEmil Constantinescu         na_loc++;
53987453f775SEmil Constantinescu       }
53997453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54007453f775SEmil Constantinescu       if(tolr>0.){
54017453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54027453f775SEmil Constantinescu         nr_loc++;
54037453f775SEmil Constantinescu       }
54047453f775SEmil Constantinescu       tol=tola+tolr;
54057453f775SEmil Constantinescu       if(tol>0.){
54067453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54077453f775SEmil Constantinescu         n_loc++;
54087453f775SEmil Constantinescu       }
54099c6b16b5SShri Abhyankar     }
54109c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54119c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54129c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54139c6b16b5SShri Abhyankar     const PetscScalar *atol;
54149c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54159c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54167453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54177453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54187453f775SEmil Constantinescu       if(tola>0.){
54197453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54207453f775SEmil Constantinescu         na_loc++;
54217453f775SEmil Constantinescu       }
54227453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54237453f775SEmil Constantinescu       if(tolr>0.){
54247453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54257453f775SEmil Constantinescu         nr_loc++;
54267453f775SEmil Constantinescu       }
54277453f775SEmil Constantinescu       tol=tola+tolr;
54287453f775SEmil Constantinescu       if(tol>0.){
54297453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54307453f775SEmil Constantinescu         n_loc++;
54317453f775SEmil Constantinescu       }
54329c6b16b5SShri Abhyankar     }
54339c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54349c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54359c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54369c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54379c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54387453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54397453f775SEmil Constantinescu       tola = ts->atol;
54407453f775SEmil Constantinescu       if(tola>0.){
54417453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54427453f775SEmil Constantinescu         na_loc++;
54437453f775SEmil Constantinescu       }
54447453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54457453f775SEmil Constantinescu       if(tolr>0.){
54467453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54477453f775SEmil Constantinescu         nr_loc++;
54487453f775SEmil Constantinescu       }
54497453f775SEmil Constantinescu       tol=tola+tolr;
54507453f775SEmil Constantinescu       if(tol>0.){
54517453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54527453f775SEmil Constantinescu         n_loc++;
54537453f775SEmil Constantinescu       }
54549c6b16b5SShri Abhyankar     }
54559c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54569c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54579c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54587453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54597453f775SEmil Constantinescu      tola = ts->atol;
54607453f775SEmil Constantinescu       if(tola>0.){
54617453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54627453f775SEmil Constantinescu         na_loc++;
54637453f775SEmil Constantinescu       }
54647453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54657453f775SEmil Constantinescu       if(tolr>0.){
54667453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54677453f775SEmil Constantinescu         nr_loc++;
54687453f775SEmil Constantinescu       }
54697453f775SEmil Constantinescu       tol=tola+tolr;
54707453f775SEmil Constantinescu       if(tol>0.){
54717453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54727453f775SEmil Constantinescu         n_loc++;
54737453f775SEmil Constantinescu       }
54749c6b16b5SShri Abhyankar     }
54759c6b16b5SShri Abhyankar   }
54769c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54779c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54789c6b16b5SShri Abhyankar 
54797453f775SEmil Constantinescu   err_loc[0] = sum;
54807453f775SEmil Constantinescu   err_loc[1] = suma;
54817453f775SEmil Constantinescu   err_loc[2] = sumr;
54827453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
54837453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
54847453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
54857453f775SEmil Constantinescu 
5486a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54877453f775SEmil Constantinescu 
54887453f775SEmil Constantinescu   gsum   = err_glb[0];
54897453f775SEmil Constantinescu   gsuma  = err_glb[1];
54907453f775SEmil Constantinescu   gsumr  = err_glb[2];
54917453f775SEmil Constantinescu   n_glb  = err_glb[3];
54927453f775SEmil Constantinescu   na_glb = err_glb[4];
54937453f775SEmil Constantinescu   nr_glb = err_glb[5];
54947453f775SEmil Constantinescu 
5495b1316ef9SEmil Constantinescu   *norm  = 0.;
5496b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5497b1316ef9SEmil Constantinescu   *norma = 0.;
5498b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5499b1316ef9SEmil Constantinescu   *normr = 0.;
5500b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
55019c6b16b5SShri Abhyankar 
55029c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55037453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
55047453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
55059c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55069c6b16b5SShri Abhyankar }
55079c6b16b5SShri Abhyankar 
55089c6b16b5SShri Abhyankar /*@
5509a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
55109c6b16b5SShri Abhyankar 
55119c6b16b5SShri Abhyankar    Collective on TS
55129c6b16b5SShri Abhyankar 
55139c6b16b5SShri Abhyankar    Input Arguments:
55149c6b16b5SShri Abhyankar +  ts - time stepping context
5515a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5516a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55179c6b16b5SShri Abhyankar 
55189c6b16b5SShri Abhyankar    Output Arguments:
55197453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55207453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
55217453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55229c6b16b5SShri Abhyankar 
55239c6b16b5SShri Abhyankar    Level: developer
55249c6b16b5SShri Abhyankar 
5525deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
55269c6b16b5SShri Abhyankar @*/
55277453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55289c6b16b5SShri Abhyankar {
55299c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55307453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
55319c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55327453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
55337453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
55347453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55359c6b16b5SShri Abhyankar 
55369c6b16b5SShri Abhyankar   PetscFunctionBegin;
55379c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5538a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5539a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5540a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5541a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5542a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5543a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
55448a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
55458a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5546a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55479c6b16b5SShri Abhyankar 
55489c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55499c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55509c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55519c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55529c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55537453f775SEmil Constantinescu 
55547453f775SEmil Constantinescu   max=0.;
55557453f775SEmil Constantinescu   maxa=0.;
55567453f775SEmil Constantinescu   maxr=0.;
55577453f775SEmil Constantinescu 
55587453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
55599c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55609c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55619c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55627453f775SEmil Constantinescu 
55637453f775SEmil Constantinescu     for (i=0; i<n; i++) {
55647453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55657453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55667453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55677453f775SEmil Constantinescu       tol  = tola+tolr;
55687453f775SEmil Constantinescu       if(tola>0.){
55697453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55707453f775SEmil Constantinescu       }
55717453f775SEmil Constantinescu       if(tolr>0.){
55727453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55737453f775SEmil Constantinescu       }
55747453f775SEmil Constantinescu       if(tol>0.){
55757453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55767453f775SEmil Constantinescu       }
55779c6b16b5SShri Abhyankar     }
55789c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55799c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55809c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55819c6b16b5SShri Abhyankar     const PetscScalar *atol;
55829c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55837453f775SEmil Constantinescu     for (i=0; i<n; i++) {
55847453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55857453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55867453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55877453f775SEmil Constantinescu       tol  = tola+tolr;
55887453f775SEmil Constantinescu       if(tola>0.){
55897453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55907453f775SEmil Constantinescu       }
55917453f775SEmil Constantinescu       if(tolr>0.){
55927453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55937453f775SEmil Constantinescu       }
55947453f775SEmil Constantinescu       if(tol>0.){
55957453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55967453f775SEmil Constantinescu       }
55979c6b16b5SShri Abhyankar     }
55989c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55999c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
56009c6b16b5SShri Abhyankar     const PetscScalar *rtol;
56019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56027453f775SEmil Constantinescu 
56037453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56047453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56057453f775SEmil Constantinescu       tola = ts->atol;
56067453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56077453f775SEmil Constantinescu       tol  = tola+tolr;
56087453f775SEmil Constantinescu       if(tola>0.){
56097453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56107453f775SEmil Constantinescu       }
56117453f775SEmil Constantinescu       if(tolr>0.){
56127453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56137453f775SEmil Constantinescu       }
56147453f775SEmil Constantinescu       if(tol>0.){
56157453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56167453f775SEmil Constantinescu       }
56179c6b16b5SShri Abhyankar     }
56189c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56199c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
56207453f775SEmil Constantinescu 
56217453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56237453f775SEmil Constantinescu       tola = ts->atol;
56247453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56257453f775SEmil Constantinescu       tol  = tola+tolr;
56267453f775SEmil Constantinescu       if(tola>0.){
56277453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56287453f775SEmil Constantinescu       }
56297453f775SEmil Constantinescu       if(tolr>0.){
56307453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56317453f775SEmil Constantinescu       }
56327453f775SEmil Constantinescu       if(tol>0.){
56337453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56347453f775SEmil Constantinescu       }
56359c6b16b5SShri Abhyankar     }
56369c6b16b5SShri Abhyankar   }
56379c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56389c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56397453f775SEmil Constantinescu   err_loc[0] = max;
56407453f775SEmil Constantinescu   err_loc[1] = maxa;
56417453f775SEmil Constantinescu   err_loc[2] = maxr;
5642a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56437453f775SEmil Constantinescu   gmax   = err_glb[0];
56447453f775SEmil Constantinescu   gmaxa  = err_glb[1];
56457453f775SEmil Constantinescu   gmaxr  = err_glb[2];
56469c6b16b5SShri Abhyankar 
56479c6b16b5SShri Abhyankar   *norm = gmax;
56487453f775SEmil Constantinescu   *norma = gmaxa;
56497453f775SEmil Constantinescu   *normr = gmaxr;
56509c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56517453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56527453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56539c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56549c6b16b5SShri Abhyankar }
56559c6b16b5SShri Abhyankar 
56561c3436cfSJed Brown /*@
56578a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
56581c3436cfSJed Brown 
56591c3436cfSJed Brown    Collective on TS
56601c3436cfSJed Brown 
56611c3436cfSJed Brown    Input Arguments:
56621c3436cfSJed Brown +  ts - time stepping context
5663a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5664a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5665a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56667619abb3SShri 
56671c3436cfSJed Brown    Output Arguments:
56688a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
56698a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
56708a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5671a4868fbcSLisandro Dalcin 
5672a4868fbcSLisandro Dalcin    Options Database Keys:
5673a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5674a4868fbcSLisandro Dalcin 
56751c3436cfSJed Brown    Level: developer
56761c3436cfSJed Brown 
56778a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
56781c3436cfSJed Brown @*/
56797453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56801c3436cfSJed Brown {
56818beabaa1SBarry Smith   PetscErrorCode ierr;
56821c3436cfSJed Brown 
56831c3436cfSJed Brown   PetscFunctionBegin;
5684a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
56857453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5686a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
56877453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5688a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56891c3436cfSJed Brown   PetscFunctionReturn(0);
56901c3436cfSJed Brown }
56911c3436cfSJed Brown 
56928a175baeSEmil Constantinescu 
56938a175baeSEmil Constantinescu /*@
56948a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
56958a175baeSEmil Constantinescu 
56968a175baeSEmil Constantinescu    Collective on TS
56978a175baeSEmil Constantinescu 
56988a175baeSEmil Constantinescu    Input Arguments:
56998a175baeSEmil Constantinescu +  ts - time stepping context
57008a175baeSEmil Constantinescu .  E - error vector
57018a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
57028a175baeSEmil Constantinescu -  Y - state vector, previous time step
57038a175baeSEmil Constantinescu 
57048a175baeSEmil Constantinescu    Output Arguments:
57058a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57068a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57078a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57088a175baeSEmil Constantinescu 
57098a175baeSEmil Constantinescu    Level: developer
57108a175baeSEmil Constantinescu 
57118a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
57128a175baeSEmil Constantinescu @*/
57138a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57148a175baeSEmil Constantinescu {
57158a175baeSEmil Constantinescu   PetscErrorCode    ierr;
57168a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
57178a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57188a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57198a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
57208a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
57218a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
57228a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57238a175baeSEmil Constantinescu 
57248a175baeSEmil Constantinescu   PetscFunctionBegin;
57258a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57268a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
57278a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
57288a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
57298a175baeSEmil Constantinescu   PetscValidType(E,2);
57308a175baeSEmil Constantinescu   PetscValidType(U,3);
57318a175baeSEmil Constantinescu   PetscValidType(Y,4);
57328a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
57338a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
57348a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
57358a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
57368a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
57378a175baeSEmil Constantinescu 
57388a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
57398a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
57408a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
57418a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
57428a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57438a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57448a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
57458a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
57468a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
57478a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
57488a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
57498a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57508a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57518a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
57528a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57538a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57548a175baeSEmil Constantinescu       if(tola>0.){
57558a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57568a175baeSEmil Constantinescu         na_loc++;
57578a175baeSEmil Constantinescu       }
57588a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57598a175baeSEmil Constantinescu       if(tolr>0.){
57608a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57618a175baeSEmil Constantinescu         nr_loc++;
57628a175baeSEmil Constantinescu       }
57638a175baeSEmil Constantinescu       tol=tola+tolr;
57648a175baeSEmil Constantinescu       if(tol>0.){
57658a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57668a175baeSEmil Constantinescu         n_loc++;
57678a175baeSEmil Constantinescu       }
57688a175baeSEmil Constantinescu     }
57698a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57708a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57718a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57728a175baeSEmil Constantinescu     const PetscScalar *atol;
57738a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57748a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
57758a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57768a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57778a175baeSEmil Constantinescu       if(tola>0.){
57788a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57798a175baeSEmil Constantinescu         na_loc++;
57808a175baeSEmil Constantinescu       }
57818a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57828a175baeSEmil Constantinescu       if(tolr>0.){
57838a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57848a175baeSEmil Constantinescu         nr_loc++;
57858a175baeSEmil Constantinescu       }
57868a175baeSEmil Constantinescu       tol=tola+tolr;
57878a175baeSEmil Constantinescu       if(tol>0.){
57888a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57898a175baeSEmil Constantinescu         n_loc++;
57908a175baeSEmil Constantinescu       }
57918a175baeSEmil Constantinescu     }
57928a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57938a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57948a175baeSEmil Constantinescu     const PetscScalar *rtol;
57958a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57968a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
57978a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57988a175baeSEmil Constantinescu       tola = ts->atol;
57998a175baeSEmil Constantinescu       if(tola>0.){
58008a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58018a175baeSEmil Constantinescu         na_loc++;
58028a175baeSEmil Constantinescu       }
58038a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58048a175baeSEmil Constantinescu       if(tolr>0.){
58058a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58068a175baeSEmil Constantinescu         nr_loc++;
58078a175baeSEmil Constantinescu       }
58088a175baeSEmil Constantinescu       tol=tola+tolr;
58098a175baeSEmil Constantinescu       if(tol>0.){
58108a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58118a175baeSEmil Constantinescu         n_loc++;
58128a175baeSEmil Constantinescu       }
58138a175baeSEmil Constantinescu     }
58148a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58158a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
58168a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58178a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58188a175baeSEmil Constantinescu      tola = ts->atol;
58198a175baeSEmil Constantinescu       if(tola>0.){
58208a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58218a175baeSEmil Constantinescu         na_loc++;
58228a175baeSEmil Constantinescu       }
58238a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58248a175baeSEmil Constantinescu       if(tolr>0.){
58258a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58268a175baeSEmil Constantinescu         nr_loc++;
58278a175baeSEmil Constantinescu       }
58288a175baeSEmil Constantinescu       tol=tola+tolr;
58298a175baeSEmil Constantinescu       if(tol>0.){
58308a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58318a175baeSEmil Constantinescu         n_loc++;
58328a175baeSEmil Constantinescu       }
58338a175baeSEmil Constantinescu     }
58348a175baeSEmil Constantinescu   }
58358a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
58368a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58378a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58388a175baeSEmil Constantinescu 
58398a175baeSEmil Constantinescu   err_loc[0] = sum;
58408a175baeSEmil Constantinescu   err_loc[1] = suma;
58418a175baeSEmil Constantinescu   err_loc[2] = sumr;
58428a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58438a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58448a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58458a175baeSEmil Constantinescu 
5846a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58478a175baeSEmil Constantinescu 
58488a175baeSEmil Constantinescu   gsum   = err_glb[0];
58498a175baeSEmil Constantinescu   gsuma  = err_glb[1];
58508a175baeSEmil Constantinescu   gsumr  = err_glb[2];
58518a175baeSEmil Constantinescu   n_glb  = err_glb[3];
58528a175baeSEmil Constantinescu   na_glb = err_glb[4];
58538a175baeSEmil Constantinescu   nr_glb = err_glb[5];
58548a175baeSEmil Constantinescu 
58558a175baeSEmil Constantinescu   *norm  = 0.;
58568a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
58578a175baeSEmil Constantinescu   *norma = 0.;
58588a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
58598a175baeSEmil Constantinescu   *normr = 0.;
58608a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58618a175baeSEmil Constantinescu 
58628a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58638a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58648a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58658a175baeSEmil Constantinescu   PetscFunctionReturn(0);
58668a175baeSEmil Constantinescu }
58678a175baeSEmil Constantinescu 
58688a175baeSEmil Constantinescu /*@
58698a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
58708a175baeSEmil Constantinescu    Collective on TS
58718a175baeSEmil Constantinescu 
58728a175baeSEmil Constantinescu    Input Arguments:
58738a175baeSEmil Constantinescu +  ts - time stepping context
58748a175baeSEmil Constantinescu .  E - error vector
58758a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58768a175baeSEmil Constantinescu -  Y - state vector, previous time step
58778a175baeSEmil Constantinescu 
58788a175baeSEmil Constantinescu    Output Arguments:
58798a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58808a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58818a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58828a175baeSEmil Constantinescu 
58838a175baeSEmil Constantinescu    Level: developer
58848a175baeSEmil Constantinescu 
58858a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
58868a175baeSEmil Constantinescu @*/
58878a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58888a175baeSEmil Constantinescu {
58898a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58908a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58918a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58928a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
58938a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58948a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
58958a175baeSEmil Constantinescu 
58968a175baeSEmil Constantinescu   PetscFunctionBegin;
58978a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58988a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58998a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
59008a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
59018a175baeSEmil Constantinescu   PetscValidType(E,2);
59028a175baeSEmil Constantinescu   PetscValidType(U,3);
59038a175baeSEmil Constantinescu   PetscValidType(Y,4);
59048a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59058a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59068a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59078a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59088a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59098a175baeSEmil Constantinescu 
59108a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59118a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59128a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59138a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59148a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59158a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59168a175baeSEmil Constantinescu 
59178a175baeSEmil Constantinescu   max=0.;
59188a175baeSEmil Constantinescu   maxa=0.;
59198a175baeSEmil Constantinescu   maxr=0.;
59208a175baeSEmil Constantinescu 
59218a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59228a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
59238a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59248a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59258a175baeSEmil Constantinescu 
59268a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59278a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59288a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59298a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59308a175baeSEmil Constantinescu       tol  = tola+tolr;
59318a175baeSEmil Constantinescu       if(tola>0.){
59328a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59338a175baeSEmil Constantinescu       }
59348a175baeSEmil Constantinescu       if(tolr>0.){
59358a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59368a175baeSEmil Constantinescu       }
59378a175baeSEmil Constantinescu       if(tol>0.){
59388a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59398a175baeSEmil Constantinescu       }
59408a175baeSEmil Constantinescu     }
59418a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59428a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59438a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59448a175baeSEmil Constantinescu     const PetscScalar *atol;
59458a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59468a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59478a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59488a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59498a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59508a175baeSEmil Constantinescu       tol  = tola+tolr;
59518a175baeSEmil Constantinescu       if(tola>0.){
59528a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59538a175baeSEmil Constantinescu       }
59548a175baeSEmil Constantinescu       if(tolr>0.){
59558a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59568a175baeSEmil Constantinescu       }
59578a175baeSEmil Constantinescu       if(tol>0.){
59588a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59598a175baeSEmil Constantinescu       }
59608a175baeSEmil Constantinescu     }
59618a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59628a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59638a175baeSEmil Constantinescu     const PetscScalar *rtol;
59648a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59658a175baeSEmil Constantinescu 
59668a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59678a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59688a175baeSEmil Constantinescu       tola = ts->atol;
59698a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59708a175baeSEmil Constantinescu       tol  = tola+tolr;
59718a175baeSEmil Constantinescu       if(tola>0.){
59728a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59738a175baeSEmil Constantinescu       }
59748a175baeSEmil Constantinescu       if(tolr>0.){
59758a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59768a175baeSEmil Constantinescu       }
59778a175baeSEmil Constantinescu       if(tol>0.){
59788a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59798a175baeSEmil Constantinescu       }
59808a175baeSEmil Constantinescu     }
59818a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59828a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
59838a175baeSEmil Constantinescu 
59848a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59858a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59868a175baeSEmil Constantinescu       tola = ts->atol;
59878a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59888a175baeSEmil Constantinescu       tol  = tola+tolr;
59898a175baeSEmil Constantinescu       if(tola>0.){
59908a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59918a175baeSEmil Constantinescu       }
59928a175baeSEmil Constantinescu       if(tolr>0.){
59938a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59948a175baeSEmil Constantinescu       }
59958a175baeSEmil Constantinescu       if(tol>0.){
59968a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59978a175baeSEmil Constantinescu       }
59988a175baeSEmil Constantinescu     }
59998a175baeSEmil Constantinescu   }
60008a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60018a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60028a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60038a175baeSEmil Constantinescu   err_loc[0] = max;
60048a175baeSEmil Constantinescu   err_loc[1] = maxa;
60058a175baeSEmil Constantinescu   err_loc[2] = maxr;
6006a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60078a175baeSEmil Constantinescu   gmax   = err_glb[0];
60088a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
60098a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
60108a175baeSEmil Constantinescu 
60118a175baeSEmil Constantinescu   *norm = gmax;
60128a175baeSEmil Constantinescu   *norma = gmaxa;
60138a175baeSEmil Constantinescu   *normr = gmaxr;
60148a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60158a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60168a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60178a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60188a175baeSEmil Constantinescu }
60198a175baeSEmil Constantinescu 
60208a175baeSEmil Constantinescu /*@
60218a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
60228a175baeSEmil Constantinescu 
60238a175baeSEmil Constantinescu    Collective on TS
60248a175baeSEmil Constantinescu 
60258a175baeSEmil Constantinescu    Input Arguments:
60268a175baeSEmil Constantinescu +  ts - time stepping context
60278a175baeSEmil Constantinescu .  E - error vector
60288a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60298a175baeSEmil Constantinescu .  Y - state vector, previous time step
60308a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60318a175baeSEmil Constantinescu 
60328a175baeSEmil Constantinescu    Output Arguments:
60338a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60348a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
60358a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
60368a175baeSEmil Constantinescu 
60378a175baeSEmil Constantinescu    Options Database Keys:
60388a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
60398a175baeSEmil Constantinescu 
60408a175baeSEmil Constantinescu    Level: developer
60418a175baeSEmil Constantinescu 
60428a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
60438a175baeSEmil Constantinescu @*/
60448a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60458a175baeSEmil Constantinescu {
60468a175baeSEmil Constantinescu   PetscErrorCode ierr;
60478a175baeSEmil Constantinescu 
60488a175baeSEmil Constantinescu   PetscFunctionBegin;
60498a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
60508a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60518a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
60528a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60538a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60548a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60558a175baeSEmil Constantinescu }
60568a175baeSEmil Constantinescu 
60578a175baeSEmil Constantinescu 
60588d59e960SJed Brown /*@
60598d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
60608d59e960SJed Brown 
60618d59e960SJed Brown    Logically Collective on TS
60628d59e960SJed Brown 
60638d59e960SJed Brown    Input Arguments:
60648d59e960SJed Brown +  ts - time stepping context
60658d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
60668d59e960SJed Brown 
60678d59e960SJed Brown    Note:
60688d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
60698d59e960SJed Brown 
60708d59e960SJed Brown    Level: intermediate
60718d59e960SJed Brown 
60728d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
60738d59e960SJed Brown @*/
60748d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
60758d59e960SJed Brown {
60768d59e960SJed Brown   PetscFunctionBegin;
60778d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60788d59e960SJed Brown   ts->cfltime_local = cfltime;
60798d59e960SJed Brown   ts->cfltime       = -1.;
60808d59e960SJed Brown   PetscFunctionReturn(0);
60818d59e960SJed Brown }
60828d59e960SJed Brown 
60838d59e960SJed Brown /*@
60848d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
60858d59e960SJed Brown 
60868d59e960SJed Brown    Collective on TS
60878d59e960SJed Brown 
60888d59e960SJed Brown    Input Arguments:
60898d59e960SJed Brown .  ts - time stepping context
60908d59e960SJed Brown 
60918d59e960SJed Brown    Output Arguments:
60928d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
60938d59e960SJed Brown 
60948d59e960SJed Brown    Level: advanced
60958d59e960SJed Brown 
60968d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
60978d59e960SJed Brown @*/
60988d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
60998d59e960SJed Brown {
61008d59e960SJed Brown   PetscErrorCode ierr;
61018d59e960SJed Brown 
61028d59e960SJed Brown   PetscFunctionBegin;
61038d59e960SJed Brown   if (ts->cfltime < 0) {
6104b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61058d59e960SJed Brown   }
61068d59e960SJed Brown   *cfltime = ts->cfltime;
61078d59e960SJed Brown   PetscFunctionReturn(0);
61088d59e960SJed Brown }
61098d59e960SJed Brown 
6110d6ebe24aSShri Abhyankar /*@
6111d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6112d6ebe24aSShri Abhyankar 
6113d6ebe24aSShri Abhyankar    Input Parameters:
6114d6ebe24aSShri Abhyankar .  ts   - the TS context.
6115d6ebe24aSShri Abhyankar .  xl   - lower bound.
6116d6ebe24aSShri Abhyankar .  xu   - upper bound.
6117d6ebe24aSShri Abhyankar 
6118d6ebe24aSShri Abhyankar    Notes:
6119d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6120e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6121d6ebe24aSShri Abhyankar 
61222bd2b0e6SSatish Balay    Level: advanced
61232bd2b0e6SSatish Balay 
6124d6ebe24aSShri Abhyankar @*/
6125d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6126d6ebe24aSShri Abhyankar {
6127d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6128d6ebe24aSShri Abhyankar   SNES           snes;
6129d6ebe24aSShri Abhyankar 
6130d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6131d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6132d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6133d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6134d6ebe24aSShri Abhyankar }
6135d6ebe24aSShri Abhyankar 
6136325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6137c6db04a5SJed Brown #include <mex.h>
6138325fc9f4SBarry Smith 
6139325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6140325fc9f4SBarry Smith 
6141325fc9f4SBarry Smith /*
6142325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6143325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6144325fc9f4SBarry Smith 
6145325fc9f4SBarry Smith    Collective on TS
6146325fc9f4SBarry Smith 
6147325fc9f4SBarry Smith    Input Parameters:
6148325fc9f4SBarry Smith +  snes - the TS context
61490910c330SBarry Smith -  u - input vector
6150325fc9f4SBarry Smith 
6151325fc9f4SBarry Smith    Output Parameter:
6152325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6153325fc9f4SBarry Smith 
6154325fc9f4SBarry Smith    Notes:
6155325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6156325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6157325fc9f4SBarry Smith    themselves.
6158325fc9f4SBarry Smith 
6159325fc9f4SBarry Smith    Level: developer
6160325fc9f4SBarry Smith 
6161325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6162325fc9f4SBarry Smith 
6163325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6164325fc9f4SBarry Smith */
61650910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6166325fc9f4SBarry Smith {
6167325fc9f4SBarry Smith   PetscErrorCode  ierr;
6168325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6169325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6170325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6171325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6172325fc9f4SBarry Smith 
6173325fc9f4SBarry Smith   PetscFunctionBegin;
6174325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
61750910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
61760910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6177325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
61780910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6179325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6180325fc9f4SBarry Smith 
6181325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
61820910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
61830910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
61840910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6185bbd56ea5SKarl Rupp 
6186325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6187325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6188325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6189325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6190325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6191325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6192325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6193325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6194325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6195325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6196325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6197325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6198325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6199325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6200325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6201325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6202325fc9f4SBarry Smith   PetscFunctionReturn(0);
6203325fc9f4SBarry Smith }
6204325fc9f4SBarry Smith 
6205325fc9f4SBarry Smith /*
6206325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6207325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6208e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6209325fc9f4SBarry Smith 
6210325fc9f4SBarry Smith    Logically Collective on TS
6211325fc9f4SBarry Smith 
6212325fc9f4SBarry Smith    Input Parameters:
6213325fc9f4SBarry Smith +  ts - the TS context
6214325fc9f4SBarry Smith -  func - function evaluation routine
6215325fc9f4SBarry Smith 
6216325fc9f4SBarry Smith    Calling sequence of func:
62170910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6218325fc9f4SBarry Smith 
6219325fc9f4SBarry Smith    Level: beginner
6220325fc9f4SBarry Smith 
6221325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6222325fc9f4SBarry Smith 
6223325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6224325fc9f4SBarry Smith */
6225cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6226325fc9f4SBarry Smith {
6227325fc9f4SBarry Smith   PetscErrorCode  ierr;
6228325fc9f4SBarry Smith   TSMatlabContext *sctx;
6229325fc9f4SBarry Smith 
6230325fc9f4SBarry Smith   PetscFunctionBegin;
6231325fc9f4SBarry Smith   /* currently sctx is memory bleed */
623295dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6233325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6234325fc9f4SBarry Smith   /*
6235325fc9f4SBarry Smith      This should work, but it doesn't
6236325fc9f4SBarry Smith   sctx->ctx = ctx;
6237325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6238325fc9f4SBarry Smith   */
6239325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6240bbd56ea5SKarl Rupp 
62410298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6242325fc9f4SBarry Smith   PetscFunctionReturn(0);
6243325fc9f4SBarry Smith }
6244325fc9f4SBarry Smith 
6245325fc9f4SBarry Smith /*
6246325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6247325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6248325fc9f4SBarry Smith 
6249325fc9f4SBarry Smith    Collective on TS
6250325fc9f4SBarry Smith 
6251325fc9f4SBarry Smith    Input Parameters:
6252cdcf91faSSean Farley +  ts - the TS context
62530910c330SBarry Smith .  u - input vector
6254325fc9f4SBarry Smith .  A, B - the matrices
6255325fc9f4SBarry Smith -  ctx - user context
6256325fc9f4SBarry Smith 
6257325fc9f4SBarry Smith    Level: developer
6258325fc9f4SBarry Smith 
6259325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6260325fc9f4SBarry Smith 
6261325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6262325fc9f4SBarry Smith @*/
6263f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6264325fc9f4SBarry Smith {
6265325fc9f4SBarry Smith   PetscErrorCode  ierr;
6266325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6267325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6268325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6269325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6270325fc9f4SBarry Smith 
6271325fc9f4SBarry Smith   PetscFunctionBegin;
6272cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62730910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6274325fc9f4SBarry Smith 
62750910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6276325fc9f4SBarry Smith 
6277cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
62780910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62790910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
62800910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
62810910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6282bbd56ea5SKarl Rupp 
6283325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6284325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6285325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6286325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6287325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6288325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6289325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6290325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6291325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6292325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6293325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6294325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6295325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6296325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6297325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6298325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6299325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6300325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6301325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6302325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6303325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6304325fc9f4SBarry Smith   PetscFunctionReturn(0);
6305325fc9f4SBarry Smith }
6306325fc9f4SBarry Smith 
6307325fc9f4SBarry Smith /*
6308325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6309e3c5b3baSBarry 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
6310325fc9f4SBarry Smith 
6311325fc9f4SBarry Smith    Logically Collective on TS
6312325fc9f4SBarry Smith 
6313325fc9f4SBarry Smith    Input Parameters:
6314cdcf91faSSean Farley +  ts - the TS context
6315325fc9f4SBarry Smith .  A,B - Jacobian matrices
6316325fc9f4SBarry Smith .  func - function evaluation routine
6317325fc9f4SBarry Smith -  ctx - user context
6318325fc9f4SBarry Smith 
6319325fc9f4SBarry Smith    Calling sequence of func:
63200910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6321325fc9f4SBarry Smith 
6322325fc9f4SBarry Smith    Level: developer
6323325fc9f4SBarry Smith 
6324325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6325325fc9f4SBarry Smith 
6326325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6327325fc9f4SBarry Smith */
6328cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6329325fc9f4SBarry Smith {
6330325fc9f4SBarry Smith   PetscErrorCode  ierr;
6331325fc9f4SBarry Smith   TSMatlabContext *sctx;
6332325fc9f4SBarry Smith 
6333325fc9f4SBarry Smith   PetscFunctionBegin;
6334325fc9f4SBarry Smith   /* currently sctx is memory bleed */
633595dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6336325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6337325fc9f4SBarry Smith   /*
6338325fc9f4SBarry Smith      This should work, but it doesn't
6339325fc9f4SBarry Smith   sctx->ctx = ctx;
6340325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6341325fc9f4SBarry Smith   */
6342325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6343bbd56ea5SKarl Rupp 
6344cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6345325fc9f4SBarry Smith   PetscFunctionReturn(0);
6346325fc9f4SBarry Smith }
6347325fc9f4SBarry Smith 
6348b5b1a830SBarry Smith /*
6349b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6350b5b1a830SBarry Smith 
6351b5b1a830SBarry Smith    Collective on TS
6352b5b1a830SBarry Smith 
6353b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6354b5b1a830SBarry Smith @*/
63550910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6356b5b1a830SBarry Smith {
6357b5b1a830SBarry Smith   PetscErrorCode  ierr;
6358b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6359a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6360b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6361b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6362b5b1a830SBarry Smith 
6363b5b1a830SBarry Smith   PetscFunctionBegin;
6364cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63650910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6366b5b1a830SBarry Smith 
6367cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
63680910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6369bbd56ea5SKarl Rupp 
6370b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6371b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6372b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6373b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6374b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6375b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6376b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6377b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6378b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6379b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6380b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6381b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6382b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6383b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6384b5b1a830SBarry Smith   PetscFunctionReturn(0);
6385b5b1a830SBarry Smith }
6386b5b1a830SBarry Smith 
6387b5b1a830SBarry Smith /*
6388b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6389b5b1a830SBarry Smith 
6390b5b1a830SBarry Smith    Level: developer
6391b5b1a830SBarry Smith 
6392b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6393b5b1a830SBarry Smith 
6394b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6395b5b1a830SBarry Smith */
6396cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6397b5b1a830SBarry Smith {
6398b5b1a830SBarry Smith   PetscErrorCode  ierr;
6399b5b1a830SBarry Smith   TSMatlabContext *sctx;
6400b5b1a830SBarry Smith 
6401b5b1a830SBarry Smith   PetscFunctionBegin;
6402b5b1a830SBarry Smith   /* currently sctx is memory bleed */
640395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6404b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6405b5b1a830SBarry Smith   /*
6406b5b1a830SBarry Smith      This should work, but it doesn't
6407b5b1a830SBarry Smith   sctx->ctx = ctx;
6408b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6409b5b1a830SBarry Smith   */
6410b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6411bbd56ea5SKarl Rupp 
64120298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6413b5b1a830SBarry Smith   PetscFunctionReturn(0);
6414b5b1a830SBarry Smith }
6415325fc9f4SBarry Smith #endif
6416b3603a34SBarry Smith 
6417b3603a34SBarry Smith /*@C
64184f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6419b3603a34SBarry Smith        in a time based line graph
6420b3603a34SBarry Smith 
6421b3603a34SBarry Smith    Collective on TS
6422b3603a34SBarry Smith 
6423b3603a34SBarry Smith    Input Parameters:
6424b3603a34SBarry Smith +  ts - the TS context
6425b3603a34SBarry Smith .  step - current time-step
6426b3603a34SBarry Smith .  ptime - current time
64277db568b7SBarry Smith .  u - current solution
64287db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6429b3603a34SBarry Smith 
6430b3d3934dSBarry Smith    Options Database:
64319ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6432b3d3934dSBarry Smith 
6433b3603a34SBarry Smith    Level: intermediate
6434b3603a34SBarry Smith 
643595452b02SPatrick Sanan    Notes:
643695452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6437b3603a34SBarry Smith 
6438b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6439b3603a34SBarry Smith 
64407db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
64417db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
64427db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
64437db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6444b3603a34SBarry Smith @*/
64457db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6446b3603a34SBarry Smith {
6447b3603a34SBarry Smith   PetscErrorCode    ierr;
64487db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6449b3603a34SBarry Smith   const PetscScalar *yy;
645080666b62SBarry Smith   Vec               v;
6451b3603a34SBarry Smith 
6452b3603a34SBarry Smith   PetscFunctionBegin;
645363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
645458ff32f7SBarry Smith   if (!step) {
6455a9f9c1f6SBarry Smith     PetscDrawAxis axis;
64566934998bSLisandro Dalcin     PetscInt      dim;
6457a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6458a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6459bab0a581SBarry Smith     if (!ctx->names) {
6460bab0a581SBarry Smith       PetscBool flg;
6461bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6462bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6463bab0a581SBarry Smith       if (flg) {
6464bab0a581SBarry Smith         PetscInt i,n;
6465bab0a581SBarry Smith         char     **names;
6466bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6467bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6468bab0a581SBarry Smith         for (i=0; i<n; i++) {
6469bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6470bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6471bab0a581SBarry Smith         }
6472bab0a581SBarry Smith         names[n] = NULL;
6473bab0a581SBarry Smith         ctx->names = names;
6474bab0a581SBarry Smith       }
6475bab0a581SBarry Smith     }
6476387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6477387f4636SBarry Smith       char      **displaynames;
6478387f4636SBarry Smith       PetscBool flg;
6479387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
648095dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6481387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6482c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6483387f4636SBarry Smith       if (flg) {
6484a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6485387f4636SBarry Smith       }
6486387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6487387f4636SBarry Smith     }
6488387f4636SBarry Smith     if (ctx->displaynames) {
6489387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6490387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6491387f4636SBarry Smith     } else if (ctx->names) {
64920910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
64930b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6494387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6495b0bc92c6SBarry Smith     } else {
6496b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6497b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6498387f4636SBarry Smith     }
64990b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
650058ff32f7SBarry Smith   }
65016934998bSLisandro Dalcin 
65026934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65036934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
650480666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65056934998bSLisandro Dalcin   if (ctx->displaynames) {
65066934998bSLisandro Dalcin     PetscInt i;
65076934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
65086934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
65096934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
65106934998bSLisandro Dalcin   } else {
6511e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6512e3efe391SJed Brown     PetscInt  i,n;
65136934998bSLisandro Dalcin     PetscReal *yreal;
651480666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6515785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6516e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65170b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6518e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6519e3efe391SJed Brown #else
65200b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6521e3efe391SJed Brown #endif
652280666b62SBarry Smith   }
65236934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
65246934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65256934998bSLisandro Dalcin 
6526b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65270b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65286934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65293923b477SBarry Smith   }
6530b3603a34SBarry Smith   PetscFunctionReturn(0);
6531b3603a34SBarry Smith }
6532b3603a34SBarry Smith 
6533b037adc7SBarry Smith /*@C
653431152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6535b037adc7SBarry Smith 
6536b037adc7SBarry Smith    Collective on TS
6537b037adc7SBarry Smith 
6538b037adc7SBarry Smith    Input Parameters:
6539b037adc7SBarry Smith +  ts - the TS context
6540b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6541b037adc7SBarry Smith 
6542b037adc7SBarry Smith    Level: intermediate
6543b037adc7SBarry Smith 
654495452b02SPatrick Sanan    Notes:
654595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65467db568b7SBarry Smith 
6547b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6548b037adc7SBarry Smith 
6549a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6550b037adc7SBarry Smith @*/
655131152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6552b037adc7SBarry Smith {
6553b037adc7SBarry Smith   PetscErrorCode    ierr;
6554b037adc7SBarry Smith   PetscInt          i;
6555b037adc7SBarry Smith 
6556b037adc7SBarry Smith   PetscFunctionBegin;
6557b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6558b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
65595537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6560b3d3934dSBarry Smith       break;
6561b3d3934dSBarry Smith     }
6562b3d3934dSBarry Smith   }
6563b3d3934dSBarry Smith   PetscFunctionReturn(0);
6564b3d3934dSBarry Smith }
6565b3d3934dSBarry Smith 
6566e673d494SBarry Smith /*@C
6567e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6568e673d494SBarry Smith 
6569e673d494SBarry Smith    Collective on TS
6570e673d494SBarry Smith 
6571e673d494SBarry Smith    Input Parameters:
6572e673d494SBarry Smith +  ts - the TS context
6573e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6574e673d494SBarry Smith 
6575e673d494SBarry Smith    Level: intermediate
6576e673d494SBarry Smith 
6577e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6578e673d494SBarry Smith 
6579a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6580e673d494SBarry Smith @*/
6581e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6582e673d494SBarry Smith {
6583e673d494SBarry Smith   PetscErrorCode    ierr;
6584e673d494SBarry Smith 
6585e673d494SBarry Smith   PetscFunctionBegin;
6586e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6587e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6588e673d494SBarry Smith   PetscFunctionReturn(0);
6589e673d494SBarry Smith }
6590e673d494SBarry Smith 
6591b3d3934dSBarry Smith /*@C
6592b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6593b3d3934dSBarry Smith 
6594b3d3934dSBarry Smith    Collective on TS
6595b3d3934dSBarry Smith 
6596b3d3934dSBarry Smith    Input Parameter:
6597b3d3934dSBarry Smith .  ts - the TS context
6598b3d3934dSBarry Smith 
6599b3d3934dSBarry Smith    Output Parameter:
6600b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6601b3d3934dSBarry Smith 
6602b3d3934dSBarry Smith    Level: intermediate
6603b3d3934dSBarry Smith 
660495452b02SPatrick Sanan    Notes:
660595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66067db568b7SBarry Smith 
6607b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6608b3d3934dSBarry Smith 
6609b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6610b3d3934dSBarry Smith @*/
6611b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6612b3d3934dSBarry Smith {
6613b3d3934dSBarry Smith   PetscInt       i;
6614b3d3934dSBarry Smith 
6615b3d3934dSBarry Smith   PetscFunctionBegin;
6616b3d3934dSBarry Smith   *names = NULL;
6617b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6618b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66195537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6620b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6621b3d3934dSBarry Smith       break;
6622387f4636SBarry Smith     }
6623387f4636SBarry Smith   }
6624387f4636SBarry Smith   PetscFunctionReturn(0);
6625387f4636SBarry Smith }
6626387f4636SBarry Smith 
6627a66092f1SBarry Smith /*@C
6628a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6629a66092f1SBarry Smith 
6630a66092f1SBarry Smith    Collective on TS
6631a66092f1SBarry Smith 
6632a66092f1SBarry Smith    Input Parameters:
6633a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6634a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6635a66092f1SBarry Smith 
6636a66092f1SBarry Smith    Level: intermediate
6637a66092f1SBarry Smith 
6638a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6639a66092f1SBarry Smith 
6640a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6641a66092f1SBarry Smith @*/
6642a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6643a66092f1SBarry Smith {
6644a66092f1SBarry Smith   PetscInt          j = 0,k;
6645a66092f1SBarry Smith   PetscErrorCode    ierr;
6646a66092f1SBarry Smith 
6647a66092f1SBarry Smith   PetscFunctionBegin;
6648a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6649a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6650a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6651a66092f1SBarry Smith   while (displaynames[j]) j++;
6652a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6653a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6654a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6655a66092f1SBarry Smith   j = 0;
6656a66092f1SBarry Smith   while (displaynames[j]) {
6657a66092f1SBarry Smith     k = 0;
6658a66092f1SBarry Smith     while (ctx->names[k]) {
6659a66092f1SBarry Smith       PetscBool flg;
6660a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6661a66092f1SBarry Smith       if (flg) {
6662a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6663a66092f1SBarry Smith         break;
6664a66092f1SBarry Smith       }
6665a66092f1SBarry Smith       k++;
6666a66092f1SBarry Smith     }
6667a66092f1SBarry Smith     j++;
6668a66092f1SBarry Smith   }
6669a66092f1SBarry Smith   PetscFunctionReturn(0);
6670a66092f1SBarry Smith }
6671a66092f1SBarry Smith 
6672387f4636SBarry Smith /*@C
6673387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6674387f4636SBarry Smith 
6675387f4636SBarry Smith    Collective on TS
6676387f4636SBarry Smith 
6677387f4636SBarry Smith    Input Parameters:
6678387f4636SBarry Smith +  ts - the TS context
6679387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6680387f4636SBarry Smith 
668195452b02SPatrick Sanan    Notes:
668295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66837db568b7SBarry Smith 
6684387f4636SBarry Smith    Level: intermediate
6685387f4636SBarry Smith 
6686387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6687387f4636SBarry Smith 
6688387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6689387f4636SBarry Smith @*/
6690387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6691387f4636SBarry Smith {
6692a66092f1SBarry Smith   PetscInt          i;
6693387f4636SBarry Smith   PetscErrorCode    ierr;
6694387f4636SBarry Smith 
6695387f4636SBarry Smith   PetscFunctionBegin;
6696387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6697387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66985537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6699b3d3934dSBarry Smith       break;
6700b037adc7SBarry Smith     }
6701b037adc7SBarry Smith   }
6702b037adc7SBarry Smith   PetscFunctionReturn(0);
6703b037adc7SBarry Smith }
6704b037adc7SBarry Smith 
670580666b62SBarry Smith /*@C
670680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
670780666b62SBarry Smith 
670880666b62SBarry Smith    Collective on TS
670980666b62SBarry Smith 
671080666b62SBarry Smith    Input Parameters:
671180666b62SBarry Smith +  ts - the TS context
671280666b62SBarry Smith .  transform - the transform function
67137684fa3eSBarry Smith .  destroy - function to destroy the optional context
671480666b62SBarry Smith -  ctx - optional context used by transform function
671580666b62SBarry Smith 
671695452b02SPatrick Sanan    Notes:
671795452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67187db568b7SBarry Smith 
671980666b62SBarry Smith    Level: intermediate
672080666b62SBarry Smith 
672180666b62SBarry Smith .keywords: TS,  vector, monitor, view
672280666b62SBarry Smith 
6723a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
672480666b62SBarry Smith @*/
67257684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
672680666b62SBarry Smith {
672780666b62SBarry Smith   PetscInt          i;
6728a66092f1SBarry Smith   PetscErrorCode    ierr;
672980666b62SBarry Smith 
673080666b62SBarry Smith   PetscFunctionBegin;
673180666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
673280666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67335537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
673480666b62SBarry Smith     }
673580666b62SBarry Smith   }
673680666b62SBarry Smith   PetscFunctionReturn(0);
673780666b62SBarry Smith }
673880666b62SBarry Smith 
6739e673d494SBarry Smith /*@C
6740e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6741e673d494SBarry Smith 
6742e673d494SBarry Smith    Collective on TSLGCtx
6743e673d494SBarry Smith 
6744e673d494SBarry Smith    Input Parameters:
6745e673d494SBarry Smith +  ts - the TS context
6746e673d494SBarry Smith .  transform - the transform function
67477684fa3eSBarry Smith .  destroy - function to destroy the optional context
6748e673d494SBarry Smith -  ctx - optional context used by transform function
6749e673d494SBarry Smith 
6750e673d494SBarry Smith    Level: intermediate
6751e673d494SBarry Smith 
6752e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6753e673d494SBarry Smith 
6754a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6755e673d494SBarry Smith @*/
67567684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6757e673d494SBarry Smith {
6758e673d494SBarry Smith   PetscFunctionBegin;
6759e673d494SBarry Smith   ctx->transform    = transform;
67607684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6761e673d494SBarry Smith   ctx->transformctx = tctx;
6762e673d494SBarry Smith   PetscFunctionReturn(0);
6763e673d494SBarry Smith }
6764e673d494SBarry Smith 
6765ef20d060SBarry Smith /*@C
67668b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6767ef20d060SBarry Smith        in a time based line graph
6768ef20d060SBarry Smith 
6769ef20d060SBarry Smith    Collective on TS
6770ef20d060SBarry Smith 
6771ef20d060SBarry Smith    Input Parameters:
6772ef20d060SBarry Smith +  ts - the TS context
6773ef20d060SBarry Smith .  step - current time-step
6774ef20d060SBarry Smith .  ptime - current time
67757db568b7SBarry Smith .  u - current solution
67767db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6777ef20d060SBarry Smith 
6778ef20d060SBarry Smith    Level: intermediate
6779ef20d060SBarry Smith 
678095452b02SPatrick Sanan    Notes:
678195452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6782abd5a294SJed Brown 
6783abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6784abd5a294SJed Brown 
6785abd5a294SJed Brown    Options Database Keys:
67864f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6787ef20d060SBarry Smith 
6788ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6789ef20d060SBarry Smith 
6790abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6791ef20d060SBarry Smith @*/
67920910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6793ef20d060SBarry Smith {
6794ef20d060SBarry Smith   PetscErrorCode    ierr;
67950b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6796ef20d060SBarry Smith   const PetscScalar *yy;
6797ef20d060SBarry Smith   Vec               y;
6798ef20d060SBarry Smith 
6799ef20d060SBarry Smith   PetscFunctionBegin;
6800a9f9c1f6SBarry Smith   if (!step) {
6801a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68026934998bSLisandro Dalcin     PetscInt      dim;
6803a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68048b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68050910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6806a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6807a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6808a9f9c1f6SBarry Smith   }
68090910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6810ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68110910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6812ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6813e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6814e3efe391SJed Brown   {
6815e3efe391SJed Brown     PetscReal *yreal;
6816e3efe391SJed Brown     PetscInt  i,n;
6817e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6818785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6819e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68200b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6821e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6822e3efe391SJed Brown   }
6823e3efe391SJed Brown #else
68240b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6825e3efe391SJed Brown #endif
6826ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6827ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6828b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68290b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
68306934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
68313923b477SBarry Smith   }
6832ef20d060SBarry Smith   PetscFunctionReturn(0);
6833ef20d060SBarry Smith }
6834ef20d060SBarry Smith 
68357cf37e64SBarry Smith /*@C
68367cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
68377cf37e64SBarry Smith 
68387cf37e64SBarry Smith    Collective on TS
68397cf37e64SBarry Smith 
68407cf37e64SBarry Smith    Input Parameters:
68417cf37e64SBarry Smith +  ts - the TS context
68427cf37e64SBarry Smith .  step - current time-step
68437cf37e64SBarry Smith .  ptime - current time
68447cf37e64SBarry Smith .  u - current solution
68457cf37e64SBarry Smith -  dctx - unused context
68467cf37e64SBarry Smith 
68477cf37e64SBarry Smith    Level: intermediate
68487cf37e64SBarry Smith 
68497cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
68507cf37e64SBarry Smith 
68517cf37e64SBarry Smith    Options Database Keys:
68527cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
68537cf37e64SBarry Smith 
68547cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
68557cf37e64SBarry Smith 
68567cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
68577cf37e64SBarry Smith @*/
6858edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
68597cf37e64SBarry Smith {
68607cf37e64SBarry Smith   PetscErrorCode    ierr;
68617cf37e64SBarry Smith   Vec               y;
68627cf37e64SBarry Smith   PetscReal         nrm;
6863edbaebb3SBarry Smith   PetscBool         flg;
68647cf37e64SBarry Smith 
68657cf37e64SBarry Smith   PetscFunctionBegin;
68667cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
68677cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68687cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6869edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6870edbaebb3SBarry Smith   if (flg) {
68717cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6872edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6873edbaebb3SBarry Smith   }
6874edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6875edbaebb3SBarry Smith   if (flg) {
6876edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6877edbaebb3SBarry Smith   }
6878edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
68797cf37e64SBarry Smith   PetscFunctionReturn(0);
68807cf37e64SBarry Smith }
68817cf37e64SBarry Smith 
6882201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6883201da799SBarry Smith {
6884201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6885201da799SBarry Smith   PetscReal      x   = ptime,y;
6886201da799SBarry Smith   PetscErrorCode ierr;
6887201da799SBarry Smith   PetscInt       its;
6888201da799SBarry Smith 
6889201da799SBarry Smith   PetscFunctionBegin;
689063e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6891201da799SBarry Smith   if (!n) {
6892201da799SBarry Smith     PetscDrawAxis axis;
6893201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6894201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6895201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6896201da799SBarry Smith     ctx->snes_its = 0;
6897201da799SBarry Smith   }
6898201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6899201da799SBarry Smith   y    = its - ctx->snes_its;
6900201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69013fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6902201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69036934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6904201da799SBarry Smith   }
6905201da799SBarry Smith   ctx->snes_its = its;
6906201da799SBarry Smith   PetscFunctionReturn(0);
6907201da799SBarry Smith }
6908201da799SBarry Smith 
6909201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6910201da799SBarry Smith {
6911201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6912201da799SBarry Smith   PetscReal      x   = ptime,y;
6913201da799SBarry Smith   PetscErrorCode ierr;
6914201da799SBarry Smith   PetscInt       its;
6915201da799SBarry Smith 
6916201da799SBarry Smith   PetscFunctionBegin;
691763e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6918201da799SBarry Smith   if (!n) {
6919201da799SBarry Smith     PetscDrawAxis axis;
6920201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6921201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6922201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6923201da799SBarry Smith     ctx->ksp_its = 0;
6924201da799SBarry Smith   }
6925201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6926201da799SBarry Smith   y    = its - ctx->ksp_its;
6927201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
692899fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6929201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69306934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6931201da799SBarry Smith   }
6932201da799SBarry Smith   ctx->ksp_its = its;
6933201da799SBarry Smith   PetscFunctionReturn(0);
6934201da799SBarry Smith }
6935f9c1d6abSBarry Smith 
6936f9c1d6abSBarry Smith /*@
6937f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6938f9c1d6abSBarry Smith 
6939f9c1d6abSBarry Smith    Collective on TS and Vec
6940f9c1d6abSBarry Smith 
6941f9c1d6abSBarry Smith    Input Parameters:
6942f9c1d6abSBarry Smith +  ts - the TS context
6943f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6944f9c1d6abSBarry Smith 
6945f9c1d6abSBarry Smith    Output Parameters:
6946f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6947f9c1d6abSBarry Smith 
6948f9c1d6abSBarry Smith    Level: developer
6949f9c1d6abSBarry Smith 
6950f9c1d6abSBarry Smith .keywords: TS, compute
6951f9c1d6abSBarry Smith 
6952f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6953f9c1d6abSBarry Smith @*/
6954f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6955f9c1d6abSBarry Smith {
6956f9c1d6abSBarry Smith   PetscErrorCode ierr;
6957f9c1d6abSBarry Smith 
6958f9c1d6abSBarry Smith   PetscFunctionBegin;
6959f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6960ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6961f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6962f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6963f9c1d6abSBarry Smith }
696424655328SShri 
6965b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6966b3d3934dSBarry Smith /*@C
6967b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6968b3d3934dSBarry Smith 
6969b3d3934dSBarry Smith    Collective on TS
6970b3d3934dSBarry Smith 
6971b3d3934dSBarry Smith    Input Parameters:
6972b3d3934dSBarry Smith .  ts  - the ODE solver object
6973b3d3934dSBarry Smith 
6974b3d3934dSBarry Smith    Output Parameter:
6975b3d3934dSBarry Smith .  ctx - the context
6976b3d3934dSBarry Smith 
6977b3d3934dSBarry Smith    Level: intermediate
6978b3d3934dSBarry Smith 
6979b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6980b3d3934dSBarry Smith 
6981b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6982b3d3934dSBarry Smith 
6983b3d3934dSBarry Smith @*/
6984b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6985b3d3934dSBarry Smith {
6986b3d3934dSBarry Smith   PetscErrorCode ierr;
6987b3d3934dSBarry Smith 
6988b3d3934dSBarry Smith   PetscFunctionBegin;
6989a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6990b3d3934dSBarry Smith   PetscFunctionReturn(0);
6991b3d3934dSBarry Smith }
6992b3d3934dSBarry Smith 
6993b3d3934dSBarry Smith /*@C
6994b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6995b3d3934dSBarry Smith 
6996b3d3934dSBarry Smith    Collective on TS
6997b3d3934dSBarry Smith 
6998b3d3934dSBarry Smith    Input Parameters:
6999b3d3934dSBarry Smith +  ts - the TS context
7000b3d3934dSBarry Smith .  step - current time-step
7001b3d3934dSBarry Smith .  ptime - current time
70027db568b7SBarry Smith .  u  - current solution
70037db568b7SBarry Smith -  dctx - the envelope context
7004b3d3934dSBarry Smith 
7005b3d3934dSBarry Smith    Options Database:
7006b3d3934dSBarry Smith .  -ts_monitor_envelope
7007b3d3934dSBarry Smith 
7008b3d3934dSBarry Smith    Level: intermediate
7009b3d3934dSBarry Smith 
701095452b02SPatrick Sanan    Notes:
701195452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7012b3d3934dSBarry Smith 
7013b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7014b3d3934dSBarry Smith 
70157db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7016b3d3934dSBarry Smith @*/
70177db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7018b3d3934dSBarry Smith {
7019b3d3934dSBarry Smith   PetscErrorCode       ierr;
70207db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7021b3d3934dSBarry Smith 
7022b3d3934dSBarry Smith   PetscFunctionBegin;
7023b3d3934dSBarry Smith   if (!ctx->max) {
7024b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7025b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7026b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7027b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7028b3d3934dSBarry Smith   } else {
7029b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7030b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7031b3d3934dSBarry Smith   }
7032b3d3934dSBarry Smith   PetscFunctionReturn(0);
7033b3d3934dSBarry Smith }
7034b3d3934dSBarry Smith 
7035b3d3934dSBarry Smith /*@C
7036b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7037b3d3934dSBarry Smith 
7038b3d3934dSBarry Smith    Collective on TS
7039b3d3934dSBarry Smith 
7040b3d3934dSBarry Smith    Input Parameter:
7041b3d3934dSBarry Smith .  ts - the TS context
7042b3d3934dSBarry Smith 
7043b3d3934dSBarry Smith    Output Parameter:
7044b3d3934dSBarry Smith +  max - the maximum values
7045b3d3934dSBarry Smith -  min - the minimum values
7046b3d3934dSBarry Smith 
704795452b02SPatrick Sanan    Notes:
704895452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
70497db568b7SBarry Smith 
7050b3d3934dSBarry Smith    Level: intermediate
7051b3d3934dSBarry Smith 
7052b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7053b3d3934dSBarry Smith 
7054b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7055b3d3934dSBarry Smith @*/
7056b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7057b3d3934dSBarry Smith {
7058b3d3934dSBarry Smith   PetscInt i;
7059b3d3934dSBarry Smith 
7060b3d3934dSBarry Smith   PetscFunctionBegin;
7061b3d3934dSBarry Smith   if (max) *max = NULL;
7062b3d3934dSBarry Smith   if (min) *min = NULL;
7063b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7064b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
70655537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7066b3d3934dSBarry Smith       if (max) *max = ctx->max;
7067b3d3934dSBarry Smith       if (min) *min = ctx->min;
7068b3d3934dSBarry Smith       break;
7069b3d3934dSBarry Smith     }
7070b3d3934dSBarry Smith   }
7071b3d3934dSBarry Smith   PetscFunctionReturn(0);
7072b3d3934dSBarry Smith }
7073b3d3934dSBarry Smith 
7074b3d3934dSBarry Smith /*@C
7075b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7076b3d3934dSBarry Smith 
7077b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7078b3d3934dSBarry Smith 
7079b3d3934dSBarry Smith    Input Parameter:
7080b3d3934dSBarry Smith .  ctx - the monitor context
7081b3d3934dSBarry Smith 
7082b3d3934dSBarry Smith    Level: intermediate
7083b3d3934dSBarry Smith 
7084b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7085b3d3934dSBarry Smith 
70867db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7087b3d3934dSBarry Smith @*/
7088b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7089b3d3934dSBarry Smith {
7090b3d3934dSBarry Smith   PetscErrorCode ierr;
7091b3d3934dSBarry Smith 
7092b3d3934dSBarry Smith   PetscFunctionBegin;
7093b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7094b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7095b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7096b3d3934dSBarry Smith   PetscFunctionReturn(0);
7097b3d3934dSBarry Smith }
7098f2dee214SBarry Smith 
709924655328SShri /*@
7100dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7101dcb233daSLisandro Dalcin 
7102dcb233daSLisandro Dalcin    Collective on TS
7103dcb233daSLisandro Dalcin 
7104dcb233daSLisandro Dalcin    Input Parameter:
7105dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7106dcb233daSLisandro Dalcin 
7107dcb233daSLisandro Dalcin    Level: advanced
7108dcb233daSLisandro Dalcin 
7109dcb233daSLisandro Dalcin    Notes:
7110dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7111dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7112dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7113dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7114dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7115dcb233daSLisandro Dalcin    discontinuous source terms).
7116dcb233daSLisandro Dalcin 
7117dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7118dcb233daSLisandro Dalcin 
7119dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7120dcb233daSLisandro Dalcin @*/
7121dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7122dcb233daSLisandro Dalcin {
7123dcb233daSLisandro Dalcin   PetscFunctionBegin;
7124dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7125dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7126dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7127dcb233daSLisandro Dalcin }
7128dcb233daSLisandro Dalcin 
7129dcb233daSLisandro Dalcin /*@
713024655328SShri    TSRollBack - Rolls back one time step
713124655328SShri 
713224655328SShri    Collective on TS
713324655328SShri 
713424655328SShri    Input Parameter:
713524655328SShri .  ts - the TS context obtained from TSCreate()
713624655328SShri 
713724655328SShri    Level: advanced
713824655328SShri 
713924655328SShri .keywords: TS, timestep, rollback
714024655328SShri 
714124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
714224655328SShri @*/
714324655328SShri PetscErrorCode  TSRollBack(TS ts)
714424655328SShri {
714524655328SShri   PetscErrorCode ierr;
714624655328SShri 
714724655328SShri   PetscFunctionBegin;
714824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7149b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
715024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
715124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
715224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
715324655328SShri   ts->ptime = ts->ptime_prev;
7154be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
71552808aa04SLisandro Dalcin   ts->steps--;
7156b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
715724655328SShri   PetscFunctionReturn(0);
715824655328SShri }
7159aeb4809dSShri Abhyankar 
7160ff22ae23SHong Zhang /*@
7161ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7162ff22ae23SHong Zhang 
7163ff22ae23SHong Zhang    Input Parameter:
7164ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7165ff22ae23SHong Zhang 
7166ff22ae23SHong Zhang    Level: advanced
7167ff22ae23SHong Zhang 
7168ff22ae23SHong Zhang .keywords: TS, getstages
7169ff22ae23SHong Zhang 
7170ff22ae23SHong Zhang .seealso: TSCreate()
7171ff22ae23SHong Zhang @*/
7172ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7173ff22ae23SHong Zhang {
7174ff22ae23SHong Zhang   PetscErrorCode ierr;
7175ff22ae23SHong Zhang 
7176ff22ae23SHong Zhang   PetscFunctionBegin;
7177ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7178ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7179ff22ae23SHong Zhang 
7180ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7181ff22ae23SHong Zhang   else {
7182ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7183ff22ae23SHong Zhang   }
7184ff22ae23SHong Zhang   PetscFunctionReturn(0);
7185ff22ae23SHong Zhang }
7186ff22ae23SHong Zhang 
7187847ff0e1SMatthew G. Knepley /*@C
7188847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7189847ff0e1SMatthew G. Knepley 
7190847ff0e1SMatthew G. Knepley   Collective on SNES
7191847ff0e1SMatthew G. Knepley 
7192847ff0e1SMatthew G. Knepley   Input Parameters:
7193847ff0e1SMatthew G. Knepley + ts - the TS context
7194847ff0e1SMatthew G. Knepley . t - current timestep
7195847ff0e1SMatthew G. Knepley . U - state vector
7196847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7197847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7198847ff0e1SMatthew G. Knepley - ctx - an optional user context
7199847ff0e1SMatthew G. Knepley 
7200847ff0e1SMatthew G. Knepley   Output Parameters:
7201847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7202847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7203847ff0e1SMatthew G. Knepley 
7204847ff0e1SMatthew G. Knepley   Level: intermediate
7205847ff0e1SMatthew G. Knepley 
7206847ff0e1SMatthew G. Knepley   Notes:
7207847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7208847ff0e1SMatthew G. Knepley 
7209847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7210847ff0e1SMatthew G. Knepley 
7211847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7212847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7213847ff0e1SMatthew G. Knepley 
7214847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7215847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7216847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7217847ff0e1SMatthew G. Knepley 
7218847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7219847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7220847ff0e1SMatthew G. Knepley @*/
7221847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7222847ff0e1SMatthew G. Knepley {
7223847ff0e1SMatthew G. Knepley   SNES           snes;
7224847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7225847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7226847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7227847ff0e1SMatthew G. Knepley 
7228847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7229c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7230847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7231847ff0e1SMatthew G. Knepley   if (!color) {
7232847ff0e1SMatthew G. Knepley     DM         dm;
7233847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7234847ff0e1SMatthew G. Knepley 
7235847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7236847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7237847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7238847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7239847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7240847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7241847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7242847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7243847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7244847ff0e1SMatthew G. Knepley     } else {
7245847ff0e1SMatthew G. Knepley       MatColoring mc;
7246847ff0e1SMatthew G. Knepley 
7247847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7248847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7249847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7250847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7251847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7252847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7253847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7254847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7255847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7256847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7257847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7258847ff0e1SMatthew G. Knepley     }
7259847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7260847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7261847ff0e1SMatthew G. Knepley   }
7262847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7263847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7264847ff0e1SMatthew G. Knepley   if (J != B) {
7265847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7266847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7267847ff0e1SMatthew G. Knepley   }
7268847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7269847ff0e1SMatthew G. Knepley }
727093b34091SDebojyoti Ghosh 
7271cb9d8021SPierre Barbier de Reuille /*@
7272cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7273cb9d8021SPierre Barbier de Reuille 
7274cb9d8021SPierre Barbier de Reuille     Input Parameters:
7275cb9d8021SPierre Barbier de Reuille     ts - the TS context
7276cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7277cb9d8021SPierre Barbier de Reuille 
7278cb9d8021SPierre Barbier de Reuille     Level: intermediate
7279cb9d8021SPierre Barbier de Reuille 
7280cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7281cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7282cb9d8021SPierre Barbier de Reuille @*/
7283cb9d8021SPierre Barbier de Reuille 
7284d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7285cb9d8021SPierre Barbier de Reuille {
7286cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7287cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7288cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7289cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7290cb9d8021SPierre Barbier de Reuille }
7291cb9d8021SPierre Barbier de Reuille 
7292cb9d8021SPierre Barbier de Reuille /*@
7293cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7294cb9d8021SPierre Barbier de Reuille 
7295cb9d8021SPierre Barbier de Reuille     Input Parameters:
7296cb9d8021SPierre Barbier de Reuille     ts - the TS context
7297cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7298d183316bSPierre Barbier de Reuille     Y - state vector to check.
7299cb9d8021SPierre Barbier de Reuille 
7300cb9d8021SPierre Barbier de Reuille     Output Parameter:
7301cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7302cb9d8021SPierre Barbier de Reuille 
7303cb9d8021SPierre Barbier de Reuille     Note:
7304cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7305cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
730696a0c994SBarry Smith 
730796a0c994SBarry Smith     Level: advanced
7308cb9d8021SPierre Barbier de Reuille @*/
7309d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7310cb9d8021SPierre Barbier de Reuille {
7311cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7312cb9d8021SPierre Barbier de Reuille 
7313cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7314cb9d8021SPierre Barbier de Reuille 
7315cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7316cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7317cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7318d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7319cb9d8021SPierre Barbier de Reuille   }
7320cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7321cb9d8021SPierre Barbier de Reuille }
73221ceb14c0SBarry Smith 
732393b34091SDebojyoti Ghosh /*@C
7324e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
732593b34091SDebojyoti Ghosh 
732693b34091SDebojyoti Ghosh   Collective on MPI_Comm
732793b34091SDebojyoti Ghosh 
732893b34091SDebojyoti Ghosh   Input Parameter:
732993b34091SDebojyoti Ghosh . tsin    - The input TS
733093b34091SDebojyoti Ghosh 
733193b34091SDebojyoti Ghosh   Output Parameter:
7332e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
733393b34091SDebojyoti Ghosh 
73345eca1a21SEmil Constantinescu   Notes:
73355eca1a21SEmil 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.
73365eca1a21SEmil Constantinescu 
7337baa10174SEmil Constantinescu   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup);
73385eca1a21SEmil Constantinescu 
73395eca1a21SEmil Constantinescu   Level: developer
734093b34091SDebojyoti Ghosh 
7341e5168f73SEmil Constantinescu .keywords: TS, clone
7342e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
734393b34091SDebojyoti Ghosh @*/
7344baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
734593b34091SDebojyoti Ghosh {
734693b34091SDebojyoti Ghosh   TS             t;
734793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7348dc846ba4SSatish Balay   SNES           snes_start;
7349dc846ba4SSatish Balay   DM             dm;
7350dc846ba4SSatish Balay   TSType         type;
735193b34091SDebojyoti Ghosh 
735293b34091SDebojyoti Ghosh   PetscFunctionBegin;
735393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
735493b34091SDebojyoti Ghosh   *tsout = NULL;
735593b34091SDebojyoti Ghosh 
73567a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
735793b34091SDebojyoti Ghosh 
735893b34091SDebojyoti Ghosh   /* General TS description */
735993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
736093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
736193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
736293b34091SDebojyoti Ghosh   t->snes_its          = 0;
736393b34091SDebojyoti Ghosh   t->nwork             = 0;
736493b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
736593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
736693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
736793b34091SDebojyoti Ghosh 
736834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
736934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7370d15a3a53SEmil Constantinescu 
737193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
737293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
737393b34091SDebojyoti Ghosh 
737493b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
737551699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
737693b34091SDebojyoti Ghosh 
7377e7069c78SShri   t->trajectory = tsin->trajectory;
7378e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7379e7069c78SShri 
7380e7069c78SShri   t->event = tsin->event;
73816b10a48eSSatish Balay   if (t->event) t->event->refct++;
7382e7069c78SShri 
738393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
738493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7385e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
738693b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
738793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
738893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
738993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
739093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
739193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
739293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
739393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
739493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
739593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
739693b34091SDebojyoti Ghosh 
739793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
739893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
739993b34091SDebojyoti Ghosh 
740093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
740193b34091SDebojyoti Ghosh 
740293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
740393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
740493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
740593b34091SDebojyoti Ghosh 
740693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
740793b34091SDebojyoti Ghosh 
74080d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
74090d4fed19SBarry Smith     PetscInt i;
74100d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
74110d4fed19SBarry Smith     for (i=0; i<10; i++) {
74120d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
74130d4fed19SBarry Smith     }
74140d4fed19SBarry Smith   }
741593b34091SDebojyoti Ghosh   *tsout = t;
741693b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
741793b34091SDebojyoti Ghosh }
7418f3b1f45cSBarry Smith 
7419f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7420f3b1f45cSBarry Smith {
7421f3b1f45cSBarry Smith   PetscErrorCode ierr;
7422f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7423f3b1f45cSBarry Smith 
7424f3b1f45cSBarry Smith   PetscFunctionBegin;
7425f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7426f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7427f3b1f45cSBarry Smith }
7428f3b1f45cSBarry Smith 
7429f3b1f45cSBarry Smith /*@
7430f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7431f3b1f45cSBarry Smith 
7432f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7433f3b1f45cSBarry Smith 
7434f3b1f45cSBarry Smith     Input Parameters:
7435f3b1f45cSBarry Smith     TS - the time stepping routine
7436f3b1f45cSBarry Smith 
7437f3b1f45cSBarry Smith    Output Parameter:
7438f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7439f3b1f45cSBarry Smith 
7440f3b1f45cSBarry Smith    Options Database:
7441f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7442f3b1f45cSBarry Smith 
7443f3b1f45cSBarry Smith    Level: advanced
7444f3b1f45cSBarry Smith 
744595452b02SPatrick Sanan    Notes:
744695452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7447f3b1f45cSBarry Smith 
7448f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7449f3b1f45cSBarry Smith @*/
7450f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7451f3b1f45cSBarry Smith {
7452f3b1f45cSBarry Smith   Mat            J,B;
7453f3b1f45cSBarry Smith   PetscErrorCode ierr;
7454f3b1f45cSBarry Smith   TSRHSJacobian  func;
7455f3b1f45cSBarry Smith   void*          ctx;
7456f3b1f45cSBarry Smith 
7457f3b1f45cSBarry Smith   PetscFunctionBegin;
7458f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7459f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7460f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7461f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7462f3b1f45cSBarry Smith }
7463f3b1f45cSBarry Smith 
7464f3b1f45cSBarry Smith /*@C
7465f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7466f3b1f45cSBarry Smith 
7467f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7468f3b1f45cSBarry Smith 
7469f3b1f45cSBarry Smith     Input Parameters:
7470f3b1f45cSBarry Smith     TS - the time stepping routine
7471f3b1f45cSBarry Smith 
7472f3b1f45cSBarry Smith    Output Parameter:
7473f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7474f3b1f45cSBarry Smith 
7475f3b1f45cSBarry Smith    Options Database:
7476f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7477f3b1f45cSBarry Smith 
747895452b02SPatrick Sanan    Notes:
747995452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7480f3b1f45cSBarry Smith 
7481f3b1f45cSBarry Smith    Level: advanced
7482f3b1f45cSBarry Smith 
7483f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7484f3b1f45cSBarry Smith @*/
7485f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7486f3b1f45cSBarry Smith {
7487f3b1f45cSBarry Smith   Mat            J,B;
7488f3b1f45cSBarry Smith   PetscErrorCode ierr;
7489f3b1f45cSBarry Smith   void           *ctx;
7490f3b1f45cSBarry Smith   TSRHSJacobian  func;
7491f3b1f45cSBarry Smith 
7492f3b1f45cSBarry Smith   PetscFunctionBegin;
7493f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7494f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7495f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7496f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7497f3b1f45cSBarry Smith }
7498