xref: /petsc/src/ts/interface/ts.c (revision 8f199f4d32795e63c5f49cc67ca3c85ae6dc1fd9)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6d763cef2SBarry Smith 
71c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
81c167fc2SEmil Constantinescu 
9d5ba7fb7SMatthew Knepley /* Logging support */
10d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
11a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
12d405a339SMatthew Knepley 
13feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1449354f04SShri Abhyankar 
151c167fc2SEmil Constantinescu 
16fde5950dSBarry Smith /*@C
17fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
18fde5950dSBarry Smith 
19fde5950dSBarry Smith    Collective on TS
20fde5950dSBarry Smith 
21fde5950dSBarry Smith    Input Parameters:
22fde5950dSBarry Smith +  ts - TS object you wish to monitor
23fde5950dSBarry Smith .  name - the monitor type one is seeking
24fde5950dSBarry Smith .  help - message indicating what monitoring is done
25fde5950dSBarry Smith .  manual - manual page for the monitor
26fde5950dSBarry Smith .  monitor - the monitor function
27fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
28fde5950dSBarry Smith 
29fde5950dSBarry Smith    Level: developer
30fde5950dSBarry Smith 
31fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
32fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
33fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
34fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
35fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
36fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
37fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
38fde5950dSBarry Smith @*/
39721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
40fde5950dSBarry Smith {
41fde5950dSBarry Smith   PetscErrorCode    ierr;
42fde5950dSBarry Smith   PetscViewer       viewer;
43fde5950dSBarry Smith   PetscViewerFormat format;
44fde5950dSBarry Smith   PetscBool         flg;
45fde5950dSBarry Smith 
46fde5950dSBarry Smith   PetscFunctionBegin;
4716413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
48fde5950dSBarry Smith   if (flg) {
49721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
50721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
51721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
52fde5950dSBarry Smith     if (monitorsetup) {
53721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
54fde5950dSBarry Smith     }
55721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
56fde5950dSBarry Smith   }
57fde5950dSBarry Smith   PetscFunctionReturn(0);
58fde5950dSBarry Smith }
59fde5950dSBarry Smith 
602ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
612ffb9264SLisandro Dalcin {
622ffb9264SLisandro Dalcin   PetscErrorCode ierr;
632ffb9264SLisandro Dalcin 
642ffb9264SLisandro Dalcin   PetscFunctionBegin;
65b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
66b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
672ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
682ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
692ffb9264SLisandro Dalcin   }
702ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
712ffb9264SLisandro Dalcin }
722ffb9264SLisandro Dalcin 
73bdad233fSMatthew Knepley /*@
74bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
75bdad233fSMatthew Knepley 
76bdad233fSMatthew Knepley    Collective on TS
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Input Parameter:
79bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley    Options Database Keys:
82e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
83ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
84ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
85ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
86ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
874dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
883e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
89a3cdaa26SBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
90a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
91a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
92a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
93a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
94a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
95f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
96f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
97ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
98847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
99bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
100de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
101de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1027cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1036934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1048b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
105de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
106de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
107de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
108de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1093e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1103e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
111fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
112e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
113110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11453ea634cSHong Zhang 
11591b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
116bdad233fSMatthew Knepley 
117bdad233fSMatthew Knepley    Level: beginner
118bdad233fSMatthew Knepley 
119a313700dSBarry Smith .seealso: TSGetType()
120bdad233fSMatthew Knepley @*/
1217087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
122bdad233fSMatthew Knepley {
123bc952696SBarry Smith   PetscBool              opt,flg,tflg;
124dfbe8321SBarry Smith   PetscErrorCode         ierr;
125eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12631748224SBarry Smith   PetscReal              time_step;
12749354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
128d1212d36SBarry Smith   char                   dir[16];
129cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1306991f827SBarry Smith   const char             *defaultType;
1316991f827SBarry Smith   char                   typeName[256];
132bdad233fSMatthew Knepley 
133bdad233fSMatthew Knepley   PetscFunctionBegin;
1340700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1356991f827SBarry Smith 
1366991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
137cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
138cd11d68dSLisandro Dalcin 
139cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1401ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1411ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1426991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1436991f827SBarry Smith   if (opt) {
1446991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1456991f827SBarry Smith   } else {
1466991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1476991f827SBarry Smith   }
148bdad233fSMatthew Knepley 
149bdad233fSMatthew Knepley   /* Handle generic TS options */
1504dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
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);
15431748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
155cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15649354f04SShri 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);
15749354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1580298fd71SBarry 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);
1590298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1600298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1610298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
163bdad233fSMatthew Knepley 
164f3b1f45cSBarry 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);
165f3b1f45cSBarry 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);
1660fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,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);
180cc9c3a59SBarry 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   }
2540ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2551b575b74SJoseph Pusztay   if (opt) {
2561b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2571b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2580ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2591b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2600ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2611b575b74SJoseph Pusztay   }
262ef20d060SBarry Smith   opt  = PETSC_FALSE;
2630dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
264a7cc72afSBarry Smith   if (opt) {
26583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
26683a4ac43SBarry Smith     PetscInt         howoften = 1;
267a80ad3e0SBarry Smith 
2680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2690ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
27083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
271bdad233fSMatthew Knepley   }
272fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2732d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2742d5ee99bSBarry Smith   if (opt) {
2752d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2762d5ee99bSBarry Smith     PetscReal        bounds[4];
2772d5ee99bSBarry Smith     PetscInt         n = 4;
2782d5ee99bSBarry Smith     PetscDraw        draw;
2796934998bSLisandro Dalcin     PetscDrawAxis    axis;
2802d5ee99bSBarry Smith 
2812d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2822d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2836934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2842d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2856934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2866934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2876934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2882d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2892d5ee99bSBarry Smith   }
2902d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2910dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2923a471f94SBarry Smith   if (opt) {
29383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
29483a4ac43SBarry Smith     PetscInt         howoften = 1;
2953a471f94SBarry Smith 
2960298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
2970ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
29883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2993a471f94SBarry Smith   }
3000ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3010ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3020ed3bfb6SBarry Smith   if (opt) {
3030ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3040ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3050ed3bfb6SBarry Smith 
3060ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
3070ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3080ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3090ed3bfb6SBarry Smith   }
310fde5950dSBarry Smith 
311ed81e22dSJed Brown   opt  = PETSC_FALSE;
31291b97e58SBarry 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);
313ed81e22dSJed Brown   if (flg) {
314ed81e22dSJed Brown     const char *ptr,*ptr2;
315ed81e22dSJed Brown     char       *filetemplate;
316ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
317ed81e22dSJed Brown     /* Do some cursory validation of the input. */
318ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
319ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
320ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
321ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
322ce94432eSBarry 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");
323ed81e22dSJed Brown       if (ptr2) break;
324ed81e22dSJed Brown     }
325ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
326ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
327ed81e22dSJed Brown   }
328bdad233fSMatthew Knepley 
329d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
330d1212d36SBarry Smith   if (flg) {
331d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
332d1212d36SBarry Smith     int                  ray = 0;
333d1212d36SBarry Smith     DMDADirection        ddir;
334d1212d36SBarry Smith     DM                   da;
335d1212d36SBarry Smith     PetscMPIInt          rank;
336d1212d36SBarry Smith 
337ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
338d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
339d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
340ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
341d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
342d1212d36SBarry Smith 
343d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
344b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
345d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
346d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
347ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
348d1212d36SBarry Smith     if (!rank) {
349d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
350d1212d36SBarry Smith     }
35151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
352d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
353d1212d36SBarry Smith   }
35451b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
35551b4a12fSMatthew G. Knepley   if (flg) {
35651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
35751b4a12fSMatthew G. Knepley     int                 ray = 0;
35851b4a12fSMatthew G. Knepley     DMDADirection       ddir;
35951b4a12fSMatthew G. Knepley     DM                  da;
36051b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
361d1212d36SBarry Smith 
36251b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
36351b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
36451b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
36551b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
36651b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3671c3436cfSJed Brown 
36851b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
369b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
37051b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
37151b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
37251b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
37351b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
37451b4a12fSMatthew G. Knepley   }
375a7a1495cSBarry Smith 
376b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
377b3d3934dSBarry Smith   if (opt) {
378b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
379b3d3934dSBarry Smith 
380b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
381b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
382b3d3934dSBarry Smith   }
383b3d3934dSBarry Smith 
384847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
385847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
386847ff0e1SMatthew G. Knepley   if (flg) {
387847ff0e1SMatthew G. Knepley     DM   dm;
388847ff0e1SMatthew G. Knepley     DMTS tdm;
389847ff0e1SMatthew G. Knepley 
390847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
391847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
392847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
393847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
394847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
395847ff0e1SMatthew G. Knepley   }
396847ff0e1SMatthew G. Knepley 
397d763cef2SBarry Smith   /* Handle specific TS options */
398d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3996991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
400d763cef2SBarry Smith   }
401fbc52257SHong Zhang 
402a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
403a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
404a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
405a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
406a7bdc993SLisandro Dalcin 
40768bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4084f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
40968bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
41068bece0bSHong Zhang   if (tflg) {
41168bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
41268bece0bSHong Zhang   }
413a05bf03eSHong Zhang 
414a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
415d763cef2SBarry Smith 
416d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4170633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
418fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
419d763cef2SBarry Smith 
4204f122a70SLisandro Dalcin   if (ts->trajectory) {
4214f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
422d763cef2SBarry Smith   }
42368bece0bSHong Zhang 
4241ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4254f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4261ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4271ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4281ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4291ef27442SStefano Zampini   }
4304f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
431d763cef2SBarry Smith   PetscFunctionReturn(0);
432d763cef2SBarry Smith }
433d763cef2SBarry Smith 
434d2daff3dSHong Zhang /*@
43578fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
43678fbdcc8SBarry Smith 
43778fbdcc8SBarry Smith    Collective on TS
43878fbdcc8SBarry Smith 
43978fbdcc8SBarry Smith    Input Parameters:
44078fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
44178fbdcc8SBarry Smith 
44278fbdcc8SBarry Smith    Output Parameters;
44378fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
44478fbdcc8SBarry Smith 
44578fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
44678fbdcc8SBarry Smith 
44778fbdcc8SBarry Smith    Level: advanced
44878fbdcc8SBarry Smith 
44978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
45078fbdcc8SBarry Smith 
45178fbdcc8SBarry Smith @*/
45278fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
45378fbdcc8SBarry Smith {
45478fbdcc8SBarry Smith   PetscFunctionBegin;
45578fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45678fbdcc8SBarry Smith   *tr = ts->trajectory;
45778fbdcc8SBarry Smith   PetscFunctionReturn(0);
45878fbdcc8SBarry Smith }
45978fbdcc8SBarry Smith 
46078fbdcc8SBarry Smith /*@
461bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
462d2daff3dSHong Zhang 
463d2daff3dSHong Zhang    Collective on TS
464d2daff3dSHong Zhang 
465d2daff3dSHong Zhang    Input Parameters:
466bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
467bc952696SBarry Smith 
46878fbdcc8SBarry Smith    Options Database:
46978fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
47078fbdcc8SBarry Smith -  -ts_trajectory_type type
47178fbdcc8SBarry Smith 
47268bece0bSHong Zhang Note: This routine should be called after all TS options have been set
473d2daff3dSHong Zhang 
474c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
47578fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
47678fbdcc8SBarry Smith 
477d2daff3dSHong Zhang    Level: intermediate
478d2daff3dSHong Zhang 
4792d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
480d2daff3dSHong Zhang 
481d2daff3dSHong Zhang @*/
482bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
483d2daff3dSHong Zhang {
484bc952696SBarry Smith   PetscErrorCode ierr;
485bc952696SBarry Smith 
486d2daff3dSHong Zhang   PetscFunctionBegin;
487d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
488bc952696SBarry Smith   if (!ts->trajectory) {
489bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
490bc952696SBarry Smith   }
491d2daff3dSHong Zhang   PetscFunctionReturn(0);
492d2daff3dSHong Zhang }
493d2daff3dSHong Zhang 
494a7a1495cSBarry Smith /*@
4952d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
4962d29f1f2SStefano Zampini 
4972d29f1f2SStefano Zampini    Collective on TS
4982d29f1f2SStefano Zampini 
4992d29f1f2SStefano Zampini    Input Parameters:
5002d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5012d29f1f2SStefano Zampini 
5022d29f1f2SStefano Zampini    Level: intermediate
5032d29f1f2SStefano Zampini 
5042d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5052d29f1f2SStefano Zampini 
5062d29f1f2SStefano Zampini @*/
5072d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5082d29f1f2SStefano Zampini {
5092d29f1f2SStefano Zampini   PetscErrorCode ierr;
5102d29f1f2SStefano Zampini 
5112d29f1f2SStefano Zampini   PetscFunctionBegin;
5122d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5132d29f1f2SStefano Zampini   if (ts->trajectory) {
5142d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5152d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5162d29f1f2SStefano Zampini   }
5172d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5182d29f1f2SStefano Zampini }
5192d29f1f2SStefano Zampini 
5202d29f1f2SStefano Zampini /*@
521a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
522a7a1495cSBarry Smith       set with TSSetRHSJacobian().
523a7a1495cSBarry Smith 
524d083f849SBarry Smith    Collective on TS
525a7a1495cSBarry Smith 
526a7a1495cSBarry Smith    Input Parameters:
527316643e7SJed Brown +  ts - the TS context
528a7a1495cSBarry Smith .  t - current timestep
5290910c330SBarry Smith -  U - input vector
530a7a1495cSBarry Smith 
531a7a1495cSBarry Smith    Output Parameters:
532a7a1495cSBarry Smith +  A - Jacobian matrix
533a7a1495cSBarry Smith .  B - optional preconditioning matrix
534a7a1495cSBarry Smith -  flag - flag indicating matrix structure
535a7a1495cSBarry Smith 
536a7a1495cSBarry Smith    Notes:
537a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
538a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
539a7a1495cSBarry Smith 
54094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
541a7a1495cSBarry Smith    flag parameter.
542a7a1495cSBarry Smith 
543a7a1495cSBarry Smith    Level: developer
544a7a1495cSBarry Smith 
54594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
546a7a1495cSBarry Smith @*/
547d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
548a7a1495cSBarry Smith {
549dfbe8321SBarry Smith   PetscErrorCode   ierr;
550270bf2e7SJed Brown   PetscObjectState Ustate;
5516c1e1eecSBarry Smith   PetscObjectId    Uid;
55224989b8cSPeter Brune   DM               dm;
553942e3340SBarry Smith   DMTS             tsdm;
55424989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
55524989b8cSPeter Brune   void             *ctx;
55624989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
557b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
558a7a1495cSBarry Smith 
559a7a1495cSBarry Smith   PetscFunctionBegin;
5600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5610910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5620910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
56324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
564942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
56524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5660298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
567b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
56859e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5696c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
570971015bcSStefano Zampini 
5716c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
572971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
573971015bcSStefano Zampini     if (A == ts->Arhs) {
574971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
575971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
576971015bcSStefano Zampini       }
577971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
578971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
579971015bcSStefano Zampini       }
580971015bcSStefano Zampini     }
581971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
582971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
583971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
584971015bcSStefano Zampini       }
585971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
586971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
587971015bcSStefano Zampini       }
588971015bcSStefano Zampini     }
589971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
590971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
5910e4ef248SJed Brown     PetscFunctionReturn(0);
592f8ede8e7SJed Brown   }
593d90be118SSean Farley 
594ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
595d90be118SSean Farley 
596e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
597971015bcSStefano Zampini     if (A == ts->Arhs) {
598971015bcSStefano Zampini       /* MatScale has a short path for this case.
599971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
600971015bcSStefano Zampini          and the matrices have not assembled yet */
601971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
60294ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
603971015bcSStefano Zampini       }
604971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
60594ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
606971015bcSStefano Zampini       }
607971015bcSStefano Zampini     }
608971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
609971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
61094ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
611971015bcSStefano Zampini       }
612971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
61394ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
614e1244c69SJed Brown       }
615971015bcSStefano Zampini     }
616e1244c69SJed Brown   }
617e1244c69SJed Brown 
61824989b8cSPeter Brune   if (rhsjacobianfunc) {
6196cd88445SBarry Smith     PetscBool missing;
62094ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
621a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
622d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
623a7a1495cSBarry Smith     PetscStackPop;
62494ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6256cd88445SBarry Smith     if (A) {
6266cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6276cd88445SBarry 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");
6286cd88445SBarry Smith     }
6296cd88445SBarry Smith     if (B && B != A) {
6306cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6316cd88445SBarry 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");
6326cd88445SBarry Smith     }
633ef66eb69SBarry Smith   } else {
63494ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
635971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
636ef66eb69SBarry Smith   }
6370e4ef248SJed Brown   ts->rhsjacobian.time  = t;
638971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
639971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6407f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
64159e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
642a7a1495cSBarry Smith   PetscFunctionReturn(0);
643a7a1495cSBarry Smith }
644a7a1495cSBarry Smith 
645316643e7SJed Brown /*@
646d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
647d763cef2SBarry Smith 
648d083f849SBarry Smith    Collective on TS
649316643e7SJed Brown 
650316643e7SJed Brown    Input Parameters:
651316643e7SJed Brown +  ts - the TS context
652316643e7SJed Brown .  t - current time
6530910c330SBarry Smith -  U - state vector
654316643e7SJed Brown 
655316643e7SJed Brown    Output Parameter:
656316643e7SJed Brown .  y - right hand side
657316643e7SJed Brown 
658316643e7SJed Brown    Note:
659316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
660316643e7SJed Brown    is used internally within the nonlinear solvers.
661316643e7SJed Brown 
662316643e7SJed Brown    Level: developer
663316643e7SJed Brown 
664316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
665316643e7SJed Brown @*/
6660910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
667d763cef2SBarry Smith {
668dfbe8321SBarry Smith   PetscErrorCode ierr;
66924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
67024989b8cSPeter Brune   TSIFunction    ifunction;
67124989b8cSPeter Brune   void           *ctx;
67224989b8cSPeter Brune   DM             dm;
67324989b8cSPeter Brune 
674d763cef2SBarry Smith   PetscFunctionBegin;
6750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6770700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6800298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
681d763cef2SBarry Smith 
682ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
683d763cef2SBarry Smith 
6840910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68524989b8cSPeter Brune   if (rhsfunction) {
686d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6870910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
688d763cef2SBarry Smith     PetscStackPop;
689214bc6a2SJed Brown   } else {
690214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
691b2cd27e8SJed Brown   }
69244a41b28SSean Farley 
6930910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
694d763cef2SBarry Smith   PetscFunctionReturn(0);
695d763cef2SBarry Smith }
696d763cef2SBarry Smith 
697ef20d060SBarry Smith /*@
698ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
699ef20d060SBarry Smith 
700d083f849SBarry Smith    Collective on TS
701ef20d060SBarry Smith 
702ef20d060SBarry Smith    Input Parameters:
703ef20d060SBarry Smith +  ts - the TS context
704ef20d060SBarry Smith -  t - current time
705ef20d060SBarry Smith 
706ef20d060SBarry Smith    Output Parameter:
7070910c330SBarry Smith .  U - the solution
708ef20d060SBarry Smith 
709ef20d060SBarry Smith    Note:
710ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
711ef20d060SBarry Smith    is used internally within the nonlinear solvers.
712ef20d060SBarry Smith 
713ef20d060SBarry Smith    Level: developer
714ef20d060SBarry Smith 
715abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
716ef20d060SBarry Smith @*/
7170910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
718ef20d060SBarry Smith {
719ef20d060SBarry Smith   PetscErrorCode     ierr;
720ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
721ef20d060SBarry Smith   void               *ctx;
722ef20d060SBarry Smith   DM                 dm;
723ef20d060SBarry Smith 
724ef20d060SBarry Smith   PetscFunctionBegin;
725ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7260910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
727ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
728ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
729ef20d060SBarry Smith 
730ef20d060SBarry Smith   if (solutionfunction) {
7319b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7320910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
733ef20d060SBarry Smith     PetscStackPop;
734ef20d060SBarry Smith   }
735ef20d060SBarry Smith   PetscFunctionReturn(0);
736ef20d060SBarry Smith }
7379b7cd975SBarry Smith /*@
7389b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7399b7cd975SBarry Smith 
740d083f849SBarry Smith    Collective on TS
7419b7cd975SBarry Smith 
7429b7cd975SBarry Smith    Input Parameters:
7439b7cd975SBarry Smith +  ts - the TS context
7449b7cd975SBarry Smith -  t - current time
7459b7cd975SBarry Smith 
7469b7cd975SBarry Smith    Output Parameter:
7479b7cd975SBarry Smith .  U - the function value
7489b7cd975SBarry Smith 
7499b7cd975SBarry Smith    Note:
7509b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7519b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7529b7cd975SBarry Smith 
7539b7cd975SBarry Smith    Level: developer
7549b7cd975SBarry Smith 
7559b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7569b7cd975SBarry Smith @*/
7579b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7589b7cd975SBarry Smith {
7599b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7609b7cd975SBarry Smith   void               *ctx;
7619b7cd975SBarry Smith   DM                 dm;
7629b7cd975SBarry Smith 
7639b7cd975SBarry Smith   PetscFunctionBegin;
7649b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7659b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7669b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7679b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7689b7cd975SBarry Smith 
7699b7cd975SBarry Smith   if (forcing) {
7709b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7719b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7729b7cd975SBarry Smith     PetscStackPop;
7739b7cd975SBarry Smith   }
7749b7cd975SBarry Smith   PetscFunctionReturn(0);
7759b7cd975SBarry Smith }
776ef20d060SBarry Smith 
777214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
778214bc6a2SJed Brown {
7792dd45cf8SJed Brown   Vec            F;
780214bc6a2SJed Brown   PetscErrorCode ierr;
781214bc6a2SJed Brown 
782214bc6a2SJed Brown   PetscFunctionBegin;
7830298fd71SBarry Smith   *Frhs = NULL;
7840298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
785214bc6a2SJed Brown   if (!ts->Frhs) {
7862dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
787214bc6a2SJed Brown   }
788214bc6a2SJed Brown   *Frhs = ts->Frhs;
789214bc6a2SJed Brown   PetscFunctionReturn(0);
790214bc6a2SJed Brown }
791214bc6a2SJed Brown 
792971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
793214bc6a2SJed Brown {
794214bc6a2SJed Brown   Mat            A,B;
7952dd45cf8SJed Brown   PetscErrorCode ierr;
79641a1d4d2SBarry Smith   TSIJacobian    ijacobian;
797214bc6a2SJed Brown 
798214bc6a2SJed Brown   PetscFunctionBegin;
799c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
800c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
80141a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
802214bc6a2SJed Brown   if (Arhs) {
803214bc6a2SJed Brown     if (!ts->Arhs) {
80441a1d4d2SBarry Smith       if (ijacobian) {
805214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
80641a1d4d2SBarry Smith       } else {
80741a1d4d2SBarry Smith         ts->Arhs = A;
80841a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
80941a1d4d2SBarry Smith       }
8103565c898SBarry Smith     } else {
8113565c898SBarry Smith       PetscBool flg;
8123565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8133565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8143565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8153565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8163565c898SBarry Smith         ts->Arhs = A;
8173565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8183565c898SBarry Smith       }
819214bc6a2SJed Brown     }
820214bc6a2SJed Brown     *Arhs = ts->Arhs;
821214bc6a2SJed Brown   }
822214bc6a2SJed Brown   if (Brhs) {
823214bc6a2SJed Brown     if (!ts->Brhs) {
824bdb70873SJed Brown       if (A != B) {
82541a1d4d2SBarry Smith         if (ijacobian) {
826214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
827bdb70873SJed Brown         } else {
82841a1d4d2SBarry Smith           ts->Brhs = B;
82941a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
83041a1d4d2SBarry Smith         }
83141a1d4d2SBarry Smith       } else {
832bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
83351699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
834bdb70873SJed Brown       }
835214bc6a2SJed Brown     }
836214bc6a2SJed Brown     *Brhs = ts->Brhs;
837214bc6a2SJed Brown   }
838214bc6a2SJed Brown   PetscFunctionReturn(0);
839214bc6a2SJed Brown }
840214bc6a2SJed Brown 
841316643e7SJed Brown /*@
8420910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
843316643e7SJed Brown 
844d083f849SBarry Smith    Collective on TS
845316643e7SJed Brown 
846316643e7SJed Brown    Input Parameters:
847316643e7SJed Brown +  ts - the TS context
848316643e7SJed Brown .  t - current time
8490910c330SBarry Smith .  U - state vector
8500910c330SBarry Smith .  Udot - time derivative of state vector
851214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
852316643e7SJed Brown 
853316643e7SJed Brown    Output Parameter:
854316643e7SJed Brown .  Y - right hand side
855316643e7SJed Brown 
856316643e7SJed Brown    Note:
857316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
858316643e7SJed Brown    is used internally within the nonlinear solvers.
859316643e7SJed Brown 
860316643e7SJed Brown    If the user did did not write their equations in implicit form, this
861316643e7SJed Brown    function recasts them in implicit form.
862316643e7SJed Brown 
863316643e7SJed Brown    Level: developer
864316643e7SJed Brown 
865316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
866316643e7SJed Brown @*/
8670910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
868316643e7SJed Brown {
869316643e7SJed Brown   PetscErrorCode ierr;
87024989b8cSPeter Brune   TSIFunction    ifunction;
87124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
87224989b8cSPeter Brune   void           *ctx;
87324989b8cSPeter Brune   DM             dm;
874316643e7SJed Brown 
875316643e7SJed Brown   PetscFunctionBegin;
8760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8780910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8790700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
880316643e7SJed Brown 
88124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8830298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
88424989b8cSPeter Brune 
885ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
886d90be118SSean Farley 
8870910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
88824989b8cSPeter Brune   if (ifunction) {
889316643e7SJed Brown     PetscStackPush("TS user implicit function");
8900910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
891316643e7SJed Brown     PetscStackPop;
892214bc6a2SJed Brown   }
893214bc6a2SJed Brown   if (imex) {
89424989b8cSPeter Brune     if (!ifunction) {
8950910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8962dd45cf8SJed Brown     }
89724989b8cSPeter Brune   } else if (rhsfunction) {
89824989b8cSPeter Brune     if (ifunction) {
899214bc6a2SJed Brown       Vec Frhs;
900214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9010910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
902214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9032dd45cf8SJed Brown     } else {
9040910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9050910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
906316643e7SJed Brown     }
9074a6899ffSJed Brown   }
9080910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
909316643e7SJed Brown   PetscFunctionReturn(0);
910316643e7SJed Brown }
911316643e7SJed Brown 
912316643e7SJed Brown /*@
913316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
914316643e7SJed Brown 
915d083f849SBarry Smith    Collective on TS
916316643e7SJed Brown 
917316643e7SJed Brown    Input
918316643e7SJed Brown       Input Parameters:
919316643e7SJed Brown +  ts - the TS context
920316643e7SJed Brown .  t - current timestep
9210910c330SBarry Smith .  U - state vector
9220910c330SBarry Smith .  Udot - time derivative of state vector
923214bc6a2SJed Brown .  shift - shift to apply, see note below
924214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
925316643e7SJed Brown 
926316643e7SJed Brown    Output Parameters:
927316643e7SJed Brown +  A - Jacobian matrix
9283565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
929316643e7SJed Brown 
930316643e7SJed Brown    Notes:
9310910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
932316643e7SJed Brown 
9330910c330SBarry Smith    dF/dU + shift*dF/dUdot
934316643e7SJed Brown 
935316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
936316643e7SJed Brown    is used internally within the nonlinear solvers.
937316643e7SJed Brown 
938316643e7SJed Brown    Level: developer
939316643e7SJed Brown 
940316643e7SJed Brown .seealso:  TSSetIJacobian()
94163495f91SJed Brown @*/
942d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
943316643e7SJed Brown {
944316643e7SJed Brown   PetscErrorCode ierr;
94524989b8cSPeter Brune   TSIJacobian    ijacobian;
94624989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
94724989b8cSPeter Brune   DM             dm;
94824989b8cSPeter Brune   void           *ctx;
949316643e7SJed Brown 
950316643e7SJed Brown   PetscFunctionBegin;
9510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9520910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9530910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
954316643e7SJed Brown   PetscValidPointer(A,6);
95594ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
956316643e7SJed Brown   PetscValidPointer(B,7);
95794ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
95824989b8cSPeter Brune 
95924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9610298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96224989b8cSPeter Brune 
963ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
964316643e7SJed Brown 
96594ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
96624989b8cSPeter Brune   if (ijacobian) {
9676cd88445SBarry Smith     PetscBool missing;
968316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
969d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
970316643e7SJed Brown     PetscStackPop;
9716cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9726cd88445SBarry 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");
9733565c898SBarry Smith     if (B != A) {
9746cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9756cd88445SBarry 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");
9766cd88445SBarry Smith     }
9774a6899ffSJed Brown   }
978214bc6a2SJed Brown   if (imex) {
979b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9804c26be97Sstefano_zampini       PetscBool assembled;
981971015bcSStefano Zampini       if (rhsjacobian) {
982971015bcSStefano Zampini         Mat Arhs = NULL;
983971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
984971015bcSStefano Zampini         if (A == Arhs) {
985971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
986971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
987971015bcSStefano Zampini         }
988971015bcSStefano Zampini       }
98994ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9904c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9914c26be97Sstefano_zampini       if (!assembled) {
9924c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9934c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9944c26be97Sstefano_zampini       }
99594ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
99694ab13aaSBarry Smith       if (A != B) {
99794ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9984c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9994c26be97Sstefano_zampini         if (!assembled) {
10004c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10014c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10024c26be97Sstefano_zampini         }
100394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1004214bc6a2SJed Brown       }
1005214bc6a2SJed Brown     }
1006214bc6a2SJed Brown   } else {
1007e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1008e1244c69SJed Brown     if (rhsjacobian) {
1009214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1010d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1011e1244c69SJed Brown     }
101294ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10133565c898SBarry Smith       PetscBool flg;
1014e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1015e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10163565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10173565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10183565c898SBarry Smith       if (!flg) {
101994ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
102094ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10213565c898SBarry Smith       }
102294ab13aaSBarry Smith       if (A != B) {
102394ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
102494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1025316643e7SJed Brown       }
1026e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1027e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1028e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
102994ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
103094ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103194ab13aaSBarry Smith         if (A != B) {
103294ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103394ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1034214bc6a2SJed Brown         }
1035316643e7SJed Brown       }
103694ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
103794ab13aaSBarry Smith       if (A != B) {
103894ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1039316643e7SJed Brown       }
1040316643e7SJed Brown     }
1041316643e7SJed Brown   }
104294ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1043316643e7SJed Brown   PetscFunctionReturn(0);
1044316643e7SJed Brown }
1045316643e7SJed Brown 
1046d763cef2SBarry Smith /*@C
1047d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1048b5abc632SBarry Smith     where U_t = G(t,u).
1049d763cef2SBarry Smith 
10503f9fe445SBarry Smith     Logically Collective on TS
1051d763cef2SBarry Smith 
1052d763cef2SBarry Smith     Input Parameters:
1053d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10540298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1055d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1056d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10570298fd71SBarry Smith           function evaluation routine (may be NULL)
1058d763cef2SBarry Smith 
1059d763cef2SBarry Smith     Calling sequence of func:
106087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith +   t - current timestep
1063d763cef2SBarry Smith .   u - input vector
1064d763cef2SBarry Smith .   F - function vector
1065d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1066d763cef2SBarry Smith 
1067d763cef2SBarry Smith     Level: beginner
1068d763cef2SBarry Smith 
106995452b02SPatrick Sanan     Notes:
107095452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10712bbac0d3SBarry Smith 
1072ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1073d763cef2SBarry Smith @*/
1074089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1075d763cef2SBarry Smith {
1076089b2837SJed Brown   PetscErrorCode ierr;
1077089b2837SJed Brown   SNES           snes;
10780298fd71SBarry Smith   Vec            ralloc = NULL;
107924989b8cSPeter Brune   DM             dm;
1080d763cef2SBarry Smith 
1081089b2837SJed Brown   PetscFunctionBegin;
10820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1083ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
108424989b8cSPeter Brune 
108524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108624989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1087089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1088e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1089e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1090e856ceecSJed Brown     r = ralloc;
1091e856ceecSJed Brown   }
1092089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1093e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1094d763cef2SBarry Smith   PetscFunctionReturn(0);
1095d763cef2SBarry Smith }
1096d763cef2SBarry Smith 
1097ef20d060SBarry Smith /*@C
1098abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1099ef20d060SBarry Smith 
1100ef20d060SBarry Smith     Logically Collective on TS
1101ef20d060SBarry Smith 
1102ef20d060SBarry Smith     Input Parameters:
1103ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1104ef20d060SBarry Smith .   f - routine for evaluating the solution
1105ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11060298fd71SBarry Smith           function evaluation routine (may be NULL)
1107ef20d060SBarry Smith 
1108ef20d060SBarry Smith     Calling sequence of func:
1109ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1110ef20d060SBarry Smith 
1111ef20d060SBarry Smith +   t - current timestep
1112ef20d060SBarry Smith .   u - output vector
1113ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1114ef20d060SBarry Smith 
11150ed3bfb6SBarry Smith     Options Database:
11160ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11170ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11180ed3bfb6SBarry Smith 
1119abd5a294SJed Brown     Notes:
1120abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1121abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1122abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1123abd5a294SJed Brown 
11244f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1125abd5a294SJed Brown 
1126ef20d060SBarry Smith     Level: beginner
1127ef20d060SBarry Smith 
11280ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1129ef20d060SBarry Smith @*/
1130ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1131ef20d060SBarry Smith {
1132ef20d060SBarry Smith   PetscErrorCode ierr;
1133ef20d060SBarry Smith   DM             dm;
1134ef20d060SBarry Smith 
1135ef20d060SBarry Smith   PetscFunctionBegin;
1136ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1137ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1138ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1139ef20d060SBarry Smith   PetscFunctionReturn(0);
1140ef20d060SBarry Smith }
1141ef20d060SBarry Smith 
11429b7cd975SBarry Smith /*@C
11439b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11449b7cd975SBarry Smith 
11459b7cd975SBarry Smith     Logically Collective on TS
11469b7cd975SBarry Smith 
11479b7cd975SBarry Smith     Input Parameters:
11489b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1149e162b725SBarry Smith .   func - routine for evaluating the forcing function
11509b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11510298fd71SBarry Smith           function evaluation routine (may be NULL)
11529b7cd975SBarry Smith 
11539b7cd975SBarry Smith     Calling sequence of func:
1154e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11559b7cd975SBarry Smith 
11569b7cd975SBarry Smith +   t - current timestep
1157e162b725SBarry Smith .   f - output vector
11589b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11599b7cd975SBarry Smith 
11609b7cd975SBarry Smith     Notes:
11619b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1162e162b725SBarry 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
1163e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1164e162b725SBarry Smith 
1165e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1166e162b725SBarry Smith 
1167e162b725SBarry 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
1168e162b725SBarry Smith     parameters can be passed in the ctx variable.
11699b7cd975SBarry Smith 
11709b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11719b7cd975SBarry Smith 
11729b7cd975SBarry Smith     Level: beginner
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11759b7cd975SBarry Smith @*/
1176e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11779b7cd975SBarry Smith {
11789b7cd975SBarry Smith   PetscErrorCode ierr;
11799b7cd975SBarry Smith   DM             dm;
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith   PetscFunctionBegin;
11829b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11839b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1184e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11859b7cd975SBarry Smith   PetscFunctionReturn(0);
11869b7cd975SBarry Smith }
11879b7cd975SBarry Smith 
1188d763cef2SBarry Smith /*@C
1189f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1190b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1191d763cef2SBarry Smith 
11923f9fe445SBarry Smith    Logically Collective on TS
1193d763cef2SBarry Smith 
1194d763cef2SBarry Smith    Input Parameters:
1195d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1196e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1197e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1198d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1199d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12000298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1201d763cef2SBarry Smith 
1202f7ab8db6SBarry Smith    Calling sequence of f:
1203ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1204d763cef2SBarry Smith 
1205d763cef2SBarry Smith +  t - current timestep
1206d763cef2SBarry Smith .  u - input vector
1207e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1208e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1209d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1210d763cef2SBarry Smith 
12116cd88445SBarry Smith    Notes:
12126cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12136cd88445SBarry Smith 
12146cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1215ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1216d763cef2SBarry Smith 
1217d763cef2SBarry Smith    Level: beginner
1218d763cef2SBarry Smith 
1219ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1220d763cef2SBarry Smith 
1221d763cef2SBarry Smith @*/
1222e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1223d763cef2SBarry Smith {
1224277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1225089b2837SJed Brown   SNES           snes;
122624989b8cSPeter Brune   DM             dm;
122724989b8cSPeter Brune   TSIJacobian    ijacobian;
1228277b19d0SLisandro Dalcin 
1229d763cef2SBarry Smith   PetscFunctionBegin;
12300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1231e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1232e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1233e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1234e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1235d763cef2SBarry Smith 
123624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
123724989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1238e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1239e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1240e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1241e1244c69SJed Brown   }
12420298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1243089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12445f659677SPeter Brune   if (!ijacobian) {
1245e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12460e4ef248SJed Brown   }
1247e5d3d808SBarry Smith   if (Amat) {
1248e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12490e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1250e5d3d808SBarry Smith     ts->Arhs = Amat;
12510e4ef248SJed Brown   }
1252e5d3d808SBarry Smith   if (Pmat) {
1253e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12540e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1255e5d3d808SBarry Smith     ts->Brhs = Pmat;
12560e4ef248SJed Brown   }
1257d763cef2SBarry Smith   PetscFunctionReturn(0);
1258d763cef2SBarry Smith }
1259d763cef2SBarry Smith 
1260316643e7SJed Brown /*@C
1261b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1262316643e7SJed Brown 
12633f9fe445SBarry Smith    Logically Collective on TS
1264316643e7SJed Brown 
1265316643e7SJed Brown    Input Parameters:
1266316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12670298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1268316643e7SJed Brown .  f   - the function evaluation routine
12690298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1270316643e7SJed Brown 
1271316643e7SJed Brown    Calling sequence of f:
1272316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1273316643e7SJed Brown 
1274316643e7SJed Brown +  t   - time at step/stage being solved
1275316643e7SJed Brown .  u   - state vector
1276316643e7SJed Brown .  u_t - time derivative of state vector
1277316643e7SJed Brown .  F   - function vector
1278316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1279316643e7SJed Brown 
1280316643e7SJed Brown    Important:
12812bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1282316643e7SJed Brown 
1283316643e7SJed Brown    Level: beginner
1284316643e7SJed Brown 
1285d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1286316643e7SJed Brown @*/
128751699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1288316643e7SJed Brown {
1289089b2837SJed Brown   PetscErrorCode ierr;
1290089b2837SJed Brown   SNES           snes;
129151699248SLisandro Dalcin   Vec            ralloc = NULL;
129224989b8cSPeter Brune   DM             dm;
1293316643e7SJed Brown 
1294316643e7SJed Brown   PetscFunctionBegin;
12950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
129651699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
129724989b8cSPeter Brune 
129824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
129924989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
130024989b8cSPeter Brune 
1301089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
130251699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
130351699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
130451699248SLisandro Dalcin     r  = ralloc;
1305e856ceecSJed Brown   }
130651699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
130751699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1308089b2837SJed Brown   PetscFunctionReturn(0);
1309089b2837SJed Brown }
1310089b2837SJed Brown 
1311089b2837SJed Brown /*@C
1312089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1313089b2837SJed Brown 
1314089b2837SJed Brown    Not Collective
1315089b2837SJed Brown 
1316089b2837SJed Brown    Input Parameter:
1317089b2837SJed Brown .  ts - the TS context
1318089b2837SJed Brown 
1319089b2837SJed Brown    Output Parameter:
13200298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13210298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13220298fd71SBarry Smith -  ctx - the function context (or NULL)
1323089b2837SJed Brown 
1324089b2837SJed Brown    Level: advanced
1325089b2837SJed Brown 
1326089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1327089b2837SJed Brown @*/
1328089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1329089b2837SJed Brown {
1330089b2837SJed Brown   PetscErrorCode ierr;
1331089b2837SJed Brown   SNES           snes;
133224989b8cSPeter Brune   DM             dm;
1333089b2837SJed Brown 
1334089b2837SJed Brown   PetscFunctionBegin;
1335089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1336089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13370298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
133824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
133924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1340089b2837SJed Brown   PetscFunctionReturn(0);
1341089b2837SJed Brown }
1342089b2837SJed Brown 
1343089b2837SJed Brown /*@C
1344089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1345089b2837SJed Brown 
1346089b2837SJed Brown    Not Collective
1347089b2837SJed Brown 
1348089b2837SJed Brown    Input Parameter:
1349089b2837SJed Brown .  ts - the TS context
1350089b2837SJed Brown 
1351089b2837SJed Brown    Output Parameter:
13520298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13530298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13540298fd71SBarry Smith -  ctx - the function context (or NULL)
1355089b2837SJed Brown 
1356089b2837SJed Brown    Level: advanced
1357089b2837SJed Brown 
13582bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1359089b2837SJed Brown @*/
1360089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1361089b2837SJed Brown {
1362089b2837SJed Brown   PetscErrorCode ierr;
1363089b2837SJed Brown   SNES           snes;
136424989b8cSPeter Brune   DM             dm;
1365089b2837SJed Brown 
1366089b2837SJed Brown   PetscFunctionBegin;
1367089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1368089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13690298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
137024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
137124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1372316643e7SJed Brown   PetscFunctionReturn(0);
1373316643e7SJed Brown }
1374316643e7SJed Brown 
1375316643e7SJed Brown /*@C
1376a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1377ae8867d6SBarry Smith         provided with TSSetIFunction().
1378316643e7SJed Brown 
13793f9fe445SBarry Smith    Logically Collective on TS
1380316643e7SJed Brown 
1381316643e7SJed Brown    Input Parameters:
1382316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1383e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1384e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1385316643e7SJed Brown .  f   - the Jacobian evaluation routine
13860298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1387316643e7SJed Brown 
1388316643e7SJed Brown    Calling sequence of f:
1389ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1390316643e7SJed Brown 
1391316643e7SJed Brown +  t    - time at step/stage being solved
13921b4a444bSJed Brown .  U    - state vector
13931b4a444bSJed Brown .  U_t  - time derivative of state vector
1394316643e7SJed Brown .  a    - shift
1395e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1396e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1397316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1398316643e7SJed Brown 
1399316643e7SJed Brown    Notes:
1400e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1401316643e7SJed Brown 
1402895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1403895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1404895c21f2SBarry Smith 
1405a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1406b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1407a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1408a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1409a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1410a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1411a4f0a591SBarry Smith 
14126cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14136cd88445SBarry Smith 
14146cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1415ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1416ca5f011dSBarry Smith 
1417316643e7SJed Brown    Level: beginner
1418316643e7SJed Brown 
1419ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1420316643e7SJed Brown 
1421316643e7SJed Brown @*/
1422e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1423316643e7SJed Brown {
1424316643e7SJed Brown   PetscErrorCode ierr;
1425089b2837SJed Brown   SNES           snes;
142624989b8cSPeter Brune   DM             dm;
1427316643e7SJed Brown 
1428316643e7SJed Brown   PetscFunctionBegin;
14290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1430e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1431e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1432e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1433e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
143424989b8cSPeter Brune 
143524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
143624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
143724989b8cSPeter Brune 
1438089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1439e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1440316643e7SJed Brown   PetscFunctionReturn(0);
1441316643e7SJed Brown }
1442316643e7SJed Brown 
1443e1244c69SJed Brown /*@
1444e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1445e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1446e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1447e1244c69SJed Brown    not been changed by the TS.
1448e1244c69SJed Brown 
1449e1244c69SJed Brown    Logically Collective
1450e1244c69SJed Brown 
1451e1244c69SJed Brown    Input Arguments:
1452e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1453e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1454e1244c69SJed Brown 
1455e1244c69SJed Brown    Level: intermediate
1456e1244c69SJed Brown 
1457e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1458e1244c69SJed Brown @*/
1459e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1460e1244c69SJed Brown {
1461e1244c69SJed Brown   PetscFunctionBegin;
1462e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1463e1244c69SJed Brown   PetscFunctionReturn(0);
1464e1244c69SJed Brown }
1465e1244c69SJed Brown 
1466efe9872eSLisandro Dalcin /*@C
1467efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1468efe9872eSLisandro Dalcin 
1469efe9872eSLisandro Dalcin    Logically Collective on TS
1470efe9872eSLisandro Dalcin 
1471efe9872eSLisandro Dalcin    Input Parameters:
1472efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1473efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1474efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1475efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1476efe9872eSLisandro Dalcin 
1477efe9872eSLisandro Dalcin    Calling sequence of fun:
1478efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1479efe9872eSLisandro Dalcin 
1480efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1481efe9872eSLisandro Dalcin .  U    - state vector
1482efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1483efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1484efe9872eSLisandro Dalcin .  F    - function vector
1485efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1486efe9872eSLisandro Dalcin 
1487efe9872eSLisandro Dalcin    Level: beginner
1488efe9872eSLisandro Dalcin 
1489efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1490efe9872eSLisandro Dalcin @*/
1491efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1492efe9872eSLisandro Dalcin {
1493efe9872eSLisandro Dalcin   DM             dm;
1494efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1495efe9872eSLisandro Dalcin 
1496efe9872eSLisandro Dalcin   PetscFunctionBegin;
1497efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1498efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1499efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1500efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1501efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1502efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1503efe9872eSLisandro Dalcin }
1504efe9872eSLisandro Dalcin 
1505efe9872eSLisandro Dalcin /*@C
1506efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin   Not Collective
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin   Input Parameter:
1511efe9872eSLisandro Dalcin . ts - the TS context
1512efe9872eSLisandro Dalcin 
1513efe9872eSLisandro Dalcin   Output Parameter:
1514efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1515efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1516efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1517efe9872eSLisandro Dalcin 
1518efe9872eSLisandro Dalcin   Level: advanced
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1521efe9872eSLisandro Dalcin @*/
1522efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1523efe9872eSLisandro Dalcin {
1524efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1525efe9872eSLisandro Dalcin   SNES           snes;
1526efe9872eSLisandro Dalcin   DM             dm;
1527efe9872eSLisandro Dalcin 
1528efe9872eSLisandro Dalcin   PetscFunctionBegin;
1529efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1530efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1531efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1532efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1533efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1534efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1535efe9872eSLisandro Dalcin }
1536efe9872eSLisandro Dalcin 
1537efe9872eSLisandro Dalcin /*@C
1538bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1539efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin    Logically Collective on TS
1542efe9872eSLisandro Dalcin 
1543efe9872eSLisandro Dalcin    Input Parameters:
1544efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1545efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1546efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1547efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1548efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1549efe9872eSLisandro Dalcin 
1550efe9872eSLisandro Dalcin    Calling sequence of jac:
1551bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1552efe9872eSLisandro Dalcin 
1553efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1554efe9872eSLisandro Dalcin .  U    - state vector
1555efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1556efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1557efe9872eSLisandro Dalcin .  v    - shift for U_t
1558efe9872eSLisandro Dalcin .  a    - shift for U_tt
1559efe9872eSLisandro 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
1560efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1561efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin    Notes:
1564efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1567efe9872eSLisandro 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.
1568efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1569bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin    Level: beginner
1572efe9872eSLisandro Dalcin 
1573efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1574efe9872eSLisandro Dalcin @*/
1575efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1576efe9872eSLisandro Dalcin {
1577efe9872eSLisandro Dalcin   DM             dm;
1578efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin   PetscFunctionBegin;
1581efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1582efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1583efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1584efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1585efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1586efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1587efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1588efe9872eSLisandro Dalcin }
1589efe9872eSLisandro Dalcin 
1590efe9872eSLisandro Dalcin /*@C
1591efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1592efe9872eSLisandro Dalcin 
1593efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1594efe9872eSLisandro Dalcin 
1595efe9872eSLisandro Dalcin   Input Parameter:
1596efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   Output Parameters:
1599efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1600efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1601efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1602efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1603efe9872eSLisandro Dalcin 
160495452b02SPatrick Sanan   Notes:
160595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   Level: advanced
1608efe9872eSLisandro Dalcin 
160980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin @*/
1612efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1613efe9872eSLisandro Dalcin {
1614efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1615efe9872eSLisandro Dalcin   SNES           snes;
1616efe9872eSLisandro Dalcin   DM             dm;
1617efe9872eSLisandro Dalcin 
1618efe9872eSLisandro Dalcin   PetscFunctionBegin;
1619efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1620efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1621efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1622efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1625efe9872eSLisandro Dalcin }
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin /*@
1628efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1629efe9872eSLisandro Dalcin 
1630d083f849SBarry Smith   Collective on TS
1631efe9872eSLisandro Dalcin 
1632efe9872eSLisandro Dalcin   Input Parameters:
1633efe9872eSLisandro Dalcin + ts - the TS context
1634efe9872eSLisandro Dalcin . t - current time
1635efe9872eSLisandro Dalcin . U - state vector
1636efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1637efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1638efe9872eSLisandro Dalcin 
1639efe9872eSLisandro Dalcin   Output Parameter:
1640efe9872eSLisandro Dalcin . F - the residual vector
1641efe9872eSLisandro Dalcin 
1642efe9872eSLisandro Dalcin   Note:
1643efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1644efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   Level: developer
1647efe9872eSLisandro Dalcin 
1648efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1649efe9872eSLisandro Dalcin @*/
1650efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1651efe9872eSLisandro Dalcin {
1652efe9872eSLisandro Dalcin   DM             dm;
1653efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1654efe9872eSLisandro Dalcin   void           *ctx;
1655efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1656efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   PetscFunctionBegin;
1659efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1660efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1661efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1662efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1663efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1664efe9872eSLisandro Dalcin 
1665efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1666efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1667efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1668efe9872eSLisandro Dalcin 
1669efe9872eSLisandro Dalcin   if (!I2Function) {
1670efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1672efe9872eSLisandro Dalcin   }
1673efe9872eSLisandro Dalcin 
1674efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1677efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1678efe9872eSLisandro Dalcin   PetscStackPop;
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin   if (rhsfunction) {
1681efe9872eSLisandro Dalcin     Vec Frhs;
1682efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1683efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1684efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1685efe9872eSLisandro Dalcin   }
1686efe9872eSLisandro Dalcin 
1687efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1688efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1689efe9872eSLisandro Dalcin }
1690efe9872eSLisandro Dalcin 
1691efe9872eSLisandro Dalcin /*@
1692efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1693efe9872eSLisandro Dalcin 
1694d083f849SBarry Smith   Collective on TS
1695efe9872eSLisandro Dalcin 
1696efe9872eSLisandro Dalcin   Input Parameters:
1697efe9872eSLisandro Dalcin + ts - the TS context
1698efe9872eSLisandro Dalcin . t - current timestep
1699efe9872eSLisandro Dalcin . U - state vector
1700efe9872eSLisandro Dalcin . V - time derivative of state vector
1701efe9872eSLisandro Dalcin . A - second time derivative of state vector
1702efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1703efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin   Output Parameters:
1706efe9872eSLisandro Dalcin + J - Jacobian matrix
1707efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   Notes:
1710efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1713efe9872eSLisandro Dalcin 
1714efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1715efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   Level: developer
1718efe9872eSLisandro Dalcin 
1719efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1720efe9872eSLisandro Dalcin @*/
1721efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1722efe9872eSLisandro Dalcin {
1723efe9872eSLisandro Dalcin   DM             dm;
1724efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1725efe9872eSLisandro Dalcin   void           *ctx;
1726efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1727efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin   PetscFunctionBegin;
1730efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1731efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1732efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1733efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1734efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1735efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1736efe9872eSLisandro Dalcin 
1737efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1738efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1739efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1740efe9872eSLisandro Dalcin 
1741efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1742efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1743efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1744efe9872eSLisandro Dalcin   }
1745efe9872eSLisandro Dalcin 
1746efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1747efe9872eSLisandro Dalcin 
1748efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1749efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1750efe9872eSLisandro Dalcin   PetscStackPop;
1751efe9872eSLisandro Dalcin 
1752efe9872eSLisandro Dalcin   if (rhsjacobian) {
1753efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1754efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1755efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1756efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1757efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1758efe9872eSLisandro Dalcin   }
1759efe9872eSLisandro Dalcin 
1760efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1761efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1762efe9872eSLisandro Dalcin }
1763efe9872eSLisandro Dalcin 
1764efe9872eSLisandro Dalcin /*@
1765efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1766efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1767efe9872eSLisandro Dalcin 
1768d083f849SBarry Smith    Logically Collective on TS
1769efe9872eSLisandro Dalcin 
1770efe9872eSLisandro Dalcin    Input Parameters:
1771efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1772efe9872eSLisandro Dalcin .  u - the solution vector
1773efe9872eSLisandro Dalcin -  v - the time derivative vector
1774efe9872eSLisandro Dalcin 
1775efe9872eSLisandro Dalcin    Level: beginner
1776efe9872eSLisandro Dalcin 
1777efe9872eSLisandro Dalcin @*/
1778efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1779efe9872eSLisandro Dalcin {
1780efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1781efe9872eSLisandro Dalcin 
1782efe9872eSLisandro Dalcin   PetscFunctionBegin;
1783efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1784efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1785efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1786efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1787efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1788efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1789efe9872eSLisandro Dalcin   ts->vec_dot = v;
1790efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1791efe9872eSLisandro Dalcin }
1792efe9872eSLisandro Dalcin 
1793efe9872eSLisandro Dalcin /*@
1794efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1795efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1796efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1797efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1798efe9872eSLisandro Dalcin 
1799efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1800efe9872eSLisandro Dalcin 
1801efe9872eSLisandro Dalcin    Input Parameter:
1802efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1803efe9872eSLisandro Dalcin 
1804efe9872eSLisandro Dalcin    Output Parameter:
1805efe9872eSLisandro Dalcin +  u - the vector containing the solution
1806efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1807efe9872eSLisandro Dalcin 
1808efe9872eSLisandro Dalcin    Level: intermediate
1809efe9872eSLisandro Dalcin 
1810efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1811efe9872eSLisandro Dalcin 
1812efe9872eSLisandro Dalcin @*/
1813efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1814efe9872eSLisandro Dalcin {
1815efe9872eSLisandro Dalcin   PetscFunctionBegin;
1816efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1817efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1818efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1819efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1820efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1821efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1822efe9872eSLisandro Dalcin }
1823efe9872eSLisandro Dalcin 
182455849f57SBarry Smith /*@C
182555849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
182655849f57SBarry Smith 
182755849f57SBarry Smith   Collective on PetscViewer
182855849f57SBarry Smith 
182955849f57SBarry Smith   Input Parameters:
183055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
183155849f57SBarry Smith            some related function before a call to TSLoad().
183255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
183355849f57SBarry Smith 
183455849f57SBarry Smith    Level: intermediate
183555849f57SBarry Smith 
183655849f57SBarry Smith   Notes:
183755849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
183855849f57SBarry Smith 
183955849f57SBarry Smith   Notes for advanced users:
184055849f57SBarry Smith   Most users should not need to know the details of the binary storage
184155849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
184255849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
184355849f57SBarry Smith   format is
184455849f57SBarry Smith .vb
184555849f57SBarry Smith      has not yet been determined
184655849f57SBarry Smith .ve
184755849f57SBarry Smith 
184855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
184955849f57SBarry Smith @*/
1850f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
185155849f57SBarry Smith {
185255849f57SBarry Smith   PetscErrorCode ierr;
185355849f57SBarry Smith   PetscBool      isbinary;
185455849f57SBarry Smith   PetscInt       classid;
185555849f57SBarry Smith   char           type[256];
18562d53ad75SBarry Smith   DMTS           sdm;
1857ad6bc421SBarry Smith   DM             dm;
185855849f57SBarry Smith 
185955849f57SBarry Smith   PetscFunctionBegin;
1860f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
186155849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
186255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
186355849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
186455849f57SBarry Smith 
1865060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1866ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1867060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1868f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1869f2c2a1b9SBarry Smith   if (ts->ops->load) {
1870f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1871f2c2a1b9SBarry Smith   }
1872ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1873ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1874ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1875f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1876f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18772d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18782d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
187955849f57SBarry Smith   PetscFunctionReturn(0);
188055849f57SBarry Smith }
188155849f57SBarry Smith 
18829804daf3SBarry Smith #include <petscdraw.h>
1883e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1884e04113cfSBarry Smith #include <petscviewersaws.h>
1885f05ece33SBarry Smith #endif
18867e2c5f70SBarry Smith /*@C
1887d763cef2SBarry Smith     TSView - Prints the TS data structure.
1888d763cef2SBarry Smith 
18894c49b128SBarry Smith     Collective on TS
1890d763cef2SBarry Smith 
1891d763cef2SBarry Smith     Input Parameters:
1892d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1893d763cef2SBarry Smith -   viewer - visualization context
1894d763cef2SBarry Smith 
1895d763cef2SBarry Smith     Options Database Key:
1896d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1897d763cef2SBarry Smith 
1898d763cef2SBarry Smith     Notes:
1899d763cef2SBarry Smith     The available visualization contexts include
1900b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1901b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1902d763cef2SBarry Smith          output where only the first processor opens
1903d763cef2SBarry Smith          the file.  All other processors send their
1904d763cef2SBarry Smith          data to the first processor to print.
1905d763cef2SBarry Smith 
1906d763cef2SBarry Smith     The user can open an alternative visualization context with
1907b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1908d763cef2SBarry Smith 
1909d763cef2SBarry Smith     Level: beginner
1910d763cef2SBarry Smith 
1911b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1912d763cef2SBarry Smith @*/
19137087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1914d763cef2SBarry Smith {
1915dfbe8321SBarry Smith   PetscErrorCode ierr;
191619fd82e9SBarry Smith   TSType         type;
19172b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19182d53ad75SBarry Smith   DMTS           sdm;
1919e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1920536b137fSBarry Smith   PetscBool      issaws;
1921f05ece33SBarry Smith #endif
1922d763cef2SBarry Smith 
1923d763cef2SBarry Smith   PetscFunctionBegin;
19240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19253050cee2SBarry Smith   if (!viewer) {
1926ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19273050cee2SBarry Smith   }
19280700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1929c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1930fd16b177SBarry Smith 
1931251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1932251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
193355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19342b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1935e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1936536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1937f05ece33SBarry Smith #endif
193832077d6dSBarry Smith   if (iascii) {
1939dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1940efd4aadfSBarry Smith     if (ts->ops->view) {
1941efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1942efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1943efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1944efd4aadfSBarry Smith     }
1945ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
194677431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1947ef85077eSLisandro Dalcin     }
1948ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19497c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1950ef85077eSLisandro Dalcin     }
1951efd4aadfSBarry Smith     if (ts->usessnes) {
1952efd4aadfSBarry Smith       PetscBool lin;
1953d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19545ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1955d763cef2SBarry Smith       }
19565ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
19571ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
1958efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1959efd4aadfSBarry Smith     }
1960193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1961a0af407cSBarry Smith     if (ts->vrtol) {
1962a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1963a0af407cSBarry Smith     } else {
1964a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1965a0af407cSBarry Smith     }
1966a0af407cSBarry Smith     if (ts->vatol) {
1967a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1968a0af407cSBarry Smith     } else {
1969a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1970a0af407cSBarry Smith     }
1971825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1972efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1973825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
19740f5bd95cSBarry Smith   } else if (isstring) {
1975a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
197636a9e3b9SBarry Smith     ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr);
197736a9e3b9SBarry Smith     if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);}
197855849f57SBarry Smith   } else if (isbinary) {
197955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
198055849f57SBarry Smith     MPI_Comm    comm;
198155849f57SBarry Smith     PetscMPIInt rank;
198255849f57SBarry Smith     char        type[256];
198355849f57SBarry Smith 
198455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
198555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
198655849f57SBarry Smith     if (!rank) {
198755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
198855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
198955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
199055849f57SBarry Smith     }
199155849f57SBarry Smith     if (ts->ops->view) {
199255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
199355849f57SBarry Smith     }
1994efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1995f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1996f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19972d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19982d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19992b0a91c0SBarry Smith   } else if (isdraw) {
20002b0a91c0SBarry Smith     PetscDraw draw;
20012b0a91c0SBarry Smith     char      str[36];
200289fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20032b0a91c0SBarry Smith 
20042b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20052b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20062b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20072b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
200851fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
200989fd9fafSBarry Smith     bottom = y - h;
20102b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20112b0a91c0SBarry Smith     if (ts->ops->view) {
20122b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20132b0a91c0SBarry Smith     }
2014efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2015efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20162b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2017e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2018536b137fSBarry Smith   } else if (issaws) {
2019d45a07a7SBarry Smith     PetscMPIInt rank;
20202657e9d9SBarry Smith     const char  *name;
20212657e9d9SBarry Smith 
20222657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2023d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2024d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2025d45a07a7SBarry Smith       char       dir[1024];
2026d45a07a7SBarry Smith 
2027e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2028a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20292657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20302657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20312657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2032d763cef2SBarry Smith     }
20330acecf5bSBarry Smith     if (ts->ops->view) {
20340acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20350acecf5bSBarry Smith     }
2036f05ece33SBarry Smith #endif
2037f05ece33SBarry Smith   }
203836a9e3b9SBarry Smith   if (ts->snes && ts->usessnes)  {
203936a9e3b9SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
204036a9e3b9SBarry Smith     ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
204136a9e3b9SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
204236a9e3b9SBarry Smith   }
204336a9e3b9SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
204436a9e3b9SBarry Smith   ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
2045f05ece33SBarry Smith 
2046b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2047251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2048b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2049d763cef2SBarry Smith   PetscFunctionReturn(0);
2050d763cef2SBarry Smith }
2051d763cef2SBarry Smith 
2052b07ff414SBarry Smith /*@
2053d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2054d763cef2SBarry Smith    the timesteppers.
2055d763cef2SBarry Smith 
20563f9fe445SBarry Smith    Logically Collective on TS
2057d763cef2SBarry Smith 
2058d763cef2SBarry Smith    Input Parameters:
2059d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2060d763cef2SBarry Smith -  usrP - optional user context
2061d763cef2SBarry Smith 
206295452b02SPatrick Sanan    Fortran Notes:
206395452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2064daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2065daf670e6SBarry Smith 
2066d763cef2SBarry Smith    Level: intermediate
2067d763cef2SBarry Smith 
2068d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2069d763cef2SBarry Smith @*/
20707087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2071d763cef2SBarry Smith {
2072d763cef2SBarry Smith   PetscFunctionBegin;
20730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2074d763cef2SBarry Smith   ts->user = usrP;
2075d763cef2SBarry Smith   PetscFunctionReturn(0);
2076d763cef2SBarry Smith }
2077d763cef2SBarry Smith 
2078b07ff414SBarry Smith /*@
2079d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2080d763cef2SBarry Smith     timestepper.
2081d763cef2SBarry Smith 
2082d763cef2SBarry Smith     Not Collective
2083d763cef2SBarry Smith 
2084d763cef2SBarry Smith     Input Parameter:
2085d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith     Output Parameter:
2088d763cef2SBarry Smith .   usrP - user context
2089d763cef2SBarry Smith 
209095452b02SPatrick Sanan    Fortran Notes:
209195452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2092daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2093daf670e6SBarry Smith 
2094d763cef2SBarry Smith     Level: intermediate
2095d763cef2SBarry Smith 
2096d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2097d763cef2SBarry Smith @*/
2098e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2099d763cef2SBarry Smith {
2100d763cef2SBarry Smith   PetscFunctionBegin;
21010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2102e71120c6SJed Brown   *(void**)usrP = ts->user;
2103d763cef2SBarry Smith   PetscFunctionReturn(0);
2104d763cef2SBarry Smith }
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith /*@
210780275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2108d763cef2SBarry Smith 
2109d763cef2SBarry Smith    Not Collective
2110d763cef2SBarry Smith 
2111d763cef2SBarry Smith    Input Parameter:
2112d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2113d763cef2SBarry Smith 
2114d763cef2SBarry Smith    Output Parameter:
211580275a0aSLisandro Dalcin .  steps - number of steps completed so far
2116d763cef2SBarry Smith 
2117d763cef2SBarry Smith    Level: intermediate
2118d763cef2SBarry Smith 
21199be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2120d763cef2SBarry Smith @*/
212180275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2122d763cef2SBarry Smith {
2123d763cef2SBarry Smith   PetscFunctionBegin;
21240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
212580275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
212680275a0aSLisandro Dalcin   *steps = ts->steps;
212780275a0aSLisandro Dalcin   PetscFunctionReturn(0);
212880275a0aSLisandro Dalcin }
212980275a0aSLisandro Dalcin 
213080275a0aSLisandro Dalcin /*@
213180275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
213280275a0aSLisandro Dalcin 
213380275a0aSLisandro Dalcin    Logically Collective on TS
213480275a0aSLisandro Dalcin 
213580275a0aSLisandro Dalcin    Input Parameters:
213680275a0aSLisandro Dalcin +  ts - the TS context
213780275a0aSLisandro Dalcin -  steps - number of steps completed so far
213880275a0aSLisandro Dalcin 
213980275a0aSLisandro Dalcin    Notes:
214080275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
214180275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
214280275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
214380275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
214480275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
214580275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
214680275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
214780275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
214880275a0aSLisandro Dalcin 
214980275a0aSLisandro Dalcin    Level: advanced
215080275a0aSLisandro Dalcin 
215180275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
215280275a0aSLisandro Dalcin @*/
215380275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
215480275a0aSLisandro Dalcin {
215580275a0aSLisandro Dalcin   PetscFunctionBegin;
215680275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
215780275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
215880275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
215980275a0aSLisandro Dalcin   ts->steps = steps;
2160d763cef2SBarry Smith   PetscFunctionReturn(0);
2161d763cef2SBarry Smith }
2162d763cef2SBarry Smith 
2163d763cef2SBarry Smith /*@
2164d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2165d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2166d763cef2SBarry Smith 
21673f9fe445SBarry Smith    Logically Collective on TS
2168d763cef2SBarry Smith 
2169d763cef2SBarry Smith    Input Parameters:
2170d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2171d763cef2SBarry Smith -  time_step - the size of the timestep
2172d763cef2SBarry Smith 
2173d763cef2SBarry Smith    Level: intermediate
2174d763cef2SBarry Smith 
2175aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2176d763cef2SBarry Smith 
2177d763cef2SBarry Smith @*/
21787087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2179d763cef2SBarry Smith {
2180d763cef2SBarry Smith   PetscFunctionBegin;
21810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2182c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2183d763cef2SBarry Smith   ts->time_step = time_step;
2184d763cef2SBarry Smith   PetscFunctionReturn(0);
2185d763cef2SBarry Smith }
2186d763cef2SBarry Smith 
2187a43b19c4SJed Brown /*@
218849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
218949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
219049354f04SShri Abhyankar      or just return at the final time TS computed.
2191a43b19c4SJed Brown 
2192a43b19c4SJed Brown   Logically Collective on TS
2193a43b19c4SJed Brown 
2194a43b19c4SJed Brown    Input Parameter:
2195a43b19c4SJed Brown +   ts - the time-step context
219649354f04SShri Abhyankar -   eftopt - exact final time option
2197a43b19c4SJed Brown 
2198feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2199feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2200feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2201feed9e9dSBarry Smith 
2202feed9e9dSBarry Smith    Options Database:
2203feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2204feed9e9dSBarry Smith 
2205ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2206ee346746SBarry Smith     then the final time you selected.
2207ee346746SBarry Smith 
2208a43b19c4SJed Brown    Level: beginner
2209a43b19c4SJed Brown 
2210f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2211a43b19c4SJed Brown @*/
221249354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2213a43b19c4SJed Brown {
2214a43b19c4SJed Brown   PetscFunctionBegin;
2215a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
221649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
221749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2218a43b19c4SJed Brown   PetscFunctionReturn(0);
2219a43b19c4SJed Brown }
2220a43b19c4SJed Brown 
2221d763cef2SBarry Smith /*@
2222f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2223f6953c82SLisandro Dalcin 
2224f6953c82SLisandro Dalcin    Not Collective
2225f6953c82SLisandro Dalcin 
2226f6953c82SLisandro Dalcin    Input Parameter:
2227f6953c82SLisandro Dalcin .  ts - the TS context
2228f6953c82SLisandro Dalcin 
2229f6953c82SLisandro Dalcin    Output Parameter:
2230f6953c82SLisandro Dalcin .  eftopt - exact final time option
2231f6953c82SLisandro Dalcin 
2232f6953c82SLisandro Dalcin    Level: beginner
2233f6953c82SLisandro Dalcin 
2234f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2235f6953c82SLisandro Dalcin @*/
2236f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2237f6953c82SLisandro Dalcin {
2238f6953c82SLisandro Dalcin   PetscFunctionBegin;
2239f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2240f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2241f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2242f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2243f6953c82SLisandro Dalcin }
2244f6953c82SLisandro Dalcin 
2245f6953c82SLisandro Dalcin /*@
2246d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2247d763cef2SBarry Smith 
2248d763cef2SBarry Smith    Not Collective
2249d763cef2SBarry Smith 
2250d763cef2SBarry Smith    Input Parameter:
2251d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2252d763cef2SBarry Smith 
2253d763cef2SBarry Smith    Output Parameter:
2254d763cef2SBarry Smith .  dt - the current timestep size
2255d763cef2SBarry Smith 
2256d763cef2SBarry Smith    Level: intermediate
2257d763cef2SBarry Smith 
2258aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2259d763cef2SBarry Smith 
2260d763cef2SBarry Smith @*/
22617087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2262d763cef2SBarry Smith {
2263d763cef2SBarry Smith   PetscFunctionBegin;
22640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2265f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2266d763cef2SBarry Smith   *dt = ts->time_step;
2267d763cef2SBarry Smith   PetscFunctionReturn(0);
2268d763cef2SBarry Smith }
2269d763cef2SBarry Smith 
2270d8e5e3e6SSatish Balay /*@
2271d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2272d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2273d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2274d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2275d763cef2SBarry Smith 
2276d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Input Parameter:
2279d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Output Parameter:
2282d763cef2SBarry Smith .  v - the vector containing the solution
2283d763cef2SBarry Smith 
228463e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
228563e21af5SBarry Smith    final time. It returns the solution at the next timestep.
228663e21af5SBarry Smith 
2287d763cef2SBarry Smith    Level: intermediate
2288d763cef2SBarry Smith 
22890ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2290d763cef2SBarry Smith 
2291d763cef2SBarry Smith @*/
22927087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2293d763cef2SBarry Smith {
2294d763cef2SBarry Smith   PetscFunctionBegin;
22950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22964482741eSBarry Smith   PetscValidPointer(v,2);
22978737fe31SLisandro Dalcin   *v = ts->vec_sol;
2298d763cef2SBarry Smith   PetscFunctionReturn(0);
2299d763cef2SBarry Smith }
2300d763cef2SBarry Smith 
230103fe5f5eSDebojyoti Ghosh /*@
2302b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
230303fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2304b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
230503fe5f5eSDebojyoti Ghosh    structure as the solution vector.
230603fe5f5eSDebojyoti Ghosh 
230703fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
230803fe5f5eSDebojyoti Ghosh 
230903fe5f5eSDebojyoti Ghosh    Parameters :
231003fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2311b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2312b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
231303fe5f5eSDebojyoti Ghosh        returned in v.
2314b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
231503fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2316b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
231703fe5f5eSDebojyoti Ghosh 
23184cdd57e5SDebojyoti Ghosh    Level: advanced
231903fe5f5eSDebojyoti Ghosh 
232003fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
232103fe5f5eSDebojyoti Ghosh 
232203fe5f5eSDebojyoti Ghosh @*/
2323b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
232403fe5f5eSDebojyoti Ghosh {
232503fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
232603fe5f5eSDebojyoti Ghosh 
232703fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
232803fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2329b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
233003fe5f5eSDebojyoti Ghosh   else {
2331b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
233203fe5f5eSDebojyoti Ghosh   }
233303fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
233403fe5f5eSDebojyoti Ghosh }
233503fe5f5eSDebojyoti Ghosh 
23364cdd57e5SDebojyoti Ghosh /*@
23374cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23384cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23394cdd57e5SDebojyoti Ghosh 
23404cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23414cdd57e5SDebojyoti Ghosh 
23424cdd57e5SDebojyoti Ghosh    Parameters :
23434cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23444cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23454cdd57e5SDebojyoti Ghosh 
23464cdd57e5SDebojyoti Ghosh    Level: intermediate
23474cdd57e5SDebojyoti Ghosh 
23484cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23494cdd57e5SDebojyoti Ghosh 
23504cdd57e5SDebojyoti Ghosh @*/
23514cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23524cdd57e5SDebojyoti Ghosh {
23534cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23544cdd57e5SDebojyoti Ghosh 
23554cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23564cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2357f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23584cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2359f6356ec7SDebojyoti Ghosh   } else {
2360f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2361f6356ec7SDebojyoti Ghosh   }
23624cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23634cdd57e5SDebojyoti Ghosh }
23644cdd57e5SDebojyoti Ghosh 
23654cdd57e5SDebojyoti Ghosh /*@
23664cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23674cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23684cdd57e5SDebojyoti Ghosh 
23694cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23704cdd57e5SDebojyoti Ghosh 
23719657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23729657682dSDebojyoti Ghosh 
23734cdd57e5SDebojyoti Ghosh    Parameters :
23744cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2375657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23764cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23774cdd57e5SDebojyoti Ghosh 
23784cdd57e5SDebojyoti Ghosh    Level: intermediate
23794cdd57e5SDebojyoti Ghosh 
238057df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23814cdd57e5SDebojyoti Ghosh 
23824cdd57e5SDebojyoti Ghosh @*/
23830a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23844cdd57e5SDebojyoti Ghosh {
23854cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23864cdd57e5SDebojyoti Ghosh 
23874cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23884cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2389f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23900a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2391f6356ec7SDebojyoti Ghosh   } else {
2392f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2393f6356ec7SDebojyoti Ghosh   }
23944cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23954cdd57e5SDebojyoti Ghosh }
23964cdd57e5SDebojyoti Ghosh 
239757df6a1bSDebojyoti Ghosh /*@
239857df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
239957df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
240057df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
240157df6a1bSDebojyoti Ghosh 
240257df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
240357df6a1bSDebojyoti Ghosh 
240457df6a1bSDebojyoti Ghosh    Parameters :
240557df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
240657df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
240757df6a1bSDebojyoti Ghosh 
240857df6a1bSDebojyoti Ghosh    Level: intermediate
240957df6a1bSDebojyoti Ghosh 
241057df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
241157df6a1bSDebojyoti Ghosh 
241257df6a1bSDebojyoti Ghosh @*/
241357df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
241457df6a1bSDebojyoti Ghosh {
241557df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
241657df6a1bSDebojyoti Ghosh 
241757df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
241857df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24199657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
242057df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
242157df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
242257df6a1bSDebojyoti Ghosh   }
242357df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
242457df6a1bSDebojyoti Ghosh }
242557df6a1bSDebojyoti Ghosh 
2426bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2427d8e5e3e6SSatish Balay /*@
2428bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2429d763cef2SBarry Smith 
2430bdad233fSMatthew Knepley   Not collective
2431d763cef2SBarry Smith 
2432bdad233fSMatthew Knepley   Input Parameters:
2433bdad233fSMatthew Knepley + ts   - The TS
2434bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2435d763cef2SBarry Smith .vb
24360910c330SBarry Smith          U_t - A U = 0      (linear)
24370910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24380910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2439d763cef2SBarry Smith .ve
2440d763cef2SBarry Smith 
2441d763cef2SBarry Smith    Level: beginner
2442d763cef2SBarry Smith 
2443bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2444d763cef2SBarry Smith @*/
24457087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2446a7cc72afSBarry Smith {
24479e2a6581SJed Brown   PetscErrorCode ierr;
24489e2a6581SJed Brown 
2449d763cef2SBarry Smith   PetscFunctionBegin;
24500700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2451bdad233fSMatthew Knepley   ts->problem_type = type;
24529e2a6581SJed Brown   if (type == TS_LINEAR) {
24539e2a6581SJed Brown     SNES snes;
24549e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24559e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24569e2a6581SJed Brown   }
2457d763cef2SBarry Smith   PetscFunctionReturn(0);
2458d763cef2SBarry Smith }
2459d763cef2SBarry Smith 
2460bdad233fSMatthew Knepley /*@C
2461bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2462bdad233fSMatthew Knepley 
2463bdad233fSMatthew Knepley   Not collective
2464bdad233fSMatthew Knepley 
2465bdad233fSMatthew Knepley   Input Parameter:
2466bdad233fSMatthew Knepley . ts   - The TS
2467bdad233fSMatthew Knepley 
2468bdad233fSMatthew Knepley   Output Parameter:
2469bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2470bdad233fSMatthew Knepley .vb
2471089b2837SJed Brown          M U_t = A U
2472089b2837SJed Brown          M(t) U_t = A(t) U
2473b5abc632SBarry Smith          F(t,U,U_t)
2474bdad233fSMatthew Knepley .ve
2475bdad233fSMatthew Knepley 
2476bdad233fSMatthew Knepley    Level: beginner
2477bdad233fSMatthew Knepley 
2478bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2479bdad233fSMatthew Knepley @*/
24807087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2481a7cc72afSBarry Smith {
2482bdad233fSMatthew Knepley   PetscFunctionBegin;
24830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24844482741eSBarry Smith   PetscValidIntPointer(type,2);
2485bdad233fSMatthew Knepley   *type = ts->problem_type;
2486bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2487bdad233fSMatthew Knepley }
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith /*@
2490d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2491d763cef2SBarry Smith    of a timestepper.
2492d763cef2SBarry Smith 
2493d763cef2SBarry Smith    Collective on TS
2494d763cef2SBarry Smith 
2495d763cef2SBarry Smith    Input Parameter:
2496d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith    Notes:
2499d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2500d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2501141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2502d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2503141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2504d763cef2SBarry Smith 
2505d763cef2SBarry Smith    Level: advanced
2506d763cef2SBarry Smith 
2507141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2508d763cef2SBarry Smith @*/
25097087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2510d763cef2SBarry Smith {
2511dfbe8321SBarry Smith   PetscErrorCode ierr;
25126c6b9e74SPeter Brune   DM             dm;
25136c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2514d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2515cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25166c6b9e74SPeter Brune   TSIJacobian    ijac;
2517efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25186c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25192ffb9264SLisandro Dalcin   PetscBool      isnone;
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith   PetscFunctionBegin;
25220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2523277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2524277b19d0SLisandro Dalcin 
25257adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2526cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2527cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2528d763cef2SBarry Smith   }
2529277b19d0SLisandro Dalcin 
2530277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2531277b19d0SLisandro Dalcin 
2532298bade4SHong Zhang   if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */
2533298bade4SHong Zhang     ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr);
2534298bade4SHong Zhang     ts->Jacp = ts->Jacprhs;
2535298bade4SHong Zhang   }
2536298bade4SHong Zhang 
2537cd4cee2dSHong Zhang   if (ts->quadraturets) {
2538cd4cee2dSHong Zhang     ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr);
2539ecf68647SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2540cd4cee2dSHong Zhang     ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr);
2541cd4cee2dSHong Zhang   }
2542cd4cee2dSHong Zhang 
2543971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2544971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2545e1244c69SJed Brown     Mat Amat,Pmat;
2546e1244c69SJed Brown     SNES snes;
2547e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2548e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2549e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2550e1244c69SJed Brown      * have displaced the RHS matrix */
2551971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2552abc0d4abSBarry 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 */
2553abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2554e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2555e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2556e1244c69SJed Brown     }
2557971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2558abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2559e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2560e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2561e1244c69SJed Brown     }
2562e1244c69SJed Brown   }
25632ffb9264SLisandro Dalcin 
25642ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25652ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25662ffb9264SLisandro Dalcin 
2567277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2568000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2569277b19d0SLisandro Dalcin   }
2570277b19d0SLisandro Dalcin 
25712ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25722ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25732ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25742ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25752ffb9264SLisandro Dalcin 
2576a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25776c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25786c6b9e74SPeter Brune    */
25796c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25800298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25816c6b9e74SPeter Brune   if (!func) {
25826c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25836c6b9e74SPeter Brune   }
2584a6772fa2SLisandro 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.
25856c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25866c6b9e74SPeter Brune    */
25870298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25880298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2589efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25900298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2591efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25926c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25936c6b9e74SPeter Brune   }
2594c0517034SDebojyoti Ghosh 
2595c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2596c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2597c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2598c0517034SDebojyoti Ghosh   }
2599c0517034SDebojyoti Ghosh 
2600277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2601277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2602277b19d0SLisandro Dalcin }
2603277b19d0SLisandro Dalcin 
2604f6a906c0SBarry Smith /*@
2605277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2606277b19d0SLisandro Dalcin 
2607277b19d0SLisandro Dalcin    Collective on TS
2608277b19d0SLisandro Dalcin 
2609277b19d0SLisandro Dalcin    Input Parameter:
2610277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2611277b19d0SLisandro Dalcin 
2612277b19d0SLisandro Dalcin    Level: beginner
2613277b19d0SLisandro Dalcin 
2614277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2615277b19d0SLisandro Dalcin @*/
2616277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2617277b19d0SLisandro Dalcin {
26181d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2619277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2620277b19d0SLisandro Dalcin 
2621277b19d0SLisandro Dalcin   PetscFunctionBegin;
2622277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2623b18ea86cSHong Zhang 
2624277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2625277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2626277b19d0SLisandro Dalcin   }
2627277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2628e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2629bbd56ea5SKarl Rupp 
26304e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26314e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2632214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26336bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2634efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2635e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2636e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
263738637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2638bbd56ea5SKarl Rupp 
2639cd4cee2dSHong Zhang   ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr);
2640ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2641ecf68647SHong Zhang   if (ts->forward_solve) {
2642ecf68647SHong Zhang     ierr = TSForwardReset(ts);CHKERRQ(ierr);
2643ecf68647SHong Zhang   }
2644cd4cee2dSHong Zhang   if (ts->quadraturets) {
2645cd4cee2dSHong Zhang     ierr = TSReset(ts->quadraturets);CHKERRQ(ierr);
264636eaed60SHong Zhang     ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2647cd4cee2dSHong Zhang   }
26481d06f6b3SHong Zhang   while (ilink) {
26491d06f6b3SHong Zhang     next = ilink->next;
26501d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
26511d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
26521d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
26531d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
26541d06f6b3SHong Zhang     ilink = next;
265587f4e208SHong Zhang   }
2656545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2657277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2658d763cef2SBarry Smith   PetscFunctionReturn(0);
2659d763cef2SBarry Smith }
2660d763cef2SBarry Smith 
2661d8e5e3e6SSatish Balay /*@
2662d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2663d763cef2SBarry Smith    with TSCreate().
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith    Collective on TS
2666d763cef2SBarry Smith 
2667d763cef2SBarry Smith    Input Parameter:
2668d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2669d763cef2SBarry Smith 
2670d763cef2SBarry Smith    Level: beginner
2671d763cef2SBarry Smith 
2672d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2673d763cef2SBarry Smith @*/
26746bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2675d763cef2SBarry Smith {
26766849ba73SBarry Smith   PetscErrorCode ierr;
2677d763cef2SBarry Smith 
2678d763cef2SBarry Smith   PetscFunctionBegin;
26796bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
2680ecf68647SHong Zhang   PetscValidHeaderSpecific(*ts,TS_CLASSID,1);
26816bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2682d763cef2SBarry Smith 
2683ecf68647SHong Zhang   ierr = TSReset(*ts);CHKERRQ(ierr);
2684ecf68647SHong Zhang   ierr = TSAdjointReset(*ts);CHKERRQ(ierr);
2685ecf68647SHong Zhang   if ((*ts)->forward_solve) {
2686ecf68647SHong Zhang     ierr = TSForwardReset(*ts);CHKERRQ(ierr);
2687ecf68647SHong Zhang   }
2688e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2689e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26906bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26916d4c513bSLisandro Dalcin 
2692bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2693bc952696SBarry Smith 
269484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26956427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26966427ac75SLisandro Dalcin 
26976bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26986bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26996bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27000dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27016d4c513bSLisandro Dalcin 
2702cd4cee2dSHong Zhang   ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr);
2703a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2704d763cef2SBarry Smith   PetscFunctionReturn(0);
2705d763cef2SBarry Smith }
2706d763cef2SBarry Smith 
2707d8e5e3e6SSatish Balay /*@
2708d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2709d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2710d763cef2SBarry Smith 
2711d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2712d763cef2SBarry Smith 
2713d763cef2SBarry Smith    Input Parameter:
2714d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2715d763cef2SBarry Smith 
2716d763cef2SBarry Smith    Output Parameter:
2717d763cef2SBarry Smith .  snes - the nonlinear solver context
2718d763cef2SBarry Smith 
2719d763cef2SBarry Smith    Notes:
2720d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2721d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
272294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27250298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2726d763cef2SBarry Smith 
2727d763cef2SBarry Smith    Level: beginner
2728d763cef2SBarry Smith 
2729d763cef2SBarry Smith @*/
27307087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2731d763cef2SBarry Smith {
2732d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2733d372ba47SLisandro Dalcin 
2734d763cef2SBarry Smith   PetscFunctionBegin;
27350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27364482741eSBarry Smith   PetscValidPointer(snes,2);
2737d372ba47SLisandro Dalcin   if (!ts->snes) {
2738ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
273916413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27400298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27413bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2742d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2743496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27449e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27459e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27469e2a6581SJed Brown     }
2747d372ba47SLisandro Dalcin   }
2748d763cef2SBarry Smith   *snes = ts->snes;
2749d763cef2SBarry Smith   PetscFunctionReturn(0);
2750d763cef2SBarry Smith }
2751d763cef2SBarry Smith 
2752deb2cd25SJed Brown /*@
2753deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2754deb2cd25SJed Brown 
2755deb2cd25SJed Brown    Collective
2756deb2cd25SJed Brown 
2757deb2cd25SJed Brown    Input Parameter:
2758deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2759deb2cd25SJed Brown -  snes - the nonlinear solver context
2760deb2cd25SJed Brown 
2761deb2cd25SJed Brown    Notes:
2762deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2763deb2cd25SJed Brown 
2764deb2cd25SJed Brown    Level: developer
2765deb2cd25SJed Brown 
2766deb2cd25SJed Brown @*/
2767deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2768deb2cd25SJed Brown {
2769deb2cd25SJed Brown   PetscErrorCode ierr;
2770d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2771deb2cd25SJed Brown 
2772deb2cd25SJed Brown   PetscFunctionBegin;
2773deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2774deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2775deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2776deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2777bbd56ea5SKarl Rupp 
2778deb2cd25SJed Brown   ts->snes = snes;
2779bbd56ea5SKarl Rupp 
27800298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27810298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2782740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27830298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2784740132f1SEmil Constantinescu   }
2785deb2cd25SJed Brown   PetscFunctionReturn(0);
2786deb2cd25SJed Brown }
2787deb2cd25SJed Brown 
2788d8e5e3e6SSatish Balay /*@
278994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2790d763cef2SBarry Smith    a TS (timestepper) context.
2791d763cef2SBarry Smith 
279294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2793d763cef2SBarry Smith 
2794d763cef2SBarry Smith    Input Parameter:
2795d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2796d763cef2SBarry Smith 
2797d763cef2SBarry Smith    Output Parameter:
279894b7f48cSBarry Smith .  ksp - the nonlinear solver context
2799d763cef2SBarry Smith 
2800d763cef2SBarry Smith    Notes:
280194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2802d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2803d763cef2SBarry Smith    KSP and PC contexts as well.
2804d763cef2SBarry Smith 
280594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28060298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2807d763cef2SBarry Smith 
2808d763cef2SBarry Smith    Level: beginner
2809d763cef2SBarry Smith 
2810d763cef2SBarry Smith @*/
28117087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2812d763cef2SBarry Smith {
2813d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2814089b2837SJed Brown   SNES           snes;
2815d372ba47SLisandro Dalcin 
2816d763cef2SBarry Smith   PetscFunctionBegin;
28170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28184482741eSBarry Smith   PetscValidPointer(ksp,2);
281917186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2820e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2821089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2822089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2823d763cef2SBarry Smith   PetscFunctionReturn(0);
2824d763cef2SBarry Smith }
2825d763cef2SBarry Smith 
2826d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2827d763cef2SBarry Smith 
2828adb62b0dSMatthew Knepley /*@
2829618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2830618ce8baSLisandro Dalcin 
2831618ce8baSLisandro Dalcin    Logically Collective on TS
2832618ce8baSLisandro Dalcin 
2833618ce8baSLisandro Dalcin    Input Parameters:
2834618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2835618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2836618ce8baSLisandro Dalcin 
2837618ce8baSLisandro Dalcin    Options Database Keys:
2838618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2839618ce8baSLisandro Dalcin 
2840618ce8baSLisandro Dalcin    Notes:
2841618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2842618ce8baSLisandro Dalcin 
2843618ce8baSLisandro Dalcin    Level: intermediate
2844618ce8baSLisandro Dalcin 
2845618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2846618ce8baSLisandro Dalcin @*/
2847618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2848618ce8baSLisandro Dalcin {
2849618ce8baSLisandro Dalcin   PetscFunctionBegin;
2850618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2851618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2852618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2853618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2854618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2855618ce8baSLisandro Dalcin }
2856618ce8baSLisandro Dalcin 
2857618ce8baSLisandro Dalcin /*@
2858618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2859618ce8baSLisandro Dalcin 
2860618ce8baSLisandro Dalcin    Not Collective
2861618ce8baSLisandro Dalcin 
2862618ce8baSLisandro Dalcin    Input Parameters:
2863618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2864618ce8baSLisandro Dalcin 
2865618ce8baSLisandro Dalcin    Output Parameter:
2866618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2867618ce8baSLisandro Dalcin 
2868618ce8baSLisandro Dalcin    Level: advanced
2869618ce8baSLisandro Dalcin 
2870618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2871618ce8baSLisandro Dalcin @*/
2872618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2873618ce8baSLisandro Dalcin {
2874618ce8baSLisandro Dalcin   PetscFunctionBegin;
2875618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2876618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2877618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2878618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2879618ce8baSLisandro Dalcin }
2880618ce8baSLisandro Dalcin 
2881618ce8baSLisandro Dalcin /*@
2882618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2883618ce8baSLisandro Dalcin 
2884618ce8baSLisandro Dalcin    Logically Collective on TS
2885618ce8baSLisandro Dalcin 
2886618ce8baSLisandro Dalcin    Input Parameters:
2887618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2888618ce8baSLisandro Dalcin -  maxtime - final time to step to
2889618ce8baSLisandro Dalcin 
2890618ce8baSLisandro Dalcin    Options Database Keys:
2891ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2892618ce8baSLisandro Dalcin 
2893618ce8baSLisandro Dalcin    Notes:
2894618ce8baSLisandro Dalcin    The default maximum time is 5.0
2895618ce8baSLisandro Dalcin 
2896618ce8baSLisandro Dalcin    Level: intermediate
2897618ce8baSLisandro Dalcin 
2898618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2899618ce8baSLisandro Dalcin @*/
2900618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2901618ce8baSLisandro Dalcin {
2902618ce8baSLisandro Dalcin   PetscFunctionBegin;
2903618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2904618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2905618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2906618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2907618ce8baSLisandro Dalcin }
2908618ce8baSLisandro Dalcin 
2909618ce8baSLisandro Dalcin /*@
2910618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2911618ce8baSLisandro Dalcin 
2912618ce8baSLisandro Dalcin    Not Collective
2913618ce8baSLisandro Dalcin 
2914618ce8baSLisandro Dalcin    Input Parameters:
2915618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2916618ce8baSLisandro Dalcin 
2917618ce8baSLisandro Dalcin    Output Parameter:
2918618ce8baSLisandro Dalcin .  maxtime - final time to step to
2919618ce8baSLisandro Dalcin 
2920618ce8baSLisandro Dalcin    Level: advanced
2921618ce8baSLisandro Dalcin 
2922618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2923618ce8baSLisandro Dalcin @*/
2924618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2925618ce8baSLisandro Dalcin {
2926618ce8baSLisandro Dalcin   PetscFunctionBegin;
2927618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2928618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2929618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2930618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2931618ce8baSLisandro Dalcin }
2932618ce8baSLisandro Dalcin 
2933618ce8baSLisandro Dalcin /*@
2934aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2935edc382c3SSatish Balay 
2936edc382c3SSatish Balay    Level: deprecated
2937edc382c3SSatish Balay 
2938aaa6c58dSLisandro Dalcin @*/
2939aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2940aaa6c58dSLisandro Dalcin {
2941aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2942aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2943aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2944aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2945aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2946aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2947aaa6c58dSLisandro Dalcin }
2948aaa6c58dSLisandro Dalcin 
2949aaa6c58dSLisandro Dalcin /*@
295019eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
2951edc382c3SSatish Balay 
2952edc382c3SSatish Balay    Level: deprecated
2953edc382c3SSatish Balay 
2954adb62b0dSMatthew Knepley @*/
29557087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2956adb62b0dSMatthew Knepley {
2957adb62b0dSMatthew Knepley   PetscFunctionBegin;
29580700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2959abc0a331SBarry Smith   if (maxsteps) {
29604482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2961adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2962adb62b0dSMatthew Knepley   }
2963abc0a331SBarry Smith   if (maxtime) {
29644482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2965adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2966adb62b0dSMatthew Knepley   }
2967adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2968adb62b0dSMatthew Knepley }
2969adb62b0dSMatthew Knepley 
2970d763cef2SBarry Smith /*@
297119eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
2972edc382c3SSatish Balay 
2973edc382c3SSatish Balay    Level: deprecated
2974edc382c3SSatish Balay 
2975d763cef2SBarry Smith @*/
29767087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2977d763cef2SBarry Smith {
2978d763cef2SBarry Smith   PetscFunctionBegin;
29790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2980c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2981c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
298239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
298339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2984d763cef2SBarry Smith   PetscFunctionReturn(0);
2985d763cef2SBarry Smith }
2986d763cef2SBarry Smith 
2987d763cef2SBarry Smith /*@
29885c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
2989edc382c3SSatish Balay 
2990edc382c3SSatish Balay    Level: deprecated
2991edc382c3SSatish Balay 
29925c5f5948SLisandro Dalcin @*/
2993e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
29945c5f5948SLisandro Dalcin 
299519eac22cSLisandro Dalcin /*@
29964f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
2997edc382c3SSatish Balay 
2998edc382c3SSatish Balay    Level: deprecated
2999edc382c3SSatish Balay 
30004f4e0956SLisandro Dalcin @*/
30014f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30024f4e0956SLisandro Dalcin 
30034f4e0956SLisandro Dalcin /*@
3004d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3005d763cef2SBarry Smith    for use by the TS routines.
3006d763cef2SBarry Smith 
3007d083f849SBarry Smith    Logically Collective on TS
3008d763cef2SBarry Smith 
3009d763cef2SBarry Smith    Input Parameters:
3010d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30110910c330SBarry Smith -  u - the solution vector
3012d763cef2SBarry Smith 
3013d763cef2SBarry Smith    Level: beginner
3014d763cef2SBarry Smith 
30150ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3016d763cef2SBarry Smith @*/
30170910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3018d763cef2SBarry Smith {
30198737fe31SLisandro Dalcin   PetscErrorCode ierr;
3020496e6a7aSJed Brown   DM             dm;
30218737fe31SLisandro Dalcin 
3022d763cef2SBarry Smith   PetscFunctionBegin;
30230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30240910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30250910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30266bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30270910c330SBarry Smith   ts->vec_sol = u;
3028bbd56ea5SKarl Rupp 
3029496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30300910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3031d763cef2SBarry Smith   PetscFunctionReturn(0);
3032d763cef2SBarry Smith }
3033d763cef2SBarry Smith 
3034ac226902SBarry Smith /*@C
3035000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30363f2090d5SJed Brown   called once at the beginning of each time step.
3037000e7ae3SMatthew Knepley 
30383f9fe445SBarry Smith   Logically Collective on TS
3039000e7ae3SMatthew Knepley 
3040000e7ae3SMatthew Knepley   Input Parameters:
3041000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3042000e7ae3SMatthew Knepley - func - The function
3043000e7ae3SMatthew Knepley 
3044000e7ae3SMatthew Knepley   Calling sequence of func:
3045000e7ae3SMatthew Knepley . func (TS ts);
3046000e7ae3SMatthew Knepley 
3047000e7ae3SMatthew Knepley   Level: intermediate
3048000e7ae3SMatthew Knepley 
3049dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3050000e7ae3SMatthew Knepley @*/
30517087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3052000e7ae3SMatthew Knepley {
3053000e7ae3SMatthew Knepley   PetscFunctionBegin;
30540700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3055ae60f76fSBarry Smith   ts->prestep = func;
3056000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3057000e7ae3SMatthew Knepley }
3058000e7ae3SMatthew Knepley 
305909ee8438SJed Brown /*@
30603f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
30613f2090d5SJed Brown 
30623f2090d5SJed Brown   Collective on TS
30633f2090d5SJed Brown 
30643f2090d5SJed Brown   Input Parameters:
30653f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
30663f2090d5SJed Brown 
30673f2090d5SJed Brown   Notes:
30683f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
30693f2090d5SJed Brown   so most users would not generally call this routine themselves.
30703f2090d5SJed Brown 
30713f2090d5SJed Brown   Level: developer
30723f2090d5SJed Brown 
30739be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
30743f2090d5SJed Brown @*/
30757087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
30763f2090d5SJed Brown {
30773f2090d5SJed Brown   PetscErrorCode ierr;
30783f2090d5SJed Brown 
30793f2090d5SJed Brown   PetscFunctionBegin;
30800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3081ae60f76fSBarry Smith   if (ts->prestep) {
30825efd42a4SStefano Zampini     Vec              U;
30835efd42a4SStefano Zampini     PetscObjectState sprev,spost;
30845efd42a4SStefano Zampini 
30855efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
30865efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3087ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
30885efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3089dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3090312ce896SJed Brown   }
30913f2090d5SJed Brown   PetscFunctionReturn(0);
30923f2090d5SJed Brown }
30933f2090d5SJed Brown 
3094b8123daeSJed Brown /*@C
3095b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3096b8123daeSJed Brown   called once at the beginning of each stage.
3097b8123daeSJed Brown 
3098b8123daeSJed Brown   Logically Collective on TS
3099b8123daeSJed Brown 
3100b8123daeSJed Brown   Input Parameters:
3101b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3102b8123daeSJed Brown - func - The function
3103b8123daeSJed Brown 
3104b8123daeSJed Brown   Calling sequence of func:
3105b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3106b8123daeSJed Brown 
3107b8123daeSJed Brown   Level: intermediate
3108b8123daeSJed Brown 
3109b8123daeSJed Brown   Note:
3110b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
311180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3112b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3113b8123daeSJed Brown 
31149be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3115b8123daeSJed Brown @*/
3116b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3117b8123daeSJed Brown {
3118b8123daeSJed Brown   PetscFunctionBegin;
3119b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3120ae60f76fSBarry Smith   ts->prestage = func;
3121b8123daeSJed Brown   PetscFunctionReturn(0);
3122b8123daeSJed Brown }
3123b8123daeSJed Brown 
31249be3e283SDebojyoti Ghosh /*@C
31259be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31269be3e283SDebojyoti Ghosh   called once at the end of each stage.
31279be3e283SDebojyoti Ghosh 
31289be3e283SDebojyoti Ghosh   Logically Collective on TS
31299be3e283SDebojyoti Ghosh 
31309be3e283SDebojyoti Ghosh   Input Parameters:
31319be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31329be3e283SDebojyoti Ghosh - func - The function
31339be3e283SDebojyoti Ghosh 
31349be3e283SDebojyoti Ghosh   Calling sequence of func:
31359be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31369be3e283SDebojyoti Ghosh 
31379be3e283SDebojyoti Ghosh   Level: intermediate
31389be3e283SDebojyoti Ghosh 
31399be3e283SDebojyoti Ghosh   Note:
31409be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
314180275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31429be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31439be3e283SDebojyoti Ghosh 
31449be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
31459be3e283SDebojyoti Ghosh @*/
31469be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
31479be3e283SDebojyoti Ghosh {
31489be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31499be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
31509be3e283SDebojyoti Ghosh   ts->poststage = func;
31519be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
31529be3e283SDebojyoti Ghosh }
31539be3e283SDebojyoti Ghosh 
3154c688d042SShri Abhyankar /*@C
3155c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3156c688d042SShri Abhyankar   called once at the end of each step evaluation.
3157c688d042SShri Abhyankar 
3158c688d042SShri Abhyankar   Logically Collective on TS
3159c688d042SShri Abhyankar 
3160c688d042SShri Abhyankar   Input Parameters:
3161c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3162c688d042SShri Abhyankar - func - The function
3163c688d042SShri Abhyankar 
3164c688d042SShri Abhyankar   Calling sequence of func:
3165c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3166c688d042SShri Abhyankar 
3167c688d042SShri Abhyankar   Level: intermediate
3168c688d042SShri Abhyankar 
3169c688d042SShri Abhyankar   Note:
31701785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
31711785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3172e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3173e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3174e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3175c688d042SShri Abhyankar 
3176c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3177c688d042SShri Abhyankar @*/
3178c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3179c688d042SShri Abhyankar {
3180c688d042SShri Abhyankar   PetscFunctionBegin;
3181c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3182c688d042SShri Abhyankar   ts->postevaluate = func;
3183c688d042SShri Abhyankar   PetscFunctionReturn(0);
3184c688d042SShri Abhyankar }
3185c688d042SShri Abhyankar 
3186b8123daeSJed Brown /*@
3187b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3188b8123daeSJed Brown 
3189b8123daeSJed Brown   Collective on TS
3190b8123daeSJed Brown 
3191b8123daeSJed Brown   Input Parameters:
3192b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
31939be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3194b8123daeSJed Brown 
3195b8123daeSJed Brown   Notes:
3196b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3197b8123daeSJed Brown   most users would not generally call this routine themselves.
3198b8123daeSJed Brown 
3199b8123daeSJed Brown   Level: developer
3200b8123daeSJed Brown 
32019be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3202b8123daeSJed Brown @*/
3203b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3204b8123daeSJed Brown {
3205b8123daeSJed Brown   PetscFunctionBegin;
3206b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3207ae60f76fSBarry Smith   if (ts->prestage) {
3208ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3209b8123daeSJed Brown   }
3210b8123daeSJed Brown   PetscFunctionReturn(0);
3211b8123daeSJed Brown }
3212b8123daeSJed Brown 
32139be3e283SDebojyoti Ghosh /*@
32149be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32159be3e283SDebojyoti Ghosh 
32169be3e283SDebojyoti Ghosh   Collective on TS
32179be3e283SDebojyoti Ghosh 
32189be3e283SDebojyoti Ghosh   Input Parameters:
32199be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32209be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32219be3e283SDebojyoti Ghosh   stageindex  - Stage number
32229be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32239be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32249be3e283SDebojyoti Ghosh 
32259be3e283SDebojyoti Ghosh   Notes:
32269be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32279be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32289be3e283SDebojyoti Ghosh 
32299be3e283SDebojyoti Ghosh   Level: developer
32309be3e283SDebojyoti Ghosh 
32319be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32329be3e283SDebojyoti Ghosh @*/
32339be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32349be3e283SDebojyoti Ghosh {
32359be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32369be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32374beae5d8SLisandro Dalcin   if (ts->poststage) {
32389be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
32399be3e283SDebojyoti Ghosh   }
32409be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32419be3e283SDebojyoti Ghosh }
32429be3e283SDebojyoti Ghosh 
3243c688d042SShri Abhyankar /*@
3244c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3245c688d042SShri Abhyankar 
3246c688d042SShri Abhyankar   Collective on TS
3247c688d042SShri Abhyankar 
3248c688d042SShri Abhyankar   Input Parameters:
3249c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3250c688d042SShri Abhyankar 
3251c688d042SShri Abhyankar   Notes:
3252c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3253c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3254c688d042SShri Abhyankar 
3255c688d042SShri Abhyankar   Level: developer
3256c688d042SShri Abhyankar 
3257c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3258c688d042SShri Abhyankar @*/
3259c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3260c688d042SShri Abhyankar {
3261c688d042SShri Abhyankar   PetscErrorCode ierr;
3262c688d042SShri Abhyankar 
3263c688d042SShri Abhyankar   PetscFunctionBegin;
3264c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3265c688d042SShri Abhyankar   if (ts->postevaluate) {
3266dcb233daSLisandro Dalcin     Vec              U;
3267dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3268dcb233daSLisandro Dalcin 
3269dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3270dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3271c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3272dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3273dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3274c688d042SShri Abhyankar   }
3275c688d042SShri Abhyankar   PetscFunctionReturn(0);
3276c688d042SShri Abhyankar }
3277c688d042SShri Abhyankar 
3278ac226902SBarry Smith /*@C
3279000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
32803f2090d5SJed Brown   called once at the end of each time step.
3281000e7ae3SMatthew Knepley 
32823f9fe445SBarry Smith   Logically Collective on TS
3283000e7ae3SMatthew Knepley 
3284000e7ae3SMatthew Knepley   Input Parameters:
3285000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3286000e7ae3SMatthew Knepley - func - The function
3287000e7ae3SMatthew Knepley 
3288000e7ae3SMatthew Knepley   Calling sequence of func:
3289b8123daeSJed Brown $ func (TS ts);
3290000e7ae3SMatthew Knepley 
32911785ff2aSShri Abhyankar   Notes:
32921785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
32931785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
32941785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
32951785ff2aSShri Abhyankar 
3296000e7ae3SMatthew Knepley   Level: intermediate
3297000e7ae3SMatthew Knepley 
3298dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3299000e7ae3SMatthew Knepley @*/
33007087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3301000e7ae3SMatthew Knepley {
3302000e7ae3SMatthew Knepley   PetscFunctionBegin;
33030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3304ae60f76fSBarry Smith   ts->poststep = func;
3305000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3306000e7ae3SMatthew Knepley }
3307000e7ae3SMatthew Knepley 
330809ee8438SJed Brown /*@
33093f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33103f2090d5SJed Brown 
33113f2090d5SJed Brown   Collective on TS
33123f2090d5SJed Brown 
33133f2090d5SJed Brown   Input Parameters:
33143f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33153f2090d5SJed Brown 
33163f2090d5SJed Brown   Notes:
33173f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33183f2090d5SJed Brown   so most users would not generally call this routine themselves.
33193f2090d5SJed Brown 
33203f2090d5SJed Brown   Level: developer
33213f2090d5SJed Brown 
33223f2090d5SJed Brown @*/
33237087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33243f2090d5SJed Brown {
33253f2090d5SJed Brown   PetscErrorCode ierr;
33263f2090d5SJed Brown 
33273f2090d5SJed Brown   PetscFunctionBegin;
33280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3329ae60f76fSBarry Smith   if (ts->poststep) {
33305efd42a4SStefano Zampini     Vec              U;
33315efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33325efd42a4SStefano Zampini 
33335efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
33345efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3335ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
33365efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3337dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
333872ac3e02SJed Brown   }
33393f2090d5SJed Brown   PetscFunctionReturn(0);
33403f2090d5SJed Brown }
33413f2090d5SJed Brown 
3342d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3343d763cef2SBarry Smith 
3344d763cef2SBarry Smith /*@C
3345a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3346d763cef2SBarry Smith    timestep to display the iteration's  progress.
3347d763cef2SBarry Smith 
33483f9fe445SBarry Smith    Logically Collective on TS
3349d763cef2SBarry Smith 
3350d763cef2SBarry Smith    Input Parameters:
3351d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3352e213d8f1SJed Brown .  monitor - monitoring routine
3353329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
33540298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3355b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
33560298fd71SBarry Smith           (may be NULL)
3357d763cef2SBarry Smith 
3358e213d8f1SJed Brown    Calling sequence of monitor:
3359dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3360d763cef2SBarry Smith 
3361d763cef2SBarry Smith +    ts - the TS context
336263e21af5SBarry 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)
33631f06c33eSBarry Smith .    time - current time
33640910c330SBarry Smith .    u - current iterate
3365d763cef2SBarry Smith -    mctx - [optional] monitoring context
3366d763cef2SBarry Smith 
3367d763cef2SBarry Smith    Notes:
3368d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3369d763cef2SBarry Smith    already has been loaded.
3370d763cef2SBarry Smith 
337195452b02SPatrick Sanan    Fortran Notes:
337295452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3373025f1a04SBarry Smith 
3374d763cef2SBarry Smith    Level: intermediate
3375d763cef2SBarry Smith 
3376a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3377d763cef2SBarry Smith @*/
3378c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3379d763cef2SBarry Smith {
338078064530SBarry Smith   PetscErrorCode ierr;
338178064530SBarry Smith   PetscInt       i;
338278064530SBarry Smith   PetscBool      identical;
338378064530SBarry Smith 
3384d763cef2SBarry Smith   PetscFunctionBegin;
33850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
338678064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
338778064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
338878064530SBarry Smith     if (identical) PetscFunctionReturn(0);
338978064530SBarry Smith   }
339017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3391d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
33928704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3393d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3394d763cef2SBarry Smith   PetscFunctionReturn(0);
3395d763cef2SBarry Smith }
3396d763cef2SBarry Smith 
3397d763cef2SBarry Smith /*@C
3398a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3399d763cef2SBarry Smith 
34003f9fe445SBarry Smith    Logically Collective on TS
3401d763cef2SBarry Smith 
3402d763cef2SBarry Smith    Input Parameters:
3403d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3404d763cef2SBarry Smith 
3405d763cef2SBarry Smith    Notes:
3406d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3407d763cef2SBarry Smith 
3408d763cef2SBarry Smith    Level: intermediate
3409d763cef2SBarry Smith 
3410a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3411d763cef2SBarry Smith @*/
34127087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3413d763cef2SBarry Smith {
3414d952e501SBarry Smith   PetscErrorCode ierr;
3415d952e501SBarry Smith   PetscInt       i;
3416d952e501SBarry Smith 
3417d763cef2SBarry Smith   PetscFunctionBegin;
34180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3419d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34208704b422SBarry Smith     if (ts->monitordestroy[i]) {
34218704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3422d952e501SBarry Smith     }
3423d952e501SBarry Smith   }
3424d763cef2SBarry Smith   ts->numbermonitors = 0;
3425d763cef2SBarry Smith   PetscFunctionReturn(0);
3426d763cef2SBarry Smith }
3427d763cef2SBarry Smith 
3428721cd6eeSBarry Smith /*@C
3429721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
34305516499fSSatish Balay 
34315516499fSSatish Balay    Level: intermediate
343241251cbbSSatish Balay 
343363e21af5SBarry Smith .seealso:  TSMonitorSet()
343441251cbbSSatish Balay @*/
3435721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3436d763cef2SBarry Smith {
3437dfbe8321SBarry Smith   PetscErrorCode ierr;
3438721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
343941aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3440d132466eSBarry Smith 
3441d763cef2SBarry Smith   PetscFunctionBegin;
34424d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
344341aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
344441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3445721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
344641aca3d6SBarry Smith   if (iascii) {
3447649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
344863e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
344963e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
345063e21af5SBarry Smith     } else {
34518392e04aSShri 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);
345263e21af5SBarry Smith     }
3453649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
345441aca3d6SBarry Smith   } else if (ibinary) {
345541aca3d6SBarry Smith     PetscMPIInt rank;
345641aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
345741aca3d6SBarry Smith     if (!rank) {
3458450a797fSBarry Smith       PetscBool skipHeader;
3459450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3460450a797fSBarry Smith 
3461450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3462450a797fSBarry Smith       if (!skipHeader) {
3463450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3464450a797fSBarry Smith        }
346541aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
346641aca3d6SBarry Smith     } else {
346741aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
346841aca3d6SBarry Smith     }
346941aca3d6SBarry Smith   }
3470721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3471d763cef2SBarry Smith   PetscFunctionReturn(0);
3472d763cef2SBarry Smith }
3473d763cef2SBarry Smith 
3474cc9c3a59SBarry Smith /*@C
3475cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3476cc9c3a59SBarry Smith 
3477cc9c3a59SBarry Smith    Level: intermediate
3478cc9c3a59SBarry Smith 
3479cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3480cc9c3a59SBarry Smith @*/
3481cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3482cc9c3a59SBarry Smith {
3483cc9c3a59SBarry Smith   PetscErrorCode ierr;
3484cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3485cc9c3a59SBarry Smith   PetscBool      iascii;
3486cc9c3a59SBarry Smith   PetscReal      max,min;
3487cc9c3a59SBarry Smith 
3488cc9c3a59SBarry Smith 
3489cc9c3a59SBarry Smith   PetscFunctionBegin;
3490cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3491cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3492cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3493cc9c3a59SBarry Smith   if (iascii) {
3494cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3495cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3496cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
34975132926bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s max %g min %g\n",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)" : "",(double)max,(double)min);CHKERRQ(ierr);
3498cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3499cc9c3a59SBarry Smith   }
3500cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3501cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3502cc9c3a59SBarry Smith }
3503cc9c3a59SBarry Smith 
3504cd652676SJed Brown /*@
3505cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3506cd652676SJed Brown 
3507cd652676SJed Brown    Collective on TS
3508cd652676SJed Brown 
3509cd652676SJed Brown    Input Argument:
3510cd652676SJed Brown +  ts - time stepping context
3511cd652676SJed Brown -  t - time to interpolate to
3512cd652676SJed Brown 
3513cd652676SJed Brown    Output Argument:
35140910c330SBarry Smith .  U - state at given time
3515cd652676SJed Brown 
3516cd652676SJed Brown    Level: intermediate
3517cd652676SJed Brown 
3518cd652676SJed Brown    Developer Notes:
3519cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3520cd652676SJed Brown 
3521874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3522cd652676SJed Brown @*/
35230910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3524cd652676SJed Brown {
3525cd652676SJed Brown   PetscErrorCode ierr;
3526cd652676SJed Brown 
3527cd652676SJed Brown   PetscFunctionBegin;
3528cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3529b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3530be5899b3SLisandro 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);
3531ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35320910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3533cd652676SJed Brown   PetscFunctionReturn(0);
3534cd652676SJed Brown }
3535cd652676SJed Brown 
3536d763cef2SBarry Smith /*@
35376d9e5789SSean Farley    TSStep - Steps one time step
3538d763cef2SBarry Smith 
3539d763cef2SBarry Smith    Collective on TS
3540d763cef2SBarry Smith 
3541d763cef2SBarry Smith    Input Parameter:
3542d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3543d763cef2SBarry Smith 
354427829d71SBarry Smith    Level: developer
3545d763cef2SBarry Smith 
3546b8123daeSJed Brown    Notes:
354727829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
354827829d71SBarry Smith 
3549b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3550b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3551b8123daeSJed Brown 
355219eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
355319eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
355425cb2221SBarry Smith 
35559be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3556d763cef2SBarry Smith @*/
3557193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3558d763cef2SBarry Smith {
3559dfbe8321SBarry Smith   PetscErrorCode   ierr;
3560fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3561be5899b3SLisandro Dalcin   PetscReal        ptime;
3562d763cef2SBarry Smith 
3563d763cef2SBarry Smith   PetscFunctionBegin;
35640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3565fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3566fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3567fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3568fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3569fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3570fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3571302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3572fffbeea8SBarry Smith 
3573d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
357468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3575d405a339SMatthew Knepley 
3576ef85077eSLisandro 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>");
3577a6772fa2SLisandro 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()");
3578be5899b3SLisandro 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");
3579a6772fa2SLisandro Dalcin 
3580be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3581be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
35822808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3583e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3584d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3585193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3586d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3587be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
35882808aa04SLisandro Dalcin   ts->steps++;
3589be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
359028d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3591362cd11cSLisandro Dalcin 
3592362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3593cef5090cSJed Brown     if (ts->errorifstepfailed) {
359408c7845fSBarry 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]);
359508c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3596d2daff3dSHong Zhang     }
359708c7845fSBarry Smith   } else if (!ts->reason) {
359808c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
359908c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
360008c7845fSBarry Smith   }
360108c7845fSBarry Smith   PetscFunctionReturn(0);
360208c7845fSBarry Smith }
360308c7845fSBarry Smith 
360408c7845fSBarry Smith /*@
36057cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36067cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36077cbde773SLisandro Dalcin 
36087cbde773SLisandro Dalcin    Collective on TS
36097cbde773SLisandro Dalcin 
36107cbde773SLisandro Dalcin    Input Arguments:
36117cbde773SLisandro Dalcin +  ts - time stepping context
36127cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36137cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36147cbde773SLisandro Dalcin 
36157cbde773SLisandro Dalcin    Output Arguments:
36167cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36177cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36187cbde773SLisandro Dalcin 
36197cbde773SLisandro Dalcin    Level: advanced
36207cbde773SLisandro Dalcin 
36217cbde773SLisandro Dalcin    Notes:
36227cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36237cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36247cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36257cbde773SLisandro Dalcin 
36267cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36277cbde773SLisandro Dalcin @*/
36287cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36297cbde773SLisandro Dalcin {
36307cbde773SLisandro Dalcin   PetscErrorCode ierr;
36317cbde773SLisandro Dalcin 
36327cbde773SLisandro Dalcin   PetscFunctionBegin;
36337cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36347cbde773SLisandro Dalcin   PetscValidType(ts,1);
36357cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
36367cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36377cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36387cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36397cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36407cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36417cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36427cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36437cbde773SLisandro Dalcin }
36447cbde773SLisandro Dalcin 
364505175c85SJed Brown /*@
364605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
364705175c85SJed Brown 
36481c3436cfSJed Brown    Collective on TS
364905175c85SJed Brown 
365005175c85SJed Brown    Input Arguments:
36511c3436cfSJed Brown +  ts - time stepping context
36521c3436cfSJed Brown .  order - desired order of accuracy
36530298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
365405175c85SJed Brown 
365505175c85SJed Brown    Output Arguments:
36560910c330SBarry Smith .  U - state at the end of the current step
365705175c85SJed Brown 
365805175c85SJed Brown    Level: advanced
365905175c85SJed Brown 
3660108c343cSJed Brown    Notes:
3661108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3662108c343cSJed 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.
3663108c343cSJed Brown 
36641c3436cfSJed Brown .seealso: TSStep(), TSAdapt
366505175c85SJed Brown @*/
36660910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
366705175c85SJed Brown {
366805175c85SJed Brown   PetscErrorCode ierr;
366905175c85SJed Brown 
367005175c85SJed Brown   PetscFunctionBegin;
367105175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
367205175c85SJed Brown   PetscValidType(ts,1);
36730910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3674ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36750910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
367605175c85SJed Brown   PetscFunctionReturn(0);
367705175c85SJed Brown }
367805175c85SJed Brown 
3679b1cb36f3SHong Zhang /*@
36806a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
36816a4d4014SLisandro Dalcin 
36826a4d4014SLisandro Dalcin    Collective on TS
36836a4d4014SLisandro Dalcin 
36846a4d4014SLisandro Dalcin    Input Parameter:
36856a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
368663e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
368763e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
36885a3a76d0SJed Brown 
36896a4d4014SLisandro Dalcin    Level: beginner
36906a4d4014SLisandro Dalcin 
36915a3a76d0SJed Brown    Notes:
36925a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
36935a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
36945a3a76d0SJed Brown    stepped over the final time.
36955a3a76d0SJed Brown 
369663e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
36976a4d4014SLisandro Dalcin @*/
3698cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
36996a4d4014SLisandro Dalcin {
3700b06615a5SLisandro Dalcin   Vec               solution;
37016a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3702f22f69f0SBarry Smith 
37036a4d4014SLisandro Dalcin   PetscFunctionBegin;
37040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3705f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3706feed9e9dSBarry Smith 
3707ee41a567SStefano 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 */
37080910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3709b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3710b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3711b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
37125a3a76d0SJed Brown     }
37130910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3714715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3715bbd56ea5SKarl Rupp   } else if (u) {
37160910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
37175a3a76d0SJed Brown   }
3718b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
371968bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3720a6772fa2SLisandro Dalcin 
3721ef85077eSLisandro 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>");
3722a6772fa2SLisandro 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()");
3723a6772fa2SLisandro 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");
3724a6772fa2SLisandro Dalcin 
3725715f1b00SHong Zhang   if (ts->forward_solve) {
3726715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3727715f1b00SHong Zhang   }
3728715f1b00SHong Zhang 
3729e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3730715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3731e7069c78SShri      TSTrajectory to incorrectly save the output files
3732e7069c78SShri   */
3733715f1b00SHong Zhang   /* reset time step and iteration counters */
37342808aa04SLisandro Dalcin   if (!ts->steps) {
37355ef26d82SJed Brown     ts->ksp_its           = 0;
37365ef26d82SJed Brown     ts->snes_its          = 0;
3737c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3738c610991cSLisandro Dalcin     ts->reject            = 0;
37392808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
37402808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
37412808aa04SLisandro Dalcin   }
374210bc0e4dSStefano 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;
3743193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3744193ac0bcSJed Brown 
3745ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3746f05ece33SBarry Smith 
3747193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3748193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3749a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3750cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3751a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3752be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3753db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3754db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3755e7069c78SShri 
37562808aa04SLisandro Dalcin     if (!ts->steps) {
37572808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37586427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37592808aa04SLisandro Dalcin     }
37606427ac75SLisandro Dalcin 
3761e1a7a14fSJed Brown     while (!ts->reason) {
37622808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37639687d888SLisandro Dalcin       if (!ts->steprollback) {
37649687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
37659687d888SLisandro Dalcin       }
3766193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3767f3b1f45cSBarry Smith       if (ts->testjacobian) {
3768f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3769f3b1f45cSBarry Smith       }
3770f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3771f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3772f3b1f45cSBarry Smith       }
3773cd4cee2dSHong Zhang       if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
3774b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3775b1cb36f3SHong Zhang       }
377658818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3777715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3778715f1b00SHong Zhang       }
377910b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3780e783b05fSHong 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. */
378158818c2dSLisandro Dalcin       if (ts->steprollback) {
378258818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
378358818c2dSLisandro Dalcin       }
3784e783b05fSHong Zhang       if (!ts->steprollback) {
37852808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37861eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3787aeb4809dSShri Abhyankar       }
3788193ac0bcSJed Brown     }
37892808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37906427ac75SLisandro Dalcin 
379149354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
37920910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3793cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3794b06615a5SLisandro Dalcin       solution = u;
379563e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
37960574a7fbSJed Brown     } else {
3797ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3798cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3799b06615a5SLisandro Dalcin       solution = ts->vec_sol;
38000574a7fbSJed Brown     }
3801193ac0bcSJed Brown   }
3802d2daff3dSHong Zhang 
3803ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
380463e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
380556f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3806ef222394SBarry Smith   if (ts->adjoint_solve) {
3807ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38082b0a91c0SBarry Smith   }
38096a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38106a4d4014SLisandro Dalcin }
38116a4d4014SLisandro Dalcin 
38127db568b7SBarry Smith /*@C
3813228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3814228d79bcSJed Brown 
3815228d79bcSJed Brown    Collective on TS
3816228d79bcSJed Brown 
3817228d79bcSJed Brown    Input Parameters:
3818228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3819228d79bcSJed Brown .  step - step number that has just completed
3820228d79bcSJed Brown .  ptime - model time of the state
38210910c330SBarry Smith -  u - state at the current model time
3822228d79bcSJed Brown 
3823228d79bcSJed Brown    Notes:
38247db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
38257db568b7SBarry Smith    Users would almost never call this routine directly.
3826228d79bcSJed Brown 
382763e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
382863e21af5SBarry Smith 
38297db568b7SBarry Smith    Level: developer
3830228d79bcSJed Brown 
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 
38458860a134SJunchao Zhang   ierr = VecLockReadPush(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   }
38498860a134SJunchao Zhang   ierr = VecLockReadPop(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 .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
38937db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
38947db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
38957db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
38967db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
38977c922b88SBarry Smith 
3898d763cef2SBarry Smith @*/
3899a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3900d763cef2SBarry Smith {
39017f52daa2SLisandro Dalcin   PetscDraw      draw;
3902dfbe8321SBarry Smith   PetscErrorCode ierr;
3903d763cef2SBarry Smith 
3904d763cef2SBarry Smith   PetscFunctionBegin;
3905b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39067f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39077f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39087f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3909287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
39107f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3911a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3912d763cef2SBarry Smith   PetscFunctionReturn(0);
3913d763cef2SBarry Smith }
3914d763cef2SBarry Smith 
3915b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3916d763cef2SBarry Smith {
39170b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3918c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3919dfbe8321SBarry Smith   PetscErrorCode ierr;
3920d763cef2SBarry Smith 
3921d763cef2SBarry Smith   PetscFunctionBegin;
392263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3923b06615a5SLisandro Dalcin   if (!step) {
3924a9f9c1f6SBarry Smith     PetscDrawAxis axis;
39258b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
3926a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
39278b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
3928a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3929a9f9c1f6SBarry Smith   }
3930c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
39318b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
39320b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3933b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
39340b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
39356934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
39363923b477SBarry Smith   }
3937d763cef2SBarry Smith   PetscFunctionReturn(0);
3938d763cef2SBarry Smith }
3939d763cef2SBarry Smith 
3940d763cef2SBarry Smith /*@C
3941a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3942a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3943d763cef2SBarry Smith 
39440b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3945d763cef2SBarry Smith 
3946d763cef2SBarry Smith    Input Parameter:
39470b039ecaSBarry Smith .  ctx - the monitor context
3948d763cef2SBarry Smith 
3949d763cef2SBarry Smith    Level: intermediate
3950d763cef2SBarry Smith 
39514f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3952d763cef2SBarry Smith @*/
3953a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3954d763cef2SBarry Smith {
3955dfbe8321SBarry Smith   PetscErrorCode ierr;
3956d763cef2SBarry Smith 
3957d763cef2SBarry Smith   PetscFunctionBegin;
39587684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
39597684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
39607684fa3eSBarry Smith   }
39610b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
396231152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3963387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3964387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3965387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
39660b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3967d763cef2SBarry Smith   PetscFunctionReturn(0);
3968d763cef2SBarry Smith }
3969d763cef2SBarry Smith 
39701b575b74SJoseph Pusztay /*
39711b575b74SJoseph Pusztay 
39721b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
39731b575b74SJoseph Pusztay 
39741b575b74SJoseph Pusztay */
39751b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
39761b575b74SJoseph Pusztay {
39771b575b74SJoseph Pusztay   PetscDraw      draw;
39781b575b74SJoseph Pusztay   PetscErrorCode ierr;
39791b575b74SJoseph Pusztay 
39801b575b74SJoseph Pusztay   PetscFunctionBegin;
39811b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
39821b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39831b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39841b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
39851b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
39861b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
39871b575b74SJoseph Pusztay   PetscFunctionReturn(0);
39881b575b74SJoseph Pusztay 
39891b575b74SJoseph Pusztay }
39901b575b74SJoseph Pusztay 
39911b575b74SJoseph Pusztay /*
39921b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
39931b575b74SJoseph Pusztay */
39941b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
39951b575b74SJoseph Pusztay {
39961b575b74SJoseph Pusztay   PetscErrorCode ierr;
39971b575b74SJoseph Pusztay 
39981b575b74SJoseph Pusztay   PetscFunctionBegin;
39991b575b74SJoseph Pusztay 
40001b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
40011b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
40021b575b74SJoseph Pusztay 
40031b575b74SJoseph Pusztay   PetscFunctionReturn(0);
40041b575b74SJoseph Pusztay 
40051b575b74SJoseph Pusztay }
40061b575b74SJoseph Pusztay 
4007d763cef2SBarry Smith /*@
4008b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4009d763cef2SBarry Smith 
4010d763cef2SBarry Smith    Not Collective
4011d763cef2SBarry Smith 
4012d763cef2SBarry Smith    Input Parameter:
4013d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4014d763cef2SBarry Smith 
4015d763cef2SBarry Smith    Output Parameter:
401619eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4017d763cef2SBarry Smith 
4018d763cef2SBarry Smith    Level: beginner
4019d763cef2SBarry Smith 
4020b8123daeSJed Brown    Note:
4021b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40229be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4023b8123daeSJed Brown 
4024*8f199f4dSPatrick Sanan .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber()
4025d763cef2SBarry Smith 
4026d763cef2SBarry Smith @*/
40277087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4028d763cef2SBarry Smith {
4029d763cef2SBarry Smith   PetscFunctionBegin;
40300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4031f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4032d763cef2SBarry Smith   *t = ts->ptime;
4033d763cef2SBarry Smith   PetscFunctionReturn(0);
4034d763cef2SBarry Smith }
4035d763cef2SBarry Smith 
4036e5e524a1SHong Zhang /*@
4037e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4038e5e524a1SHong Zhang 
4039e5e524a1SHong Zhang    Not Collective
4040e5e524a1SHong Zhang 
4041e5e524a1SHong Zhang    Input Parameter:
4042e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4043e5e524a1SHong Zhang 
4044e5e524a1SHong Zhang    Output Parameter:
4045e5e524a1SHong Zhang .  t  - the previous time
4046e5e524a1SHong Zhang 
4047e5e524a1SHong Zhang    Level: beginner
4048e5e524a1SHong Zhang 
4049aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4050e5e524a1SHong Zhang 
4051e5e524a1SHong Zhang @*/
4052e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4053e5e524a1SHong Zhang {
4054e5e524a1SHong Zhang   PetscFunctionBegin;
4055e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4056e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4057e5e524a1SHong Zhang   *t = ts->ptime_prev;
4058e5e524a1SHong Zhang   PetscFunctionReturn(0);
4059e5e524a1SHong Zhang }
4060e5e524a1SHong Zhang 
40616a4d4014SLisandro Dalcin /*@
40626a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40636a4d4014SLisandro Dalcin 
40643f9fe445SBarry Smith    Logically Collective on TS
40656a4d4014SLisandro Dalcin 
40666a4d4014SLisandro Dalcin    Input Parameters:
40676a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40686a4d4014SLisandro Dalcin -  time - the time
40696a4d4014SLisandro Dalcin 
40706a4d4014SLisandro Dalcin    Level: intermediate
40716a4d4014SLisandro Dalcin 
407219eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40736a4d4014SLisandro Dalcin 
40746a4d4014SLisandro Dalcin @*/
40757087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40766a4d4014SLisandro Dalcin {
40776a4d4014SLisandro Dalcin   PetscFunctionBegin;
40780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4079c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40806a4d4014SLisandro Dalcin   ts->ptime = t;
40816a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40826a4d4014SLisandro Dalcin }
40836a4d4014SLisandro Dalcin 
4084d763cef2SBarry Smith /*@C
4085d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4086d763cef2SBarry Smith    TS options in the database.
4087d763cef2SBarry Smith 
40883f9fe445SBarry Smith    Logically Collective on TS
4089d763cef2SBarry Smith 
4090d763cef2SBarry Smith    Input Parameter:
4091d763cef2SBarry Smith +  ts     - The TS context
4092d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4093d763cef2SBarry Smith 
4094d763cef2SBarry Smith    Notes:
4095d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4096d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4097d763cef2SBarry Smith    hyphen.
4098d763cef2SBarry Smith 
4099d763cef2SBarry Smith    Level: advanced
4100d763cef2SBarry Smith 
4101d763cef2SBarry Smith .seealso: TSSetFromOptions()
4102d763cef2SBarry Smith 
4103d763cef2SBarry Smith @*/
41047087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4105d763cef2SBarry Smith {
4106dfbe8321SBarry Smith   PetscErrorCode ierr;
4107089b2837SJed Brown   SNES           snes;
4108d763cef2SBarry Smith 
4109d763cef2SBarry Smith   PetscFunctionBegin;
41100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4111d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4112089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4113089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4114d763cef2SBarry Smith   PetscFunctionReturn(0);
4115d763cef2SBarry Smith }
4116d763cef2SBarry Smith 
4117d763cef2SBarry Smith /*@C
4118d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4119d763cef2SBarry Smith    TS options in the database.
4120d763cef2SBarry Smith 
41213f9fe445SBarry Smith    Logically Collective on TS
4122d763cef2SBarry Smith 
4123d763cef2SBarry Smith    Input Parameter:
4124d763cef2SBarry Smith +  ts     - The TS context
4125d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4126d763cef2SBarry Smith 
4127d763cef2SBarry Smith    Notes:
4128d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4129d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4130d763cef2SBarry Smith    hyphen.
4131d763cef2SBarry Smith 
4132d763cef2SBarry Smith    Level: advanced
4133d763cef2SBarry Smith 
4134d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4135d763cef2SBarry Smith 
4136d763cef2SBarry Smith @*/
41377087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4138d763cef2SBarry Smith {
4139dfbe8321SBarry Smith   PetscErrorCode ierr;
4140089b2837SJed Brown   SNES           snes;
4141d763cef2SBarry Smith 
4142d763cef2SBarry Smith   PetscFunctionBegin;
41430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4144d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4145089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4146089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4147d763cef2SBarry Smith   PetscFunctionReturn(0);
4148d763cef2SBarry Smith }
4149d763cef2SBarry Smith 
4150d763cef2SBarry Smith /*@C
4151d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4152d763cef2SBarry Smith    TS options in the database.
4153d763cef2SBarry Smith 
4154d763cef2SBarry Smith    Not Collective
4155d763cef2SBarry Smith 
4156d763cef2SBarry Smith    Input Parameter:
4157d763cef2SBarry Smith .  ts - The TS context
4158d763cef2SBarry Smith 
4159d763cef2SBarry Smith    Output Parameter:
4160d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4161d763cef2SBarry Smith 
416295452b02SPatrick Sanan    Notes:
416395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4164d763cef2SBarry Smith    sufficient length to hold the prefix.
4165d763cef2SBarry Smith 
4166d763cef2SBarry Smith    Level: intermediate
4167d763cef2SBarry Smith 
4168d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4169d763cef2SBarry Smith @*/
41707087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4171d763cef2SBarry Smith {
4172dfbe8321SBarry Smith   PetscErrorCode ierr;
4173d763cef2SBarry Smith 
4174d763cef2SBarry Smith   PetscFunctionBegin;
41750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41764482741eSBarry Smith   PetscValidPointer(prefix,2);
4177d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4178d763cef2SBarry Smith   PetscFunctionReturn(0);
4179d763cef2SBarry Smith }
4180d763cef2SBarry Smith 
4181d763cef2SBarry Smith /*@C
4182d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4183d763cef2SBarry Smith 
4184d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4185d763cef2SBarry Smith 
4186d763cef2SBarry Smith    Input Parameter:
4187d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4188d763cef2SBarry Smith 
4189d763cef2SBarry Smith    Output Parameters:
4190e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4191e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4192e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4193e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4194d763cef2SBarry Smith 
419595452b02SPatrick Sanan    Notes:
419695452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4197d763cef2SBarry Smith 
4198d763cef2SBarry Smith    Level: intermediate
4199d763cef2SBarry Smith 
420080275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4201d763cef2SBarry Smith 
4202d763cef2SBarry Smith @*/
4203e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4204d763cef2SBarry Smith {
4205089b2837SJed Brown   PetscErrorCode ierr;
420624989b8cSPeter Brune   DM             dm;
4207089b2837SJed Brown 
4208d763cef2SBarry Smith   PetscFunctionBegin;
420923a57915SBarry Smith   if (Amat || Pmat) {
421023a57915SBarry Smith     SNES snes;
4211089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
421223a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4213e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
421423a57915SBarry Smith   }
421524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
421624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4217d763cef2SBarry Smith   PetscFunctionReturn(0);
4218d763cef2SBarry Smith }
4219d763cef2SBarry Smith 
42202eca1d9cSJed Brown /*@C
42212eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42222eca1d9cSJed Brown 
42232eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42242eca1d9cSJed Brown 
42252eca1d9cSJed Brown    Input Parameter:
42262eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42272eca1d9cSJed Brown 
42282eca1d9cSJed Brown    Output Parameters:
4229e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4230e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42312eca1d9cSJed Brown .  f   - The function to compute the matrices
42322eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42332eca1d9cSJed Brown 
423495452b02SPatrick Sanan    Notes:
423595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42362eca1d9cSJed Brown 
42372eca1d9cSJed Brown    Level: advanced
42382eca1d9cSJed Brown 
423980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42402eca1d9cSJed Brown 
42412eca1d9cSJed Brown @*/
4242e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42432eca1d9cSJed Brown {
4244089b2837SJed Brown   PetscErrorCode ierr;
424524989b8cSPeter Brune   DM             dm;
42460910c330SBarry Smith 
42472eca1d9cSJed Brown   PetscFunctionBegin;
4248c0aab802Sstefano_zampini   if (Amat || Pmat) {
4249c0aab802Sstefano_zampini     SNES snes;
4250089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4251f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4252e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4253c0aab802Sstefano_zampini   }
425424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
425524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
42562eca1d9cSJed Brown   PetscFunctionReturn(0);
42572eca1d9cSJed Brown }
42582eca1d9cSJed Brown 
42591713a123SBarry Smith /*@C
426083a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
42611713a123SBarry Smith    VecView() for the solution at each timestep
42621713a123SBarry Smith 
42631713a123SBarry Smith    Collective on TS
42641713a123SBarry Smith 
42651713a123SBarry Smith    Input Parameters:
42661713a123SBarry Smith +  ts - the TS context
42671713a123SBarry Smith .  step - current time-step
4268142b95e3SSatish Balay .  ptime - current time
42690298fd71SBarry Smith -  dummy - either a viewer or NULL
42701713a123SBarry Smith 
427199fdda47SBarry Smith    Options Database:
427299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
427399fdda47SBarry Smith 
427495452b02SPatrick Sanan    Notes:
427595452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
427699fdda47SBarry Smith        will look bad
427799fdda47SBarry Smith 
42781713a123SBarry Smith    Level: intermediate
42791713a123SBarry Smith 
4280a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42811713a123SBarry Smith @*/
42820910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42831713a123SBarry Smith {
4284dfbe8321SBarry Smith   PetscErrorCode   ierr;
428583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4286473a3ab2SBarry Smith   PetscDraw        draw;
42871713a123SBarry Smith 
42881713a123SBarry Smith   PetscFunctionBegin;
42896083293cSBarry Smith   if (!step && ictx->showinitial) {
42906083293cSBarry Smith     if (!ictx->initialsolution) {
42910910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
42921713a123SBarry Smith     }
42930910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
42946083293cSBarry Smith   }
4295b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42960dcf80beSBarry Smith 
42976083293cSBarry Smith   if (ictx->showinitial) {
42986083293cSBarry Smith     PetscReal pause;
42996083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
43006083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
43016083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
43026083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
43036083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
43046083293cSBarry Smith   }
43050910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4306473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
430751fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4308473a3ab2SBarry Smith     char      time[32];
4309473a3ab2SBarry Smith 
4310473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
431185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4312473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4313473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
431451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4315473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4316473a3ab2SBarry Smith   }
4317473a3ab2SBarry Smith 
43186083293cSBarry Smith   if (ictx->showinitial) {
43196083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
43206083293cSBarry Smith   }
43211713a123SBarry Smith   PetscFunctionReturn(0);
43221713a123SBarry Smith }
43231713a123SBarry Smith 
43249110b2e7SHong Zhang /*@C
43252d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43262d5ee99bSBarry Smith 
43272d5ee99bSBarry Smith    Collective on TS
43282d5ee99bSBarry Smith 
43292d5ee99bSBarry Smith    Input Parameters:
43302d5ee99bSBarry Smith +  ts - the TS context
43312d5ee99bSBarry Smith .  step - current time-step
43322d5ee99bSBarry Smith .  ptime - current time
43332d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43342d5ee99bSBarry Smith 
43352d5ee99bSBarry Smith    Level: intermediate
43362d5ee99bSBarry Smith 
43372d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43382d5ee99bSBarry Smith @*/
43392d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43402d5ee99bSBarry Smith {
43412d5ee99bSBarry Smith   PetscErrorCode    ierr;
43422d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43432d5ee99bSBarry Smith   PetscDraw         draw;
43446934998bSLisandro Dalcin   PetscDrawAxis     axis;
43452d5ee99bSBarry Smith   PetscInt          n;
43462d5ee99bSBarry Smith   PetscMPIInt       size;
43476934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43482d5ee99bSBarry Smith   char              time[32];
43492d5ee99bSBarry Smith   const PetscScalar *U;
43502d5ee99bSBarry Smith 
43512d5ee99bSBarry Smith   PetscFunctionBegin;
43526934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43536934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43542d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43556934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43562d5ee99bSBarry Smith 
43572d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43586934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43596934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43606934998bSLisandro Dalcin   if (!step) {
43616934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43626934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
43636934998bSLisandro Dalcin   }
43642d5ee99bSBarry Smith 
43652d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
43666934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
43676934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4368a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43696934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
43702d5ee99bSBarry Smith 
43716934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
43726934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
43732d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43744b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
437585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43762d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
437751fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43782d5ee99bSBarry Smith   }
43796934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
43802d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
438156d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
43826934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
43832d5ee99bSBarry Smith   PetscFunctionReturn(0);
43842d5ee99bSBarry Smith }
43852d5ee99bSBarry Smith 
43866083293cSBarry Smith /*@C
438783a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43886083293cSBarry Smith 
43896083293cSBarry Smith    Collective on TS
43906083293cSBarry Smith 
43916083293cSBarry Smith    Input Parameters:
43926083293cSBarry Smith .    ctx - the monitor context
43936083293cSBarry Smith 
43946083293cSBarry Smith    Level: intermediate
43956083293cSBarry Smith 
439683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
43976083293cSBarry Smith @*/
439883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
43996083293cSBarry Smith {
44006083293cSBarry Smith   PetscErrorCode ierr;
44016083293cSBarry Smith 
44026083293cSBarry Smith   PetscFunctionBegin;
440383a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
440483a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
440583a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44066083293cSBarry Smith   PetscFunctionReturn(0);
44076083293cSBarry Smith }
44086083293cSBarry Smith 
44096083293cSBarry Smith /*@C
441083a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
44116083293cSBarry Smith 
44126083293cSBarry Smith    Collective on TS
44136083293cSBarry Smith 
44146083293cSBarry Smith    Input Parameter:
44156083293cSBarry Smith .    ts - time-step context
44166083293cSBarry Smith 
44176083293cSBarry Smith    Output Patameter:
44186083293cSBarry Smith .    ctx - the monitor context
44196083293cSBarry Smith 
442099fdda47SBarry Smith    Options Database:
442199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
442299fdda47SBarry Smith 
44236083293cSBarry Smith    Level: intermediate
44246083293cSBarry Smith 
442583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44266083293cSBarry Smith @*/
442783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44286083293cSBarry Smith {
44296083293cSBarry Smith   PetscErrorCode   ierr;
44306083293cSBarry Smith 
44316083293cSBarry Smith   PetscFunctionBegin;
4432b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
443383a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4434e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4435bbd56ea5SKarl Rupp 
443683a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4437473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4438c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4439473a3ab2SBarry Smith 
4440473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4441c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44426083293cSBarry Smith   PetscFunctionReturn(0);
44436083293cSBarry Smith }
44446083293cSBarry Smith 
44453a471f94SBarry Smith /*@C
44460ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
44470ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
44480ed3bfb6SBarry Smith 
44490ed3bfb6SBarry Smith    Collective on TS
44500ed3bfb6SBarry Smith 
44510ed3bfb6SBarry Smith    Input Parameters:
44520ed3bfb6SBarry Smith +  ts - the TS context
44530ed3bfb6SBarry Smith .  step - current time-step
44540ed3bfb6SBarry Smith .  ptime - current time
44550ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
44560ed3bfb6SBarry Smith 
44570ed3bfb6SBarry Smith    Options Database:
44580ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44590ed3bfb6SBarry Smith 
44600ed3bfb6SBarry Smith    Level: intermediate
44610ed3bfb6SBarry Smith 
44620ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44630ed3bfb6SBarry Smith @*/
44640ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44650ed3bfb6SBarry Smith {
44660ed3bfb6SBarry Smith   PetscErrorCode   ierr;
44670ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
44680ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
44690ed3bfb6SBarry Smith   Vec              work;
44700ed3bfb6SBarry Smith 
44710ed3bfb6SBarry Smith   PetscFunctionBegin;
44720ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44730ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44740ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44750ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44760ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44770ed3bfb6SBarry Smith   PetscFunctionReturn(0);
44780ed3bfb6SBarry Smith }
44790ed3bfb6SBarry Smith 
44800ed3bfb6SBarry Smith /*@C
448183a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
44823a471f94SBarry Smith    VecView() for the error at each timestep
44833a471f94SBarry Smith 
44843a471f94SBarry Smith    Collective on TS
44853a471f94SBarry Smith 
44863a471f94SBarry Smith    Input Parameters:
44873a471f94SBarry Smith +  ts - the TS context
44883a471f94SBarry Smith .  step - current time-step
44893a471f94SBarry Smith .  ptime - current time
44900298fd71SBarry Smith -  dummy - either a viewer or NULL
44913a471f94SBarry Smith 
44920ed3bfb6SBarry Smith    Options Database:
44930ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44940ed3bfb6SBarry Smith 
44953a471f94SBarry Smith    Level: intermediate
44963a471f94SBarry Smith 
44970ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44983a471f94SBarry Smith @*/
44990910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45003a471f94SBarry Smith {
45013a471f94SBarry Smith   PetscErrorCode   ierr;
450283a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
450383a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
45043a471f94SBarry Smith   Vec              work;
45053a471f94SBarry Smith 
45063a471f94SBarry Smith   PetscFunctionBegin;
4507b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45080910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45093a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45100910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
45113a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45123a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45133a471f94SBarry Smith   PetscFunctionReturn(0);
45143a471f94SBarry Smith }
45153a471f94SBarry Smith 
4516af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
45176c699258SBarry Smith /*@
45182a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
45196c699258SBarry Smith 
4520d083f849SBarry Smith    Logically Collective on ts
45216c699258SBarry Smith 
45226c699258SBarry Smith    Input Parameters:
45232a808120SBarry Smith +  ts - the ODE integrator object
45242a808120SBarry Smith -  dm - the dm, cannot be NULL
45256c699258SBarry Smith 
4526e03a659cSJed Brown    Notes:
4527e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4528e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4529e03a659cSJed Brown    different problems using the same function space.
4530e03a659cSJed Brown 
45316c699258SBarry Smith    Level: intermediate
45326c699258SBarry Smith 
45336c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
45346c699258SBarry Smith @*/
45357087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
45366c699258SBarry Smith {
45376c699258SBarry Smith   PetscErrorCode ierr;
4538089b2837SJed Brown   SNES           snes;
4539942e3340SBarry Smith   DMTS           tsdm;
45406c699258SBarry Smith 
45416c699258SBarry Smith   PetscFunctionBegin;
45420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45432a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
454470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4545942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
45462a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4547942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4548942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
454924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
455024989b8cSPeter Brune         tsdm->originaldm = dm;
455124989b8cSPeter Brune       }
455224989b8cSPeter Brune     }
45536bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
455424989b8cSPeter Brune   }
45556c699258SBarry Smith   ts->dm = dm;
4556bbd56ea5SKarl Rupp 
4557089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4558089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
45596c699258SBarry Smith   PetscFunctionReturn(0);
45606c699258SBarry Smith }
45616c699258SBarry Smith 
45626c699258SBarry Smith /*@
45636c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
45646c699258SBarry Smith 
45653f9fe445SBarry Smith    Not Collective
45666c699258SBarry Smith 
45676c699258SBarry Smith    Input Parameter:
45686c699258SBarry Smith . ts - the preconditioner context
45696c699258SBarry Smith 
45706c699258SBarry Smith    Output Parameter:
45716c699258SBarry Smith .  dm - the dm
45726c699258SBarry Smith 
45736c699258SBarry Smith    Level: intermediate
45746c699258SBarry Smith 
45756c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
45766c699258SBarry Smith @*/
45777087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
45786c699258SBarry Smith {
4579496e6a7aSJed Brown   PetscErrorCode ierr;
4580496e6a7aSJed Brown 
45816c699258SBarry Smith   PetscFunctionBegin;
45820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4583496e6a7aSJed Brown   if (!ts->dm) {
4584ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4585496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4586496e6a7aSJed Brown   }
45876c699258SBarry Smith   *dm = ts->dm;
45886c699258SBarry Smith   PetscFunctionReturn(0);
45896c699258SBarry Smith }
45901713a123SBarry Smith 
45910f5c6efeSJed Brown /*@
45920f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45930f5c6efeSJed Brown 
45943f9fe445SBarry Smith    Logically Collective on SNES
45950f5c6efeSJed Brown 
45960f5c6efeSJed Brown    Input Parameter:
4597d42a1c89SJed Brown + snes - nonlinear solver
45980910c330SBarry Smith . U - the current state at which to evaluate the residual
4599d42a1c89SJed Brown - ctx - user context, must be a TS
46000f5c6efeSJed Brown 
46010f5c6efeSJed Brown    Output Parameter:
46020f5c6efeSJed Brown . F - the nonlinear residual
46030f5c6efeSJed Brown 
46040f5c6efeSJed Brown    Notes:
46050f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46060f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
46070f5c6efeSJed Brown 
46080f5c6efeSJed Brown    Level: advanced
46090f5c6efeSJed Brown 
46100f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
46110f5c6efeSJed Brown @*/
46120910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
46130f5c6efeSJed Brown {
46140f5c6efeSJed Brown   TS             ts = (TS)ctx;
46150f5c6efeSJed Brown   PetscErrorCode ierr;
46160f5c6efeSJed Brown 
46170f5c6efeSJed Brown   PetscFunctionBegin;
46180f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46190910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46200f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46210f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46220910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46230f5c6efeSJed Brown   PetscFunctionReturn(0);
46240f5c6efeSJed Brown }
46250f5c6efeSJed Brown 
46260f5c6efeSJed Brown /*@
46270f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
46280f5c6efeSJed Brown 
46290f5c6efeSJed Brown    Collective on SNES
46300f5c6efeSJed Brown 
46310f5c6efeSJed Brown    Input Parameter:
46320f5c6efeSJed Brown + snes - nonlinear solver
46330910c330SBarry Smith . U - the current state at which to evaluate the residual
46340f5c6efeSJed Brown - ctx - user context, must be a TS
46350f5c6efeSJed Brown 
46360f5c6efeSJed Brown    Output Parameter:
46370f5c6efeSJed Brown + A - the Jacobian
46380f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
46390f5c6efeSJed Brown - flag - indicates any structure change in the matrix
46400f5c6efeSJed Brown 
46410f5c6efeSJed Brown    Notes:
46420f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46430f5c6efeSJed Brown 
46440f5c6efeSJed Brown    Level: developer
46450f5c6efeSJed Brown 
46460f5c6efeSJed Brown .seealso: SNESSetJacobian()
46470f5c6efeSJed Brown @*/
4648d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46490f5c6efeSJed Brown {
46500f5c6efeSJed Brown   TS             ts = (TS)ctx;
46510f5c6efeSJed Brown   PetscErrorCode ierr;
46520f5c6efeSJed Brown 
46530f5c6efeSJed Brown   PetscFunctionBegin;
46540f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46550910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46560f5c6efeSJed Brown   PetscValidPointer(A,3);
465794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
46580f5c6efeSJed Brown   PetscValidPointer(B,4);
465994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
46600f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4661d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
46620f5c6efeSJed Brown   PetscFunctionReturn(0);
46630f5c6efeSJed Brown }
4664325fc9f4SBarry Smith 
46650e4ef248SJed Brown /*@C
46669ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
46670e4ef248SJed Brown 
46680e4ef248SJed Brown    Collective on TS
46690e4ef248SJed Brown 
46700e4ef248SJed Brown    Input Arguments:
46710e4ef248SJed Brown +  ts - time stepping context
46720e4ef248SJed Brown .  t - time at which to evaluate
46730910c330SBarry Smith .  U - state at which to evaluate
46740e4ef248SJed Brown -  ctx - context
46750e4ef248SJed Brown 
46760e4ef248SJed Brown    Output Arguments:
46770e4ef248SJed Brown .  F - right hand side
46780e4ef248SJed Brown 
46790e4ef248SJed Brown    Level: intermediate
46800e4ef248SJed Brown 
46810e4ef248SJed Brown    Notes:
46820e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
46830e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
46840e4ef248SJed Brown 
46850e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
46860e4ef248SJed Brown @*/
46870910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46880e4ef248SJed Brown {
46890e4ef248SJed Brown   PetscErrorCode ierr;
46900e4ef248SJed Brown   Mat            Arhs,Brhs;
46910e4ef248SJed Brown 
46920e4ef248SJed Brown   PetscFunctionBegin;
46930e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4694d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46950910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46960e4ef248SJed Brown   PetscFunctionReturn(0);
46970e4ef248SJed Brown }
46980e4ef248SJed Brown 
46990e4ef248SJed Brown /*@C
47000e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
47010e4ef248SJed Brown 
47020e4ef248SJed Brown    Collective on TS
47030e4ef248SJed Brown 
47040e4ef248SJed Brown    Input Arguments:
47050e4ef248SJed Brown +  ts - time stepping context
47060e4ef248SJed Brown .  t - time at which to evaluate
47070910c330SBarry Smith .  U - state at which to evaluate
47080e4ef248SJed Brown -  ctx - context
47090e4ef248SJed Brown 
47100e4ef248SJed Brown    Output Arguments:
47110e4ef248SJed Brown +  A - pointer to operator
47120e4ef248SJed Brown .  B - pointer to preconditioning matrix
47130e4ef248SJed Brown -  flg - matrix structure flag
47140e4ef248SJed Brown 
47150e4ef248SJed Brown    Level: intermediate
47160e4ef248SJed Brown 
47170e4ef248SJed Brown    Notes:
47180e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47190e4ef248SJed Brown 
47200e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47210e4ef248SJed Brown @*/
4722d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47230e4ef248SJed Brown {
47240e4ef248SJed Brown   PetscFunctionBegin;
47250e4ef248SJed Brown   PetscFunctionReturn(0);
47260e4ef248SJed Brown }
47270e4ef248SJed Brown 
47280026cea9SSean Farley /*@C
47290026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
47300026cea9SSean Farley 
47310026cea9SSean Farley    Collective on TS
47320026cea9SSean Farley 
47330026cea9SSean Farley    Input Arguments:
47340026cea9SSean Farley +  ts - time stepping context
47350026cea9SSean Farley .  t - time at which to evaluate
47360910c330SBarry Smith .  U - state at which to evaluate
47370910c330SBarry Smith .  Udot - time derivative of state vector
47380026cea9SSean Farley -  ctx - context
47390026cea9SSean Farley 
47400026cea9SSean Farley    Output Arguments:
47410026cea9SSean Farley .  F - left hand side
47420026cea9SSean Farley 
47430026cea9SSean Farley    Level: intermediate
47440026cea9SSean Farley 
47450026cea9SSean Farley    Notes:
47460910c330SBarry 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
47470026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47480026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47490026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47500026cea9SSean Farley 
47519ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47529ae8fd06SBarry Smith 
47539ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47540026cea9SSean Farley @*/
47550910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
47560026cea9SSean Farley {
47570026cea9SSean Farley   PetscErrorCode ierr;
47580026cea9SSean Farley   Mat            A,B;
47590026cea9SSean Farley 
47600026cea9SSean Farley   PetscFunctionBegin;
47610298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4762d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
47630910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
47640026cea9SSean Farley   PetscFunctionReturn(0);
47650026cea9SSean Farley }
47660026cea9SSean Farley 
47670026cea9SSean Farley /*@C
4768b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
47690026cea9SSean Farley 
47700026cea9SSean Farley    Collective on TS
47710026cea9SSean Farley 
47720026cea9SSean Farley    Input Arguments:
47730026cea9SSean Farley +  ts - time stepping context
47740026cea9SSean Farley .  t - time at which to evaluate
47750910c330SBarry Smith .  U - state at which to evaluate
47760910c330SBarry Smith .  Udot - time derivative of state vector
47770026cea9SSean Farley .  shift - shift to apply
47780026cea9SSean Farley -  ctx - context
47790026cea9SSean Farley 
47800026cea9SSean Farley    Output Arguments:
47810026cea9SSean Farley +  A - pointer to operator
47820026cea9SSean Farley .  B - pointer to preconditioning matrix
47830026cea9SSean Farley -  flg - matrix structure flag
47840026cea9SSean Farley 
4785b41af12eSJed Brown    Level: advanced
47860026cea9SSean Farley 
47870026cea9SSean Farley    Notes:
47880026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47890026cea9SSean Farley 
4790b41af12eSJed Brown    It is only appropriate for problems of the form
4791b41af12eSJed Brown 
4792b41af12eSJed Brown $     M Udot = F(U,t)
4793b41af12eSJed Brown 
4794b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4795b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4796b41af12eSJed Brown   an implicit operator of the form
4797b41af12eSJed Brown 
4798b41af12eSJed Brown $    shift*M + J
4799b41af12eSJed Brown 
4800b41af12eSJed 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
4801b41af12eSJed Brown   a copy of M or reassemble it when requested.
4802b41af12eSJed Brown 
48030026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
48040026cea9SSean Farley @*/
4805d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
48060026cea9SSean Farley {
4807b41af12eSJed Brown   PetscErrorCode ierr;
4808b41af12eSJed Brown 
48090026cea9SSean Farley   PetscFunctionBegin;
481094ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4811b41af12eSJed Brown   ts->ijacobian.shift = shift;
48120026cea9SSean Farley   PetscFunctionReturn(0);
48130026cea9SSean Farley }
4814b41af12eSJed Brown 
4815e817cc15SEmil Constantinescu /*@
4816e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4817e817cc15SEmil Constantinescu 
4818e817cc15SEmil Constantinescu    Not Collective
4819e817cc15SEmil Constantinescu 
4820e817cc15SEmil Constantinescu    Input Parameter:
4821e817cc15SEmil Constantinescu .  ts - the TS context
4822e817cc15SEmil Constantinescu 
4823e817cc15SEmil Constantinescu    Output Parameter:
48244e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4825e817cc15SEmil Constantinescu 
4826e817cc15SEmil Constantinescu    Level: beginner
4827e817cc15SEmil Constantinescu 
4828e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4829e817cc15SEmil Constantinescu @*/
4830e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4831e817cc15SEmil Constantinescu {
4832e817cc15SEmil Constantinescu   PetscFunctionBegin;
4833e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4834e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4835e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4836e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4837e817cc15SEmil Constantinescu }
4838e817cc15SEmil Constantinescu 
4839e817cc15SEmil Constantinescu /*@
4840e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4841e817cc15SEmil Constantinescu 
4842e817cc15SEmil Constantinescu    Not Collective
4843e817cc15SEmil Constantinescu 
4844e817cc15SEmil Constantinescu    Input Parameter:
4845e817cc15SEmil Constantinescu +  ts - the TS context
48461297b224SEmil Constantinescu -  equation_type - see TSEquationType
4847e817cc15SEmil Constantinescu 
4848e817cc15SEmil Constantinescu    Level: advanced
4849e817cc15SEmil Constantinescu 
4850e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4851e817cc15SEmil Constantinescu @*/
4852e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4853e817cc15SEmil Constantinescu {
4854e817cc15SEmil Constantinescu   PetscFunctionBegin;
4855e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4856e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4857e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4858e817cc15SEmil Constantinescu }
48590026cea9SSean Farley 
48604af1b03aSJed Brown /*@
48614af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
48624af1b03aSJed Brown 
48634af1b03aSJed Brown    Not Collective
48644af1b03aSJed Brown 
48654af1b03aSJed Brown    Input Parameter:
48664af1b03aSJed Brown .  ts - the TS context
48674af1b03aSJed Brown 
48684af1b03aSJed Brown    Output Parameter:
48694af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
48704af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
48714af1b03aSJed Brown 
4872487e0bb9SJed Brown    Level: beginner
48734af1b03aSJed Brown 
4874cd652676SJed Brown    Notes:
4875cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48764af1b03aSJed Brown 
48774af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48784af1b03aSJed Brown @*/
48794af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48804af1b03aSJed Brown {
48814af1b03aSJed Brown   PetscFunctionBegin;
48824af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48834af1b03aSJed Brown   PetscValidPointer(reason,2);
48844af1b03aSJed Brown   *reason = ts->reason;
48854af1b03aSJed Brown   PetscFunctionReturn(0);
48864af1b03aSJed Brown }
48874af1b03aSJed Brown 
4888d6ad946cSShri Abhyankar /*@
4889d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4890d6ad946cSShri Abhyankar 
48916b221cbeSPatrick Sanan    Logically Collective; reason must contain common value
4892d6ad946cSShri Abhyankar 
48936b221cbeSPatrick Sanan    Input Parameters:
4894d6ad946cSShri Abhyankar +  ts - the TS context
48956b221cbeSPatrick Sanan -  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4896d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4897d6ad946cSShri Abhyankar 
4898f5abba47SShri Abhyankar    Level: advanced
4899d6ad946cSShri Abhyankar 
4900d6ad946cSShri Abhyankar    Notes:
49016b221cbeSPatrick Sanan    Can only be called while TSSolve() is active.
4902d6ad946cSShri Abhyankar 
4903d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4904d6ad946cSShri Abhyankar @*/
4905d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4906d6ad946cSShri Abhyankar {
4907d6ad946cSShri Abhyankar   PetscFunctionBegin;
4908d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4909d6ad946cSShri Abhyankar   ts->reason = reason;
4910d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4911d6ad946cSShri Abhyankar }
4912d6ad946cSShri Abhyankar 
4913cc708dedSBarry Smith /*@
4914cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4915cc708dedSBarry Smith 
4916cc708dedSBarry Smith    Not Collective
4917cc708dedSBarry Smith 
4918cc708dedSBarry Smith    Input Parameter:
4919cc708dedSBarry Smith .  ts - the TS context
4920cc708dedSBarry Smith 
4921cc708dedSBarry Smith    Output Parameter:
492219eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4923cc708dedSBarry Smith 
4924487e0bb9SJed Brown    Level: beginner
4925cc708dedSBarry Smith 
4926cc708dedSBarry Smith    Notes:
4927cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4928cc708dedSBarry Smith 
4929cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4930cc708dedSBarry Smith @*/
4931cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4932cc708dedSBarry Smith {
4933cc708dedSBarry Smith   PetscFunctionBegin;
4934cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4935cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4936cc708dedSBarry Smith   *ftime = ts->solvetime;
4937cc708dedSBarry Smith   PetscFunctionReturn(0);
4938cc708dedSBarry Smith }
4939cc708dedSBarry Smith 
49402c18e0fdSBarry Smith /*@
49415ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49429f67acb7SJed Brown    used by the time integrator.
49439f67acb7SJed Brown 
49449f67acb7SJed Brown    Not Collective
49459f67acb7SJed Brown 
49469f67acb7SJed Brown    Input Parameter:
49479f67acb7SJed Brown .  ts - TS context
49489f67acb7SJed Brown 
49499f67acb7SJed Brown    Output Parameter:
49509f67acb7SJed Brown .  nits - number of nonlinear iterations
49519f67acb7SJed Brown 
49529f67acb7SJed Brown    Notes:
49539f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49549f67acb7SJed Brown 
49559f67acb7SJed Brown    Level: intermediate
49569f67acb7SJed Brown 
49575ef26d82SJed Brown .seealso:  TSGetKSPIterations()
49589f67acb7SJed Brown @*/
49595ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
49609f67acb7SJed Brown {
49619f67acb7SJed Brown   PetscFunctionBegin;
49629f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49639f67acb7SJed Brown   PetscValidIntPointer(nits,2);
49645ef26d82SJed Brown   *nits = ts->snes_its;
49659f67acb7SJed Brown   PetscFunctionReturn(0);
49669f67acb7SJed Brown }
49679f67acb7SJed Brown 
49689f67acb7SJed Brown /*@
49695ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
49709f67acb7SJed Brown    used by the time integrator.
49719f67acb7SJed Brown 
49729f67acb7SJed Brown    Not Collective
49739f67acb7SJed Brown 
49749f67acb7SJed Brown    Input Parameter:
49759f67acb7SJed Brown .  ts - TS context
49769f67acb7SJed Brown 
49779f67acb7SJed Brown    Output Parameter:
49789f67acb7SJed Brown .  lits - number of linear iterations
49799f67acb7SJed Brown 
49809f67acb7SJed Brown    Notes:
49819f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49829f67acb7SJed Brown 
49839f67acb7SJed Brown    Level: intermediate
49849f67acb7SJed Brown 
49855ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
49869f67acb7SJed Brown @*/
49875ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
49889f67acb7SJed Brown {
49899f67acb7SJed Brown   PetscFunctionBegin;
49909f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49919f67acb7SJed Brown   PetscValidIntPointer(lits,2);
49925ef26d82SJed Brown   *lits = ts->ksp_its;
49939f67acb7SJed Brown   PetscFunctionReturn(0);
49949f67acb7SJed Brown }
49959f67acb7SJed Brown 
4996cef5090cSJed Brown /*@
4997cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4998cef5090cSJed Brown 
4999cef5090cSJed Brown    Not Collective
5000cef5090cSJed Brown 
5001cef5090cSJed Brown    Input Parameter:
5002cef5090cSJed Brown .  ts - TS context
5003cef5090cSJed Brown 
5004cef5090cSJed Brown    Output Parameter:
5005cef5090cSJed Brown .  rejects - number of steps rejected
5006cef5090cSJed Brown 
5007cef5090cSJed Brown    Notes:
5008cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5009cef5090cSJed Brown 
5010cef5090cSJed Brown    Level: intermediate
5011cef5090cSJed Brown 
50125ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5013cef5090cSJed Brown @*/
5014cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5015cef5090cSJed Brown {
5016cef5090cSJed Brown   PetscFunctionBegin;
5017cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5018cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5019cef5090cSJed Brown   *rejects = ts->reject;
5020cef5090cSJed Brown   PetscFunctionReturn(0);
5021cef5090cSJed Brown }
5022cef5090cSJed Brown 
5023cef5090cSJed Brown /*@
5024cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5025cef5090cSJed Brown 
5026cef5090cSJed Brown    Not Collective
5027cef5090cSJed Brown 
5028cef5090cSJed Brown    Input Parameter:
5029cef5090cSJed Brown .  ts - TS context
5030cef5090cSJed Brown 
5031cef5090cSJed Brown    Output Parameter:
5032cef5090cSJed Brown .  fails - number of failed nonlinear solves
5033cef5090cSJed Brown 
5034cef5090cSJed Brown    Notes:
5035cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5036cef5090cSJed Brown 
5037cef5090cSJed Brown    Level: intermediate
5038cef5090cSJed Brown 
50395ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5040cef5090cSJed Brown @*/
5041cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5042cef5090cSJed Brown {
5043cef5090cSJed Brown   PetscFunctionBegin;
5044cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5045cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5046cef5090cSJed Brown   *fails = ts->num_snes_failures;
5047cef5090cSJed Brown   PetscFunctionReturn(0);
5048cef5090cSJed Brown }
5049cef5090cSJed Brown 
5050cef5090cSJed Brown /*@
5051cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5052cef5090cSJed Brown 
5053cef5090cSJed Brown    Not Collective
5054cef5090cSJed Brown 
5055cef5090cSJed Brown    Input Parameter:
5056cef5090cSJed Brown +  ts - TS context
5057cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5058cef5090cSJed Brown 
5059cef5090cSJed Brown    Notes:
5060cef5090cSJed Brown    The counter is reset to zero for each step
5061cef5090cSJed Brown 
5062cef5090cSJed Brown    Options Database Key:
5063cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5064cef5090cSJed Brown 
5065cef5090cSJed Brown    Level: intermediate
5066cef5090cSJed Brown 
50675ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5068cef5090cSJed Brown @*/
5069cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5070cef5090cSJed Brown {
5071cef5090cSJed Brown   PetscFunctionBegin;
5072cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5073cef5090cSJed Brown   ts->max_reject = rejects;
5074cef5090cSJed Brown   PetscFunctionReturn(0);
5075cef5090cSJed Brown }
5076cef5090cSJed Brown 
5077cef5090cSJed Brown /*@
5078cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5079cef5090cSJed Brown 
5080cef5090cSJed Brown    Not Collective
5081cef5090cSJed Brown 
5082cef5090cSJed Brown    Input Parameter:
5083cef5090cSJed Brown +  ts - TS context
5084cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5085cef5090cSJed Brown 
5086cef5090cSJed Brown    Notes:
5087cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5088cef5090cSJed Brown 
5089cef5090cSJed Brown    Options Database Key:
5090cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5091cef5090cSJed Brown 
5092cef5090cSJed Brown    Level: intermediate
5093cef5090cSJed Brown 
50945ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5095cef5090cSJed Brown @*/
5096cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5097cef5090cSJed Brown {
5098cef5090cSJed Brown   PetscFunctionBegin;
5099cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5100cef5090cSJed Brown   ts->max_snes_failures = fails;
5101cef5090cSJed Brown   PetscFunctionReturn(0);
5102cef5090cSJed Brown }
5103cef5090cSJed Brown 
5104cef5090cSJed Brown /*@
5105cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5106cef5090cSJed Brown 
5107cef5090cSJed Brown    Not Collective
5108cef5090cSJed Brown 
5109cef5090cSJed Brown    Input Parameter:
5110cef5090cSJed Brown +  ts - TS context
5111cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5112cef5090cSJed Brown 
5113cef5090cSJed Brown    Options Database Key:
5114cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5115cef5090cSJed Brown 
5116cef5090cSJed Brown    Level: intermediate
5117cef5090cSJed Brown 
51185ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5119cef5090cSJed Brown @*/
5120cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5121cef5090cSJed Brown {
5122cef5090cSJed Brown   PetscFunctionBegin;
5123cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5124cef5090cSJed Brown   ts->errorifstepfailed = err;
5125cef5090cSJed Brown   PetscFunctionReturn(0);
5126cef5090cSJed Brown }
5127cef5090cSJed Brown 
5128fb1732b5SBarry Smith /*@C
5129fde5950dSBarry 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
5130fb1732b5SBarry Smith 
5131fb1732b5SBarry Smith    Collective on TS
5132fb1732b5SBarry Smith 
5133fb1732b5SBarry Smith    Input Parameters:
5134fb1732b5SBarry Smith +  ts - the TS context
5135fb1732b5SBarry Smith .  step - current time-step
5136fb1732b5SBarry Smith .  ptime - current time
51370910c330SBarry Smith .  u - current state
5138721cd6eeSBarry Smith -  vf - viewer and its format
5139fb1732b5SBarry Smith 
5140fb1732b5SBarry Smith    Level: intermediate
5141fb1732b5SBarry Smith 
5142fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5143fb1732b5SBarry Smith @*/
5144721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5145fb1732b5SBarry Smith {
5146fb1732b5SBarry Smith   PetscErrorCode ierr;
5147fb1732b5SBarry Smith 
5148fb1732b5SBarry Smith   PetscFunctionBegin;
5149721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5150721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5151721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5152ed81e22dSJed Brown   PetscFunctionReturn(0);
5153ed81e22dSJed Brown }
5154ed81e22dSJed Brown 
5155ed81e22dSJed Brown /*@C
5156ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5157ed81e22dSJed Brown 
5158ed81e22dSJed Brown    Collective on TS
5159ed81e22dSJed Brown 
5160ed81e22dSJed Brown    Input Parameters:
5161ed81e22dSJed Brown +  ts - the TS context
5162ed81e22dSJed Brown .  step - current time-step
5163ed81e22dSJed Brown .  ptime - current time
51640910c330SBarry Smith .  u - current state
5165ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5166ed81e22dSJed Brown 
5167ed81e22dSJed Brown    Level: intermediate
5168ed81e22dSJed Brown 
5169ed81e22dSJed Brown    Notes:
5170ed81e22dSJed 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.
5171ed81e22dSJed Brown    These are named according to the file name template.
5172ed81e22dSJed Brown 
5173ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5174ed81e22dSJed Brown 
5175ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5176ed81e22dSJed Brown @*/
51770910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5178ed81e22dSJed Brown {
5179ed81e22dSJed Brown   PetscErrorCode ierr;
5180ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5181ed81e22dSJed Brown   PetscViewer    viewer;
5182ed81e22dSJed Brown 
5183ed81e22dSJed Brown   PetscFunctionBegin;
518463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
51858caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5186ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
51870910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5188ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5189ed81e22dSJed Brown   PetscFunctionReturn(0);
5190ed81e22dSJed Brown }
5191ed81e22dSJed Brown 
5192ed81e22dSJed Brown /*@C
5193ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5194ed81e22dSJed Brown 
5195ed81e22dSJed Brown    Collective on TS
5196ed81e22dSJed Brown 
5197ed81e22dSJed Brown    Input Parameters:
5198ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5199ed81e22dSJed Brown 
5200ed81e22dSJed Brown    Level: intermediate
5201ed81e22dSJed Brown 
5202ed81e22dSJed Brown    Note:
5203ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5204ed81e22dSJed Brown 
5205ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5206ed81e22dSJed Brown @*/
5207ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5208ed81e22dSJed Brown {
5209ed81e22dSJed Brown   PetscErrorCode ierr;
5210ed81e22dSJed Brown 
5211ed81e22dSJed Brown   PetscFunctionBegin;
5212ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5213fb1732b5SBarry Smith   PetscFunctionReturn(0);
5214fb1732b5SBarry Smith }
5215fb1732b5SBarry Smith 
521684df9cb4SJed Brown /*@
5217552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
521884df9cb4SJed Brown 
5219ed81e22dSJed Brown    Collective on TS if controller has not been created yet
522084df9cb4SJed Brown 
522184df9cb4SJed Brown    Input Arguments:
5222ed81e22dSJed Brown .  ts - time stepping context
522384df9cb4SJed Brown 
522484df9cb4SJed Brown    Output Arguments:
5225ed81e22dSJed Brown .  adapt - adaptive controller
522684df9cb4SJed Brown 
522784df9cb4SJed Brown    Level: intermediate
522884df9cb4SJed Brown 
5229ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
523084df9cb4SJed Brown @*/
5231552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
523284df9cb4SJed Brown {
523384df9cb4SJed Brown   PetscErrorCode ierr;
523484df9cb4SJed Brown 
523584df9cb4SJed Brown   PetscFunctionBegin;
523684df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5237bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
523884df9cb4SJed Brown   if (!ts->adapt) {
5239ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
52403bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
52411c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
524284df9cb4SJed Brown   }
5243bec58848SLisandro Dalcin   *adapt = ts->adapt;
524484df9cb4SJed Brown   PetscFunctionReturn(0);
524584df9cb4SJed Brown }
5246d6ebe24aSShri Abhyankar 
52471c3436cfSJed Brown /*@
52481c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
52491c3436cfSJed Brown 
52501c3436cfSJed Brown    Logically Collective
52511c3436cfSJed Brown 
52521c3436cfSJed Brown    Input Arguments:
52531c3436cfSJed Brown +  ts - time integration context
52541c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
52550298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
52561c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
52570298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
52581c3436cfSJed Brown 
5259a3cdaa26SBarry Smith    Options Database keys:
5260a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5261a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5262a3cdaa26SBarry Smith 
52633ff766beSShri Abhyankar    Notes:
52643ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
52653ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
52663ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
52673ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
52683ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
52693ff766beSShri Abhyankar    differential variables.
52703ff766beSShri Abhyankar 
52711c3436cfSJed Brown    Level: beginner
52721c3436cfSJed Brown 
5273c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
52741c3436cfSJed Brown @*/
52751c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
52761c3436cfSJed Brown {
52771c3436cfSJed Brown   PetscErrorCode ierr;
52781c3436cfSJed Brown 
52791c3436cfSJed Brown   PetscFunctionBegin;
5280c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
52811c3436cfSJed Brown   if (vatol) {
52821c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
52831c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
52841c3436cfSJed Brown     ts->vatol = vatol;
52851c3436cfSJed Brown   }
5286c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
52871c3436cfSJed Brown   if (vrtol) {
52881c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
52891c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
52901c3436cfSJed Brown     ts->vrtol = vrtol;
52911c3436cfSJed Brown   }
52921c3436cfSJed Brown   PetscFunctionReturn(0);
52931c3436cfSJed Brown }
52941c3436cfSJed Brown 
5295c5033834SJed Brown /*@
5296c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5297c5033834SJed Brown 
5298c5033834SJed Brown    Logically Collective
5299c5033834SJed Brown 
5300c5033834SJed Brown    Input Arguments:
5301c5033834SJed Brown .  ts - time integration context
5302c5033834SJed Brown 
5303c5033834SJed Brown    Output Arguments:
53040298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53050298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53060298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53070298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5308c5033834SJed Brown 
5309c5033834SJed Brown    Level: beginner
5310c5033834SJed Brown 
5311c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5312c5033834SJed Brown @*/
5313c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5314c5033834SJed Brown {
5315c5033834SJed Brown   PetscFunctionBegin;
5316c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5317c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5318c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5319c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5320c5033834SJed Brown   PetscFunctionReturn(0);
5321c5033834SJed Brown }
5322c5033834SJed Brown 
53239c6b16b5SShri Abhyankar /*@
5324a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
53259c6b16b5SShri Abhyankar 
53269c6b16b5SShri Abhyankar    Collective on TS
53279c6b16b5SShri Abhyankar 
53289c6b16b5SShri Abhyankar    Input Arguments:
53299c6b16b5SShri Abhyankar +  ts - time stepping context
5330a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5331a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
53329c6b16b5SShri Abhyankar 
53339c6b16b5SShri Abhyankar    Output Arguments:
53347453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
53357453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
53367453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
53379c6b16b5SShri Abhyankar 
53389c6b16b5SShri Abhyankar    Level: developer
53399c6b16b5SShri Abhyankar 
5340deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
53419c6b16b5SShri Abhyankar @*/
53427453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53439c6b16b5SShri Abhyankar {
53449c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
53459c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
53467453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
53477453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
53489c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
53497453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
53507453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
53517453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
53529c6b16b5SShri Abhyankar 
53539c6b16b5SShri Abhyankar   PetscFunctionBegin;
53549c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5355a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5356a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5357a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5358a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5359a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5360a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
53618a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
53628a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5363a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
53649c6b16b5SShri Abhyankar 
53659c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
53669c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
53679c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
53689c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
53699c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
53707453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
53717453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
53727453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
53739c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
53749c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
53759c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53769c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53779c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
537876cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
53797453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
53807453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
53817453f775SEmil Constantinescu       if(tola>0.){
53827453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
53837453f775SEmil Constantinescu         na_loc++;
53847453f775SEmil Constantinescu       }
53857453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53867453f775SEmil Constantinescu       if(tolr>0.){
53877453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
53887453f775SEmil Constantinescu         nr_loc++;
53897453f775SEmil Constantinescu       }
53907453f775SEmil Constantinescu       tol=tola+tolr;
53917453f775SEmil Constantinescu       if(tol>0.){
53927453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
53937453f775SEmil Constantinescu         n_loc++;
53947453f775SEmil Constantinescu       }
53959c6b16b5SShri Abhyankar     }
53969c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53989c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
53999c6b16b5SShri Abhyankar     const PetscScalar *atol;
54009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54019c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
540276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54037453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54047453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54057453f775SEmil Constantinescu       if(tola>0.){
54067453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54077453f775SEmil Constantinescu         na_loc++;
54087453f775SEmil Constantinescu       }
54097453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54107453f775SEmil Constantinescu       if(tolr>0.){
54117453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54127453f775SEmil Constantinescu         nr_loc++;
54137453f775SEmil Constantinescu       }
54147453f775SEmil Constantinescu       tol=tola+tolr;
54157453f775SEmil Constantinescu       if(tol>0.){
54167453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54177453f775SEmil Constantinescu         n_loc++;
54187453f775SEmil Constantinescu       }
54199c6b16b5SShri Abhyankar     }
54209c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54219c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54229c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54239c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54249c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
542576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54267453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54277453f775SEmil Constantinescu       tola = ts->atol;
54287453f775SEmil Constantinescu       if(tola>0.){
54297453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54307453f775SEmil Constantinescu         na_loc++;
54317453f775SEmil Constantinescu       }
54327453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54337453f775SEmil Constantinescu       if(tolr>0.){
54347453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54357453f775SEmil Constantinescu         nr_loc++;
54367453f775SEmil Constantinescu       }
54377453f775SEmil Constantinescu       tol=tola+tolr;
54387453f775SEmil Constantinescu       if(tol>0.){
54397453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54407453f775SEmil Constantinescu         n_loc++;
54417453f775SEmil Constantinescu       }
54429c6b16b5SShri Abhyankar     }
54439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54449c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54459c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
544676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
54477453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54487453f775SEmil Constantinescu       tola = ts->atol;
54497453f775SEmil Constantinescu       if(tola>0.){
54507453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54517453f775SEmil Constantinescu         na_loc++;
54527453f775SEmil Constantinescu       }
54537453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54547453f775SEmil Constantinescu       if(tolr>0.){
54557453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54567453f775SEmil Constantinescu         nr_loc++;
54577453f775SEmil Constantinescu       }
54587453f775SEmil Constantinescu       tol=tola+tolr;
54597453f775SEmil Constantinescu       if(tol>0.){
54607453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54617453f775SEmil Constantinescu         n_loc++;
54627453f775SEmil Constantinescu       }
54639c6b16b5SShri Abhyankar     }
54649c6b16b5SShri Abhyankar   }
54659c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54669c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54679c6b16b5SShri Abhyankar 
54687453f775SEmil Constantinescu   err_loc[0] = sum;
54697453f775SEmil Constantinescu   err_loc[1] = suma;
54707453f775SEmil Constantinescu   err_loc[2] = sumr;
54717453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
54727453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
54737453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
54747453f775SEmil Constantinescu 
5475a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54767453f775SEmil Constantinescu 
54777453f775SEmil Constantinescu   gsum   = err_glb[0];
54787453f775SEmil Constantinescu   gsuma  = err_glb[1];
54797453f775SEmil Constantinescu   gsumr  = err_glb[2];
54807453f775SEmil Constantinescu   n_glb  = err_glb[3];
54817453f775SEmil Constantinescu   na_glb = err_glb[4];
54827453f775SEmil Constantinescu   nr_glb = err_glb[5];
54837453f775SEmil Constantinescu 
5484b1316ef9SEmil Constantinescu   *norm  = 0.;
5485b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5486b1316ef9SEmil Constantinescu   *norma = 0.;
5487b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5488b1316ef9SEmil Constantinescu   *normr = 0.;
5489b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
54909c6b16b5SShri Abhyankar 
54919c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
54927453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
54937453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
54949c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
54959c6b16b5SShri Abhyankar }
54969c6b16b5SShri Abhyankar 
54979c6b16b5SShri Abhyankar /*@
5498a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
54999c6b16b5SShri Abhyankar 
55009c6b16b5SShri Abhyankar    Collective on TS
55019c6b16b5SShri Abhyankar 
55029c6b16b5SShri Abhyankar    Input Arguments:
55039c6b16b5SShri Abhyankar +  ts - time stepping context
5504a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5505a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55069c6b16b5SShri Abhyankar 
55079c6b16b5SShri Abhyankar    Output Arguments:
55087453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55097453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
55107453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55119c6b16b5SShri Abhyankar 
55129c6b16b5SShri Abhyankar    Level: developer
55139c6b16b5SShri Abhyankar 
5514deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
55159c6b16b5SShri Abhyankar @*/
55167453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55179c6b16b5SShri Abhyankar {
55189c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55197453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
55209c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55217453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
55227453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
55237453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55249c6b16b5SShri Abhyankar 
55259c6b16b5SShri Abhyankar   PetscFunctionBegin;
55269c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5527a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5528a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5529a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5530a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5531a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5532a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
55338a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
55348a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5535a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55369c6b16b5SShri Abhyankar 
55379c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55389c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55399c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55409c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55419c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55427453f775SEmil Constantinescu 
55437453f775SEmil Constantinescu   max=0.;
55447453f775SEmil Constantinescu   maxa=0.;
55457453f775SEmil Constantinescu   maxr=0.;
55467453f775SEmil Constantinescu 
55477453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
55489c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55499c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55517453f775SEmil Constantinescu 
55527453f775SEmil Constantinescu     for (i=0; i<n; i++) {
555376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55547453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55557453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55567453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55577453f775SEmil Constantinescu       tol  = tola+tolr;
55587453f775SEmil Constantinescu       if(tola>0.){
55597453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55607453f775SEmil Constantinescu       }
55617453f775SEmil Constantinescu       if(tolr>0.){
55627453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55637453f775SEmil Constantinescu       }
55647453f775SEmil Constantinescu       if(tol>0.){
55657453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55667453f775SEmil Constantinescu       }
55679c6b16b5SShri Abhyankar     }
55689c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55699c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55709c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55719c6b16b5SShri Abhyankar     const PetscScalar *atol;
55729c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55737453f775SEmil Constantinescu     for (i=0; i<n; i++) {
557476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55757453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55767453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55777453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55787453f775SEmil Constantinescu       tol  = tola+tolr;
55797453f775SEmil Constantinescu       if(tola>0.){
55807453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55817453f775SEmil Constantinescu       }
55827453f775SEmil Constantinescu       if(tolr>0.){
55837453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55847453f775SEmil Constantinescu       }
55857453f775SEmil Constantinescu       if(tol>0.){
55867453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55877453f775SEmil Constantinescu       }
55889c6b16b5SShri Abhyankar     }
55899c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55909c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55919c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55929c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55937453f775SEmil Constantinescu 
55947453f775SEmil Constantinescu     for (i=0; i<n; i++) {
559576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55967453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55977453f775SEmil Constantinescu       tola = ts->atol;
55987453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55997453f775SEmil Constantinescu       tol  = tola+tolr;
56007453f775SEmil Constantinescu       if(tola>0.){
56017453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56027453f775SEmil Constantinescu       }
56037453f775SEmil Constantinescu       if(tolr>0.){
56047453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56057453f775SEmil Constantinescu       }
56067453f775SEmil Constantinescu       if(tol>0.){
56077453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56087453f775SEmil Constantinescu       }
56099c6b16b5SShri Abhyankar     }
56109c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56119c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
56127453f775SEmil Constantinescu 
56137453f775SEmil Constantinescu     for (i=0; i<n; i++) {
561476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
56157453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56167453f775SEmil Constantinescu       tola = ts->atol;
56177453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56187453f775SEmil Constantinescu       tol  = tola+tolr;
56197453f775SEmil Constantinescu       if(tola>0.){
56207453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56217453f775SEmil Constantinescu       }
56227453f775SEmil Constantinescu       if(tolr>0.){
56237453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56247453f775SEmil Constantinescu       }
56257453f775SEmil Constantinescu       if(tol>0.){
56267453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56277453f775SEmil Constantinescu       }
56289c6b16b5SShri Abhyankar     }
56299c6b16b5SShri Abhyankar   }
56309c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56319c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56327453f775SEmil Constantinescu   err_loc[0] = max;
56337453f775SEmil Constantinescu   err_loc[1] = maxa;
56347453f775SEmil Constantinescu   err_loc[2] = maxr;
5635a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56367453f775SEmil Constantinescu   gmax   = err_glb[0];
56377453f775SEmil Constantinescu   gmaxa  = err_glb[1];
56387453f775SEmil Constantinescu   gmaxr  = err_glb[2];
56399c6b16b5SShri Abhyankar 
56409c6b16b5SShri Abhyankar   *norm = gmax;
56417453f775SEmil Constantinescu   *norma = gmaxa;
56427453f775SEmil Constantinescu   *normr = gmaxr;
56439c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56447453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56457453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56469c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56479c6b16b5SShri Abhyankar }
56489c6b16b5SShri Abhyankar 
56491c3436cfSJed Brown /*@
56508a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
56511c3436cfSJed Brown 
56521c3436cfSJed Brown    Collective on TS
56531c3436cfSJed Brown 
56541c3436cfSJed Brown    Input Arguments:
56551c3436cfSJed Brown +  ts - time stepping context
5656a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5657a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5658a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56597619abb3SShri 
56601c3436cfSJed Brown    Output Arguments:
56618a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
56628a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
56638a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5664a4868fbcSLisandro Dalcin 
5665a4868fbcSLisandro Dalcin    Options Database Keys:
5666a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5667a4868fbcSLisandro Dalcin 
56681c3436cfSJed Brown    Level: developer
56691c3436cfSJed Brown 
56708a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
56711c3436cfSJed Brown @*/
56727453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56731c3436cfSJed Brown {
56748beabaa1SBarry Smith   PetscErrorCode ierr;
56751c3436cfSJed Brown 
56761c3436cfSJed Brown   PetscFunctionBegin;
5677a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
56787453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5679a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
56807453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5681a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
56821c3436cfSJed Brown   PetscFunctionReturn(0);
56831c3436cfSJed Brown }
56841c3436cfSJed Brown 
56858a175baeSEmil Constantinescu 
56868a175baeSEmil Constantinescu /*@
56878a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
56888a175baeSEmil Constantinescu 
56898a175baeSEmil Constantinescu    Collective on TS
56908a175baeSEmil Constantinescu 
56918a175baeSEmil Constantinescu    Input Arguments:
56928a175baeSEmil Constantinescu +  ts - time stepping context
56938a175baeSEmil Constantinescu .  E - error vector
56948a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
56958a175baeSEmil Constantinescu -  Y - state vector, previous time step
56968a175baeSEmil Constantinescu 
56978a175baeSEmil Constantinescu    Output Arguments:
56988a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56998a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57008a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57018a175baeSEmil Constantinescu 
57028a175baeSEmil Constantinescu    Level: developer
57038a175baeSEmil Constantinescu 
57048a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
57058a175baeSEmil Constantinescu @*/
57068a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57078a175baeSEmil Constantinescu {
57088a175baeSEmil Constantinescu   PetscErrorCode    ierr;
57098a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
57108a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57118a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57128a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
57138a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
57148a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
57158a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57168a175baeSEmil Constantinescu 
57178a175baeSEmil Constantinescu   PetscFunctionBegin;
57188a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57198a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
57208a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
57218a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
57228a175baeSEmil Constantinescu   PetscValidType(E,2);
57238a175baeSEmil Constantinescu   PetscValidType(U,3);
57248a175baeSEmil Constantinescu   PetscValidType(Y,4);
57258a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
57268a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
57278a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
57288a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
57298a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
57308a175baeSEmil Constantinescu 
57318a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
57328a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
57338a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
57348a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
57358a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57368a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57378a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
57388a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
57398a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
57408a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
57418a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
57428a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57438a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57448a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
574576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57468a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57478a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57488a175baeSEmil Constantinescu       if(tola>0.){
57498a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57508a175baeSEmil Constantinescu         na_loc++;
57518a175baeSEmil Constantinescu       }
57528a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57538a175baeSEmil Constantinescu       if(tolr>0.){
57548a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57558a175baeSEmil Constantinescu         nr_loc++;
57568a175baeSEmil Constantinescu       }
57578a175baeSEmil Constantinescu       tol=tola+tolr;
57588a175baeSEmil Constantinescu       if(tol>0.){
57598a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57608a175baeSEmil Constantinescu         n_loc++;
57618a175baeSEmil Constantinescu       }
57628a175baeSEmil Constantinescu     }
57638a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57648a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57658a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57668a175baeSEmil Constantinescu     const PetscScalar *atol;
57678a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57688a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
576976cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57708a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57718a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57728a175baeSEmil Constantinescu       if(tola>0.){
57738a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57748a175baeSEmil Constantinescu         na_loc++;
57758a175baeSEmil Constantinescu       }
57768a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57778a175baeSEmil Constantinescu       if(tolr>0.){
57788a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57798a175baeSEmil Constantinescu         nr_loc++;
57808a175baeSEmil Constantinescu       }
57818a175baeSEmil Constantinescu       tol=tola+tolr;
57828a175baeSEmil Constantinescu       if(tol>0.){
57838a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57848a175baeSEmil Constantinescu         n_loc++;
57858a175baeSEmil Constantinescu       }
57868a175baeSEmil Constantinescu     }
57878a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57888a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57898a175baeSEmil Constantinescu     const PetscScalar *rtol;
57908a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
579276cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57948a175baeSEmil Constantinescu       tola = ts->atol;
57958a175baeSEmil Constantinescu       if(tola>0.){
57968a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57978a175baeSEmil Constantinescu         na_loc++;
57988a175baeSEmil Constantinescu       }
57998a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58008a175baeSEmil Constantinescu       if(tolr>0.){
58018a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58028a175baeSEmil Constantinescu         nr_loc++;
58038a175baeSEmil Constantinescu       }
58048a175baeSEmil Constantinescu       tol=tola+tolr;
58058a175baeSEmil Constantinescu       if(tol>0.){
58068a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58078a175baeSEmil Constantinescu         n_loc++;
58088a175baeSEmil Constantinescu       }
58098a175baeSEmil Constantinescu     }
58108a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58118a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
58128a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
581376cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58148a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58158a175baeSEmil Constantinescu       tola = ts->atol;
58168a175baeSEmil Constantinescu       if(tola>0.){
58178a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58188a175baeSEmil Constantinescu         na_loc++;
58198a175baeSEmil Constantinescu       }
58208a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58218a175baeSEmil Constantinescu       if(tolr>0.){
58228a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58238a175baeSEmil Constantinescu         nr_loc++;
58248a175baeSEmil Constantinescu       }
58258a175baeSEmil Constantinescu       tol=tola+tolr;
58268a175baeSEmil Constantinescu       if(tol>0.){
58278a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58288a175baeSEmil Constantinescu         n_loc++;
58298a175baeSEmil Constantinescu       }
58308a175baeSEmil Constantinescu     }
58318a175baeSEmil Constantinescu   }
58328a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
58338a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58348a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58358a175baeSEmil Constantinescu 
58368a175baeSEmil Constantinescu   err_loc[0] = sum;
58378a175baeSEmil Constantinescu   err_loc[1] = suma;
58388a175baeSEmil Constantinescu   err_loc[2] = sumr;
58398a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58408a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58418a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58428a175baeSEmil Constantinescu 
5843a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58448a175baeSEmil Constantinescu 
58458a175baeSEmil Constantinescu   gsum   = err_glb[0];
58468a175baeSEmil Constantinescu   gsuma  = err_glb[1];
58478a175baeSEmil Constantinescu   gsumr  = err_glb[2];
58488a175baeSEmil Constantinescu   n_glb  = err_glb[3];
58498a175baeSEmil Constantinescu   na_glb = err_glb[4];
58508a175baeSEmil Constantinescu   nr_glb = err_glb[5];
58518a175baeSEmil Constantinescu 
58528a175baeSEmil Constantinescu   *norm  = 0.;
58538a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
58548a175baeSEmil Constantinescu   *norma = 0.;
58558a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
58568a175baeSEmil Constantinescu   *normr = 0.;
58578a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58588a175baeSEmil Constantinescu 
58598a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58608a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58618a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58628a175baeSEmil Constantinescu   PetscFunctionReturn(0);
58638a175baeSEmil Constantinescu }
58648a175baeSEmil Constantinescu 
58658a175baeSEmil Constantinescu /*@
58668a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
58678a175baeSEmil Constantinescu    Collective on TS
58688a175baeSEmil Constantinescu 
58698a175baeSEmil Constantinescu    Input Arguments:
58708a175baeSEmil Constantinescu +  ts - time stepping context
58718a175baeSEmil Constantinescu .  E - error vector
58728a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58738a175baeSEmil Constantinescu -  Y - state vector, previous time step
58748a175baeSEmil Constantinescu 
58758a175baeSEmil Constantinescu    Output Arguments:
58768a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58778a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58788a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58798a175baeSEmil Constantinescu 
58808a175baeSEmil Constantinescu    Level: developer
58818a175baeSEmil Constantinescu 
58828a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
58838a175baeSEmil Constantinescu @*/
58848a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58858a175baeSEmil Constantinescu {
58868a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58878a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58888a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58898a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
58908a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58918a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
58928a175baeSEmil Constantinescu 
58938a175baeSEmil Constantinescu   PetscFunctionBegin;
58948a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58958a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58968a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
58978a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
58988a175baeSEmil Constantinescu   PetscValidType(E,2);
58998a175baeSEmil Constantinescu   PetscValidType(U,3);
59008a175baeSEmil Constantinescu   PetscValidType(Y,4);
59018a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59028a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59038a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59048a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59058a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59068a175baeSEmil Constantinescu 
59078a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59088a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59098a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59108a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59118a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59128a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59138a175baeSEmil Constantinescu 
59148a175baeSEmil Constantinescu   max=0.;
59158a175baeSEmil Constantinescu   maxa=0.;
59168a175baeSEmil Constantinescu   maxr=0.;
59178a175baeSEmil Constantinescu 
59188a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59198a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
59208a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59218a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59228a175baeSEmil Constantinescu 
59238a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
592476cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59258a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59268a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59278a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59288a175baeSEmil Constantinescu       tol  = tola+tolr;
59298a175baeSEmil Constantinescu       if(tola>0.){
59308a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59318a175baeSEmil Constantinescu       }
59328a175baeSEmil Constantinescu       if(tolr>0.){
59338a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59348a175baeSEmil Constantinescu       }
59358a175baeSEmil Constantinescu       if(tol>0.){
59368a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59378a175baeSEmil Constantinescu       }
59388a175baeSEmil Constantinescu     }
59398a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59408a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59418a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59428a175baeSEmil Constantinescu     const PetscScalar *atol;
59438a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59448a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
594576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59468a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59478a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59488a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59498a175baeSEmil Constantinescu       tol  = tola+tolr;
59508a175baeSEmil Constantinescu       if(tola>0.){
59518a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59528a175baeSEmil Constantinescu       }
59538a175baeSEmil Constantinescu       if(tolr>0.){
59548a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59558a175baeSEmil Constantinescu       }
59568a175baeSEmil Constantinescu       if(tol>0.){
59578a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59588a175baeSEmil Constantinescu       }
59598a175baeSEmil Constantinescu     }
59608a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59618a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59628a175baeSEmil Constantinescu     const PetscScalar *rtol;
59638a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59648a175baeSEmil Constantinescu 
59658a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
596676cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
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++) {
598576cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59868a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59878a175baeSEmil Constantinescu       tola = ts->atol;
59888a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59898a175baeSEmil Constantinescu       tol  = tola+tolr;
59908a175baeSEmil Constantinescu       if(tola>0.){
59918a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59928a175baeSEmil Constantinescu       }
59938a175baeSEmil Constantinescu       if(tolr>0.){
59948a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59958a175baeSEmil Constantinescu       }
59968a175baeSEmil Constantinescu       if(tol>0.){
59978a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59988a175baeSEmil Constantinescu       }
59998a175baeSEmil Constantinescu     }
60008a175baeSEmil Constantinescu   }
60018a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60028a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60038a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60048a175baeSEmil Constantinescu   err_loc[0] = max;
60058a175baeSEmil Constantinescu   err_loc[1] = maxa;
60068a175baeSEmil Constantinescu   err_loc[2] = maxr;
6007a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60088a175baeSEmil Constantinescu   gmax   = err_glb[0];
60098a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
60108a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
60118a175baeSEmil Constantinescu 
60128a175baeSEmil Constantinescu   *norm = gmax;
60138a175baeSEmil Constantinescu   *norma = gmaxa;
60148a175baeSEmil Constantinescu   *normr = gmaxr;
60158a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60168a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60178a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60188a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60198a175baeSEmil Constantinescu }
60208a175baeSEmil Constantinescu 
60218a175baeSEmil Constantinescu /*@
60228a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
60238a175baeSEmil Constantinescu 
60248a175baeSEmil Constantinescu    Collective on TS
60258a175baeSEmil Constantinescu 
60268a175baeSEmil Constantinescu    Input Arguments:
60278a175baeSEmil Constantinescu +  ts - time stepping context
60288a175baeSEmil Constantinescu .  E - error vector
60298a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60308a175baeSEmil Constantinescu .  Y - state vector, previous time step
60318a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60328a175baeSEmil Constantinescu 
60338a175baeSEmil Constantinescu    Output Arguments:
60348a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60358a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
60368a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
60378a175baeSEmil Constantinescu 
60388a175baeSEmil Constantinescu    Options Database Keys:
60398a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
60408a175baeSEmil Constantinescu 
60418a175baeSEmil Constantinescu    Level: developer
60428a175baeSEmil Constantinescu 
60438a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
60448a175baeSEmil Constantinescu @*/
60458a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60468a175baeSEmil Constantinescu {
60478a175baeSEmil Constantinescu   PetscErrorCode ierr;
60488a175baeSEmil Constantinescu 
60498a175baeSEmil Constantinescu   PetscFunctionBegin;
60508a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
60518a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60528a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
60538a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60548a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60558a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60568a175baeSEmil Constantinescu }
60578a175baeSEmil Constantinescu 
60588a175baeSEmil Constantinescu 
60598d59e960SJed Brown /*@
60608d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
60618d59e960SJed Brown 
60628d59e960SJed Brown    Logically Collective on TS
60638d59e960SJed Brown 
60648d59e960SJed Brown    Input Arguments:
60658d59e960SJed Brown +  ts - time stepping context
60668d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
60678d59e960SJed Brown 
60688d59e960SJed Brown    Note:
60698d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
60708d59e960SJed Brown 
60718d59e960SJed Brown    Level: intermediate
60728d59e960SJed Brown 
60738d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
60748d59e960SJed Brown @*/
60758d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
60768d59e960SJed Brown {
60778d59e960SJed Brown   PetscFunctionBegin;
60788d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60798d59e960SJed Brown   ts->cfltime_local = cfltime;
60808d59e960SJed Brown   ts->cfltime       = -1.;
60818d59e960SJed Brown   PetscFunctionReturn(0);
60828d59e960SJed Brown }
60838d59e960SJed Brown 
60848d59e960SJed Brown /*@
60858d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
60868d59e960SJed Brown 
60878d59e960SJed Brown    Collective on TS
60888d59e960SJed Brown 
60898d59e960SJed Brown    Input Arguments:
60908d59e960SJed Brown .  ts - time stepping context
60918d59e960SJed Brown 
60928d59e960SJed Brown    Output Arguments:
60938d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
60948d59e960SJed Brown 
60958d59e960SJed Brown    Level: advanced
60968d59e960SJed Brown 
60978d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
60988d59e960SJed Brown @*/
60998d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
61008d59e960SJed Brown {
61018d59e960SJed Brown   PetscErrorCode ierr;
61028d59e960SJed Brown 
61038d59e960SJed Brown   PetscFunctionBegin;
61048d59e960SJed Brown   if (ts->cfltime < 0) {
6105b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61068d59e960SJed Brown   }
61078d59e960SJed Brown   *cfltime = ts->cfltime;
61088d59e960SJed Brown   PetscFunctionReturn(0);
61098d59e960SJed Brown }
61108d59e960SJed Brown 
6111d6ebe24aSShri Abhyankar /*@
6112d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6113d6ebe24aSShri Abhyankar 
6114d6ebe24aSShri Abhyankar    Input Parameters:
6115d6ebe24aSShri Abhyankar .  ts   - the TS context.
6116d6ebe24aSShri Abhyankar .  xl   - lower bound.
6117d6ebe24aSShri Abhyankar .  xu   - upper bound.
6118d6ebe24aSShri Abhyankar 
6119d6ebe24aSShri Abhyankar    Notes:
6120d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6121e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6122d6ebe24aSShri Abhyankar 
61232bd2b0e6SSatish Balay    Level: advanced
61242bd2b0e6SSatish Balay 
6125d6ebe24aSShri Abhyankar @*/
6126d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6127d6ebe24aSShri Abhyankar {
6128d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6129d6ebe24aSShri Abhyankar   SNES           snes;
6130d6ebe24aSShri Abhyankar 
6131d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6132d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6133d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6134d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6135d6ebe24aSShri Abhyankar }
6136d6ebe24aSShri Abhyankar 
6137325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6138c6db04a5SJed Brown #include <mex.h>
6139325fc9f4SBarry Smith 
6140325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6141325fc9f4SBarry Smith 
6142325fc9f4SBarry Smith /*
6143325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6144325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6145325fc9f4SBarry Smith 
6146325fc9f4SBarry Smith    Collective on TS
6147325fc9f4SBarry Smith 
6148325fc9f4SBarry Smith    Input Parameters:
6149325fc9f4SBarry Smith +  snes - the TS context
61500910c330SBarry Smith -  u - input vector
6151325fc9f4SBarry Smith 
6152325fc9f4SBarry Smith    Output Parameter:
6153325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6154325fc9f4SBarry Smith 
6155325fc9f4SBarry Smith    Notes:
6156325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6157325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6158325fc9f4SBarry Smith    themselves.
6159325fc9f4SBarry Smith 
6160325fc9f4SBarry Smith    Level: developer
6161325fc9f4SBarry Smith 
6162325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6163325fc9f4SBarry Smith */
61640910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6165325fc9f4SBarry Smith {
6166325fc9f4SBarry Smith   PetscErrorCode  ierr;
6167325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6168325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6169325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6170325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6171325fc9f4SBarry Smith 
6172325fc9f4SBarry Smith   PetscFunctionBegin;
6173325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
61740910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
61750910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6176325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
61770910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6178325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6179325fc9f4SBarry Smith 
6180325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
61810910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
61820910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
61830910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6184bbd56ea5SKarl Rupp 
6185325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6186325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6187325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6188325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6189325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6190325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6191325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6192325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6193325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6194325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6195325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6196325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6197325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6198325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6199325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6200325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6201325fc9f4SBarry Smith   PetscFunctionReturn(0);
6202325fc9f4SBarry Smith }
6203325fc9f4SBarry Smith 
6204325fc9f4SBarry Smith /*
6205325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6206325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6207e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6208325fc9f4SBarry Smith 
6209325fc9f4SBarry Smith    Logically Collective on TS
6210325fc9f4SBarry Smith 
6211325fc9f4SBarry Smith    Input Parameters:
6212325fc9f4SBarry Smith +  ts - the TS context
6213325fc9f4SBarry Smith -  func - function evaluation routine
6214325fc9f4SBarry Smith 
6215325fc9f4SBarry Smith    Calling sequence of func:
62160910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6217325fc9f4SBarry Smith 
6218325fc9f4SBarry Smith    Level: beginner
6219325fc9f4SBarry Smith 
6220325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6221325fc9f4SBarry Smith */
6222cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6223325fc9f4SBarry Smith {
6224325fc9f4SBarry Smith   PetscErrorCode  ierr;
6225325fc9f4SBarry Smith   TSMatlabContext *sctx;
6226325fc9f4SBarry Smith 
6227325fc9f4SBarry Smith   PetscFunctionBegin;
6228325fc9f4SBarry Smith   /* currently sctx is memory bleed */
622995dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6230325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6231325fc9f4SBarry Smith   /*
6232325fc9f4SBarry Smith      This should work, but it doesn't
6233325fc9f4SBarry Smith   sctx->ctx = ctx;
6234325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6235325fc9f4SBarry Smith   */
6236325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6237bbd56ea5SKarl Rupp 
62380298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6239325fc9f4SBarry Smith   PetscFunctionReturn(0);
6240325fc9f4SBarry Smith }
6241325fc9f4SBarry Smith 
6242325fc9f4SBarry Smith /*
6243325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6244325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6245325fc9f4SBarry Smith 
6246325fc9f4SBarry Smith    Collective on TS
6247325fc9f4SBarry Smith 
6248325fc9f4SBarry Smith    Input Parameters:
6249cdcf91faSSean Farley +  ts - the TS context
62500910c330SBarry Smith .  u - input vector
6251325fc9f4SBarry Smith .  A, B - the matrices
6252325fc9f4SBarry Smith -  ctx - user context
6253325fc9f4SBarry Smith 
6254325fc9f4SBarry Smith    Level: developer
6255325fc9f4SBarry Smith 
6256325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6257325fc9f4SBarry Smith @*/
6258f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6259325fc9f4SBarry Smith {
6260325fc9f4SBarry Smith   PetscErrorCode  ierr;
6261325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6262325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6263325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6264325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6265325fc9f4SBarry Smith 
6266325fc9f4SBarry Smith   PetscFunctionBegin;
6267cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62680910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6269325fc9f4SBarry Smith 
62700910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6271325fc9f4SBarry Smith 
6272cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
62730910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62740910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
62750910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
62760910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6277bbd56ea5SKarl Rupp 
6278325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6279325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6280325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6281325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6282325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6283325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6284325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6285325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6286325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6287325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6288325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6289325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6290325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6291325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6292325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6293325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6294325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6295325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6296325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6297325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6298325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6299325fc9f4SBarry Smith   PetscFunctionReturn(0);
6300325fc9f4SBarry Smith }
6301325fc9f4SBarry Smith 
6302325fc9f4SBarry Smith /*
6303325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6304e3c5b3baSBarry 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
6305325fc9f4SBarry Smith 
6306325fc9f4SBarry Smith    Logically Collective on TS
6307325fc9f4SBarry Smith 
6308325fc9f4SBarry Smith    Input Parameters:
6309cdcf91faSSean Farley +  ts - the TS context
6310325fc9f4SBarry Smith .  A,B - Jacobian matrices
6311325fc9f4SBarry Smith .  func - function evaluation routine
6312325fc9f4SBarry Smith -  ctx - user context
6313325fc9f4SBarry Smith 
6314325fc9f4SBarry Smith    Calling sequence of func:
63150910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6316325fc9f4SBarry Smith 
6317325fc9f4SBarry Smith    Level: developer
6318325fc9f4SBarry Smith 
6319325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6320325fc9f4SBarry Smith */
6321cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6322325fc9f4SBarry Smith {
6323325fc9f4SBarry Smith   PetscErrorCode  ierr;
6324325fc9f4SBarry Smith   TSMatlabContext *sctx;
6325325fc9f4SBarry Smith 
6326325fc9f4SBarry Smith   PetscFunctionBegin;
6327325fc9f4SBarry Smith   /* currently sctx is memory bleed */
632895dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6329325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6330325fc9f4SBarry Smith   /*
6331325fc9f4SBarry Smith      This should work, but it doesn't
6332325fc9f4SBarry Smith   sctx->ctx = ctx;
6333325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6334325fc9f4SBarry Smith   */
6335325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6336bbd56ea5SKarl Rupp 
6337cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6338325fc9f4SBarry Smith   PetscFunctionReturn(0);
6339325fc9f4SBarry Smith }
6340325fc9f4SBarry Smith 
6341b5b1a830SBarry Smith /*
6342b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6343b5b1a830SBarry Smith 
6344b5b1a830SBarry Smith    Collective on TS
6345b5b1a830SBarry Smith 
6346b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6347b5b1a830SBarry Smith @*/
63480910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6349b5b1a830SBarry Smith {
6350b5b1a830SBarry Smith   PetscErrorCode  ierr;
6351b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6352a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6353b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6354b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6355b5b1a830SBarry Smith 
6356b5b1a830SBarry Smith   PetscFunctionBegin;
6357cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63580910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6359b5b1a830SBarry Smith 
6360cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
63610910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6362bbd56ea5SKarl Rupp 
6363b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6364b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6365b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6366b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6367b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6368b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6369b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6370b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6371b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6372b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6373b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6374b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6375b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6376b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6377b5b1a830SBarry Smith   PetscFunctionReturn(0);
6378b5b1a830SBarry Smith }
6379b5b1a830SBarry Smith 
6380b5b1a830SBarry Smith /*
6381b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6382b5b1a830SBarry Smith 
6383b5b1a830SBarry Smith    Level: developer
6384b5b1a830SBarry Smith 
6385b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6386b5b1a830SBarry Smith */
6387cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6388b5b1a830SBarry Smith {
6389b5b1a830SBarry Smith   PetscErrorCode  ierr;
6390b5b1a830SBarry Smith   TSMatlabContext *sctx;
6391b5b1a830SBarry Smith 
6392b5b1a830SBarry Smith   PetscFunctionBegin;
6393b5b1a830SBarry Smith   /* currently sctx is memory bleed */
639495dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6395b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6396b5b1a830SBarry Smith   /*
6397b5b1a830SBarry Smith      This should work, but it doesn't
6398b5b1a830SBarry Smith   sctx->ctx = ctx;
6399b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6400b5b1a830SBarry Smith   */
6401b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6402bbd56ea5SKarl Rupp 
64030298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6404b5b1a830SBarry Smith   PetscFunctionReturn(0);
6405b5b1a830SBarry Smith }
6406325fc9f4SBarry Smith #endif
6407b3603a34SBarry Smith 
6408b3603a34SBarry Smith /*@C
64094f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6410b3603a34SBarry Smith        in a time based line graph
6411b3603a34SBarry Smith 
6412b3603a34SBarry Smith    Collective on TS
6413b3603a34SBarry Smith 
6414b3603a34SBarry Smith    Input Parameters:
6415b3603a34SBarry Smith +  ts - the TS context
6416b3603a34SBarry Smith .  step - current time-step
6417b3603a34SBarry Smith .  ptime - current time
64187db568b7SBarry Smith .  u - current solution
64197db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6420b3603a34SBarry Smith 
6421b3d3934dSBarry Smith    Options Database:
64229ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6423b3d3934dSBarry Smith 
6424b3603a34SBarry Smith    Level: intermediate
6425b3603a34SBarry Smith 
642695452b02SPatrick Sanan    Notes:
642795452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6428b3603a34SBarry Smith 
64297db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
64307db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
64317db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
64327db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6433b3603a34SBarry Smith @*/
64347db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6435b3603a34SBarry Smith {
6436b3603a34SBarry Smith   PetscErrorCode    ierr;
64377db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6438b3603a34SBarry Smith   const PetscScalar *yy;
643980666b62SBarry Smith   Vec               v;
6440b3603a34SBarry Smith 
6441b3603a34SBarry Smith   PetscFunctionBegin;
644263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
644358ff32f7SBarry Smith   if (!step) {
6444a9f9c1f6SBarry Smith     PetscDrawAxis axis;
64456934998bSLisandro Dalcin     PetscInt      dim;
6446a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
64470ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6448bab0a581SBarry Smith     if (!ctx->names) {
6449bab0a581SBarry Smith       PetscBool flg;
6450bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6451bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6452bab0a581SBarry Smith       if (flg) {
6453bab0a581SBarry Smith         PetscInt i,n;
6454bab0a581SBarry Smith         char     **names;
6455bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6456bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6457bab0a581SBarry Smith         for (i=0; i<n; i++) {
6458bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6459bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6460bab0a581SBarry Smith         }
6461bab0a581SBarry Smith         names[n] = NULL;
6462bab0a581SBarry Smith         ctx->names = names;
6463bab0a581SBarry Smith       }
6464bab0a581SBarry Smith     }
6465387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6466387f4636SBarry Smith       char      **displaynames;
6467387f4636SBarry Smith       PetscBool flg;
6468387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
646995dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6470387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6471c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6472387f4636SBarry Smith       if (flg) {
6473a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6474387f4636SBarry Smith       }
6475387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6476387f4636SBarry Smith     }
6477387f4636SBarry Smith     if (ctx->displaynames) {
6478387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6479387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6480387f4636SBarry Smith     } else if (ctx->names) {
64810910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
64820b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6483387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6484b0bc92c6SBarry Smith     } else {
6485b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6486b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6487387f4636SBarry Smith     }
64880b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
648958ff32f7SBarry Smith   }
64906934998bSLisandro Dalcin 
64916934998bSLisandro Dalcin   if (!ctx->transform) v = u;
64926934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
649380666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
64946934998bSLisandro Dalcin   if (ctx->displaynames) {
64956934998bSLisandro Dalcin     PetscInt i;
64966934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
64976934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
64986934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
64996934998bSLisandro Dalcin   } else {
6500e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6501e3efe391SJed Brown     PetscInt  i,n;
65026934998bSLisandro Dalcin     PetscReal *yreal;
650380666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6504785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6505e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65060b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6507e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6508e3efe391SJed Brown #else
65090b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6510e3efe391SJed Brown #endif
651180666b62SBarry Smith   }
65126934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
65136934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65146934998bSLisandro Dalcin 
6515b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65160b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65176934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65183923b477SBarry Smith   }
6519b3603a34SBarry Smith   PetscFunctionReturn(0);
6520b3603a34SBarry Smith }
6521b3603a34SBarry Smith 
6522b037adc7SBarry Smith /*@C
652331152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6524b037adc7SBarry Smith 
6525b037adc7SBarry Smith    Collective on TS
6526b037adc7SBarry Smith 
6527b037adc7SBarry Smith    Input Parameters:
6528b037adc7SBarry Smith +  ts - the TS context
6529b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6530b037adc7SBarry Smith 
6531b037adc7SBarry Smith    Level: intermediate
6532b037adc7SBarry Smith 
653395452b02SPatrick Sanan    Notes:
653495452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65357db568b7SBarry Smith 
6536a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6537b037adc7SBarry Smith @*/
653831152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6539b037adc7SBarry Smith {
6540b037adc7SBarry Smith   PetscErrorCode    ierr;
6541b037adc7SBarry Smith   PetscInt          i;
6542b037adc7SBarry Smith 
6543b037adc7SBarry Smith   PetscFunctionBegin;
6544b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6545b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
65465537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6547b3d3934dSBarry Smith       break;
6548b3d3934dSBarry Smith     }
6549b3d3934dSBarry Smith   }
6550b3d3934dSBarry Smith   PetscFunctionReturn(0);
6551b3d3934dSBarry Smith }
6552b3d3934dSBarry Smith 
6553e673d494SBarry Smith /*@C
6554e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6555e673d494SBarry Smith 
6556e673d494SBarry Smith    Collective on TS
6557e673d494SBarry Smith 
6558e673d494SBarry Smith    Input Parameters:
6559e673d494SBarry Smith +  ts - the TS context
6560e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6561e673d494SBarry Smith 
6562e673d494SBarry Smith    Level: intermediate
6563e673d494SBarry Smith 
6564a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6565e673d494SBarry Smith @*/
6566e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6567e673d494SBarry Smith {
6568e673d494SBarry Smith   PetscErrorCode    ierr;
6569e673d494SBarry Smith 
6570e673d494SBarry Smith   PetscFunctionBegin;
6571e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6572e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6573e673d494SBarry Smith   PetscFunctionReturn(0);
6574e673d494SBarry Smith }
6575e673d494SBarry Smith 
6576b3d3934dSBarry Smith /*@C
6577b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6578b3d3934dSBarry Smith 
6579b3d3934dSBarry Smith    Collective on TS
6580b3d3934dSBarry Smith 
6581b3d3934dSBarry Smith    Input Parameter:
6582b3d3934dSBarry Smith .  ts - the TS context
6583b3d3934dSBarry Smith 
6584b3d3934dSBarry Smith    Output Parameter:
6585b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6586b3d3934dSBarry Smith 
6587b3d3934dSBarry Smith    Level: intermediate
6588b3d3934dSBarry Smith 
658995452b02SPatrick Sanan    Notes:
659095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65917db568b7SBarry Smith 
6592b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6593b3d3934dSBarry Smith @*/
6594b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6595b3d3934dSBarry Smith {
6596b3d3934dSBarry Smith   PetscInt       i;
6597b3d3934dSBarry Smith 
6598b3d3934dSBarry Smith   PetscFunctionBegin;
6599b3d3934dSBarry Smith   *names = NULL;
6600b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6601b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66025537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6603b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6604b3d3934dSBarry Smith       break;
6605387f4636SBarry Smith     }
6606387f4636SBarry Smith   }
6607387f4636SBarry Smith   PetscFunctionReturn(0);
6608387f4636SBarry Smith }
6609387f4636SBarry Smith 
6610a66092f1SBarry Smith /*@C
6611a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6612a66092f1SBarry Smith 
6613a66092f1SBarry Smith    Collective on TS
6614a66092f1SBarry Smith 
6615a66092f1SBarry Smith    Input Parameters:
6616a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6617a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6618a66092f1SBarry Smith 
6619a66092f1SBarry Smith    Level: intermediate
6620a66092f1SBarry Smith 
6621a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6622a66092f1SBarry Smith @*/
6623a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6624a66092f1SBarry Smith {
6625a66092f1SBarry Smith   PetscInt          j = 0,k;
6626a66092f1SBarry Smith   PetscErrorCode    ierr;
6627a66092f1SBarry Smith 
6628a66092f1SBarry Smith   PetscFunctionBegin;
6629a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6630a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6631a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6632a66092f1SBarry Smith   while (displaynames[j]) j++;
6633a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6634a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6635a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6636a66092f1SBarry Smith   j = 0;
6637a66092f1SBarry Smith   while (displaynames[j]) {
6638a66092f1SBarry Smith     k = 0;
6639a66092f1SBarry Smith     while (ctx->names[k]) {
6640a66092f1SBarry Smith       PetscBool flg;
6641a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6642a66092f1SBarry Smith       if (flg) {
6643a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6644a66092f1SBarry Smith         break;
6645a66092f1SBarry Smith       }
6646a66092f1SBarry Smith       k++;
6647a66092f1SBarry Smith     }
6648a66092f1SBarry Smith     j++;
6649a66092f1SBarry Smith   }
6650a66092f1SBarry Smith   PetscFunctionReturn(0);
6651a66092f1SBarry Smith }
6652a66092f1SBarry Smith 
6653387f4636SBarry Smith /*@C
6654387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6655387f4636SBarry Smith 
6656387f4636SBarry Smith    Collective on TS
6657387f4636SBarry Smith 
6658387f4636SBarry Smith    Input Parameters:
6659387f4636SBarry Smith +  ts - the TS context
6660387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6661387f4636SBarry Smith 
666295452b02SPatrick Sanan    Notes:
666395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66647db568b7SBarry Smith 
6665387f4636SBarry Smith    Level: intermediate
6666387f4636SBarry Smith 
6667387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6668387f4636SBarry Smith @*/
6669387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6670387f4636SBarry Smith {
6671a66092f1SBarry Smith   PetscInt          i;
6672387f4636SBarry Smith   PetscErrorCode    ierr;
6673387f4636SBarry Smith 
6674387f4636SBarry Smith   PetscFunctionBegin;
6675387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6676387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66775537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6678b3d3934dSBarry Smith       break;
6679b037adc7SBarry Smith     }
6680b037adc7SBarry Smith   }
6681b037adc7SBarry Smith   PetscFunctionReturn(0);
6682b037adc7SBarry Smith }
6683b037adc7SBarry Smith 
668480666b62SBarry Smith /*@C
668580666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
668680666b62SBarry Smith 
668780666b62SBarry Smith    Collective on TS
668880666b62SBarry Smith 
668980666b62SBarry Smith    Input Parameters:
669080666b62SBarry Smith +  ts - the TS context
669180666b62SBarry Smith .  transform - the transform function
66927684fa3eSBarry Smith .  destroy - function to destroy the optional context
669380666b62SBarry Smith -  ctx - optional context used by transform function
669480666b62SBarry Smith 
669595452b02SPatrick Sanan    Notes:
669695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66977db568b7SBarry Smith 
669880666b62SBarry Smith    Level: intermediate
669980666b62SBarry Smith 
6700a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
670180666b62SBarry Smith @*/
67027684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
670380666b62SBarry Smith {
670480666b62SBarry Smith   PetscInt          i;
6705a66092f1SBarry Smith   PetscErrorCode    ierr;
670680666b62SBarry Smith 
670780666b62SBarry Smith   PetscFunctionBegin;
670880666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
670980666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67105537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
671180666b62SBarry Smith     }
671280666b62SBarry Smith   }
671380666b62SBarry Smith   PetscFunctionReturn(0);
671480666b62SBarry Smith }
671580666b62SBarry Smith 
6716e673d494SBarry Smith /*@C
6717e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6718e673d494SBarry Smith 
6719e673d494SBarry Smith    Collective on TSLGCtx
6720e673d494SBarry Smith 
6721e673d494SBarry Smith    Input Parameters:
6722e673d494SBarry Smith +  ts - the TS context
6723e673d494SBarry Smith .  transform - the transform function
67247684fa3eSBarry Smith .  destroy - function to destroy the optional context
6725e673d494SBarry Smith -  ctx - optional context used by transform function
6726e673d494SBarry Smith 
6727e673d494SBarry Smith    Level: intermediate
6728e673d494SBarry Smith 
6729a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6730e673d494SBarry Smith @*/
67317684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6732e673d494SBarry Smith {
6733e673d494SBarry Smith   PetscFunctionBegin;
6734e673d494SBarry Smith   ctx->transform    = transform;
67357684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6736e673d494SBarry Smith   ctx->transformctx = tctx;
6737e673d494SBarry Smith   PetscFunctionReturn(0);
6738e673d494SBarry Smith }
6739e673d494SBarry Smith 
6740ef20d060SBarry Smith /*@C
67418b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6742ef20d060SBarry Smith        in a time based line graph
6743ef20d060SBarry Smith 
6744ef20d060SBarry Smith    Collective on TS
6745ef20d060SBarry Smith 
6746ef20d060SBarry Smith    Input Parameters:
6747ef20d060SBarry Smith +  ts - the TS context
6748ef20d060SBarry Smith .  step - current time-step
6749ef20d060SBarry Smith .  ptime - current time
67507db568b7SBarry Smith .  u - current solution
67517db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6752ef20d060SBarry Smith 
6753ef20d060SBarry Smith    Level: intermediate
6754ef20d060SBarry Smith 
675595452b02SPatrick Sanan    Notes:
675695452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6757abd5a294SJed Brown 
6758abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6759abd5a294SJed Brown 
6760abd5a294SJed Brown    Options Database Keys:
67614f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6762ef20d060SBarry Smith 
6763abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6764ef20d060SBarry Smith @*/
67650910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6766ef20d060SBarry Smith {
6767ef20d060SBarry Smith   PetscErrorCode    ierr;
67680b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6769ef20d060SBarry Smith   const PetscScalar *yy;
6770ef20d060SBarry Smith   Vec               y;
6771ef20d060SBarry Smith 
6772ef20d060SBarry Smith   PetscFunctionBegin;
6773a9f9c1f6SBarry Smith   if (!step) {
6774a9f9c1f6SBarry Smith     PetscDrawAxis axis;
67756934998bSLisandro Dalcin     PetscInt      dim;
6776a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
67778b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
67780910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6779a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6780a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6781a9f9c1f6SBarry Smith   }
67820910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6783ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
67840910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6785ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6786e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6787e3efe391SJed Brown   {
6788e3efe391SJed Brown     PetscReal *yreal;
6789e3efe391SJed Brown     PetscInt  i,n;
6790e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6791785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6792e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
67930b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6794e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6795e3efe391SJed Brown   }
6796e3efe391SJed Brown #else
67970b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6798e3efe391SJed Brown #endif
6799ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6800ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6801b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68020b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
68036934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
68043923b477SBarry Smith   }
6805ef20d060SBarry Smith   PetscFunctionReturn(0);
6806ef20d060SBarry Smith }
6807ef20d060SBarry Smith 
68085e3b7effSJoseph Pusztay /*@C
68095e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
68105e3b7effSJoseph Pusztay 
68115e3b7effSJoseph Pusztay    Input Parameters:
68125e3b7effSJoseph Pusztay +  ts - the TS context
68135e3b7effSJoseph Pusztay .  step - current time-step
68145e3b7effSJoseph Pusztay .  ptime - current time
68155e3b7effSJoseph Pusztay .  u - current solution
68165e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
68175e3b7effSJoseph Pusztay 
68185e3b7effSJoseph Pusztay    Options Database:
6819918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
68205e3b7effSJoseph Pusztay 
68215e3b7effSJoseph Pusztay    Level: intermediate
68225e3b7effSJoseph Pusztay 
68235e3b7effSJoseph Pusztay @*/
68240ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
68251b575b74SJoseph Pusztay {
68261b575b74SJoseph Pusztay   PetscErrorCode    ierr;
68271b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
68281b575b74SJoseph Pusztay   const PetscScalar *yy;
6829b1670a61SJoseph Pusztay   PetscReal       *y,*x;
68301b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
68311b575b74SJoseph Pusztay   DM                dm;
68321b575b74SJoseph Pusztay 
68331b575b74SJoseph Pusztay   PetscFunctionBegin;
68341b575b74SJoseph Pusztay 
68351b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
68361b575b74SJoseph Pusztay   if (!step) {
68371b575b74SJoseph Pusztay     PetscDrawAxis axis;
68381b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
68391b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
6840895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
6841895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
68421b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
68431b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
68441b575b74SJoseph Pusztay     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
68451b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
68461b575b74SJoseph Pusztay     Np /= 2*dim;
68471b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
68481b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
68491b575b74SJoseph Pusztay   }
68501b575b74SJoseph Pusztay 
68511b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
68521b575b74SJoseph Pusztay   Np /= 2*dim;
68531b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
6854895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
68551b575b74SJoseph Pusztay   /* get points from solution vector */
68561b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
6857b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
6858b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
6859895f37d5SJoseph Pusztay   }
68601b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
68611b575b74SJoseph Pusztay 
68621b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68631b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
68641b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
68651b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
68661b575b74SJoseph Pusztay   }
68671b575b74SJoseph Pusztay 
6868918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
6869918b1d10SJoseph Pusztay 
68701b575b74SJoseph Pusztay   PetscFunctionReturn(0);
68711b575b74SJoseph Pusztay }
68721b575b74SJoseph Pusztay 
68731b575b74SJoseph Pusztay 
68741b575b74SJoseph Pusztay 
68757cf37e64SBarry Smith /*@C
68767cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
68777cf37e64SBarry Smith 
68787cf37e64SBarry Smith    Collective on TS
68797cf37e64SBarry Smith 
68807cf37e64SBarry Smith    Input Parameters:
68817cf37e64SBarry Smith +  ts - the TS context
68827cf37e64SBarry Smith .  step - current time-step
68837cf37e64SBarry Smith .  ptime - current time
68847cf37e64SBarry Smith .  u - current solution
68857cf37e64SBarry Smith -  dctx - unused context
68867cf37e64SBarry Smith 
68877cf37e64SBarry Smith    Level: intermediate
68887cf37e64SBarry Smith 
68897cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
68907cf37e64SBarry Smith 
68917cf37e64SBarry Smith    Options Database Keys:
68927cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
68937cf37e64SBarry Smith 
68947cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
68957cf37e64SBarry Smith @*/
6896edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
68977cf37e64SBarry Smith {
68987cf37e64SBarry Smith   PetscErrorCode    ierr;
68997cf37e64SBarry Smith   Vec               y;
69007cf37e64SBarry Smith   PetscReal         nrm;
6901edbaebb3SBarry Smith   PetscBool         flg;
69027cf37e64SBarry Smith 
69037cf37e64SBarry Smith   PetscFunctionBegin;
69047cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
69057cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69067cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6907edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6908edbaebb3SBarry Smith   if (flg) {
69097cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6910edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6911edbaebb3SBarry Smith   }
6912edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6913edbaebb3SBarry Smith   if (flg) {
6914edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6915edbaebb3SBarry Smith   }
6916edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69177cf37e64SBarry Smith   PetscFunctionReturn(0);
69187cf37e64SBarry Smith }
69197cf37e64SBarry Smith 
6920201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6921201da799SBarry Smith {
6922201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6923201da799SBarry Smith   PetscReal      x   = ptime,y;
6924201da799SBarry Smith   PetscErrorCode ierr;
6925201da799SBarry Smith   PetscInt       its;
6926201da799SBarry Smith 
6927201da799SBarry Smith   PetscFunctionBegin;
692863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6929201da799SBarry Smith   if (!n) {
6930201da799SBarry Smith     PetscDrawAxis axis;
6931201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6932201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6933201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6934201da799SBarry Smith     ctx->snes_its = 0;
6935201da799SBarry Smith   }
6936201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6937201da799SBarry Smith   y    = its - ctx->snes_its;
6938201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69393fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6940201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69416934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6942201da799SBarry Smith   }
6943201da799SBarry Smith   ctx->snes_its = its;
6944201da799SBarry Smith   PetscFunctionReturn(0);
6945201da799SBarry Smith }
6946201da799SBarry Smith 
6947201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6948201da799SBarry Smith {
6949201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6950201da799SBarry Smith   PetscReal      x   = ptime,y;
6951201da799SBarry Smith   PetscErrorCode ierr;
6952201da799SBarry Smith   PetscInt       its;
6953201da799SBarry Smith 
6954201da799SBarry Smith   PetscFunctionBegin;
695563e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6956201da799SBarry Smith   if (!n) {
6957201da799SBarry Smith     PetscDrawAxis axis;
6958201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6959201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6960201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6961201da799SBarry Smith     ctx->ksp_its = 0;
6962201da799SBarry Smith   }
6963201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6964201da799SBarry Smith   y    = its - ctx->ksp_its;
6965201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
696699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6967201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69686934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6969201da799SBarry Smith   }
6970201da799SBarry Smith   ctx->ksp_its = its;
6971201da799SBarry Smith   PetscFunctionReturn(0);
6972201da799SBarry Smith }
6973f9c1d6abSBarry Smith 
6974f9c1d6abSBarry Smith /*@
6975f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6976f9c1d6abSBarry Smith 
6977d083f849SBarry Smith    Collective on TS
6978f9c1d6abSBarry Smith 
6979f9c1d6abSBarry Smith    Input Parameters:
6980f9c1d6abSBarry Smith +  ts - the TS context
6981f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6982f9c1d6abSBarry Smith 
6983f9c1d6abSBarry Smith    Output Parameters:
6984f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6985f9c1d6abSBarry Smith 
6986f9c1d6abSBarry Smith    Level: developer
6987f9c1d6abSBarry Smith 
6988f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6989f9c1d6abSBarry Smith @*/
6990f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6991f9c1d6abSBarry Smith {
6992f9c1d6abSBarry Smith   PetscErrorCode ierr;
6993f9c1d6abSBarry Smith 
6994f9c1d6abSBarry Smith   PetscFunctionBegin;
6995f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6996ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6997f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6998f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6999f9c1d6abSBarry Smith }
700024655328SShri 
7001b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7002b3d3934dSBarry Smith /*@C
7003b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7004b3d3934dSBarry Smith 
7005b3d3934dSBarry Smith    Collective on TS
7006b3d3934dSBarry Smith 
7007b3d3934dSBarry Smith    Input Parameters:
7008b3d3934dSBarry Smith .  ts  - the ODE solver object
7009b3d3934dSBarry Smith 
7010b3d3934dSBarry Smith    Output Parameter:
7011b3d3934dSBarry Smith .  ctx - the context
7012b3d3934dSBarry Smith 
7013b3d3934dSBarry Smith    Level: intermediate
7014b3d3934dSBarry Smith 
7015b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7016b3d3934dSBarry Smith 
7017b3d3934dSBarry Smith @*/
7018b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7019b3d3934dSBarry Smith {
7020b3d3934dSBarry Smith   PetscErrorCode ierr;
7021b3d3934dSBarry Smith 
7022b3d3934dSBarry Smith   PetscFunctionBegin;
7023a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7024b3d3934dSBarry Smith   PetscFunctionReturn(0);
7025b3d3934dSBarry Smith }
7026b3d3934dSBarry Smith 
7027b3d3934dSBarry Smith /*@C
7028b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7029b3d3934dSBarry Smith 
7030b3d3934dSBarry Smith    Collective on TS
7031b3d3934dSBarry Smith 
7032b3d3934dSBarry Smith    Input Parameters:
7033b3d3934dSBarry Smith +  ts - the TS context
7034b3d3934dSBarry Smith .  step - current time-step
7035b3d3934dSBarry Smith .  ptime - current time
70367db568b7SBarry Smith .  u  - current solution
70377db568b7SBarry Smith -  dctx - the envelope context
7038b3d3934dSBarry Smith 
7039b3d3934dSBarry Smith    Options Database:
7040b3d3934dSBarry Smith .  -ts_monitor_envelope
7041b3d3934dSBarry Smith 
7042b3d3934dSBarry Smith    Level: intermediate
7043b3d3934dSBarry Smith 
704495452b02SPatrick Sanan    Notes:
704595452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7046b3d3934dSBarry Smith 
70477db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7048b3d3934dSBarry Smith @*/
70497db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7050b3d3934dSBarry Smith {
7051b3d3934dSBarry Smith   PetscErrorCode       ierr;
70527db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7053b3d3934dSBarry Smith 
7054b3d3934dSBarry Smith   PetscFunctionBegin;
7055b3d3934dSBarry Smith   if (!ctx->max) {
7056b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7057b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7058b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7059b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7060b3d3934dSBarry Smith   } else {
7061b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7062b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7063b3d3934dSBarry Smith   }
7064b3d3934dSBarry Smith   PetscFunctionReturn(0);
7065b3d3934dSBarry Smith }
7066b3d3934dSBarry Smith 
7067b3d3934dSBarry Smith /*@C
7068b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7069b3d3934dSBarry Smith 
7070b3d3934dSBarry Smith    Collective on TS
7071b3d3934dSBarry Smith 
7072b3d3934dSBarry Smith    Input Parameter:
7073b3d3934dSBarry Smith .  ts - the TS context
7074b3d3934dSBarry Smith 
7075b3d3934dSBarry Smith    Output Parameter:
7076b3d3934dSBarry Smith +  max - the maximum values
7077b3d3934dSBarry Smith -  min - the minimum values
7078b3d3934dSBarry Smith 
707995452b02SPatrick Sanan    Notes:
708095452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
70817db568b7SBarry Smith 
7082b3d3934dSBarry Smith    Level: intermediate
7083b3d3934dSBarry Smith 
7084b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7085b3d3934dSBarry Smith @*/
7086b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7087b3d3934dSBarry Smith {
7088b3d3934dSBarry Smith   PetscInt i;
7089b3d3934dSBarry Smith 
7090b3d3934dSBarry Smith   PetscFunctionBegin;
7091b3d3934dSBarry Smith   if (max) *max = NULL;
7092b3d3934dSBarry Smith   if (min) *min = NULL;
7093b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7094b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
70955537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7096b3d3934dSBarry Smith       if (max) *max = ctx->max;
7097b3d3934dSBarry Smith       if (min) *min = ctx->min;
7098b3d3934dSBarry Smith       break;
7099b3d3934dSBarry Smith     }
7100b3d3934dSBarry Smith   }
7101b3d3934dSBarry Smith   PetscFunctionReturn(0);
7102b3d3934dSBarry Smith }
7103b3d3934dSBarry Smith 
7104b3d3934dSBarry Smith /*@C
7105b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7106b3d3934dSBarry Smith 
7107b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7108b3d3934dSBarry Smith 
7109b3d3934dSBarry Smith    Input Parameter:
7110b3d3934dSBarry Smith .  ctx - the monitor context
7111b3d3934dSBarry Smith 
7112b3d3934dSBarry Smith    Level: intermediate
7113b3d3934dSBarry Smith 
71147db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7115b3d3934dSBarry Smith @*/
7116b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7117b3d3934dSBarry Smith {
7118b3d3934dSBarry Smith   PetscErrorCode ierr;
7119b3d3934dSBarry Smith 
7120b3d3934dSBarry Smith   PetscFunctionBegin;
7121b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7122b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7123b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7124b3d3934dSBarry Smith   PetscFunctionReturn(0);
7125b3d3934dSBarry Smith }
7126f2dee214SBarry Smith 
712724655328SShri /*@
7128dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7129dcb233daSLisandro Dalcin 
7130dcb233daSLisandro Dalcin    Collective on TS
7131dcb233daSLisandro Dalcin 
7132dcb233daSLisandro Dalcin    Input Parameter:
7133dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7134dcb233daSLisandro Dalcin 
7135dcb233daSLisandro Dalcin    Level: advanced
7136dcb233daSLisandro Dalcin 
7137dcb233daSLisandro Dalcin    Notes:
7138dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7139dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7140dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7141dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7142dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7143dcb233daSLisandro Dalcin    discontinuous source terms).
7144dcb233daSLisandro Dalcin 
7145dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7146dcb233daSLisandro Dalcin @*/
7147dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7148dcb233daSLisandro Dalcin {
7149dcb233daSLisandro Dalcin   PetscFunctionBegin;
7150dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7151dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7152dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7153dcb233daSLisandro Dalcin }
7154dcb233daSLisandro Dalcin 
7155dcb233daSLisandro Dalcin /*@
715624655328SShri    TSRollBack - Rolls back one time step
715724655328SShri 
715824655328SShri    Collective on TS
715924655328SShri 
716024655328SShri    Input Parameter:
716124655328SShri .  ts - the TS context obtained from TSCreate()
716224655328SShri 
716324655328SShri    Level: advanced
716424655328SShri 
716524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
716624655328SShri @*/
716724655328SShri PetscErrorCode  TSRollBack(TS ts)
716824655328SShri {
716924655328SShri   PetscErrorCode ierr;
717024655328SShri 
717124655328SShri   PetscFunctionBegin;
717224655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7173b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
717424655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
717524655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
717624655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
717724655328SShri   ts->ptime = ts->ptime_prev;
7178be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
71792808aa04SLisandro Dalcin   ts->steps--;
7180b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
718124655328SShri   PetscFunctionReturn(0);
718224655328SShri }
7183aeb4809dSShri Abhyankar 
7184ff22ae23SHong Zhang /*@
7185ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7186ff22ae23SHong Zhang 
7187ff22ae23SHong Zhang    Input Parameter:
7188ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7189ff22ae23SHong Zhang 
71900429704eSStefano Zampini    Output Parameters:
71910429704eSStefano Zampini +  ns - the number of stages
71920429704eSStefano Zampini -  Y - the current stage vectors
71930429704eSStefano Zampini 
7194ff22ae23SHong Zhang    Level: advanced
7195ff22ae23SHong Zhang 
71960429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
71970429704eSStefano Zampini 
7198ff22ae23SHong Zhang .seealso: TSCreate()
7199ff22ae23SHong Zhang @*/
7200ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7201ff22ae23SHong Zhang {
7202ff22ae23SHong Zhang   PetscErrorCode ierr;
7203ff22ae23SHong Zhang 
7204ff22ae23SHong Zhang   PetscFunctionBegin;
7205ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
72060429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
72070429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
72080429704eSStefano Zampini   if (!ts->ops->getstages) {
72090429704eSStefano Zampini     if (ns) *ns = 0;
72100429704eSStefano Zampini     if (Y) *Y = NULL;
72110429704eSStefano Zampini   } else {
7212ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7213ff22ae23SHong Zhang   }
7214ff22ae23SHong Zhang   PetscFunctionReturn(0);
7215ff22ae23SHong Zhang }
7216ff22ae23SHong Zhang 
7217847ff0e1SMatthew G. Knepley /*@C
7218847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7219847ff0e1SMatthew G. Knepley 
7220847ff0e1SMatthew G. Knepley   Collective on SNES
7221847ff0e1SMatthew G. Knepley 
7222847ff0e1SMatthew G. Knepley   Input Parameters:
7223847ff0e1SMatthew G. Knepley + ts - the TS context
7224847ff0e1SMatthew G. Knepley . t - current timestep
7225847ff0e1SMatthew G. Knepley . U - state vector
7226847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7227847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7228847ff0e1SMatthew G. Knepley - ctx - an optional user context
7229847ff0e1SMatthew G. Knepley 
7230847ff0e1SMatthew G. Knepley   Output Parameters:
7231847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7232847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7233847ff0e1SMatthew G. Knepley 
7234847ff0e1SMatthew G. Knepley   Level: intermediate
7235847ff0e1SMatthew G. Knepley 
7236847ff0e1SMatthew G. Knepley   Notes:
7237847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7238847ff0e1SMatthew G. Knepley 
7239847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7240847ff0e1SMatthew G. Knepley 
7241847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7242847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7243847ff0e1SMatthew G. Knepley 
7244847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7245847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7246847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7247847ff0e1SMatthew G. Knepley 
7248847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7249847ff0e1SMatthew G. Knepley @*/
7250847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7251847ff0e1SMatthew G. Knepley {
7252847ff0e1SMatthew G. Knepley   SNES           snes;
7253847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7254847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7255847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7256847ff0e1SMatthew G. Knepley 
7257847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7258c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7259847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7260847ff0e1SMatthew G. Knepley   if (!color) {
7261847ff0e1SMatthew G. Knepley     DM         dm;
7262847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7263847ff0e1SMatthew G. Knepley 
7264847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7265847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7266847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7267847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7268847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7269847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7270847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7271847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7272847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7273847ff0e1SMatthew G. Knepley     } else {
7274847ff0e1SMatthew G. Knepley       MatColoring mc;
7275847ff0e1SMatthew G. Knepley 
7276847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7277847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7278847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7279847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7280847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7281847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7282847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7283847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7284847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7285847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7286847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7287847ff0e1SMatthew G. Knepley     }
7288847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7289847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7290847ff0e1SMatthew G. Knepley   }
7291847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7292847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7293847ff0e1SMatthew G. Knepley   if (J != B) {
7294847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7295847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7296847ff0e1SMatthew G. Knepley   }
7297847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7298847ff0e1SMatthew G. Knepley }
729993b34091SDebojyoti Ghosh 
7300cb9d8021SPierre Barbier de Reuille /*@
7301cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7302cb9d8021SPierre Barbier de Reuille 
7303cb9d8021SPierre Barbier de Reuille     Input Parameters:
7304cb9d8021SPierre Barbier de Reuille     ts - the TS context
7305cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7306cb9d8021SPierre Barbier de Reuille 
7307cb9d8021SPierre Barbier de Reuille     Level: intermediate
7308cb9d8021SPierre Barbier de Reuille 
7309cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7310cb9d8021SPierre Barbier de Reuille @*/
7311cb9d8021SPierre Barbier de Reuille 
7312d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7313cb9d8021SPierre Barbier de Reuille {
7314cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7315cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7316cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7317cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7318cb9d8021SPierre Barbier de Reuille }
7319cb9d8021SPierre Barbier de Reuille 
7320cb9d8021SPierre Barbier de Reuille /*@
7321cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7322cb9d8021SPierre Barbier de Reuille 
7323cb9d8021SPierre Barbier de Reuille     Input Parameters:
7324cb9d8021SPierre Barbier de Reuille     ts - the TS context
7325cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7326d183316bSPierre Barbier de Reuille     Y - state vector to check.
7327cb9d8021SPierre Barbier de Reuille 
7328cb9d8021SPierre Barbier de Reuille     Output Parameter:
7329cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7330cb9d8021SPierre Barbier de Reuille 
7331cb9d8021SPierre Barbier de Reuille     Note:
7332cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7333cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
733496a0c994SBarry Smith 
733596a0c994SBarry Smith     Level: advanced
7336cb9d8021SPierre Barbier de Reuille @*/
7337d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7338cb9d8021SPierre Barbier de Reuille {
7339cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7340cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7341cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7342cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7343d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7344cb9d8021SPierre Barbier de Reuille   }
7345cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7346cb9d8021SPierre Barbier de Reuille }
73471ceb14c0SBarry Smith 
734893b34091SDebojyoti Ghosh /*@C
7349e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
735093b34091SDebojyoti Ghosh 
7351d083f849SBarry Smith   Collective
735293b34091SDebojyoti Ghosh 
735393b34091SDebojyoti Ghosh   Input Parameter:
735493b34091SDebojyoti Ghosh . tsin    - The input TS
735593b34091SDebojyoti Ghosh 
735693b34091SDebojyoti Ghosh   Output Parameter:
7357e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
735893b34091SDebojyoti Ghosh 
73595eca1a21SEmil Constantinescu   Notes:
73605eca1a21SEmil 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.
73615eca1a21SEmil Constantinescu 
7362928bb9adSStefano Zampini   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); TSSetSNES(ts,snes_dup);
73635eca1a21SEmil Constantinescu 
73645eca1a21SEmil Constantinescu   Level: developer
736593b34091SDebojyoti Ghosh 
7366e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
736793b34091SDebojyoti Ghosh @*/
7368baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
736993b34091SDebojyoti Ghosh {
737093b34091SDebojyoti Ghosh   TS             t;
737193b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7372dc846ba4SSatish Balay   SNES           snes_start;
7373dc846ba4SSatish Balay   DM             dm;
7374dc846ba4SSatish Balay   TSType         type;
737593b34091SDebojyoti Ghosh 
737693b34091SDebojyoti Ghosh   PetscFunctionBegin;
737793b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
737893b34091SDebojyoti Ghosh   *tsout = NULL;
737993b34091SDebojyoti Ghosh 
73807a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
738193b34091SDebojyoti Ghosh 
738293b34091SDebojyoti Ghosh   /* General TS description */
738393b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
738493b34091SDebojyoti Ghosh   t->setupcalled       = 0;
738593b34091SDebojyoti Ghosh   t->ksp_its           = 0;
738693b34091SDebojyoti Ghosh   t->snes_its          = 0;
738793b34091SDebojyoti Ghosh   t->nwork             = 0;
738893b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
738993b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
739093b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
739193b34091SDebojyoti Ghosh 
739234561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
739334561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7394d15a3a53SEmil Constantinescu 
739593b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
739693b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
739793b34091SDebojyoti Ghosh 
739893b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
739951699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
740093b34091SDebojyoti Ghosh 
7401e7069c78SShri   t->trajectory = tsin->trajectory;
7402e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7403e7069c78SShri 
7404e7069c78SShri   t->event = tsin->event;
74056b10a48eSSatish Balay   if (t->event) t->event->refct++;
7406e7069c78SShri 
740793b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
740893b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7409e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
741093b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
741193b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
741293b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
741393b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
741493b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
741593b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
741693b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
741793b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
741893b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
741993b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
742093b34091SDebojyoti Ghosh 
742193b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
742293b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
742393b34091SDebojyoti Ghosh 
742493b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
742593b34091SDebojyoti Ghosh 
742693b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
742793b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
742893b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
742993b34091SDebojyoti Ghosh 
743093b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
743193b34091SDebojyoti Ghosh 
74320d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
74330d4fed19SBarry Smith     PetscInt i;
74340d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
74350d4fed19SBarry Smith     for (i=0; i<10; i++) {
74360d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
74370d4fed19SBarry Smith     }
74380d4fed19SBarry Smith   }
743993b34091SDebojyoti Ghosh   *tsout = t;
744093b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
744193b34091SDebojyoti Ghosh }
7442f3b1f45cSBarry Smith 
7443f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7444f3b1f45cSBarry Smith {
7445f3b1f45cSBarry Smith   PetscErrorCode ierr;
7446f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7447f3b1f45cSBarry Smith 
7448f3b1f45cSBarry Smith   PetscFunctionBegin;
7449f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7450f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7451f3b1f45cSBarry Smith }
7452f3b1f45cSBarry Smith 
7453f3b1f45cSBarry Smith /*@
7454f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7455f3b1f45cSBarry Smith 
7456d083f849SBarry Smith    Logically Collective on TS
7457f3b1f45cSBarry Smith 
7458f3b1f45cSBarry Smith     Input Parameters:
7459f3b1f45cSBarry Smith     TS - the time stepping routine
7460f3b1f45cSBarry Smith 
7461f3b1f45cSBarry Smith    Output Parameter:
7462f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7463f3b1f45cSBarry Smith 
7464f3b1f45cSBarry Smith    Options Database:
7465f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7466f3b1f45cSBarry Smith 
7467f3b1f45cSBarry Smith    Level: advanced
7468f3b1f45cSBarry Smith 
746995452b02SPatrick Sanan    Notes:
747095452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7471f3b1f45cSBarry Smith 
7472f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7473f3b1f45cSBarry Smith @*/
7474f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7475f3b1f45cSBarry Smith {
7476f3b1f45cSBarry Smith   Mat            J,B;
7477f3b1f45cSBarry Smith   PetscErrorCode ierr;
7478f3b1f45cSBarry Smith   TSRHSJacobian  func;
7479f3b1f45cSBarry Smith   void*          ctx;
7480f3b1f45cSBarry Smith 
7481f3b1f45cSBarry Smith   PetscFunctionBegin;
7482f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7483f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7484f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7485f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7486f3b1f45cSBarry Smith }
7487f3b1f45cSBarry Smith 
7488f3b1f45cSBarry Smith /*@C
7489f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7490f3b1f45cSBarry Smith 
7491d083f849SBarry Smith    Logically Collective on TS
7492f3b1f45cSBarry Smith 
7493f3b1f45cSBarry Smith     Input Parameters:
7494f3b1f45cSBarry Smith     TS - the time stepping routine
7495f3b1f45cSBarry Smith 
7496f3b1f45cSBarry Smith    Output Parameter:
7497f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7498f3b1f45cSBarry Smith 
7499f3b1f45cSBarry Smith    Options Database:
7500f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7501f3b1f45cSBarry Smith 
750295452b02SPatrick Sanan    Notes:
750395452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7504f3b1f45cSBarry Smith 
7505f3b1f45cSBarry Smith    Level: advanced
7506f3b1f45cSBarry Smith 
7507f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7508f3b1f45cSBarry Smith @*/
7509f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7510f3b1f45cSBarry Smith {
7511f3b1f45cSBarry Smith   Mat            J,B;
7512f3b1f45cSBarry Smith   PetscErrorCode ierr;
7513f3b1f45cSBarry Smith   void           *ctx;
7514f3b1f45cSBarry Smith   TSRHSJacobian  func;
7515f3b1f45cSBarry Smith 
7516f3b1f45cSBarry Smith   PetscFunctionBegin;
7517f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7518f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7519f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7520f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7521f3b1f45cSBarry Smith }
75220fe4d17eSHong Zhang 
75230fe4d17eSHong Zhang /*@
75240fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
75250fe4d17eSHong Zhang 
75260fe4d17eSHong Zhang   Logically collective
75270fe4d17eSHong Zhang 
75280fe4d17eSHong Zhang   Input Parameter:
75290fe4d17eSHong Zhang +  ts - timestepping context
75300fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
75310fe4d17eSHong Zhang 
75320fe4d17eSHong Zhang   Options Database:
75330fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
75340fe4d17eSHong Zhang 
75350fe4d17eSHong Zhang   Notes:
75360fe4d17eSHong Zhang     This is only useful for multirate methods
75370fe4d17eSHong Zhang 
75380fe4d17eSHong Zhang   Level: intermediate
75390fe4d17eSHong Zhang 
75400fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
75410fe4d17eSHong Zhang @*/
75420fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
75430fe4d17eSHong Zhang {
75440fe4d17eSHong Zhang   PetscFunctionBegin;
75450fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
75460fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
75470fe4d17eSHong Zhang   PetscFunctionReturn(0);
75480fe4d17eSHong Zhang }
75490fe4d17eSHong Zhang 
75500fe4d17eSHong Zhang /*@
75510fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
75520fe4d17eSHong Zhang 
75530fe4d17eSHong Zhang   Not collective
75540fe4d17eSHong Zhang 
75550fe4d17eSHong Zhang   Input Parameter:
75560fe4d17eSHong Zhang .  ts - timestepping context
75570fe4d17eSHong Zhang 
75580fe4d17eSHong Zhang   Output Parameter:
75590fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
75600fe4d17eSHong Zhang 
75610fe4d17eSHong Zhang   Level: intermediate
75620fe4d17eSHong Zhang 
75630fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
75640fe4d17eSHong Zhang @*/
75650fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
75660fe4d17eSHong Zhang {
75670fe4d17eSHong Zhang   PetscFunctionBegin;
75680fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
75690fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
75700fe4d17eSHong Zhang   PetscFunctionReturn(0);
75710fe4d17eSHong Zhang }
7572