xref: /petsc/src/ts/interface/ts.c (revision a264d7a6a0f1b632cad8e878dc337a9c96437ee3)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
144a2ae208SSatish Balay #undef __FUNCT__
15bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
16bdad233fSMatthew Knepley /*
17bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Collective on TS
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Input Parameter:
22bdad233fSMatthew Knepley . ts - The ts
23bdad233fSMatthew Knepley 
24bdad233fSMatthew Knepley   Level: intermediate
25bdad233fSMatthew Knepley 
26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
28bdad233fSMatthew Knepley */
296849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
30bdad233fSMatthew Knepley {
31ace3abfcSBarry Smith   PetscBool      opt;
322fc52814SBarry Smith   const char     *defaultType;
33bdad233fSMatthew Knepley   char           typeName[256];
34dfbe8321SBarry Smith   PetscErrorCode ierr;
35bdad233fSMatthew Knepley 
36bdad233fSMatthew Knepley   PetscFunctionBegin;
37bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
38bbd56ea5SKarl Rupp   else defaultType = TSEULER;
39bdad233fSMatthew Knepley 
40607a6623SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);}
41*a264d7a6SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
42a7cc72afSBarry Smith   if (opt) {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   } else {
45bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
46bdad233fSMatthew Knepley   }
47bdad233fSMatthew Knepley   PetscFunctionReturn(0);
48bdad233fSMatthew Knepley }
49bdad233fSMatthew Knepley 
502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
512d5ee99bSBarry Smith   PetscViewer   viewer;
524b363babSBarry Smith   PetscDrawAxis axis;
532d5ee99bSBarry Smith   Vec           initialsolution;
542d5ee99bSBarry Smith   PetscBool     showinitial;
552d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
562d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
572d5ee99bSBarry Smith   int           color;
582d5ee99bSBarry Smith };
592d5ee99bSBarry Smith 
60bdad233fSMatthew Knepley #undef __FUNCT__
61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
62bdad233fSMatthew Knepley /*@
63bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Collective on TS
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley    Input Parameter:
68bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
69bdad233fSMatthew Knepley 
70bdad233fSMatthew Knepley    Options Database Keys:
714d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
72bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
733bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
74bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
75bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
76de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
77de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
78de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
79de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
80de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
81de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
82de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
832d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
84de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
8591b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
8691b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
8791b97e58SBarry Smith 
8891b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
89bdad233fSMatthew Knepley 
90bdad233fSMatthew Knepley    Level: beginner
91bdad233fSMatthew Knepley 
92bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
93bdad233fSMatthew Knepley 
94a313700dSBarry Smith .seealso: TSGetType()
95bdad233fSMatthew Knepley @*/
967087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
97bdad233fSMatthew Knepley {
98ace3abfcSBarry Smith   PetscBool              opt,flg;
99dfbe8321SBarry Smith   PetscErrorCode         ierr;
100649052a6SBarry Smith   PetscViewer            monviewer;
101eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
102089b2837SJed Brown   SNES                   snes;
1031c3436cfSJed Brown   TSAdapt                adapt;
10431748224SBarry Smith   PetscReal              time_step;
10549354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
106d1212d36SBarry Smith   char                   dir[16];
107bdad233fSMatthew Knepley 
108bdad233fSMatthew Knepley   PetscFunctionBegin;
1090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1103194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
111a43b19c4SJed Brown   /* Handle TS type options */
112a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley   /* Handle generic TS options */
1150298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1160298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1170298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11831748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
11931748224SBarry Smith   if (flg) {
12031748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12131748224SBarry Smith   }
12249354f04SShri 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);
12349354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1240298fd71SBarry 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);
1250298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1260298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1280298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
129bdad233fSMatthew Knepley 
13056f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13156f85f32SBarry Smith   {
13256f85f32SBarry Smith   PetscBool set;
13356f85f32SBarry Smith   flg  = PETSC_FALSE;
13456f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
13556f85f32SBarry Smith   if (set) {
13656f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
13756f85f32SBarry Smith   }
13856f85f32SBarry Smith   }
13956f85f32SBarry Smith #endif
14056f85f32SBarry Smith 
141bdad233fSMatthew Knepley   /* Monitor options */
142a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
143eabae89aSBarry Smith   if (flg) {
144ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
145649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
146bdad233fSMatthew Knepley   }
1475180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1485180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1495180491cSLisandro Dalcin 
1504f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
151a7cc72afSBarry Smith   if (opt) {
1520b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1533923b477SBarry Smith     PetscInt       howoften = 1;
154a80ad3e0SBarry Smith 
1550298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
156ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1574f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
158b3603a34SBarry Smith   }
1594f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
160b3603a34SBarry Smith   if (opt) {
1610b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1623923b477SBarry Smith     PetscInt       howoften = 1;
163b3603a34SBarry Smith 
1640298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
16522d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1664f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
167bdad233fSMatthew Knepley   }
1684f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
169ef20d060SBarry Smith   if (opt) {
1700b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1713923b477SBarry Smith     PetscInt       howoften = 1;
172ef20d060SBarry Smith 
1730298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
17422d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1754f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
176ef20d060SBarry Smith   }
177201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
178201da799SBarry Smith   if (opt) {
179201da799SBarry Smith     TSMonitorLGCtx ctx;
180201da799SBarry Smith     PetscInt       howoften = 1;
181201da799SBarry Smith 
1820298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
18322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
184201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
185201da799SBarry Smith   }
186201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
187201da799SBarry Smith   if (opt) {
188201da799SBarry Smith     TSMonitorLGCtx ctx;
189201da799SBarry Smith     PetscInt       howoften = 1;
190201da799SBarry Smith 
1910298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
19222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
193201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
194201da799SBarry Smith   }
1958189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1968189c53fSBarry Smith   if (opt) {
1978189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1988189c53fSBarry Smith     PetscInt          howoften = 1;
1998189c53fSBarry Smith 
2000298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
20122d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2028189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2038189c53fSBarry Smith   }
204ef20d060SBarry Smith   opt  = PETSC_FALSE;
2050dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
206a7cc72afSBarry Smith   if (opt) {
20783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
20883a4ac43SBarry Smith     PetscInt         howoften = 1;
209a80ad3e0SBarry Smith 
2100298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
211ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
21283a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
213bdad233fSMatthew Knepley   }
214fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2152d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2162d5ee99bSBarry Smith   if (opt) {
2172d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2182d5ee99bSBarry Smith     PetscReal        bounds[4];
2192d5ee99bSBarry Smith     PetscInt         n = 4;
2202d5ee99bSBarry Smith     PetscDraw        draw;
2212d5ee99bSBarry Smith 
2222d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2232d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2242d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2252d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2262d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2274b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2284b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
229f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2304b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
23107995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2322d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2332d5ee99bSBarry Smith   }
2342d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2350dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2363a471f94SBarry Smith   if (opt) {
23783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
23883a4ac43SBarry Smith     PetscInt         howoften = 1;
2393a471f94SBarry Smith 
2400298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
241ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
24283a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2433a471f94SBarry Smith   }
2443a471f94SBarry Smith   opt  = PETSC_FALSE;
24591b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
246fb1732b5SBarry Smith   if (flg) {
247fb1732b5SBarry Smith     PetscViewer ctx;
248fb1732b5SBarry Smith     if (monfilename[0]) {
249ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
250c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
251fb1732b5SBarry Smith     } else {
252ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2530298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
254fb1732b5SBarry Smith     }
255fb1732b5SBarry Smith   }
256ed81e22dSJed Brown   opt  = PETSC_FALSE;
25791b97e58SBarry 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);
258ed81e22dSJed Brown   if (flg) {
259ed81e22dSJed Brown     const char *ptr,*ptr2;
260ed81e22dSJed Brown     char       *filetemplate;
261ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
262ed81e22dSJed Brown     /* Do some cursory validation of the input. */
263ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
264ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
265ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
266ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
267ce94432eSBarry 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");
268ed81e22dSJed Brown       if (ptr2) break;
269ed81e22dSJed Brown     }
270ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
271ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
272ed81e22dSJed Brown   }
273bdad233fSMatthew Knepley 
274d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
275d1212d36SBarry Smith   if (flg) {
276d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
277d1212d36SBarry Smith     int                 ray = 0;
278d1212d36SBarry Smith     DMDADirection       ddir;
279d1212d36SBarry Smith     DM                  da;
280d1212d36SBarry Smith     PetscMPIInt         rank;
281d1212d36SBarry Smith 
282ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
283d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
284d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
285ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
286d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
287d1212d36SBarry Smith 
288d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
289d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
290d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
291d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
292ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
293d1212d36SBarry Smith     if (!rank) {
294d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
295d1212d36SBarry Smith     }
296d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
297d1212d36SBarry Smith   }
298d1212d36SBarry Smith 
2997781c08eSBarry Smith   /*
3007781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3017781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3027781c08eSBarry Smith   */
303552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
3041c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
3051c3436cfSJed Brown 
306d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
307d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
308d52bd9f3SBarry Smith 
309bdad233fSMatthew Knepley   /* Handle specific TS options */
310abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
311bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
312bdad233fSMatthew Knepley   }
3135d973c19SBarry Smith 
3145d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3155d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
316bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
317bdad233fSMatthew Knepley   PetscFunctionReturn(0);
318bdad233fSMatthew Knepley }
319bdad233fSMatthew Knepley 
320bdad233fSMatthew Knepley #undef __FUNCT__
321cdcf91faSSean Farley #undef __FUNCT__
3224a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
323a7a1495cSBarry Smith /*@
3248c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
325a7a1495cSBarry Smith       set with TSSetRHSJacobian().
326a7a1495cSBarry Smith 
327a7a1495cSBarry Smith    Collective on TS and Vec
328a7a1495cSBarry Smith 
329a7a1495cSBarry Smith    Input Parameters:
330316643e7SJed Brown +  ts - the TS context
331a7a1495cSBarry Smith .  t - current timestep
3320910c330SBarry Smith -  U - input vector
333a7a1495cSBarry Smith 
334a7a1495cSBarry Smith    Output Parameters:
335a7a1495cSBarry Smith +  A - Jacobian matrix
336a7a1495cSBarry Smith .  B - optional preconditioning matrix
337a7a1495cSBarry Smith -  flag - flag indicating matrix structure
338a7a1495cSBarry Smith 
339a7a1495cSBarry Smith    Notes:
340a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
341a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
342a7a1495cSBarry Smith 
34394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
344a7a1495cSBarry Smith    flag parameter.
345a7a1495cSBarry Smith 
346a7a1495cSBarry Smith    Level: developer
347a7a1495cSBarry Smith 
348a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
349a7a1495cSBarry Smith 
35094b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
351a7a1495cSBarry Smith @*/
3520910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
353a7a1495cSBarry Smith {
354dfbe8321SBarry Smith   PetscErrorCode ierr;
3550910c330SBarry Smith   PetscInt       Ustate;
35624989b8cSPeter Brune   DM             dm;
357942e3340SBarry Smith   DMTS           tsdm;
35824989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
35924989b8cSPeter Brune   void           *ctx;
36024989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
361a7a1495cSBarry Smith 
362a7a1495cSBarry Smith   PetscFunctionBegin;
3630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3640910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3650910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
36624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
367942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
36824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3690298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
3700910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3710910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3720e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3730e4ef248SJed Brown     PetscFunctionReturn(0);
374f8ede8e7SJed Brown   }
375d90be118SSean Farley 
376ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
377d90be118SSean Farley 
378e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
379e1244c69SJed Brown     ierr = MatShift(*A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
380e1244c69SJed Brown     ierr = MatScale(*A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
381e1244c69SJed Brown     if (*A != *B) {
382e1244c69SJed Brown       ierr = MatShift(*B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
383e1244c69SJed Brown       ierr = MatScale(*B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
384e1244c69SJed Brown     }
385e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
386e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
387e1244c69SJed Brown   }
388e1244c69SJed Brown 
38924989b8cSPeter Brune   if (rhsjacobianfunc) {
3900910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
391a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
392a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3930910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
394a7a1495cSBarry Smith     PetscStackPop;
3950910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
396a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3970700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3980700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
399ef66eb69SBarry Smith   } else {
400214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
401214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
402214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
403ef66eb69SBarry Smith   }
4040e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4050910c330SBarry Smith   ts->rhsjacobian.X          = U;
4060910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
4070e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
408a7a1495cSBarry Smith   PetscFunctionReturn(0);
409a7a1495cSBarry Smith }
410a7a1495cSBarry Smith 
4114a2ae208SSatish Balay #undef __FUNCT__
4124a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
413316643e7SJed Brown /*@
414d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
415d763cef2SBarry Smith 
416316643e7SJed Brown    Collective on TS and Vec
417316643e7SJed Brown 
418316643e7SJed Brown    Input Parameters:
419316643e7SJed Brown +  ts - the TS context
420316643e7SJed Brown .  t - current time
4210910c330SBarry Smith -  U - state vector
422316643e7SJed Brown 
423316643e7SJed Brown    Output Parameter:
424316643e7SJed Brown .  y - right hand side
425316643e7SJed Brown 
426316643e7SJed Brown    Note:
427316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
428316643e7SJed Brown    is used internally within the nonlinear solvers.
429316643e7SJed Brown 
430316643e7SJed Brown    Level: developer
431316643e7SJed Brown 
432316643e7SJed Brown .keywords: TS, compute
433316643e7SJed Brown 
434316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
435316643e7SJed Brown @*/
4360910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
437d763cef2SBarry Smith {
438dfbe8321SBarry Smith   PetscErrorCode ierr;
43924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
44024989b8cSPeter Brune   TSIFunction    ifunction;
44124989b8cSPeter Brune   void           *ctx;
44224989b8cSPeter Brune   DM             dm;
44324989b8cSPeter Brune 
444d763cef2SBarry Smith   PetscFunctionBegin;
4450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4470700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
44824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
44924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4500298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
451d763cef2SBarry Smith 
452ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
453d763cef2SBarry Smith 
4540910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
45524989b8cSPeter Brune   if (rhsfunction) {
456d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4570910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
458d763cef2SBarry Smith     PetscStackPop;
459214bc6a2SJed Brown   } else {
460214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
461b2cd27e8SJed Brown   }
46244a41b28SSean Farley 
4630910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
464d763cef2SBarry Smith   PetscFunctionReturn(0);
465d763cef2SBarry Smith }
466d763cef2SBarry Smith 
4674a2ae208SSatish Balay #undef __FUNCT__
468ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
469ef20d060SBarry Smith /*@
470ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
471ef20d060SBarry Smith 
472ef20d060SBarry Smith    Collective on TS and Vec
473ef20d060SBarry Smith 
474ef20d060SBarry Smith    Input Parameters:
475ef20d060SBarry Smith +  ts - the TS context
476ef20d060SBarry Smith -  t - current time
477ef20d060SBarry Smith 
478ef20d060SBarry Smith    Output Parameter:
4790910c330SBarry Smith .  U - the solution
480ef20d060SBarry Smith 
481ef20d060SBarry Smith    Note:
482ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
483ef20d060SBarry Smith    is used internally within the nonlinear solvers.
484ef20d060SBarry Smith 
485ef20d060SBarry Smith    Level: developer
486ef20d060SBarry Smith 
487ef20d060SBarry Smith .keywords: TS, compute
488ef20d060SBarry Smith 
489abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
490ef20d060SBarry Smith @*/
4910910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
492ef20d060SBarry Smith {
493ef20d060SBarry Smith   PetscErrorCode     ierr;
494ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
495ef20d060SBarry Smith   void               *ctx;
496ef20d060SBarry Smith   DM                 dm;
497ef20d060SBarry Smith 
498ef20d060SBarry Smith   PetscFunctionBegin;
499ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5000910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
501ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
502ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
503ef20d060SBarry Smith 
504ef20d060SBarry Smith   if (solutionfunction) {
5059b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5060910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
507ef20d060SBarry Smith     PetscStackPop;
508ef20d060SBarry Smith   }
509ef20d060SBarry Smith   PetscFunctionReturn(0);
510ef20d060SBarry Smith }
5119b7cd975SBarry Smith #undef __FUNCT__
5129b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5139b7cd975SBarry Smith /*@
5149b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5159b7cd975SBarry Smith 
5169b7cd975SBarry Smith    Collective on TS and Vec
5179b7cd975SBarry Smith 
5189b7cd975SBarry Smith    Input Parameters:
5199b7cd975SBarry Smith +  ts - the TS context
5209b7cd975SBarry Smith -  t - current time
5219b7cd975SBarry Smith 
5229b7cd975SBarry Smith    Output Parameter:
5239b7cd975SBarry Smith .  U - the function value
5249b7cd975SBarry Smith 
5259b7cd975SBarry Smith    Note:
5269b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5279b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5289b7cd975SBarry Smith 
5299b7cd975SBarry Smith    Level: developer
5309b7cd975SBarry Smith 
5319b7cd975SBarry Smith .keywords: TS, compute
5329b7cd975SBarry Smith 
5339b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5349b7cd975SBarry Smith @*/
5359b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5369b7cd975SBarry Smith {
5379b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5389b7cd975SBarry Smith   void               *ctx;
5399b7cd975SBarry Smith   DM                 dm;
5409b7cd975SBarry Smith 
5419b7cd975SBarry Smith   PetscFunctionBegin;
5429b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5439b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5449b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5459b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5469b7cd975SBarry Smith 
5479b7cd975SBarry Smith   if (forcing) {
5489b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5499b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5509b7cd975SBarry Smith     PetscStackPop;
5519b7cd975SBarry Smith   }
5529b7cd975SBarry Smith   PetscFunctionReturn(0);
5539b7cd975SBarry Smith }
554ef20d060SBarry Smith 
555ef20d060SBarry Smith #undef __FUNCT__
556214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
557214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
558214bc6a2SJed Brown {
5592dd45cf8SJed Brown   Vec            F;
560214bc6a2SJed Brown   PetscErrorCode ierr;
561214bc6a2SJed Brown 
562214bc6a2SJed Brown   PetscFunctionBegin;
5630298fd71SBarry Smith   *Frhs = NULL;
5640298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
565214bc6a2SJed Brown   if (!ts->Frhs) {
5662dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
567214bc6a2SJed Brown   }
568214bc6a2SJed Brown   *Frhs = ts->Frhs;
569214bc6a2SJed Brown   PetscFunctionReturn(0);
570214bc6a2SJed Brown }
571214bc6a2SJed Brown 
572214bc6a2SJed Brown #undef __FUNCT__
573214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
574214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
575214bc6a2SJed Brown {
576214bc6a2SJed Brown   Mat            A,B;
5772dd45cf8SJed Brown   PetscErrorCode ierr;
578214bc6a2SJed Brown 
579214bc6a2SJed Brown   PetscFunctionBegin;
5800298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
581214bc6a2SJed Brown   if (Arhs) {
582214bc6a2SJed Brown     if (!ts->Arhs) {
583214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
584214bc6a2SJed Brown     }
585214bc6a2SJed Brown     *Arhs = ts->Arhs;
586214bc6a2SJed Brown   }
587214bc6a2SJed Brown   if (Brhs) {
588214bc6a2SJed Brown     if (!ts->Brhs) {
589214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
590214bc6a2SJed Brown     }
591214bc6a2SJed Brown     *Brhs = ts->Brhs;
592214bc6a2SJed Brown   }
593214bc6a2SJed Brown   PetscFunctionReturn(0);
594214bc6a2SJed Brown }
595214bc6a2SJed Brown 
596214bc6a2SJed Brown #undef __FUNCT__
597316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
598316643e7SJed Brown /*@
5990910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
600316643e7SJed Brown 
601316643e7SJed Brown    Collective on TS and Vec
602316643e7SJed Brown 
603316643e7SJed Brown    Input Parameters:
604316643e7SJed Brown +  ts - the TS context
605316643e7SJed Brown .  t - current time
6060910c330SBarry Smith .  U - state vector
6070910c330SBarry Smith .  Udot - time derivative of state vector
608214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
609316643e7SJed Brown 
610316643e7SJed Brown    Output Parameter:
611316643e7SJed Brown .  Y - right hand side
612316643e7SJed Brown 
613316643e7SJed Brown    Note:
614316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
615316643e7SJed Brown    is used internally within the nonlinear solvers.
616316643e7SJed Brown 
617316643e7SJed Brown    If the user did did not write their equations in implicit form, this
618316643e7SJed Brown    function recasts them in implicit form.
619316643e7SJed Brown 
620316643e7SJed Brown    Level: developer
621316643e7SJed Brown 
622316643e7SJed Brown .keywords: TS, compute
623316643e7SJed Brown 
624316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
625316643e7SJed Brown @*/
6260910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
627316643e7SJed Brown {
628316643e7SJed Brown   PetscErrorCode ierr;
62924989b8cSPeter Brune   TSIFunction    ifunction;
63024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
63124989b8cSPeter Brune   void           *ctx;
63224989b8cSPeter Brune   DM             dm;
633316643e7SJed Brown 
634316643e7SJed Brown   PetscFunctionBegin;
6350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6360910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6370910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6380700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
639316643e7SJed Brown 
64024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
64124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6420298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
64324989b8cSPeter Brune 
644ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
645d90be118SSean Farley 
6460910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
64724989b8cSPeter Brune   if (ifunction) {
648316643e7SJed Brown     PetscStackPush("TS user implicit function");
6490910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
650316643e7SJed Brown     PetscStackPop;
651214bc6a2SJed Brown   }
652214bc6a2SJed Brown   if (imex) {
65324989b8cSPeter Brune     if (!ifunction) {
6540910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6552dd45cf8SJed Brown     }
65624989b8cSPeter Brune   } else if (rhsfunction) {
65724989b8cSPeter Brune     if (ifunction) {
658214bc6a2SJed Brown       Vec Frhs;
659214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6600910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
661214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6622dd45cf8SJed Brown     } else {
6630910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6640910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
665316643e7SJed Brown     }
6664a6899ffSJed Brown   }
6670910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
668316643e7SJed Brown   PetscFunctionReturn(0);
669316643e7SJed Brown }
670316643e7SJed Brown 
671316643e7SJed Brown #undef __FUNCT__
672316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
673316643e7SJed Brown /*@
674316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
675316643e7SJed Brown 
676316643e7SJed Brown    Collective on TS and Vec
677316643e7SJed Brown 
678316643e7SJed Brown    Input
679316643e7SJed Brown       Input Parameters:
680316643e7SJed Brown +  ts - the TS context
681316643e7SJed Brown .  t - current timestep
6820910c330SBarry Smith .  U - state vector
6830910c330SBarry Smith .  Udot - time derivative of state vector
684214bc6a2SJed Brown .  shift - shift to apply, see note below
685214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
686316643e7SJed Brown 
687316643e7SJed Brown    Output Parameters:
688316643e7SJed Brown +  A - Jacobian matrix
689316643e7SJed Brown .  B - optional preconditioning matrix
690316643e7SJed Brown -  flag - flag indicating matrix structure
691316643e7SJed Brown 
692316643e7SJed Brown    Notes:
6930910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
694316643e7SJed Brown 
6950910c330SBarry Smith    dF/dU + shift*dF/dUdot
696316643e7SJed Brown 
697316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
698316643e7SJed Brown    is used internally within the nonlinear solvers.
699316643e7SJed Brown 
700316643e7SJed Brown    Level: developer
701316643e7SJed Brown 
702316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
703316643e7SJed Brown 
704316643e7SJed Brown .seealso:  TSSetIJacobian()
70563495f91SJed Brown @*/
7060910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
707316643e7SJed Brown {
708316643e7SJed Brown   PetscErrorCode ierr;
70924989b8cSPeter Brune   TSIJacobian    ijacobian;
71024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
71124989b8cSPeter Brune   DM             dm;
71224989b8cSPeter Brune   void           *ctx;
713316643e7SJed Brown 
714316643e7SJed Brown   PetscFunctionBegin;
7150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7170910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
718316643e7SJed Brown   PetscValidPointer(A,6);
7190700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
720316643e7SJed Brown   PetscValidPointer(B,7);
7210700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
722316643e7SJed Brown   PetscValidPointer(flg,8);
72324989b8cSPeter Brune 
72424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
72524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7260298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
72724989b8cSPeter Brune 
728ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
729316643e7SJed Brown 
7304e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7310910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
73224989b8cSPeter Brune   if (ijacobian) {
7332dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
734316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7350910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
736316643e7SJed Brown     PetscStackPop;
737214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
738214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
739214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7404a6899ffSJed Brown   }
741214bc6a2SJed Brown   if (imex) {
742b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
743214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7442dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
745214bc6a2SJed Brown       if (*A != *B) {
746214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
747214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
748214bc6a2SJed Brown       }
749214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
750214bc6a2SJed Brown     }
751214bc6a2SJed Brown   } else {
752e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
753e1244c69SJed Brown     MatStructure flg2;
754e1244c69SJed Brown     if (rhsjacobian) {
755e1244c69SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
756e1244c69SJed Brown       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
757e1244c69SJed Brown     }
758e1244c69SJed Brown     if (Arhs == *A) {           /* No IJacobian, so we only have the RHS matrix */
759e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
760e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
761214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
762214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
763316643e7SJed Brown       if (*A != *B) {
764316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
765316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
766316643e7SJed Brown       }
767e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
768e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
769e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
770e1244c69SJed Brown         ierr = MatZeroEntries(*A);CHKERRQ(ierr);
771e1244c69SJed Brown         ierr = MatShift(*A,shift);CHKERRQ(ierr);
772e1244c69SJed Brown         if (*A != *B) {
773e1244c69SJed Brown           ierr = MatZeroEntries(*B);CHKERRQ(ierr);
774e1244c69SJed Brown           ierr = MatShift(*B,shift);CHKERRQ(ierr);
775e1244c69SJed Brown         }
776e1244c69SJed Brown       }
777214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
778214bc6a2SJed Brown       if (*A != *B) {
779214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
780214bc6a2SJed Brown       }
781214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
782214bc6a2SJed Brown     }
783316643e7SJed Brown   }
7840026cea9SSean Farley 
7850910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
786316643e7SJed Brown   PetscFunctionReturn(0);
787316643e7SJed Brown }
788316643e7SJed Brown 
789316643e7SJed Brown #undef __FUNCT__
7904a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
791d763cef2SBarry Smith /*@C
792d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
793b5abc632SBarry Smith     where U_t = G(t,u).
794d763cef2SBarry Smith 
7953f9fe445SBarry Smith     Logically Collective on TS
796d763cef2SBarry Smith 
797d763cef2SBarry Smith     Input Parameters:
798d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
7990298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
800d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
801d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8020298fd71SBarry Smith           function evaluation routine (may be NULL)
803d763cef2SBarry Smith 
804d763cef2SBarry Smith     Calling sequence of func:
80587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
806d763cef2SBarry Smith 
807d763cef2SBarry Smith +   t - current timestep
808d763cef2SBarry Smith .   u - input vector
809d763cef2SBarry Smith .   F - function vector
810d763cef2SBarry Smith -   ctx - [optional] user-defined function context
811d763cef2SBarry Smith 
812d763cef2SBarry Smith     Level: beginner
813d763cef2SBarry Smith 
814d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
815d763cef2SBarry Smith 
816d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
817d763cef2SBarry Smith @*/
818089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
819d763cef2SBarry Smith {
820089b2837SJed Brown   PetscErrorCode ierr;
821089b2837SJed Brown   SNES           snes;
8220298fd71SBarry Smith   Vec            ralloc = NULL;
82324989b8cSPeter Brune   DM             dm;
824d763cef2SBarry Smith 
825089b2837SJed Brown   PetscFunctionBegin;
8260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
827ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
82824989b8cSPeter Brune 
82924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
83024989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
831089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
832e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
833e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
834e856ceecSJed Brown     r    = ralloc;
835e856ceecSJed Brown   }
836089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
837e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
838d763cef2SBarry Smith   PetscFunctionReturn(0);
839d763cef2SBarry Smith }
840d763cef2SBarry Smith 
8414a2ae208SSatish Balay #undef __FUNCT__
842ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
843ef20d060SBarry Smith /*@C
844abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
845ef20d060SBarry Smith 
846ef20d060SBarry Smith     Logically Collective on TS
847ef20d060SBarry Smith 
848ef20d060SBarry Smith     Input Parameters:
849ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
850ef20d060SBarry Smith .   f - routine for evaluating the solution
851ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8520298fd71SBarry Smith           function evaluation routine (may be NULL)
853ef20d060SBarry Smith 
854ef20d060SBarry Smith     Calling sequence of func:
855ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
856ef20d060SBarry Smith 
857ef20d060SBarry Smith +   t - current timestep
858ef20d060SBarry Smith .   u - output vector
859ef20d060SBarry Smith -   ctx - [optional] user-defined function context
860ef20d060SBarry Smith 
861abd5a294SJed Brown     Notes:
862abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
863abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
864abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
865abd5a294SJed Brown 
8664f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
867abd5a294SJed Brown 
868ef20d060SBarry Smith     Level: beginner
869ef20d060SBarry Smith 
870ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
871ef20d060SBarry Smith 
8729b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
873ef20d060SBarry Smith @*/
874ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
875ef20d060SBarry Smith {
876ef20d060SBarry Smith   PetscErrorCode ierr;
877ef20d060SBarry Smith   DM             dm;
878ef20d060SBarry Smith 
879ef20d060SBarry Smith   PetscFunctionBegin;
880ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
881ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
882ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
883ef20d060SBarry Smith   PetscFunctionReturn(0);
884ef20d060SBarry Smith }
885ef20d060SBarry Smith 
886ef20d060SBarry Smith #undef __FUNCT__
8879b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8889b7cd975SBarry Smith /*@C
8899b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8909b7cd975SBarry Smith 
8919b7cd975SBarry Smith     Logically Collective on TS
8929b7cd975SBarry Smith 
8939b7cd975SBarry Smith     Input Parameters:
8949b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
8959b7cd975SBarry Smith .   f - routine for evaluating the forcing function
8969b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
8970298fd71SBarry Smith           function evaluation routine (may be NULL)
8989b7cd975SBarry Smith 
8999b7cd975SBarry Smith     Calling sequence of func:
9009b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9019b7cd975SBarry Smith 
9029b7cd975SBarry Smith +   t - current timestep
9039b7cd975SBarry Smith .   u - output vector
9049b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9059b7cd975SBarry Smith 
9069b7cd975SBarry Smith     Notes:
9079b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9089b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9099b7cd975SBarry Smith 
9109b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9119b7cd975SBarry Smith 
9129b7cd975SBarry Smith     Level: beginner
9139b7cd975SBarry Smith 
9149b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9159b7cd975SBarry Smith 
9169b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9179b7cd975SBarry Smith @*/
9189b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9199b7cd975SBarry Smith {
9209b7cd975SBarry Smith   PetscErrorCode ierr;
9219b7cd975SBarry Smith   DM             dm;
9229b7cd975SBarry Smith 
9239b7cd975SBarry Smith   PetscFunctionBegin;
9249b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9259b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9269b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9279b7cd975SBarry Smith   PetscFunctionReturn(0);
9289b7cd975SBarry Smith }
9299b7cd975SBarry Smith 
9309b7cd975SBarry Smith #undef __FUNCT__
9314a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
932d763cef2SBarry Smith /*@C
933d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
934b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
935d763cef2SBarry Smith 
9363f9fe445SBarry Smith    Logically Collective on TS
937d763cef2SBarry Smith 
938d763cef2SBarry Smith    Input Parameters:
939d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
940e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
941e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
942d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
943d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9440298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
945d763cef2SBarry Smith 
946d763cef2SBarry Smith    Calling sequence of func:
94787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
948d763cef2SBarry Smith 
949d763cef2SBarry Smith +  t - current timestep
950d763cef2SBarry Smith .  u - input vector
951e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
952e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
953d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
95494b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
955d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
956d763cef2SBarry Smith 
957d763cef2SBarry Smith    Notes:
95894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
959d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
960d763cef2SBarry Smith 
961d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
962d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
96356335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
964d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
965d763cef2SBarry Smith    throughout the global iterations.
966d763cef2SBarry Smith 
967d763cef2SBarry Smith    Level: beginner
968d763cef2SBarry Smith 
969d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
970d763cef2SBarry Smith 
971e1244c69SJed Brown .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse()
972d763cef2SBarry Smith 
973d763cef2SBarry Smith @*/
974e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
975d763cef2SBarry Smith {
976277b19d0SLisandro Dalcin   PetscErrorCode ierr;
977089b2837SJed Brown   SNES           snes;
97824989b8cSPeter Brune   DM             dm;
97924989b8cSPeter Brune   TSIJacobian    ijacobian;
980277b19d0SLisandro Dalcin 
981d763cef2SBarry Smith   PetscFunctionBegin;
9820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
983e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
984e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
985e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
986e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
987d763cef2SBarry Smith 
98824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
98924989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
990e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
991e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
992e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
993e1244c69SJed Brown   }
9940298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
995089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
9965f659677SPeter Brune   if (!ijacobian) {
997e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
9980e4ef248SJed Brown   }
999e5d3d808SBarry Smith   if (Amat) {
1000e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10010e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1002bbd56ea5SKarl Rupp 
1003e5d3d808SBarry Smith     ts->Arhs = Amat;
10040e4ef248SJed Brown   }
1005e5d3d808SBarry Smith   if (Pmat) {
1006e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10070e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1008bbd56ea5SKarl Rupp 
1009e5d3d808SBarry Smith     ts->Brhs = Pmat;
10100e4ef248SJed Brown   }
1011d763cef2SBarry Smith   PetscFunctionReturn(0);
1012d763cef2SBarry Smith }
1013d763cef2SBarry Smith 
1014316643e7SJed Brown 
1015316643e7SJed Brown #undef __FUNCT__
1016316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1017316643e7SJed Brown /*@C
1018b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1019316643e7SJed Brown 
10203f9fe445SBarry Smith    Logically Collective on TS
1021316643e7SJed Brown 
1022316643e7SJed Brown    Input Parameters:
1023316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10240298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1025316643e7SJed Brown .  f   - the function evaluation routine
10260298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1027316643e7SJed Brown 
1028316643e7SJed Brown    Calling sequence of f:
1029316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1030316643e7SJed Brown 
1031316643e7SJed Brown +  t   - time at step/stage being solved
1032316643e7SJed Brown .  u   - state vector
1033316643e7SJed Brown .  u_t - time derivative of state vector
1034316643e7SJed Brown .  F   - function vector
1035316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1036316643e7SJed Brown 
1037316643e7SJed Brown    Important:
1038d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1039316643e7SJed Brown 
1040316643e7SJed Brown    Level: beginner
1041316643e7SJed Brown 
1042316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1043316643e7SJed Brown 
1044d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1045316643e7SJed Brown @*/
1046089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1047316643e7SJed Brown {
1048089b2837SJed Brown   PetscErrorCode ierr;
1049089b2837SJed Brown   SNES           snes;
10500298fd71SBarry Smith   Vec            resalloc = NULL;
105124989b8cSPeter Brune   DM             dm;
1052316643e7SJed Brown 
1053316643e7SJed Brown   PetscFunctionBegin;
10540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1055ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
105624989b8cSPeter Brune 
105724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
105924989b8cSPeter Brune 
1060089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1061e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1062e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1063e856ceecSJed Brown     res  = resalloc;
1064e856ceecSJed Brown   }
1065089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1066e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1067089b2837SJed Brown   PetscFunctionReturn(0);
1068089b2837SJed Brown }
1069089b2837SJed Brown 
1070089b2837SJed Brown #undef __FUNCT__
1071089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1072089b2837SJed Brown /*@C
1073089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1074089b2837SJed Brown 
1075089b2837SJed Brown    Not Collective
1076089b2837SJed Brown 
1077089b2837SJed Brown    Input Parameter:
1078089b2837SJed Brown .  ts - the TS context
1079089b2837SJed Brown 
1080089b2837SJed Brown    Output Parameter:
10810298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10820298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10830298fd71SBarry Smith -  ctx - the function context (or NULL)
1084089b2837SJed Brown 
1085089b2837SJed Brown    Level: advanced
1086089b2837SJed Brown 
1087089b2837SJed Brown .keywords: TS, nonlinear, get, function
1088089b2837SJed Brown 
1089089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1090089b2837SJed Brown @*/
1091089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1092089b2837SJed Brown {
1093089b2837SJed Brown   PetscErrorCode ierr;
1094089b2837SJed Brown   SNES           snes;
109524989b8cSPeter Brune   DM             dm;
1096089b2837SJed Brown 
1097089b2837SJed Brown   PetscFunctionBegin;
1098089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1099089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11000298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
110124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1103089b2837SJed Brown   PetscFunctionReturn(0);
1104089b2837SJed Brown }
1105089b2837SJed Brown 
1106089b2837SJed Brown #undef __FUNCT__
1107089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1108089b2837SJed Brown /*@C
1109089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1110089b2837SJed Brown 
1111089b2837SJed Brown    Not Collective
1112089b2837SJed Brown 
1113089b2837SJed Brown    Input Parameter:
1114089b2837SJed Brown .  ts - the TS context
1115089b2837SJed Brown 
1116089b2837SJed Brown    Output Parameter:
11170298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11180298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11190298fd71SBarry Smith -  ctx - the function context (or NULL)
1120089b2837SJed Brown 
1121089b2837SJed Brown    Level: advanced
1122089b2837SJed Brown 
1123089b2837SJed Brown .keywords: TS, nonlinear, get, function
1124089b2837SJed Brown 
1125089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1126089b2837SJed Brown @*/
1127089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1128089b2837SJed Brown {
1129089b2837SJed Brown   PetscErrorCode ierr;
1130089b2837SJed Brown   SNES           snes;
113124989b8cSPeter Brune   DM             dm;
1132089b2837SJed Brown 
1133089b2837SJed Brown   PetscFunctionBegin;
1134089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1135089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11360298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
113724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
113824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1139316643e7SJed Brown   PetscFunctionReturn(0);
1140316643e7SJed Brown }
1141316643e7SJed Brown 
1142316643e7SJed Brown #undef __FUNCT__
1143316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1144316643e7SJed Brown /*@C
1145a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1146a4f0a591SBarry Smith         you provided with TSSetIFunction().
1147316643e7SJed Brown 
11483f9fe445SBarry Smith    Logically Collective on TS
1149316643e7SJed Brown 
1150316643e7SJed Brown    Input Parameters:
1151316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1152e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1153e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1154316643e7SJed Brown .  f   - the Jacobian evaluation routine
11550298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1156316643e7SJed Brown 
1157316643e7SJed Brown    Calling sequence of f:
1158e5d3d808SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx);
1159316643e7SJed Brown 
1160316643e7SJed Brown +  t    - time at step/stage being solved
11611b4a444bSJed Brown .  U    - state vector
11621b4a444bSJed Brown .  U_t  - time derivative of state vector
1163316643e7SJed Brown .  a    - shift
1164e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1165e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1166316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1167316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1168316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1169316643e7SJed Brown 
1170316643e7SJed Brown    Notes:
1171e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1172316643e7SJed Brown 
1173a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1174b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1175a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1176a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1177a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1178a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1179a4f0a591SBarry Smith 
1180316643e7SJed Brown    Level: beginner
1181316643e7SJed Brown 
1182316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1183316643e7SJed Brown 
11848d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault()
1185316643e7SJed Brown 
1186316643e7SJed Brown @*/
1187e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1188316643e7SJed Brown {
1189316643e7SJed Brown   PetscErrorCode ierr;
1190089b2837SJed Brown   SNES           snes;
119124989b8cSPeter Brune   DM             dm;
1192316643e7SJed Brown 
1193316643e7SJed Brown   PetscFunctionBegin;
11940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1195e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1196e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1197e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1198e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
119924989b8cSPeter Brune 
120024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120124989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
120224989b8cSPeter Brune 
1203089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1204e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1205316643e7SJed Brown   PetscFunctionReturn(0);
1206316643e7SJed Brown }
1207316643e7SJed Brown 
12084a2ae208SSatish Balay #undef __FUNCT__
1209e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1210e1244c69SJed Brown /*@
1211e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1212e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1213e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1214e1244c69SJed Brown    not been changed by the TS.
1215e1244c69SJed Brown 
1216e1244c69SJed Brown    Logically Collective
1217e1244c69SJed Brown 
1218e1244c69SJed Brown    Input Arguments:
1219e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1220e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1221e1244c69SJed Brown 
1222e1244c69SJed Brown    Level: intermediate
1223e1244c69SJed Brown 
1224e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1225e1244c69SJed Brown @*/
1226e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1227e1244c69SJed Brown {
1228e1244c69SJed Brown   PetscFunctionBegin;
1229e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1230e1244c69SJed Brown   PetscFunctionReturn(0);
1231e1244c69SJed Brown }
1232e1244c69SJed Brown 
1233e1244c69SJed Brown #undef __FUNCT__
123455849f57SBarry Smith #define __FUNCT__ "TSLoad"
123555849f57SBarry Smith /*@C
123655849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
123755849f57SBarry Smith 
123855849f57SBarry Smith   Collective on PetscViewer
123955849f57SBarry Smith 
124055849f57SBarry Smith   Input Parameters:
124155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
124255849f57SBarry Smith            some related function before a call to TSLoad().
124355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
124455849f57SBarry Smith 
124555849f57SBarry Smith    Level: intermediate
124655849f57SBarry Smith 
124755849f57SBarry Smith   Notes:
124855849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
124955849f57SBarry Smith 
125055849f57SBarry Smith   Notes for advanced users:
125155849f57SBarry Smith   Most users should not need to know the details of the binary storage
125255849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
125355849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
125455849f57SBarry Smith   format is
125555849f57SBarry Smith .vb
125655849f57SBarry Smith      has not yet been determined
125755849f57SBarry Smith .ve
125855849f57SBarry Smith 
125955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
126055849f57SBarry Smith @*/
1261f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
126255849f57SBarry Smith {
126355849f57SBarry Smith   PetscErrorCode ierr;
126455849f57SBarry Smith   PetscBool      isbinary;
126555849f57SBarry Smith   PetscInt       classid;
126655849f57SBarry Smith   char           type[256];
12672d53ad75SBarry Smith   DMTS           sdm;
1268ad6bc421SBarry Smith   DM             dm;
126955849f57SBarry Smith 
127055849f57SBarry Smith   PetscFunctionBegin;
1271f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
127255849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
127355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
127455849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
127555849f57SBarry Smith 
127655849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1277ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
127855849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1279f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1280f2c2a1b9SBarry Smith   if (ts->ops->load) {
1281f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1282f2c2a1b9SBarry Smith   }
1283ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1284ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1285ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1286f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1287f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12882d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12892d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
129055849f57SBarry Smith   PetscFunctionReturn(0);
129155849f57SBarry Smith }
129255849f57SBarry Smith 
12939804daf3SBarry Smith #include <petscdraw.h>
1294e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1295e04113cfSBarry Smith #include <petscviewersaws.h>
1296f05ece33SBarry Smith #endif
129755849f57SBarry Smith #undef __FUNCT__
12984a2ae208SSatish Balay #define __FUNCT__ "TSView"
12997e2c5f70SBarry Smith /*@C
1300d763cef2SBarry Smith     TSView - Prints the TS data structure.
1301d763cef2SBarry Smith 
13024c49b128SBarry Smith     Collective on TS
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith     Input Parameters:
1305d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1306d763cef2SBarry Smith -   viewer - visualization context
1307d763cef2SBarry Smith 
1308d763cef2SBarry Smith     Options Database Key:
1309d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith     Notes:
1312d763cef2SBarry Smith     The available visualization contexts include
1313b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1314b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1315d763cef2SBarry Smith          output where only the first processor opens
1316d763cef2SBarry Smith          the file.  All other processors send their
1317d763cef2SBarry Smith          data to the first processor to print.
1318d763cef2SBarry Smith 
1319d763cef2SBarry Smith     The user can open an alternative visualization context with
1320b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1321d763cef2SBarry Smith 
1322d763cef2SBarry Smith     Level: beginner
1323d763cef2SBarry Smith 
1324d763cef2SBarry Smith .keywords: TS, timestep, view
1325d763cef2SBarry Smith 
1326b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1327d763cef2SBarry Smith @*/
13287087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1329d763cef2SBarry Smith {
1330dfbe8321SBarry Smith   PetscErrorCode ierr;
133119fd82e9SBarry Smith   TSType         type;
13322b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13332d53ad75SBarry Smith   DMTS           sdm;
1334e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1335f05ece33SBarry Smith   PetscBool      isams;
1336f05ece33SBarry Smith #endif
1337d763cef2SBarry Smith 
1338d763cef2SBarry Smith   PetscFunctionBegin;
13390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13403050cee2SBarry Smith   if (!viewer) {
1341ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13423050cee2SBarry Smith   }
13430700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1344c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1345fd16b177SBarry Smith 
1346251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1347251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
134855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13492b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1350e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1351e04113cfSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr);
1352f05ece33SBarry Smith #endif
135332077d6dSBarry Smith   if (iascii) {
1354dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
135577431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1356a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1357d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13585ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1359c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1360d763cef2SBarry Smith     }
13615ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1362193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13632d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13642d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1365d52bd9f3SBarry Smith     if (ts->ops->view) {
1366d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1367d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1368d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1369d52bd9f3SBarry Smith     }
13700f5bd95cSBarry Smith   } else if (isstring) {
1371a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1372b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
137355849f57SBarry Smith   } else if (isbinary) {
137455849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
137555849f57SBarry Smith     MPI_Comm    comm;
137655849f57SBarry Smith     PetscMPIInt rank;
137755849f57SBarry Smith     char        type[256];
137855849f57SBarry Smith 
137955849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
138055849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
138155849f57SBarry Smith     if (!rank) {
138255849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
138355849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
138455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
138555849f57SBarry Smith     }
138655849f57SBarry Smith     if (ts->ops->view) {
138755849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
138855849f57SBarry Smith     }
1389f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1390f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13912d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13922d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13932b0a91c0SBarry Smith   } else if (isdraw) {
13942b0a91c0SBarry Smith     PetscDraw draw;
13952b0a91c0SBarry Smith     char      str[36];
139689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13972b0a91c0SBarry Smith 
13982b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13992b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14002b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14012b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14020298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
140389fd9fafSBarry Smith     bottom = y - h;
14042b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14052b0a91c0SBarry Smith     if (ts->ops->view) {
14062b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14072b0a91c0SBarry Smith     }
14082b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1409e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1410f05ece33SBarry Smith   } else if (isams) {
1411d45a07a7SBarry Smith     PetscMPIInt rank;
14122657e9d9SBarry Smith     const char  *name;
14132657e9d9SBarry Smith 
14142657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1415d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1416d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1417d45a07a7SBarry Smith       char       dir[1024];
1418d45a07a7SBarry Smith 
1419e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1420a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14212657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14222657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14232657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1424d763cef2SBarry Smith     }
14250acecf5bSBarry Smith     if (ts->ops->view) {
14260acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14270acecf5bSBarry Smith     }
1428f05ece33SBarry Smith #endif
1429f05ece33SBarry Smith   }
1430f05ece33SBarry Smith 
1431b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1432251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1433b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1434d763cef2SBarry Smith   PetscFunctionReturn(0);
1435d763cef2SBarry Smith }
1436d763cef2SBarry Smith 
1437d763cef2SBarry Smith 
14384a2ae208SSatish Balay #undef __FUNCT__
14394a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1440b07ff414SBarry Smith /*@
1441d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1442d763cef2SBarry Smith    the timesteppers.
1443d763cef2SBarry Smith 
14443f9fe445SBarry Smith    Logically Collective on TS
1445d763cef2SBarry Smith 
1446d763cef2SBarry Smith    Input Parameters:
1447d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1448d763cef2SBarry Smith -  usrP - optional user context
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Level: intermediate
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1455d763cef2SBarry Smith @*/
14567087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1457d763cef2SBarry Smith {
1458d763cef2SBarry Smith   PetscFunctionBegin;
14590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1460d763cef2SBarry Smith   ts->user = usrP;
1461d763cef2SBarry Smith   PetscFunctionReturn(0);
1462d763cef2SBarry Smith }
1463d763cef2SBarry Smith 
14644a2ae208SSatish Balay #undef __FUNCT__
14654a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1466b07ff414SBarry Smith /*@
1467d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1468d763cef2SBarry Smith     timestepper.
1469d763cef2SBarry Smith 
1470d763cef2SBarry Smith     Not Collective
1471d763cef2SBarry Smith 
1472d763cef2SBarry Smith     Input Parameter:
1473d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1474d763cef2SBarry Smith 
1475d763cef2SBarry Smith     Output Parameter:
1476d763cef2SBarry Smith .   usrP - user context
1477d763cef2SBarry Smith 
1478d763cef2SBarry Smith     Level: intermediate
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1483d763cef2SBarry Smith @*/
1484e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1485d763cef2SBarry Smith {
1486d763cef2SBarry Smith   PetscFunctionBegin;
14870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1488e71120c6SJed Brown   *(void**)usrP = ts->user;
1489d763cef2SBarry Smith   PetscFunctionReturn(0);
1490d763cef2SBarry Smith }
1491d763cef2SBarry Smith 
14924a2ae208SSatish Balay #undef __FUNCT__
14934a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1494d763cef2SBarry Smith /*@
1495b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1496d763cef2SBarry Smith 
1497d763cef2SBarry Smith    Not Collective
1498d763cef2SBarry Smith 
1499d763cef2SBarry Smith    Input Parameter:
1500d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith    Output Parameter:
1503b8123daeSJed Brown .  iter - number of steps completed so far
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith    Level: intermediate
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1508b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1509d763cef2SBarry Smith @*/
15107087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1511d763cef2SBarry Smith {
1512d763cef2SBarry Smith   PetscFunctionBegin;
15130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15144482741eSBarry Smith   PetscValidIntPointer(iter,2);
1515d763cef2SBarry Smith   *iter = ts->steps;
1516d763cef2SBarry Smith   PetscFunctionReturn(0);
1517d763cef2SBarry Smith }
1518d763cef2SBarry Smith 
15194a2ae208SSatish Balay #undef __FUNCT__
15204a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1521d763cef2SBarry Smith /*@
1522d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1523d763cef2SBarry Smith    as well as the initial time.
1524d763cef2SBarry Smith 
15253f9fe445SBarry Smith    Logically Collective on TS
1526d763cef2SBarry Smith 
1527d763cef2SBarry Smith    Input Parameters:
1528d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1529d763cef2SBarry Smith .  initial_time - the initial time
1530d763cef2SBarry Smith -  time_step - the size of the timestep
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith    Level: intermediate
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1535d763cef2SBarry Smith 
1536d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1537d763cef2SBarry Smith @*/
15387087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1539d763cef2SBarry Smith {
1540e144a568SJed Brown   PetscErrorCode ierr;
1541e144a568SJed Brown 
1542d763cef2SBarry Smith   PetscFunctionBegin;
15430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1544e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1545d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1546d763cef2SBarry Smith   PetscFunctionReturn(0);
1547d763cef2SBarry Smith }
1548d763cef2SBarry Smith 
15494a2ae208SSatish Balay #undef __FUNCT__
15504a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1551d763cef2SBarry Smith /*@
1552d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1553d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1554d763cef2SBarry Smith 
15553f9fe445SBarry Smith    Logically Collective on TS
1556d763cef2SBarry Smith 
1557d763cef2SBarry Smith    Input Parameters:
1558d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1559d763cef2SBarry Smith -  time_step - the size of the timestep
1560d763cef2SBarry Smith 
1561d763cef2SBarry Smith    Level: intermediate
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith .keywords: TS, set, timestep
1566d763cef2SBarry Smith @*/
15677087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1568d763cef2SBarry Smith {
1569d763cef2SBarry Smith   PetscFunctionBegin;
15700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1571c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1572d763cef2SBarry Smith   ts->time_step      = time_step;
157331748224SBarry Smith   ts->time_step_orig = time_step;
1574d763cef2SBarry Smith   PetscFunctionReturn(0);
1575d763cef2SBarry Smith }
1576d763cef2SBarry Smith 
15774a2ae208SSatish Balay #undef __FUNCT__
1578a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1579a43b19c4SJed Brown /*@
158049354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
158149354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
158249354f04SShri Abhyankar      or just return at the final time TS computed.
1583a43b19c4SJed Brown 
1584a43b19c4SJed Brown   Logically Collective on TS
1585a43b19c4SJed Brown 
1586a43b19c4SJed Brown    Input Parameter:
1587a43b19c4SJed Brown +   ts - the time-step context
158849354f04SShri Abhyankar -   eftopt - exact final time option
1589a43b19c4SJed Brown 
1590a43b19c4SJed Brown    Level: beginner
1591a43b19c4SJed Brown 
1592a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1593a43b19c4SJed Brown @*/
159449354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1595a43b19c4SJed Brown {
1596a43b19c4SJed Brown   PetscFunctionBegin;
1597a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
159849354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
159949354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1600a43b19c4SJed Brown   PetscFunctionReturn(0);
1601a43b19c4SJed Brown }
1602a43b19c4SJed Brown 
1603a43b19c4SJed Brown #undef __FUNCT__
16044a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1605d763cef2SBarry Smith /*@
1606d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1607d763cef2SBarry Smith 
1608d763cef2SBarry Smith    Not Collective
1609d763cef2SBarry Smith 
1610d763cef2SBarry Smith    Input Parameter:
1611d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1612d763cef2SBarry Smith 
1613d763cef2SBarry Smith    Output Parameter:
1614d763cef2SBarry Smith .  dt - the current timestep size
1615d763cef2SBarry Smith 
1616d763cef2SBarry Smith    Level: intermediate
1617d763cef2SBarry Smith 
1618d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1619d763cef2SBarry Smith 
1620d763cef2SBarry Smith .keywords: TS, get, timestep
1621d763cef2SBarry Smith @*/
16227087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1623d763cef2SBarry Smith {
1624d763cef2SBarry Smith   PetscFunctionBegin;
16250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1626f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1627d763cef2SBarry Smith   *dt = ts->time_step;
1628d763cef2SBarry Smith   PetscFunctionReturn(0);
1629d763cef2SBarry Smith }
1630d763cef2SBarry Smith 
16314a2ae208SSatish Balay #undef __FUNCT__
16324a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1633d8e5e3e6SSatish Balay /*@
1634d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1635d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1636d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1637d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1638d763cef2SBarry Smith 
1639d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1640d763cef2SBarry Smith 
1641d763cef2SBarry Smith    Input Parameter:
1642d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith    Output Parameter:
1645d763cef2SBarry Smith .  v - the vector containing the solution
1646d763cef2SBarry Smith 
1647d763cef2SBarry Smith    Level: intermediate
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith .seealso: TSGetTimeStep()
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1652d763cef2SBarry Smith @*/
16537087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1654d763cef2SBarry Smith {
1655d763cef2SBarry Smith   PetscFunctionBegin;
16560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16574482741eSBarry Smith   PetscValidPointer(v,2);
16588737fe31SLisandro Dalcin   *v = ts->vec_sol;
1659d763cef2SBarry Smith   PetscFunctionReturn(0);
1660d763cef2SBarry Smith }
1661d763cef2SBarry Smith 
1662bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
16634a2ae208SSatish Balay #undef __FUNCT__
1664bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1665d8e5e3e6SSatish Balay /*@
1666bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1667d763cef2SBarry Smith 
1668bdad233fSMatthew Knepley   Not collective
1669d763cef2SBarry Smith 
1670bdad233fSMatthew Knepley   Input Parameters:
1671bdad233fSMatthew Knepley + ts   - The TS
1672bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1673d763cef2SBarry Smith .vb
16740910c330SBarry Smith          U_t - A U = 0      (linear)
16750910c330SBarry Smith          U_t - A(t) U = 0   (linear)
16760910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1677d763cef2SBarry Smith .ve
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Level: beginner
1680d763cef2SBarry Smith 
1681bdad233fSMatthew Knepley .keywords: TS, problem type
1682bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1683d763cef2SBarry Smith @*/
16847087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1685a7cc72afSBarry Smith {
16869e2a6581SJed Brown   PetscErrorCode ierr;
16879e2a6581SJed Brown 
1688d763cef2SBarry Smith   PetscFunctionBegin;
16890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1690bdad233fSMatthew Knepley   ts->problem_type = type;
16919e2a6581SJed Brown   if (type == TS_LINEAR) {
16929e2a6581SJed Brown     SNES snes;
16939e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
16949e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
16959e2a6581SJed Brown   }
1696d763cef2SBarry Smith   PetscFunctionReturn(0);
1697d763cef2SBarry Smith }
1698d763cef2SBarry Smith 
1699bdad233fSMatthew Knepley #undef __FUNCT__
1700bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1701bdad233fSMatthew Knepley /*@C
1702bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1703bdad233fSMatthew Knepley 
1704bdad233fSMatthew Knepley   Not collective
1705bdad233fSMatthew Knepley 
1706bdad233fSMatthew Knepley   Input Parameter:
1707bdad233fSMatthew Knepley . ts   - The TS
1708bdad233fSMatthew Knepley 
1709bdad233fSMatthew Knepley   Output Parameter:
1710bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1711bdad233fSMatthew Knepley .vb
1712089b2837SJed Brown          M U_t = A U
1713089b2837SJed Brown          M(t) U_t = A(t) U
1714b5abc632SBarry Smith          F(t,U,U_t)
1715bdad233fSMatthew Knepley .ve
1716bdad233fSMatthew Knepley 
1717bdad233fSMatthew Knepley    Level: beginner
1718bdad233fSMatthew Knepley 
1719bdad233fSMatthew Knepley .keywords: TS, problem type
1720bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1721bdad233fSMatthew Knepley @*/
17227087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1723a7cc72afSBarry Smith {
1724bdad233fSMatthew Knepley   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17264482741eSBarry Smith   PetscValidIntPointer(type,2);
1727bdad233fSMatthew Knepley   *type = ts->problem_type;
1728bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1729bdad233fSMatthew Knepley }
1730d763cef2SBarry Smith 
17314a2ae208SSatish Balay #undef __FUNCT__
17324a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1733d763cef2SBarry Smith /*@
1734d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1735d763cef2SBarry Smith    of a timestepper.
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith    Collective on TS
1738d763cef2SBarry Smith 
1739d763cef2SBarry Smith    Input Parameter:
1740d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1741d763cef2SBarry Smith 
1742d763cef2SBarry Smith    Notes:
1743d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1744d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1745d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1746d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1747d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1748d763cef2SBarry Smith 
1749d763cef2SBarry Smith    Level: advanced
1750d763cef2SBarry Smith 
1751d763cef2SBarry Smith .keywords: TS, timestep, setup
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1754d763cef2SBarry Smith @*/
17557087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1756d763cef2SBarry Smith {
1757dfbe8321SBarry Smith   PetscErrorCode ierr;
17586c6b9e74SPeter Brune   DM             dm;
17596c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17606c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
17616c6b9e74SPeter Brune   TSIJacobian    ijac;
17626c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1763d763cef2SBarry Smith 
1764d763cef2SBarry Smith   PetscFunctionBegin;
17650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1766277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1767277b19d0SLisandro Dalcin 
17687adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
17699596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1770d763cef2SBarry Smith   }
1771277b19d0SLisandro Dalcin 
1772277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1773277b19d0SLisandro Dalcin 
1774552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
17751c3436cfSJed Brown 
1776e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1777e1244c69SJed Brown     Mat Amat,Pmat;
1778e1244c69SJed Brown     SNES snes;
1779e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1780e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1781e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1782e1244c69SJed Brown      * have displaced the RHS matrix */
1783e1244c69SJed Brown     if (Amat == ts->Arhs) {
1784e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1785e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1786e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1787e1244c69SJed Brown     }
1788e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1789e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1790e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1791e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1792e1244c69SJed Brown     }
1793e1244c69SJed Brown   }
1794e1244c69SJed Brown 
1795277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1796000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1797277b19d0SLisandro Dalcin   }
1798277b19d0SLisandro Dalcin 
17996c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18006c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18016c6b9e74SPeter Brune    */
18026c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18030298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18046c6b9e74SPeter Brune   if (!func) {
18056c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18066c6b9e74SPeter Brune   }
18076c6b9e74SPeter Brune   /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
18086c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18096c6b9e74SPeter Brune    */
18100298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18110298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18120298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18136c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18146c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18156c6b9e74SPeter Brune   }
1816277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1817277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1818277b19d0SLisandro Dalcin }
1819277b19d0SLisandro Dalcin 
1820277b19d0SLisandro Dalcin #undef __FUNCT__
1821277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1822277b19d0SLisandro Dalcin /*@
1823277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1824277b19d0SLisandro Dalcin 
1825277b19d0SLisandro Dalcin    Collective on TS
1826277b19d0SLisandro Dalcin 
1827277b19d0SLisandro Dalcin    Input Parameter:
1828277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1829277b19d0SLisandro Dalcin 
1830277b19d0SLisandro Dalcin    Level: beginner
1831277b19d0SLisandro Dalcin 
1832277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1833277b19d0SLisandro Dalcin 
1834277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1835277b19d0SLisandro Dalcin @*/
1836277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1837277b19d0SLisandro Dalcin {
1838277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1839277b19d0SLisandro Dalcin 
1840277b19d0SLisandro Dalcin   PetscFunctionBegin;
1841277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1842277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1843277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1844277b19d0SLisandro Dalcin   }
1845277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1846bbd56ea5SKarl Rupp 
18474e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
18484e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1849214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
18506bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1851e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1852e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
185338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1854bbd56ea5SKarl Rupp 
1855277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1856d763cef2SBarry Smith   PetscFunctionReturn(0);
1857d763cef2SBarry Smith }
1858d763cef2SBarry Smith 
18594a2ae208SSatish Balay #undef __FUNCT__
18604a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1861d8e5e3e6SSatish Balay /*@
1862d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1863d763cef2SBarry Smith    with TSCreate().
1864d763cef2SBarry Smith 
1865d763cef2SBarry Smith    Collective on TS
1866d763cef2SBarry Smith 
1867d763cef2SBarry Smith    Input Parameter:
1868d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1869d763cef2SBarry Smith 
1870d763cef2SBarry Smith    Level: beginner
1871d763cef2SBarry Smith 
1872d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1873d763cef2SBarry Smith 
1874d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1875d763cef2SBarry Smith @*/
18766bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1877d763cef2SBarry Smith {
18786849ba73SBarry Smith   PetscErrorCode ierr;
1879d763cef2SBarry Smith 
1880d763cef2SBarry Smith   PetscFunctionBegin;
18816bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
18826bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
18836bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1884d763cef2SBarry Smith 
18856bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1886277b19d0SLisandro Dalcin 
1887e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
1888e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
18896bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
18906d4c513bSLisandro Dalcin 
189184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
18926bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
18936bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
18946bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
18956d4c513bSLisandro Dalcin 
1896a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1897d763cef2SBarry Smith   PetscFunctionReturn(0);
1898d763cef2SBarry Smith }
1899d763cef2SBarry Smith 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1902d8e5e3e6SSatish Balay /*@
1903d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1904d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1905d763cef2SBarry Smith 
1906d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1907d763cef2SBarry Smith 
1908d763cef2SBarry Smith    Input Parameter:
1909d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1910d763cef2SBarry Smith 
1911d763cef2SBarry Smith    Output Parameter:
1912d763cef2SBarry Smith .  snes - the nonlinear solver context
1913d763cef2SBarry Smith 
1914d763cef2SBarry Smith    Notes:
1915d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1916d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
191794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1918d763cef2SBarry Smith 
1919d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
19200298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1921d763cef2SBarry Smith 
1922d763cef2SBarry Smith    Level: beginner
1923d763cef2SBarry Smith 
1924d763cef2SBarry Smith .keywords: timestep, get, SNES
1925d763cef2SBarry Smith @*/
19267087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1927d763cef2SBarry Smith {
1928d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1929d372ba47SLisandro Dalcin 
1930d763cef2SBarry Smith   PetscFunctionBegin;
19310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19324482741eSBarry Smith   PetscValidPointer(snes,2);
1933d372ba47SLisandro Dalcin   if (!ts->snes) {
1934ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
19350298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19363bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
1937d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1938496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
19399e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
19409e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
19419e2a6581SJed Brown     }
1942d372ba47SLisandro Dalcin   }
1943d763cef2SBarry Smith   *snes = ts->snes;
1944d763cef2SBarry Smith   PetscFunctionReturn(0);
1945d763cef2SBarry Smith }
1946d763cef2SBarry Smith 
19474a2ae208SSatish Balay #undef __FUNCT__
1948deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1949deb2cd25SJed Brown /*@
1950deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1951deb2cd25SJed Brown 
1952deb2cd25SJed Brown    Collective
1953deb2cd25SJed Brown 
1954deb2cd25SJed Brown    Input Parameter:
1955deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1956deb2cd25SJed Brown -  snes - the nonlinear solver context
1957deb2cd25SJed Brown 
1958deb2cd25SJed Brown    Notes:
1959deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1960deb2cd25SJed Brown 
1961deb2cd25SJed Brown    Level: developer
1962deb2cd25SJed Brown 
1963deb2cd25SJed Brown .keywords: timestep, set, SNES
1964deb2cd25SJed Brown @*/
1965deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1966deb2cd25SJed Brown {
1967deb2cd25SJed Brown   PetscErrorCode ierr;
1968740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1969deb2cd25SJed Brown 
1970deb2cd25SJed Brown   PetscFunctionBegin;
1971deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1972deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1973deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1974deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1975bbd56ea5SKarl Rupp 
1976deb2cd25SJed Brown   ts->snes = snes;
1977bbd56ea5SKarl Rupp 
19780298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19790298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1980740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
19810298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1982740132f1SEmil Constantinescu   }
1983deb2cd25SJed Brown   PetscFunctionReturn(0);
1984deb2cd25SJed Brown }
1985deb2cd25SJed Brown 
1986deb2cd25SJed Brown #undef __FUNCT__
198794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1988d8e5e3e6SSatish Balay /*@
198994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1990d763cef2SBarry Smith    a TS (timestepper) context.
1991d763cef2SBarry Smith 
199294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith    Input Parameter:
1995d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith    Output Parameter:
199894b7f48cSBarry Smith .  ksp - the nonlinear solver context
1999d763cef2SBarry Smith 
2000d763cef2SBarry Smith    Notes:
200194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2002d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2003d763cef2SBarry Smith    KSP and PC contexts as well.
2004d763cef2SBarry Smith 
200594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
20060298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2007d763cef2SBarry Smith 
2008d763cef2SBarry Smith    Level: beginner
2009d763cef2SBarry Smith 
201094b7f48cSBarry Smith .keywords: timestep, get, KSP
2011d763cef2SBarry Smith @*/
20127087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2013d763cef2SBarry Smith {
2014d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2015089b2837SJed Brown   SNES           snes;
2016d372ba47SLisandro Dalcin 
2017d763cef2SBarry Smith   PetscFunctionBegin;
20180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20194482741eSBarry Smith   PetscValidPointer(ksp,2);
202017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2021e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2022089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2023089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2024d763cef2SBarry Smith   PetscFunctionReturn(0);
2025d763cef2SBarry Smith }
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2028d763cef2SBarry Smith 
20294a2ae208SSatish Balay #undef __FUNCT__
2030adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2031adb62b0dSMatthew Knepley /*@
2032adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2033adb62b0dSMatthew Knepley    maximum time for iteration.
2034adb62b0dSMatthew Knepley 
20353f9fe445SBarry Smith    Not Collective
2036adb62b0dSMatthew Knepley 
2037adb62b0dSMatthew Knepley    Input Parameters:
2038adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
20390298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
20400298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2041adb62b0dSMatthew Knepley 
2042adb62b0dSMatthew Knepley    Level: intermediate
2043adb62b0dSMatthew Knepley 
2044adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2045adb62b0dSMatthew Knepley @*/
20467087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2047adb62b0dSMatthew Knepley {
2048adb62b0dSMatthew Knepley   PetscFunctionBegin;
20490700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2050abc0a331SBarry Smith   if (maxsteps) {
20514482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2052adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2053adb62b0dSMatthew Knepley   }
2054abc0a331SBarry Smith   if (maxtime) {
20554482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2056adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2057adb62b0dSMatthew Knepley   }
2058adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2059adb62b0dSMatthew Knepley }
2060adb62b0dSMatthew Knepley 
2061adb62b0dSMatthew Knepley #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2063d763cef2SBarry Smith /*@
2064d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2065d763cef2SBarry Smith    maximum time for iteration.
2066d763cef2SBarry Smith 
20673f9fe445SBarry Smith    Logically Collective on TS
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith    Input Parameters:
2070d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2071d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2072d763cef2SBarry Smith -  maxtime - final time to iterate to
2073d763cef2SBarry Smith 
2074d763cef2SBarry Smith    Options Database Keys:
2075d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
20763bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith    Notes:
2079d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith    Level: intermediate
2082d763cef2SBarry Smith 
2083d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2084a43b19c4SJed Brown 
2085a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2086d763cef2SBarry Smith @*/
20877087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2088d763cef2SBarry Smith {
2089d763cef2SBarry Smith   PetscFunctionBegin;
20900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2091c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2092c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
209339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
209439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2095d763cef2SBarry Smith   PetscFunctionReturn(0);
2096d763cef2SBarry Smith }
2097d763cef2SBarry Smith 
20984a2ae208SSatish Balay #undef __FUNCT__
20994a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2100d763cef2SBarry Smith /*@
2101d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2102d763cef2SBarry Smith    for use by the TS routines.
2103d763cef2SBarry Smith 
21043f9fe445SBarry Smith    Logically Collective on TS and Vec
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith    Input Parameters:
2107d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
21080910c330SBarry Smith -  u - the solution vector
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Level: beginner
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2113d763cef2SBarry Smith @*/
21140910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2115d763cef2SBarry Smith {
21168737fe31SLisandro Dalcin   PetscErrorCode ierr;
2117496e6a7aSJed Brown   DM             dm;
21188737fe31SLisandro Dalcin 
2119d763cef2SBarry Smith   PetscFunctionBegin;
21200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21210910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
21220910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
21236bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2124bbd56ea5SKarl Rupp 
21250910c330SBarry Smith   ts->vec_sol = u;
2126bbd56ea5SKarl Rupp 
2127496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
21280910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2129d763cef2SBarry Smith   PetscFunctionReturn(0);
2130d763cef2SBarry Smith }
2131d763cef2SBarry Smith 
2132e74ef692SMatthew Knepley #undef __FUNCT__
2133e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2134ac226902SBarry Smith /*@C
2135000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
21363f2090d5SJed Brown   called once at the beginning of each time step.
2137000e7ae3SMatthew Knepley 
21383f9fe445SBarry Smith   Logically Collective on TS
2139000e7ae3SMatthew Knepley 
2140000e7ae3SMatthew Knepley   Input Parameters:
2141000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2142000e7ae3SMatthew Knepley - func - The function
2143000e7ae3SMatthew Knepley 
2144000e7ae3SMatthew Knepley   Calling sequence of func:
2145000e7ae3SMatthew Knepley . func (TS ts);
2146000e7ae3SMatthew Knepley 
2147000e7ae3SMatthew Knepley   Level: intermediate
2148000e7ae3SMatthew Knepley 
2149b8123daeSJed Brown   Note:
2150b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2151b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2152b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2153b8123daeSJed Brown 
2154000e7ae3SMatthew Knepley .keywords: TS, timestep
2155b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2156000e7ae3SMatthew Knepley @*/
21577087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2158000e7ae3SMatthew Knepley {
2159000e7ae3SMatthew Knepley   PetscFunctionBegin;
21600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2161ae60f76fSBarry Smith   ts->prestep = func;
2162000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2163000e7ae3SMatthew Knepley }
2164000e7ae3SMatthew Knepley 
2165e74ef692SMatthew Knepley #undef __FUNCT__
21663f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
216709ee8438SJed Brown /*@
21683f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
21693f2090d5SJed Brown 
21703f2090d5SJed Brown   Collective on TS
21713f2090d5SJed Brown 
21723f2090d5SJed Brown   Input Parameters:
21733f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21743f2090d5SJed Brown 
21753f2090d5SJed Brown   Notes:
21763f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
21773f2090d5SJed Brown   so most users would not generally call this routine themselves.
21783f2090d5SJed Brown 
21793f2090d5SJed Brown   Level: developer
21803f2090d5SJed Brown 
21813f2090d5SJed Brown .keywords: TS, timestep
2182b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
21833f2090d5SJed Brown @*/
21847087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
21853f2090d5SJed Brown {
21863f2090d5SJed Brown   PetscErrorCode ierr;
21873f2090d5SJed Brown 
21883f2090d5SJed Brown   PetscFunctionBegin;
21890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2190ae60f76fSBarry Smith   if (ts->prestep) {
2191ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2192312ce896SJed Brown   }
21933f2090d5SJed Brown   PetscFunctionReturn(0);
21943f2090d5SJed Brown }
21953f2090d5SJed Brown 
21963f2090d5SJed Brown #undef __FUNCT__
2197b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2198b8123daeSJed Brown /*@C
2199b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2200b8123daeSJed Brown   called once at the beginning of each stage.
2201b8123daeSJed Brown 
2202b8123daeSJed Brown   Logically Collective on TS
2203b8123daeSJed Brown 
2204b8123daeSJed Brown   Input Parameters:
2205b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2206b8123daeSJed Brown - func - The function
2207b8123daeSJed Brown 
2208b8123daeSJed Brown   Calling sequence of func:
2209b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2210b8123daeSJed Brown 
2211b8123daeSJed Brown   Level: intermediate
2212b8123daeSJed Brown 
2213b8123daeSJed Brown   Note:
2214b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2215b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2216b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2217b8123daeSJed Brown 
2218b8123daeSJed Brown .keywords: TS, timestep
2219b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2220b8123daeSJed Brown @*/
2221b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2222b8123daeSJed Brown {
2223b8123daeSJed Brown   PetscFunctionBegin;
2224b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2225ae60f76fSBarry Smith   ts->prestage = func;
2226b8123daeSJed Brown   PetscFunctionReturn(0);
2227b8123daeSJed Brown }
2228b8123daeSJed Brown 
2229b8123daeSJed Brown #undef __FUNCT__
2230b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2231b8123daeSJed Brown /*@
2232b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2233b8123daeSJed Brown 
2234b8123daeSJed Brown   Collective on TS
2235b8123daeSJed Brown 
2236b8123daeSJed Brown   Input Parameters:
2237b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2238b8123daeSJed Brown 
2239b8123daeSJed Brown   Notes:
2240b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2241b8123daeSJed Brown   most users would not generally call this routine themselves.
2242b8123daeSJed Brown 
2243b8123daeSJed Brown   Level: developer
2244b8123daeSJed Brown 
2245b8123daeSJed Brown .keywords: TS, timestep
2246b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2247b8123daeSJed Brown @*/
2248b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2249b8123daeSJed Brown {
2250b8123daeSJed Brown   PetscErrorCode ierr;
2251b8123daeSJed Brown 
2252b8123daeSJed Brown   PetscFunctionBegin;
2253b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2254ae60f76fSBarry Smith   if (ts->prestage) {
2255ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2256b8123daeSJed Brown   }
2257b8123daeSJed Brown   PetscFunctionReturn(0);
2258b8123daeSJed Brown }
2259b8123daeSJed Brown 
2260b8123daeSJed Brown #undef __FUNCT__
2261e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2262ac226902SBarry Smith /*@C
2263000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
22643f2090d5SJed Brown   called once at the end of each time step.
2265000e7ae3SMatthew Knepley 
22663f9fe445SBarry Smith   Logically Collective on TS
2267000e7ae3SMatthew Knepley 
2268000e7ae3SMatthew Knepley   Input Parameters:
2269000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2270000e7ae3SMatthew Knepley - func - The function
2271000e7ae3SMatthew Knepley 
2272000e7ae3SMatthew Knepley   Calling sequence of func:
2273b8123daeSJed Brown $ func (TS ts);
2274000e7ae3SMatthew Knepley 
2275000e7ae3SMatthew Knepley   Level: intermediate
2276000e7ae3SMatthew Knepley 
2277000e7ae3SMatthew Knepley .keywords: TS, timestep
2278b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2279000e7ae3SMatthew Knepley @*/
22807087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2281000e7ae3SMatthew Knepley {
2282000e7ae3SMatthew Knepley   PetscFunctionBegin;
22830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2284ae60f76fSBarry Smith   ts->poststep = func;
2285000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2286000e7ae3SMatthew Knepley }
2287000e7ae3SMatthew Knepley 
2288e74ef692SMatthew Knepley #undef __FUNCT__
22893f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
229009ee8438SJed Brown /*@
22913f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
22923f2090d5SJed Brown 
22933f2090d5SJed Brown   Collective on TS
22943f2090d5SJed Brown 
22953f2090d5SJed Brown   Input Parameters:
22963f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
22973f2090d5SJed Brown 
22983f2090d5SJed Brown   Notes:
22993f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
23003f2090d5SJed Brown   so most users would not generally call this routine themselves.
23013f2090d5SJed Brown 
23023f2090d5SJed Brown   Level: developer
23033f2090d5SJed Brown 
23043f2090d5SJed Brown .keywords: TS, timestep
23053f2090d5SJed Brown @*/
23067087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
23073f2090d5SJed Brown {
23083f2090d5SJed Brown   PetscErrorCode ierr;
23093f2090d5SJed Brown 
23103f2090d5SJed Brown   PetscFunctionBegin;
23110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2312ae60f76fSBarry Smith   if (ts->poststep) {
2313ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
231472ac3e02SJed Brown   }
23153f2090d5SJed Brown   PetscFunctionReturn(0);
23163f2090d5SJed Brown }
23173f2090d5SJed Brown 
2318d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2319d763cef2SBarry Smith 
23204a2ae208SSatish Balay #undef __FUNCT__
2321a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2322d763cef2SBarry Smith /*@C
2323a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2324d763cef2SBarry Smith    timestep to display the iteration's  progress.
2325d763cef2SBarry Smith 
23263f9fe445SBarry Smith    Logically Collective on TS
2327d763cef2SBarry Smith 
2328d763cef2SBarry Smith    Input Parameters:
2329d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2330e213d8f1SJed Brown .  monitor - monitoring routine
2331329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
23320298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2333b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
23340298fd71SBarry Smith           (may be NULL)
2335d763cef2SBarry Smith 
2336e213d8f1SJed Brown    Calling sequence of monitor:
23370910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2338d763cef2SBarry Smith 
2339d763cef2SBarry Smith +    ts - the TS context
234088c05cc5SBarry Smith .    steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have
234188c05cc5SBarry Smith                                been interpolated to)
23421f06c33eSBarry Smith .    time - current time
23430910c330SBarry Smith .    u - current iterate
2344d763cef2SBarry Smith -    mctx - [optional] monitoring context
2345d763cef2SBarry Smith 
2346d763cef2SBarry Smith    Notes:
2347d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2348d763cef2SBarry Smith    already has been loaded.
2349d763cef2SBarry Smith 
2350025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2351025f1a04SBarry Smith 
2352d763cef2SBarry Smith    Level: intermediate
2353d763cef2SBarry Smith 
2354d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2355d763cef2SBarry Smith 
2356a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2357d763cef2SBarry Smith @*/
2358c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2359d763cef2SBarry Smith {
2360d763cef2SBarry Smith   PetscFunctionBegin;
23610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2363d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
23648704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2365d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2366d763cef2SBarry Smith   PetscFunctionReturn(0);
2367d763cef2SBarry Smith }
2368d763cef2SBarry Smith 
23694a2ae208SSatish Balay #undef __FUNCT__
2370a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2371d763cef2SBarry Smith /*@C
2372a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2373d763cef2SBarry Smith 
23743f9fe445SBarry Smith    Logically Collective on TS
2375d763cef2SBarry Smith 
2376d763cef2SBarry Smith    Input Parameters:
2377d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2378d763cef2SBarry Smith 
2379d763cef2SBarry Smith    Notes:
2380d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2381d763cef2SBarry Smith 
2382d763cef2SBarry Smith    Level: intermediate
2383d763cef2SBarry Smith 
2384d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2385d763cef2SBarry Smith 
2386a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2387d763cef2SBarry Smith @*/
23887087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2389d763cef2SBarry Smith {
2390d952e501SBarry Smith   PetscErrorCode ierr;
2391d952e501SBarry Smith   PetscInt       i;
2392d952e501SBarry Smith 
2393d763cef2SBarry Smith   PetscFunctionBegin;
23940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2395d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
23968704b422SBarry Smith     if (ts->monitordestroy[i]) {
23978704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2398d952e501SBarry Smith     }
2399d952e501SBarry Smith   }
2400d763cef2SBarry Smith   ts->numbermonitors = 0;
2401d763cef2SBarry Smith   PetscFunctionReturn(0);
2402d763cef2SBarry Smith }
2403d763cef2SBarry Smith 
24044a2ae208SSatish Balay #undef __FUNCT__
2405a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2406d8e5e3e6SSatish Balay /*@
2407a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
24085516499fSSatish Balay 
24095516499fSSatish Balay    Level: intermediate
241041251cbbSSatish Balay 
24115516499fSSatish Balay .keywords: TS, set, monitor
24125516499fSSatish Balay 
241341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
241441251cbbSSatish Balay @*/
2415649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2416d763cef2SBarry Smith {
2417dfbe8321SBarry Smith   PetscErrorCode ierr;
2418ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2419d132466eSBarry Smith 
2420d763cef2SBarry Smith   PetscFunctionBegin;
2421649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2422971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2423649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2424d763cef2SBarry Smith   PetscFunctionReturn(0);
2425d763cef2SBarry Smith }
2426d763cef2SBarry Smith 
24274a2ae208SSatish Balay #undef __FUNCT__
2428cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2429cd652676SJed Brown /*@
2430cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2431cd652676SJed Brown 
2432cd652676SJed Brown    Logically Collective on TS
2433cd652676SJed Brown 
2434cd652676SJed Brown    Input Argument:
2435cd652676SJed Brown .  ts - time stepping context
2436cd652676SJed Brown 
2437cd652676SJed Brown    Output Argument:
2438cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2439cd652676SJed Brown 
2440cd652676SJed Brown    Level: intermediate
2441cd652676SJed Brown 
2442cd652676SJed Brown .keywords: TS, set
2443cd652676SJed Brown 
2444cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2445cd652676SJed Brown @*/
2446cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2447cd652676SJed Brown {
2448cd652676SJed Brown   PetscFunctionBegin;
2449cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2450cd652676SJed Brown   ts->retain_stages = flg;
2451cd652676SJed Brown   PetscFunctionReturn(0);
2452cd652676SJed Brown }
2453cd652676SJed Brown 
2454cd652676SJed Brown #undef __FUNCT__
2455cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2456cd652676SJed Brown /*@
2457cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2458cd652676SJed Brown 
2459cd652676SJed Brown    Collective on TS
2460cd652676SJed Brown 
2461cd652676SJed Brown    Input Argument:
2462cd652676SJed Brown +  ts - time stepping context
2463cd652676SJed Brown -  t - time to interpolate to
2464cd652676SJed Brown 
2465cd652676SJed Brown    Output Argument:
24660910c330SBarry Smith .  U - state at given time
2467cd652676SJed Brown 
2468cd652676SJed Brown    Notes:
2469cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2470cd652676SJed Brown 
2471cd652676SJed Brown    Level: intermediate
2472cd652676SJed Brown 
2473cd652676SJed Brown    Developer Notes:
2474cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2475cd652676SJed Brown 
2476cd652676SJed Brown .keywords: TS, set
2477cd652676SJed Brown 
2478cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2479cd652676SJed Brown @*/
24800910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2481cd652676SJed Brown {
2482cd652676SJed Brown   PetscErrorCode ierr;
2483cd652676SJed Brown 
2484cd652676SJed Brown   PetscFunctionBegin;
2485cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2486b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2487ce94432eSBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
2488ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
24890910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2490cd652676SJed Brown   PetscFunctionReturn(0);
2491cd652676SJed Brown }
2492cd652676SJed Brown 
2493cd652676SJed Brown #undef __FUNCT__
24944a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2495d763cef2SBarry Smith /*@
24966d9e5789SSean Farley    TSStep - Steps one time step
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith    Collective on TS
2499d763cef2SBarry Smith 
2500d763cef2SBarry Smith    Input Parameter:
2501d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2502d763cef2SBarry Smith 
25036d9e5789SSean Farley    Level: intermediate
2504d763cef2SBarry Smith 
2505b8123daeSJed Brown    Notes:
2506b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2507b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2508b8123daeSJed Brown 
250925cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
251025cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
251125cb2221SBarry Smith 
2512d763cef2SBarry Smith .keywords: TS, timestep, solve
2513d763cef2SBarry Smith 
251425cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2515d763cef2SBarry Smith @*/
2516193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2517d763cef2SBarry Smith {
2518362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2519dfbe8321SBarry Smith   PetscErrorCode ierr;
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith   PetscFunctionBegin;
25220700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2523d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2524d405a339SMatthew Knepley 
2525362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2526362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2527bbd56ea5SKarl Rupp 
2528d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2529193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2530d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2531bbd56ea5SKarl Rupp 
2532362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2533362cd11cSLisandro Dalcin 
2534362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2535cef5090cSJed Brown     if (ts->errorifstepfailed) {
2536cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2537ce94432eSBarry Smith         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]);
2538ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2539cef5090cSJed Brown     }
2540362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2541db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2542db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2543362cd11cSLisandro Dalcin   }
2544d763cef2SBarry Smith   PetscFunctionReturn(0);
2545d763cef2SBarry Smith }
2546d763cef2SBarry Smith 
25474a2ae208SSatish Balay #undef __FUNCT__
254805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
254905175c85SJed Brown /*@
255005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
255105175c85SJed Brown 
25521c3436cfSJed Brown    Collective on TS
255305175c85SJed Brown 
255405175c85SJed Brown    Input Arguments:
25551c3436cfSJed Brown +  ts - time stepping context
25561c3436cfSJed Brown .  order - desired order of accuracy
25570298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
255805175c85SJed Brown 
255905175c85SJed Brown    Output Arguments:
25600910c330SBarry Smith .  U - state at the end of the current step
256105175c85SJed Brown 
256205175c85SJed Brown    Level: advanced
256305175c85SJed Brown 
2564108c343cSJed Brown    Notes:
2565108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2566108c343cSJed 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.
2567108c343cSJed Brown 
25681c3436cfSJed Brown .seealso: TSStep(), TSAdapt
256905175c85SJed Brown @*/
25700910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
257105175c85SJed Brown {
257205175c85SJed Brown   PetscErrorCode ierr;
257305175c85SJed Brown 
257405175c85SJed Brown   PetscFunctionBegin;
257505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
257605175c85SJed Brown   PetscValidType(ts,1);
25770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2578ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
25790910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
258005175c85SJed Brown   PetscFunctionReturn(0);
258105175c85SJed Brown }
258205175c85SJed Brown 
258305175c85SJed Brown #undef __FUNCT__
25846a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
25856a4d4014SLisandro Dalcin /*@
25866a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
25876a4d4014SLisandro Dalcin 
25886a4d4014SLisandro Dalcin    Collective on TS
25896a4d4014SLisandro Dalcin 
25906a4d4014SLisandro Dalcin    Input Parameter:
25916a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2592cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
25935a3a76d0SJed Brown 
25946a4d4014SLisandro Dalcin    Level: beginner
25956a4d4014SLisandro Dalcin 
25965a3a76d0SJed Brown    Notes:
25975a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
25985a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
25995a3a76d0SJed Brown    stepped over the final time.
26005a3a76d0SJed Brown 
26016a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
26026a4d4014SLisandro Dalcin 
26036a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
26046a4d4014SLisandro Dalcin @*/
2605cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
26066a4d4014SLisandro Dalcin {
26074d7d938eSLisandro Dalcin   PetscBool         flg;
26084d7d938eSLisandro Dalcin   PetscViewer       viewer;
2609b06615a5SLisandro Dalcin   Vec               solution;
26106a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2611cffb1e40SBarry Smith   PetscViewerFormat format;
2612f22f69f0SBarry Smith 
26136a4d4014SLisandro Dalcin   PetscFunctionBegin;
26140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2615f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
261649354f04SShri Abhyankar   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
2617b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
26180910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
2619b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
2620b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
2621b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
26225a3a76d0SJed Brown     }
26230910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2624bbd56ea5SKarl Rupp   } else if (u) {
26250910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
26265a3a76d0SJed Brown   }
2627b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
26286a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2629193ac0bcSJed Brown   ts->steps             = 0;
26305ef26d82SJed Brown   ts->ksp_its           = 0;
26315ef26d82SJed Brown   ts->snes_its          = 0;
2632c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2633c610991cSLisandro Dalcin   ts->reject            = 0;
2634193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2635193ac0bcSJed Brown 
2636f05ece33SBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view_pre",&viewer,&format,&flg);CHKERRQ(ierr);
2637f05ece33SBarry Smith   if (flg && !PetscPreLoadingOn) {
2638f05ece33SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
2639f05ece33SBarry Smith     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2640f05ece33SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2641f05ece33SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2642f05ece33SBarry Smith   }
2643f05ece33SBarry Smith 
2644193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2645193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
26460910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2647cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2648193ac0bcSJed Brown   } else {
26496a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2650db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2651db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2652e1a7a14fSJed Brown     while (!ts->reason) {
2653b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2654193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2655193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2656193ac0bcSJed Brown     }
265749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
26580910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2659cc708dedSBarry Smith       ts->solvetime = ts->max_time;
2660b06615a5SLisandro Dalcin       solution = u;
26610574a7fbSJed Brown     } else {
2662ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2663cc708dedSBarry Smith       ts->solvetime = ts->ptime;
2664b06615a5SLisandro Dalcin       solution = ts->vec_sol;
26650574a7fbSJed Brown     }
2666b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
2667193ac0bcSJed Brown   }
2668ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
26694d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2670cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
26714d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2672cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2673cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
26742b0a91c0SBarry Smith   }
267556f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
26766a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26776a4d4014SLisandro Dalcin }
26786a4d4014SLisandro Dalcin 
26796a4d4014SLisandro Dalcin #undef __FUNCT__
26804a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2681228d79bcSJed Brown /*@
2682228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2683228d79bcSJed Brown 
2684228d79bcSJed Brown    Collective on TS
2685228d79bcSJed Brown 
2686228d79bcSJed Brown    Input Parameters:
2687228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2688228d79bcSJed Brown .  step - step number that has just completed
2689228d79bcSJed Brown .  ptime - model time of the state
26900910c330SBarry Smith -  u - state at the current model time
2691228d79bcSJed Brown 
2692228d79bcSJed Brown    Notes:
2693228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2694228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2695228d79bcSJed Brown 
2696228d79bcSJed Brown    Level: advanced
2697228d79bcSJed Brown 
2698228d79bcSJed Brown .keywords: TS, timestep
2699228d79bcSJed Brown @*/
27000910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2701d763cef2SBarry Smith {
27026849ba73SBarry Smith   PetscErrorCode ierr;
2703a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2704d763cef2SBarry Smith 
2705d763cef2SBarry Smith   PetscFunctionBegin;
2706b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2707b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
2708d763cef2SBarry Smith   for (i=0; i<n; i++) {
27090910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2710d763cef2SBarry Smith   }
2711d763cef2SBarry Smith   PetscFunctionReturn(0);
2712d763cef2SBarry Smith }
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
27150b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
27160b039ecaSBarry Smith   PetscDrawLG lg;
27170b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2718201da799SBarry Smith   PetscInt    ksp_its,snes_its;
27190b039ecaSBarry Smith };
27200b039ecaSBarry Smith 
2721d763cef2SBarry Smith 
27224a2ae208SSatish Balay #undef __FUNCT__
2723a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2724d763cef2SBarry Smith /*@C
2725a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2726a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith    Collective on TS
2729d763cef2SBarry Smith 
2730d763cef2SBarry Smith    Input Parameters:
2731d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2732d763cef2SBarry Smith .  label - the title to put in the title bar
27337c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2734a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2735a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith    Output Parameter:
27380b039ecaSBarry Smith .  ctx - the context
2739d763cef2SBarry Smith 
2740d763cef2SBarry Smith    Options Database Key:
27414f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
27424f09c107SBarry Smith .  -ts_monitor_lg_solution -
274399fdda47SBarry Smith .  -ts_monitor_lg_error -
274499fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
274599fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
274699fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2747d763cef2SBarry Smith 
2748d763cef2SBarry Smith    Notes:
2749a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith    Level: intermediate
2752d763cef2SBarry Smith 
27537c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2754d763cef2SBarry Smith 
27554f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
27567c922b88SBarry Smith 
2757d763cef2SBarry Smith @*/
2758a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2759d763cef2SBarry Smith {
2760b0a32e0cSBarry Smith   PetscDraw      win;
2761dfbe8321SBarry Smith   PetscErrorCode ierr;
276299fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2763d763cef2SBarry Smith 
2764d763cef2SBarry Smith   PetscFunctionBegin;
2765201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2766a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
27673fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
27680b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
27690298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
277099fdda47SBarry Smith   if (flg) {
27710b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
277299fdda47SBarry Smith   }
27733bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
2774a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2775d763cef2SBarry Smith   PetscFunctionReturn(0);
2776d763cef2SBarry Smith }
2777d763cef2SBarry Smith 
27784a2ae208SSatish Balay #undef __FUNCT__
27794f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
2780b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
2781d763cef2SBarry Smith {
27820b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2783c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2784dfbe8321SBarry Smith   PetscErrorCode ierr;
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith   PetscFunctionBegin;
2787b06615a5SLisandro Dalcin   if (!step) {
2788a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2789a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2790a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2791a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2792a9f9c1f6SBarry Smith   }
2793c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
27940b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
2795b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
27960b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
27973923b477SBarry Smith   }
2798d763cef2SBarry Smith   PetscFunctionReturn(0);
2799d763cef2SBarry Smith }
2800d763cef2SBarry Smith 
28014a2ae208SSatish Balay #undef __FUNCT__
2802a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2803d763cef2SBarry Smith /*@C
2804a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2805a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2806d763cef2SBarry Smith 
28070b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith    Input Parameter:
28100b039ecaSBarry Smith .  ctx - the monitor context
2811d763cef2SBarry Smith 
2812d763cef2SBarry Smith    Level: intermediate
2813d763cef2SBarry Smith 
2814d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2815d763cef2SBarry Smith 
28164f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2817d763cef2SBarry Smith @*/
2818a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2819d763cef2SBarry Smith {
2820b0a32e0cSBarry Smith   PetscDraw      draw;
2821dfbe8321SBarry Smith   PetscErrorCode ierr;
2822d763cef2SBarry Smith 
2823d763cef2SBarry Smith   PetscFunctionBegin;
28240b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
28256bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
28260b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
28270b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2828d763cef2SBarry Smith   PetscFunctionReturn(0);
2829d763cef2SBarry Smith }
2830d763cef2SBarry Smith 
28314a2ae208SSatish Balay #undef __FUNCT__
28324a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2833d763cef2SBarry Smith /*@
2834b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2835d763cef2SBarry Smith 
2836d763cef2SBarry Smith    Not Collective
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith    Input Parameter:
2839d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2840d763cef2SBarry Smith 
2841d763cef2SBarry Smith    Output Parameter:
2842d763cef2SBarry Smith .  t  - the current time
2843d763cef2SBarry Smith 
2844d763cef2SBarry Smith    Level: beginner
2845d763cef2SBarry Smith 
2846b8123daeSJed Brown    Note:
2847b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2848b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2849b8123daeSJed Brown 
2850d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2851d763cef2SBarry Smith 
2852d763cef2SBarry Smith .keywords: TS, get, time
2853d763cef2SBarry Smith @*/
28547087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2855d763cef2SBarry Smith {
2856d763cef2SBarry Smith   PetscFunctionBegin;
28570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2858f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2859d763cef2SBarry Smith   *t = ts->ptime;
2860d763cef2SBarry Smith   PetscFunctionReturn(0);
2861d763cef2SBarry Smith }
2862d763cef2SBarry Smith 
28634a2ae208SSatish Balay #undef __FUNCT__
28646a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
28656a4d4014SLisandro Dalcin /*@
28666a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
28676a4d4014SLisandro Dalcin 
28683f9fe445SBarry Smith    Logically Collective on TS
28696a4d4014SLisandro Dalcin 
28706a4d4014SLisandro Dalcin    Input Parameters:
28716a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
28726a4d4014SLisandro Dalcin -  time - the time
28736a4d4014SLisandro Dalcin 
28746a4d4014SLisandro Dalcin    Level: intermediate
28756a4d4014SLisandro Dalcin 
28766a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
28776a4d4014SLisandro Dalcin 
28786a4d4014SLisandro Dalcin .keywords: TS, set, time
28796a4d4014SLisandro Dalcin @*/
28807087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
28816a4d4014SLisandro Dalcin {
28826a4d4014SLisandro Dalcin   PetscFunctionBegin;
28830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2884c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
28856a4d4014SLisandro Dalcin   ts->ptime = t;
28866a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
28876a4d4014SLisandro Dalcin }
28886a4d4014SLisandro Dalcin 
28896a4d4014SLisandro Dalcin #undef __FUNCT__
28904a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2891d763cef2SBarry Smith /*@C
2892d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2893d763cef2SBarry Smith    TS options in the database.
2894d763cef2SBarry Smith 
28953f9fe445SBarry Smith    Logically Collective on TS
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    Input Parameter:
2898d763cef2SBarry Smith +  ts     - The TS context
2899d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2900d763cef2SBarry Smith 
2901d763cef2SBarry Smith    Notes:
2902d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2903d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2904d763cef2SBarry Smith    hyphen.
2905d763cef2SBarry Smith 
2906d763cef2SBarry Smith    Level: advanced
2907d763cef2SBarry Smith 
2908d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2909d763cef2SBarry Smith 
2910d763cef2SBarry Smith .seealso: TSSetFromOptions()
2911d763cef2SBarry Smith 
2912d763cef2SBarry Smith @*/
29137087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2914d763cef2SBarry Smith {
2915dfbe8321SBarry Smith   PetscErrorCode ierr;
2916089b2837SJed Brown   SNES           snes;
2917d763cef2SBarry Smith 
2918d763cef2SBarry Smith   PetscFunctionBegin;
29190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2920d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2921089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2922089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2923d763cef2SBarry Smith   PetscFunctionReturn(0);
2924d763cef2SBarry Smith }
2925d763cef2SBarry Smith 
2926d763cef2SBarry Smith 
29274a2ae208SSatish Balay #undef __FUNCT__
29284a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2929d763cef2SBarry Smith /*@C
2930d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2931d763cef2SBarry Smith    TS options in the database.
2932d763cef2SBarry Smith 
29333f9fe445SBarry Smith    Logically Collective on TS
2934d763cef2SBarry Smith 
2935d763cef2SBarry Smith    Input Parameter:
2936d763cef2SBarry Smith +  ts     - The TS context
2937d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2938d763cef2SBarry Smith 
2939d763cef2SBarry Smith    Notes:
2940d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2941d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2942d763cef2SBarry Smith    hyphen.
2943d763cef2SBarry Smith 
2944d763cef2SBarry Smith    Level: advanced
2945d763cef2SBarry Smith 
2946d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2947d763cef2SBarry Smith 
2948d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2949d763cef2SBarry Smith 
2950d763cef2SBarry Smith @*/
29517087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2952d763cef2SBarry Smith {
2953dfbe8321SBarry Smith   PetscErrorCode ierr;
2954089b2837SJed Brown   SNES           snes;
2955d763cef2SBarry Smith 
2956d763cef2SBarry Smith   PetscFunctionBegin;
29570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2958d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2959089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2960089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2961d763cef2SBarry Smith   PetscFunctionReturn(0);
2962d763cef2SBarry Smith }
2963d763cef2SBarry Smith 
29644a2ae208SSatish Balay #undef __FUNCT__
29654a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2966d763cef2SBarry Smith /*@C
2967d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2968d763cef2SBarry Smith    TS options in the database.
2969d763cef2SBarry Smith 
2970d763cef2SBarry Smith    Not Collective
2971d763cef2SBarry Smith 
2972d763cef2SBarry Smith    Input Parameter:
2973d763cef2SBarry Smith .  ts - The TS context
2974d763cef2SBarry Smith 
2975d763cef2SBarry Smith    Output Parameter:
2976d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2977d763cef2SBarry Smith 
2978d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2979d763cef2SBarry Smith    sufficient length to hold the prefix.
2980d763cef2SBarry Smith 
2981d763cef2SBarry Smith    Level: intermediate
2982d763cef2SBarry Smith 
2983d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2984d763cef2SBarry Smith 
2985d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2986d763cef2SBarry Smith @*/
29877087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2988d763cef2SBarry Smith {
2989dfbe8321SBarry Smith   PetscErrorCode ierr;
2990d763cef2SBarry Smith 
2991d763cef2SBarry Smith   PetscFunctionBegin;
29920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29934482741eSBarry Smith   PetscValidPointer(prefix,2);
2994d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2995d763cef2SBarry Smith   PetscFunctionReturn(0);
2996d763cef2SBarry Smith }
2997d763cef2SBarry Smith 
29984a2ae208SSatish Balay #undef __FUNCT__
29994a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3000d763cef2SBarry Smith /*@C
3001d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3002d763cef2SBarry Smith 
3003d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3004d763cef2SBarry Smith 
3005d763cef2SBarry Smith    Input Parameter:
3006d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3007d763cef2SBarry Smith 
3008d763cef2SBarry Smith    Output Parameters:
3009e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3010e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3011e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3012e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3013d763cef2SBarry Smith 
30140298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3015d763cef2SBarry Smith 
3016d763cef2SBarry Smith    Level: intermediate
3017d763cef2SBarry Smith 
301826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3019d763cef2SBarry Smith 
3020d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3021d763cef2SBarry Smith @*/
3022e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3023d763cef2SBarry Smith {
3024089b2837SJed Brown   PetscErrorCode ierr;
3025089b2837SJed Brown   SNES           snes;
302624989b8cSPeter Brune   DM             dm;
3027089b2837SJed Brown 
3028d763cef2SBarry Smith   PetscFunctionBegin;
3029089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3030e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
303124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
303224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3033d763cef2SBarry Smith   PetscFunctionReturn(0);
3034d763cef2SBarry Smith }
3035d763cef2SBarry Smith 
30361713a123SBarry Smith #undef __FUNCT__
30372eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
30382eca1d9cSJed Brown /*@C
30392eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
30402eca1d9cSJed Brown 
30412eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
30422eca1d9cSJed Brown 
30432eca1d9cSJed Brown    Input Parameter:
30442eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
30452eca1d9cSJed Brown 
30462eca1d9cSJed Brown    Output Parameters:
3047e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3048e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
30492eca1d9cSJed Brown .  f   - The function to compute the matrices
30502eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
30512eca1d9cSJed Brown 
30520298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
30532eca1d9cSJed Brown 
30542eca1d9cSJed Brown    Level: advanced
30552eca1d9cSJed Brown 
30562eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
30572eca1d9cSJed Brown 
30582eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
30592eca1d9cSJed Brown @*/
3060e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
30612eca1d9cSJed Brown {
3062089b2837SJed Brown   PetscErrorCode ierr;
3063089b2837SJed Brown   SNES           snes;
306424989b8cSPeter Brune   DM             dm;
30650910c330SBarry Smith 
30662eca1d9cSJed Brown   PetscFunctionBegin;
3067089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3068f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3069e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
307024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
307124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
30722eca1d9cSJed Brown   PetscFunctionReturn(0);
30732eca1d9cSJed Brown }
30742eca1d9cSJed Brown 
30756083293cSBarry Smith 
30762eca1d9cSJed Brown #undef __FUNCT__
307783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
30781713a123SBarry Smith /*@C
307983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
30801713a123SBarry Smith    VecView() for the solution at each timestep
30811713a123SBarry Smith 
30821713a123SBarry Smith    Collective on TS
30831713a123SBarry Smith 
30841713a123SBarry Smith    Input Parameters:
30851713a123SBarry Smith +  ts - the TS context
30861713a123SBarry Smith .  step - current time-step
3087142b95e3SSatish Balay .  ptime - current time
30880298fd71SBarry Smith -  dummy - either a viewer or NULL
30891713a123SBarry Smith 
309099fdda47SBarry Smith    Options Database:
309199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
309299fdda47SBarry Smith 
309399fdda47SBarry Smith    Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
309499fdda47SBarry Smith        will look bad
309599fdda47SBarry Smith 
30961713a123SBarry Smith    Level: intermediate
30971713a123SBarry Smith 
30981713a123SBarry Smith .keywords: TS,  vector, monitor, view
30991713a123SBarry Smith 
3100a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31011713a123SBarry Smith @*/
31020910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31031713a123SBarry Smith {
3104dfbe8321SBarry Smith   PetscErrorCode   ierr;
310583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3106473a3ab2SBarry Smith   PetscDraw        draw;
31071713a123SBarry Smith 
31081713a123SBarry Smith   PetscFunctionBegin;
31096083293cSBarry Smith   if (!step && ictx->showinitial) {
31106083293cSBarry Smith     if (!ictx->initialsolution) {
31110910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
31121713a123SBarry Smith     }
31130910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
31146083293cSBarry Smith   }
3115b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
31160dcf80beSBarry Smith 
31176083293cSBarry Smith   if (ictx->showinitial) {
31186083293cSBarry Smith     PetscReal pause;
31196083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
31206083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
31216083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
31226083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
31236083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
31246083293cSBarry Smith   }
31250910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3126473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3127473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3128473a3ab2SBarry Smith     char      time[32];
3129473a3ab2SBarry Smith     size_t    len;
3130473a3ab2SBarry Smith 
3131473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3132473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3133473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3134473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
31350298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3136473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3137473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3138473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3139473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3140473a3ab2SBarry Smith   }
3141473a3ab2SBarry Smith 
31426083293cSBarry Smith   if (ictx->showinitial) {
31436083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
31446083293cSBarry Smith   }
31451713a123SBarry Smith   PetscFunctionReturn(0);
31461713a123SBarry Smith }
31471713a123SBarry Smith 
31482d5ee99bSBarry Smith #undef __FUNCT__
31492d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
31502d5ee99bSBarry Smith /*@C
31512d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
31522d5ee99bSBarry Smith 
31532d5ee99bSBarry Smith    Collective on TS
31542d5ee99bSBarry Smith 
31552d5ee99bSBarry Smith    Input Parameters:
31562d5ee99bSBarry Smith +  ts - the TS context
31572d5ee99bSBarry Smith .  step - current time-step
31582d5ee99bSBarry Smith .  ptime - current time
31592d5ee99bSBarry Smith -  dummy - either a viewer or NULL
31602d5ee99bSBarry Smith 
31612d5ee99bSBarry Smith    Level: intermediate
31622d5ee99bSBarry Smith 
31632d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
31642d5ee99bSBarry Smith 
31652d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31662d5ee99bSBarry Smith @*/
31672d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31682d5ee99bSBarry Smith {
31692d5ee99bSBarry Smith   PetscErrorCode    ierr;
31702d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
31712d5ee99bSBarry Smith   PetscDraw         draw;
31722d5ee99bSBarry Smith   MPI_Comm          comm;
31732d5ee99bSBarry Smith   PetscInt          n;
31742d5ee99bSBarry Smith   PetscMPIInt       size;
31752d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
31762d5ee99bSBarry Smith   char              time[32];
31772d5ee99bSBarry Smith   size_t            len;
31782d5ee99bSBarry Smith   const PetscScalar *U;
31792d5ee99bSBarry Smith 
31802d5ee99bSBarry Smith   PetscFunctionBegin;
31812d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
31822d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
31832d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
31842d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
31852d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
31862d5ee99bSBarry Smith 
31872d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
31882d5ee99bSBarry Smith 
31892d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
31904b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3191ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3192a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3193a4494fc1SBarry Smith       PetscFunctionReturn(0);
3194a4494fc1SBarry Smith   }
31952d5ee99bSBarry Smith   if (!step) ictx->color++;
31962d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
31972d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
31982d5ee99bSBarry Smith 
31992d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
32004b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
32012d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
32022d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
32032d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
32042d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
32052d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
32062d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
32072d5ee99bSBarry Smith   }
32082d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
32092d5ee99bSBarry Smith   PetscFunctionReturn(0);
32102d5ee99bSBarry Smith }
32112d5ee99bSBarry Smith 
32121713a123SBarry Smith 
32136c699258SBarry Smith #undef __FUNCT__
321483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
32156083293cSBarry Smith /*@C
321683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
32176083293cSBarry Smith 
32186083293cSBarry Smith    Collective on TS
32196083293cSBarry Smith 
32206083293cSBarry Smith    Input Parameters:
32216083293cSBarry Smith .    ctx - the monitor context
32226083293cSBarry Smith 
32236083293cSBarry Smith    Level: intermediate
32246083293cSBarry Smith 
32256083293cSBarry Smith .keywords: TS,  vector, monitor, view
32266083293cSBarry Smith 
322783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
32286083293cSBarry Smith @*/
322983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
32306083293cSBarry Smith {
32316083293cSBarry Smith   PetscErrorCode ierr;
32326083293cSBarry Smith 
32336083293cSBarry Smith   PetscFunctionBegin;
32344b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
323583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
323683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
323783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
32386083293cSBarry Smith   PetscFunctionReturn(0);
32396083293cSBarry Smith }
32406083293cSBarry Smith 
32416083293cSBarry Smith #undef __FUNCT__
324283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
32436083293cSBarry Smith /*@C
324483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
32456083293cSBarry Smith 
32466083293cSBarry Smith    Collective on TS
32476083293cSBarry Smith 
32486083293cSBarry Smith    Input Parameter:
32496083293cSBarry Smith .    ts - time-step context
32506083293cSBarry Smith 
32516083293cSBarry Smith    Output Patameter:
32526083293cSBarry Smith .    ctx - the monitor context
32536083293cSBarry Smith 
325499fdda47SBarry Smith    Options Database:
325599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
325699fdda47SBarry Smith 
32576083293cSBarry Smith    Level: intermediate
32586083293cSBarry Smith 
32596083293cSBarry Smith .keywords: TS,  vector, monitor, view
32606083293cSBarry Smith 
326183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
32626083293cSBarry Smith @*/
326383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
32646083293cSBarry Smith {
32656083293cSBarry Smith   PetscErrorCode   ierr;
32666083293cSBarry Smith 
32676083293cSBarry Smith   PetscFunctionBegin;
326883a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
326983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3270e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3271bbd56ea5SKarl Rupp 
327283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3273473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
32740298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3275473a3ab2SBarry Smith 
3276473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
32770298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
32782d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
32796083293cSBarry Smith   PetscFunctionReturn(0);
32806083293cSBarry Smith }
32816083293cSBarry Smith 
32826083293cSBarry Smith #undef __FUNCT__
328383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
32843a471f94SBarry Smith /*@C
328583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
32863a471f94SBarry Smith    VecView() for the error at each timestep
32873a471f94SBarry Smith 
32883a471f94SBarry Smith    Collective on TS
32893a471f94SBarry Smith 
32903a471f94SBarry Smith    Input Parameters:
32913a471f94SBarry Smith +  ts - the TS context
32923a471f94SBarry Smith .  step - current time-step
32933a471f94SBarry Smith .  ptime - current time
32940298fd71SBarry Smith -  dummy - either a viewer or NULL
32953a471f94SBarry Smith 
32963a471f94SBarry Smith    Level: intermediate
32973a471f94SBarry Smith 
32983a471f94SBarry Smith .keywords: TS,  vector, monitor, view
32993a471f94SBarry Smith 
33003a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
33013a471f94SBarry Smith @*/
33020910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
33033a471f94SBarry Smith {
33043a471f94SBarry Smith   PetscErrorCode   ierr;
330583a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
330683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
33073a471f94SBarry Smith   Vec              work;
33083a471f94SBarry Smith 
33093a471f94SBarry Smith   PetscFunctionBegin;
3310b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
33110910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
33123a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
33130910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
33143a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
33153a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
33163a471f94SBarry Smith   PetscFunctionReturn(0);
33173a471f94SBarry Smith }
33183a471f94SBarry Smith 
33192a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
33203a471f94SBarry Smith #undef __FUNCT__
33216c699258SBarry Smith #define __FUNCT__ "TSSetDM"
33226c699258SBarry Smith /*@
33236c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
33246c699258SBarry Smith 
33253f9fe445SBarry Smith    Logically Collective on TS and DM
33266c699258SBarry Smith 
33276c699258SBarry Smith    Input Parameters:
33286c699258SBarry Smith +  ts - the preconditioner context
33296c699258SBarry Smith -  dm - the dm
33306c699258SBarry Smith 
33316c699258SBarry Smith    Level: intermediate
33326c699258SBarry Smith 
33336c699258SBarry Smith 
33346c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
33356c699258SBarry Smith @*/
33367087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
33376c699258SBarry Smith {
33386c699258SBarry Smith   PetscErrorCode ierr;
3339089b2837SJed Brown   SNES           snes;
3340942e3340SBarry Smith   DMTS           tsdm;
33416c699258SBarry Smith 
33426c699258SBarry Smith   PetscFunctionBegin;
33430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
334470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3345942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
33462a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3347942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3348942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
334924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
335024989b8cSPeter Brune         tsdm->originaldm = dm;
335124989b8cSPeter Brune       }
335224989b8cSPeter Brune     }
33536bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
335424989b8cSPeter Brune   }
33556c699258SBarry Smith   ts->dm = dm;
3356bbd56ea5SKarl Rupp 
3357089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3358089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
33596c699258SBarry Smith   PetscFunctionReturn(0);
33606c699258SBarry Smith }
33616c699258SBarry Smith 
33626c699258SBarry Smith #undef __FUNCT__
33636c699258SBarry Smith #define __FUNCT__ "TSGetDM"
33646c699258SBarry Smith /*@
33656c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
33666c699258SBarry Smith 
33673f9fe445SBarry Smith    Not Collective
33686c699258SBarry Smith 
33696c699258SBarry Smith    Input Parameter:
33706c699258SBarry Smith . ts - the preconditioner context
33716c699258SBarry Smith 
33726c699258SBarry Smith    Output Parameter:
33736c699258SBarry Smith .  dm - the dm
33746c699258SBarry Smith 
33756c699258SBarry Smith    Level: intermediate
33766c699258SBarry Smith 
33776c699258SBarry Smith 
33786c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
33796c699258SBarry Smith @*/
33807087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
33816c699258SBarry Smith {
3382496e6a7aSJed Brown   PetscErrorCode ierr;
3383496e6a7aSJed Brown 
33846c699258SBarry Smith   PetscFunctionBegin;
33850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3386496e6a7aSJed Brown   if (!ts->dm) {
3387ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3388496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3389496e6a7aSJed Brown   }
33906c699258SBarry Smith   *dm = ts->dm;
33916c699258SBarry Smith   PetscFunctionReturn(0);
33926c699258SBarry Smith }
33931713a123SBarry Smith 
33940f5c6efeSJed Brown #undef __FUNCT__
33950f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
33960f5c6efeSJed Brown /*@
33970f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
33980f5c6efeSJed Brown 
33993f9fe445SBarry Smith    Logically Collective on SNES
34000f5c6efeSJed Brown 
34010f5c6efeSJed Brown    Input Parameter:
3402d42a1c89SJed Brown + snes - nonlinear solver
34030910c330SBarry Smith . U - the current state at which to evaluate the residual
3404d42a1c89SJed Brown - ctx - user context, must be a TS
34050f5c6efeSJed Brown 
34060f5c6efeSJed Brown    Output Parameter:
34070f5c6efeSJed Brown . F - the nonlinear residual
34080f5c6efeSJed Brown 
34090f5c6efeSJed Brown    Notes:
34100f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
34110f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
34120f5c6efeSJed Brown 
34130f5c6efeSJed Brown    Level: advanced
34140f5c6efeSJed Brown 
34150f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
34160f5c6efeSJed Brown @*/
34170910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
34180f5c6efeSJed Brown {
34190f5c6efeSJed Brown   TS             ts = (TS)ctx;
34200f5c6efeSJed Brown   PetscErrorCode ierr;
34210f5c6efeSJed Brown 
34220f5c6efeSJed Brown   PetscFunctionBegin;
34230f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34240910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34250f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
34260f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
34270910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
34280f5c6efeSJed Brown   PetscFunctionReturn(0);
34290f5c6efeSJed Brown }
34300f5c6efeSJed Brown 
34310f5c6efeSJed Brown #undef __FUNCT__
34320f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
34330f5c6efeSJed Brown /*@
34340f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
34350f5c6efeSJed Brown 
34360f5c6efeSJed Brown    Collective on SNES
34370f5c6efeSJed Brown 
34380f5c6efeSJed Brown    Input Parameter:
34390f5c6efeSJed Brown + snes - nonlinear solver
34400910c330SBarry Smith . U - the current state at which to evaluate the residual
34410f5c6efeSJed Brown - ctx - user context, must be a TS
34420f5c6efeSJed Brown 
34430f5c6efeSJed Brown    Output Parameter:
34440f5c6efeSJed Brown + A - the Jacobian
34450f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
34460f5c6efeSJed Brown - flag - indicates any structure change in the matrix
34470f5c6efeSJed Brown 
34480f5c6efeSJed Brown    Notes:
34490f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
34500f5c6efeSJed Brown 
34510f5c6efeSJed Brown    Level: developer
34520f5c6efeSJed Brown 
34530f5c6efeSJed Brown .seealso: SNESSetJacobian()
34540f5c6efeSJed Brown @*/
34550910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
34560f5c6efeSJed Brown {
34570f5c6efeSJed Brown   TS             ts = (TS)ctx;
34580f5c6efeSJed Brown   PetscErrorCode ierr;
34590f5c6efeSJed Brown 
34600f5c6efeSJed Brown   PetscFunctionBegin;
34610f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34630f5c6efeSJed Brown   PetscValidPointer(A,3);
34640f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
34650f5c6efeSJed Brown   PetscValidPointer(B,4);
34660f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
34670f5c6efeSJed Brown   PetscValidPointer(flag,5);
34680f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
34690910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
34700f5c6efeSJed Brown   PetscFunctionReturn(0);
34710f5c6efeSJed Brown }
3472325fc9f4SBarry Smith 
34730e4ef248SJed Brown #undef __FUNCT__
34740e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
34750e4ef248SJed Brown /*@C
34760e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
34770e4ef248SJed Brown 
34780e4ef248SJed Brown    Collective on TS
34790e4ef248SJed Brown 
34800e4ef248SJed Brown    Input Arguments:
34810e4ef248SJed Brown +  ts - time stepping context
34820e4ef248SJed Brown .  t - time at which to evaluate
34830910c330SBarry Smith .  U - state at which to evaluate
34840e4ef248SJed Brown -  ctx - context
34850e4ef248SJed Brown 
34860e4ef248SJed Brown    Output Arguments:
34870e4ef248SJed Brown .  F - right hand side
34880e4ef248SJed Brown 
34890e4ef248SJed Brown    Level: intermediate
34900e4ef248SJed Brown 
34910e4ef248SJed Brown    Notes:
34920e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
34930e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
34940e4ef248SJed Brown 
34950e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
34960e4ef248SJed Brown @*/
34970910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
34980e4ef248SJed Brown {
34990e4ef248SJed Brown   PetscErrorCode ierr;
35000e4ef248SJed Brown   Mat            Arhs,Brhs;
35010e4ef248SJed Brown   MatStructure   flg2;
35020e4ef248SJed Brown 
35030e4ef248SJed Brown   PetscFunctionBegin;
35040e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
35050910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
35060910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
35070e4ef248SJed Brown   PetscFunctionReturn(0);
35080e4ef248SJed Brown }
35090e4ef248SJed Brown 
35100e4ef248SJed Brown #undef __FUNCT__
35110e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
35120e4ef248SJed Brown /*@C
35130e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
35140e4ef248SJed Brown 
35150e4ef248SJed Brown    Collective on TS
35160e4ef248SJed Brown 
35170e4ef248SJed Brown    Input Arguments:
35180e4ef248SJed Brown +  ts - time stepping context
35190e4ef248SJed Brown .  t - time at which to evaluate
35200910c330SBarry Smith .  U - state at which to evaluate
35210e4ef248SJed Brown -  ctx - context
35220e4ef248SJed Brown 
35230e4ef248SJed Brown    Output Arguments:
35240e4ef248SJed Brown +  A - pointer to operator
35250e4ef248SJed Brown .  B - pointer to preconditioning matrix
35260e4ef248SJed Brown -  flg - matrix structure flag
35270e4ef248SJed Brown 
35280e4ef248SJed Brown    Level: intermediate
35290e4ef248SJed Brown 
35300e4ef248SJed Brown    Notes:
35310e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
35320e4ef248SJed Brown 
35330e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
35340e4ef248SJed Brown @*/
35350910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
35360e4ef248SJed Brown {
35370e4ef248SJed Brown   PetscFunctionBegin;
35380e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
35390e4ef248SJed Brown   PetscFunctionReturn(0);
35400e4ef248SJed Brown }
35410e4ef248SJed Brown 
35420026cea9SSean Farley #undef __FUNCT__
35430026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
35440026cea9SSean Farley /*@C
35450026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
35460026cea9SSean Farley 
35470026cea9SSean Farley    Collective on TS
35480026cea9SSean Farley 
35490026cea9SSean Farley    Input Arguments:
35500026cea9SSean Farley +  ts - time stepping context
35510026cea9SSean Farley .  t - time at which to evaluate
35520910c330SBarry Smith .  U - state at which to evaluate
35530910c330SBarry Smith .  Udot - time derivative of state vector
35540026cea9SSean Farley -  ctx - context
35550026cea9SSean Farley 
35560026cea9SSean Farley    Output Arguments:
35570026cea9SSean Farley .  F - left hand side
35580026cea9SSean Farley 
35590026cea9SSean Farley    Level: intermediate
35600026cea9SSean Farley 
35610026cea9SSean Farley    Notes:
35620910c330SBarry 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
35630026cea9SSean Farley    user is required to write their own TSComputeIFunction.
35640026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
35650026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
35660026cea9SSean Farley 
35670026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
35680026cea9SSean Farley @*/
35690910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
35700026cea9SSean Farley {
35710026cea9SSean Farley   PetscErrorCode ierr;
35720026cea9SSean Farley   Mat            A,B;
35730026cea9SSean Farley   MatStructure   flg2;
35740026cea9SSean Farley 
35750026cea9SSean Farley   PetscFunctionBegin;
35760298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
35770910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
35780910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
35790026cea9SSean Farley   PetscFunctionReturn(0);
35800026cea9SSean Farley }
35810026cea9SSean Farley 
35820026cea9SSean Farley #undef __FUNCT__
35830026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
35840026cea9SSean Farley /*@C
3585b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
35860026cea9SSean Farley 
35870026cea9SSean Farley    Collective on TS
35880026cea9SSean Farley 
35890026cea9SSean Farley    Input Arguments:
35900026cea9SSean Farley +  ts - time stepping context
35910026cea9SSean Farley .  t - time at which to evaluate
35920910c330SBarry Smith .  U - state at which to evaluate
35930910c330SBarry Smith .  Udot - time derivative of state vector
35940026cea9SSean Farley .  shift - shift to apply
35950026cea9SSean Farley -  ctx - context
35960026cea9SSean Farley 
35970026cea9SSean Farley    Output Arguments:
35980026cea9SSean Farley +  A - pointer to operator
35990026cea9SSean Farley .  B - pointer to preconditioning matrix
36000026cea9SSean Farley -  flg - matrix structure flag
36010026cea9SSean Farley 
3602b41af12eSJed Brown    Level: advanced
36030026cea9SSean Farley 
36040026cea9SSean Farley    Notes:
36050026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
36060026cea9SSean Farley 
3607b41af12eSJed Brown    It is only appropriate for problems of the form
3608b41af12eSJed Brown 
3609b41af12eSJed Brown $     M Udot = F(U,t)
3610b41af12eSJed Brown 
3611b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
3612b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
3613b41af12eSJed Brown   an implicit operator of the form
3614b41af12eSJed Brown 
3615b41af12eSJed Brown $    shift*M + J
3616b41af12eSJed Brown 
3617b41af12eSJed 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
3618b41af12eSJed Brown   a copy of M or reassemble it when requested.
3619b41af12eSJed Brown 
36200026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
36210026cea9SSean Farley @*/
36220910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
36230026cea9SSean Farley {
3624b41af12eSJed Brown   PetscErrorCode ierr;
3625b41af12eSJed Brown 
36260026cea9SSean Farley   PetscFunctionBegin;
3627b41af12eSJed Brown   ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
3628b41af12eSJed Brown   ts->ijacobian.shift = shift;
36290026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
36300026cea9SSean Farley   PetscFunctionReturn(0);
36310026cea9SSean Farley }
3632b41af12eSJed Brown 
3633e817cc15SEmil Constantinescu #undef __FUNCT__
3634e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3635e817cc15SEmil Constantinescu /*@
3636e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3637e817cc15SEmil Constantinescu 
3638e817cc15SEmil Constantinescu    Not Collective
3639e817cc15SEmil Constantinescu 
3640e817cc15SEmil Constantinescu    Input Parameter:
3641e817cc15SEmil Constantinescu .  ts - the TS context
3642e817cc15SEmil Constantinescu 
3643e817cc15SEmil Constantinescu    Output Parameter:
36444e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3645e817cc15SEmil Constantinescu 
3646e817cc15SEmil Constantinescu    Level: beginner
3647e817cc15SEmil Constantinescu 
3648e817cc15SEmil Constantinescu .keywords: TS, equation type
3649e817cc15SEmil Constantinescu 
3650e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3651e817cc15SEmil Constantinescu @*/
3652e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3653e817cc15SEmil Constantinescu {
3654e817cc15SEmil Constantinescu   PetscFunctionBegin;
3655e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3656e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3657e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3658e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3659e817cc15SEmil Constantinescu }
3660e817cc15SEmil Constantinescu 
3661e817cc15SEmil Constantinescu #undef __FUNCT__
3662e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3663e817cc15SEmil Constantinescu /*@
3664e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3665e817cc15SEmil Constantinescu 
3666e817cc15SEmil Constantinescu    Not Collective
3667e817cc15SEmil Constantinescu 
3668e817cc15SEmil Constantinescu    Input Parameter:
3669e817cc15SEmil Constantinescu +  ts - the TS context
36704e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3671e817cc15SEmil Constantinescu 
3672e817cc15SEmil Constantinescu    Level: advanced
3673e817cc15SEmil Constantinescu 
3674e817cc15SEmil Constantinescu .keywords: TS, equation type
3675e817cc15SEmil Constantinescu 
3676e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3677e817cc15SEmil Constantinescu @*/
3678e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3679e817cc15SEmil Constantinescu {
3680e817cc15SEmil Constantinescu   PetscFunctionBegin;
3681e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3682e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3683e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3684e817cc15SEmil Constantinescu }
36850026cea9SSean Farley 
36864af1b03aSJed Brown #undef __FUNCT__
36874af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
36884af1b03aSJed Brown /*@
36894af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
36904af1b03aSJed Brown 
36914af1b03aSJed Brown    Not Collective
36924af1b03aSJed Brown 
36934af1b03aSJed Brown    Input Parameter:
36944af1b03aSJed Brown .  ts - the TS context
36954af1b03aSJed Brown 
36964af1b03aSJed Brown    Output Parameter:
36974af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
36984af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
36994af1b03aSJed Brown 
3700487e0bb9SJed Brown    Level: beginner
37014af1b03aSJed Brown 
3702cd652676SJed Brown    Notes:
3703cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
37044af1b03aSJed Brown 
37054af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
37064af1b03aSJed Brown 
37074af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
37084af1b03aSJed Brown @*/
37094af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
37104af1b03aSJed Brown {
37114af1b03aSJed Brown   PetscFunctionBegin;
37124af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37134af1b03aSJed Brown   PetscValidPointer(reason,2);
37144af1b03aSJed Brown   *reason = ts->reason;
37154af1b03aSJed Brown   PetscFunctionReturn(0);
37164af1b03aSJed Brown }
37174af1b03aSJed Brown 
3718fb1732b5SBarry Smith #undef __FUNCT__
3719d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3720d6ad946cSShri Abhyankar /*@
3721d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3722d6ad946cSShri Abhyankar 
3723d6ad946cSShri Abhyankar    Not Collective
3724d6ad946cSShri Abhyankar 
3725d6ad946cSShri Abhyankar    Input Parameter:
3726d6ad946cSShri Abhyankar +  ts - the TS context
3727d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3728d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3729d6ad946cSShri Abhyankar 
3730f5abba47SShri Abhyankar    Level: advanced
3731d6ad946cSShri Abhyankar 
3732d6ad946cSShri Abhyankar    Notes:
3733d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3734d6ad946cSShri Abhyankar 
3735d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3736d6ad946cSShri Abhyankar 
3737d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3738d6ad946cSShri Abhyankar @*/
3739d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3740d6ad946cSShri Abhyankar {
3741d6ad946cSShri Abhyankar   PetscFunctionBegin;
3742d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3743d6ad946cSShri Abhyankar   ts->reason = reason;
3744d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3745d6ad946cSShri Abhyankar }
3746d6ad946cSShri Abhyankar 
3747d6ad946cSShri Abhyankar #undef __FUNCT__
3748cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3749cc708dedSBarry Smith /*@
3750cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3751cc708dedSBarry Smith 
3752cc708dedSBarry Smith    Not Collective
3753cc708dedSBarry Smith 
3754cc708dedSBarry Smith    Input Parameter:
3755cc708dedSBarry Smith .  ts - the TS context
3756cc708dedSBarry Smith 
3757cc708dedSBarry Smith    Output Parameter:
3758cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3759cc708dedSBarry Smith 
3760487e0bb9SJed Brown    Level: beginner
3761cc708dedSBarry Smith 
3762cc708dedSBarry Smith    Notes:
3763cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3764cc708dedSBarry Smith 
3765cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3766cc708dedSBarry Smith 
3767cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3768cc708dedSBarry Smith @*/
3769cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3770cc708dedSBarry Smith {
3771cc708dedSBarry Smith   PetscFunctionBegin;
3772cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3773cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3774cc708dedSBarry Smith   *ftime = ts->solvetime;
3775cc708dedSBarry Smith   PetscFunctionReturn(0);
3776cc708dedSBarry Smith }
3777cc708dedSBarry Smith 
3778cc708dedSBarry Smith #undef __FUNCT__
37795ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
37809f67acb7SJed Brown /*@
37815ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
37829f67acb7SJed Brown    used by the time integrator.
37839f67acb7SJed Brown 
37849f67acb7SJed Brown    Not Collective
37859f67acb7SJed Brown 
37869f67acb7SJed Brown    Input Parameter:
37879f67acb7SJed Brown .  ts - TS context
37889f67acb7SJed Brown 
37899f67acb7SJed Brown    Output Parameter:
37909f67acb7SJed Brown .  nits - number of nonlinear iterations
37919f67acb7SJed Brown 
37929f67acb7SJed Brown    Notes:
37939f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
37949f67acb7SJed Brown 
37959f67acb7SJed Brown    Level: intermediate
37969f67acb7SJed Brown 
37979f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
37989f67acb7SJed Brown 
37995ef26d82SJed Brown .seealso:  TSGetKSPIterations()
38009f67acb7SJed Brown @*/
38015ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
38029f67acb7SJed Brown {
38039f67acb7SJed Brown   PetscFunctionBegin;
38049f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38059f67acb7SJed Brown   PetscValidIntPointer(nits,2);
38065ef26d82SJed Brown   *nits = ts->snes_its;
38079f67acb7SJed Brown   PetscFunctionReturn(0);
38089f67acb7SJed Brown }
38099f67acb7SJed Brown 
38109f67acb7SJed Brown #undef __FUNCT__
38115ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
38129f67acb7SJed Brown /*@
38135ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
38149f67acb7SJed Brown    used by the time integrator.
38159f67acb7SJed Brown 
38169f67acb7SJed Brown    Not Collective
38179f67acb7SJed Brown 
38189f67acb7SJed Brown    Input Parameter:
38199f67acb7SJed Brown .  ts - TS context
38209f67acb7SJed Brown 
38219f67acb7SJed Brown    Output Parameter:
38229f67acb7SJed Brown .  lits - number of linear iterations
38239f67acb7SJed Brown 
38249f67acb7SJed Brown    Notes:
38259f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
38269f67acb7SJed Brown 
38279f67acb7SJed Brown    Level: intermediate
38289f67acb7SJed Brown 
38299f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
38309f67acb7SJed Brown 
38315ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
38329f67acb7SJed Brown @*/
38335ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
38349f67acb7SJed Brown {
38359f67acb7SJed Brown   PetscFunctionBegin;
38369f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38379f67acb7SJed Brown   PetscValidIntPointer(lits,2);
38385ef26d82SJed Brown   *lits = ts->ksp_its;
38399f67acb7SJed Brown   PetscFunctionReturn(0);
38409f67acb7SJed Brown }
38419f67acb7SJed Brown 
38429f67acb7SJed Brown #undef __FUNCT__
3843cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3844cef5090cSJed Brown /*@
3845cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3846cef5090cSJed Brown 
3847cef5090cSJed Brown    Not Collective
3848cef5090cSJed Brown 
3849cef5090cSJed Brown    Input Parameter:
3850cef5090cSJed Brown .  ts - TS context
3851cef5090cSJed Brown 
3852cef5090cSJed Brown    Output Parameter:
3853cef5090cSJed Brown .  rejects - number of steps rejected
3854cef5090cSJed Brown 
3855cef5090cSJed Brown    Notes:
3856cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3857cef5090cSJed Brown 
3858cef5090cSJed Brown    Level: intermediate
3859cef5090cSJed Brown 
3860cef5090cSJed Brown .keywords: TS, get, number
3861cef5090cSJed Brown 
38625ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3863cef5090cSJed Brown @*/
3864cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3865cef5090cSJed Brown {
3866cef5090cSJed Brown   PetscFunctionBegin;
3867cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3868cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3869cef5090cSJed Brown   *rejects = ts->reject;
3870cef5090cSJed Brown   PetscFunctionReturn(0);
3871cef5090cSJed Brown }
3872cef5090cSJed Brown 
3873cef5090cSJed Brown #undef __FUNCT__
3874cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3875cef5090cSJed Brown /*@
3876cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3877cef5090cSJed Brown 
3878cef5090cSJed Brown    Not Collective
3879cef5090cSJed Brown 
3880cef5090cSJed Brown    Input Parameter:
3881cef5090cSJed Brown .  ts - TS context
3882cef5090cSJed Brown 
3883cef5090cSJed Brown    Output Parameter:
3884cef5090cSJed Brown .  fails - number of failed nonlinear solves
3885cef5090cSJed Brown 
3886cef5090cSJed Brown    Notes:
3887cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3888cef5090cSJed Brown 
3889cef5090cSJed Brown    Level: intermediate
3890cef5090cSJed Brown 
3891cef5090cSJed Brown .keywords: TS, get, number
3892cef5090cSJed Brown 
38935ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3894cef5090cSJed Brown @*/
3895cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3896cef5090cSJed Brown {
3897cef5090cSJed Brown   PetscFunctionBegin;
3898cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3899cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3900cef5090cSJed Brown   *fails = ts->num_snes_failures;
3901cef5090cSJed Brown   PetscFunctionReturn(0);
3902cef5090cSJed Brown }
3903cef5090cSJed Brown 
3904cef5090cSJed Brown #undef __FUNCT__
3905cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3906cef5090cSJed Brown /*@
3907cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3908cef5090cSJed Brown 
3909cef5090cSJed Brown    Not Collective
3910cef5090cSJed Brown 
3911cef5090cSJed Brown    Input Parameter:
3912cef5090cSJed Brown +  ts - TS context
3913cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3914cef5090cSJed Brown 
3915cef5090cSJed Brown    Notes:
3916cef5090cSJed Brown    The counter is reset to zero for each step
3917cef5090cSJed Brown 
3918cef5090cSJed Brown    Options Database Key:
3919cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3920cef5090cSJed Brown 
3921cef5090cSJed Brown    Level: intermediate
3922cef5090cSJed Brown 
3923cef5090cSJed Brown .keywords: TS, set, maximum, number
3924cef5090cSJed Brown 
39255ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3926cef5090cSJed Brown @*/
3927cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3928cef5090cSJed Brown {
3929cef5090cSJed Brown   PetscFunctionBegin;
3930cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3931cef5090cSJed Brown   ts->max_reject = rejects;
3932cef5090cSJed Brown   PetscFunctionReturn(0);
3933cef5090cSJed Brown }
3934cef5090cSJed Brown 
3935cef5090cSJed Brown #undef __FUNCT__
3936cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3937cef5090cSJed Brown /*@
3938cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3939cef5090cSJed Brown 
3940cef5090cSJed Brown    Not Collective
3941cef5090cSJed Brown 
3942cef5090cSJed Brown    Input Parameter:
3943cef5090cSJed Brown +  ts - TS context
3944cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3945cef5090cSJed Brown 
3946cef5090cSJed Brown    Notes:
3947cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3948cef5090cSJed Brown 
3949cef5090cSJed Brown    Options Database Key:
3950cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3951cef5090cSJed Brown 
3952cef5090cSJed Brown    Level: intermediate
3953cef5090cSJed Brown 
3954cef5090cSJed Brown .keywords: TS, set, maximum, number
3955cef5090cSJed Brown 
39565ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3957cef5090cSJed Brown @*/
3958cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3959cef5090cSJed Brown {
3960cef5090cSJed Brown   PetscFunctionBegin;
3961cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3962cef5090cSJed Brown   ts->max_snes_failures = fails;
3963cef5090cSJed Brown   PetscFunctionReturn(0);
3964cef5090cSJed Brown }
3965cef5090cSJed Brown 
3966cef5090cSJed Brown #undef __FUNCT__
3967cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3968cef5090cSJed Brown /*@
3969cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3970cef5090cSJed Brown 
3971cef5090cSJed Brown    Not Collective
3972cef5090cSJed Brown 
3973cef5090cSJed Brown    Input Parameter:
3974cef5090cSJed Brown +  ts - TS context
3975cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3976cef5090cSJed Brown 
3977cef5090cSJed Brown    Options Database Key:
3978cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3979cef5090cSJed Brown 
3980cef5090cSJed Brown    Level: intermediate
3981cef5090cSJed Brown 
3982cef5090cSJed Brown .keywords: TS, set, error
3983cef5090cSJed Brown 
39845ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3985cef5090cSJed Brown @*/
3986cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3987cef5090cSJed Brown {
3988cef5090cSJed Brown   PetscFunctionBegin;
3989cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3990cef5090cSJed Brown   ts->errorifstepfailed = err;
3991cef5090cSJed Brown   PetscFunctionReturn(0);
3992cef5090cSJed Brown }
3993cef5090cSJed Brown 
3994cef5090cSJed Brown #undef __FUNCT__
3995fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3996fb1732b5SBarry Smith /*@C
3997fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3998fb1732b5SBarry Smith 
3999fb1732b5SBarry Smith    Collective on TS
4000fb1732b5SBarry Smith 
4001fb1732b5SBarry Smith    Input Parameters:
4002fb1732b5SBarry Smith +  ts - the TS context
4003fb1732b5SBarry Smith .  step - current time-step
4004fb1732b5SBarry Smith .  ptime - current time
40050910c330SBarry Smith .  u - current state
4006fb1732b5SBarry Smith -  viewer - binary viewer
4007fb1732b5SBarry Smith 
4008fb1732b5SBarry Smith    Level: intermediate
4009fb1732b5SBarry Smith 
4010fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4011fb1732b5SBarry Smith 
4012fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4013fb1732b5SBarry Smith @*/
40140910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4015fb1732b5SBarry Smith {
4016fb1732b5SBarry Smith   PetscErrorCode ierr;
4017ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4018fb1732b5SBarry Smith 
4019fb1732b5SBarry Smith   PetscFunctionBegin;
40200910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4021ed81e22dSJed Brown   PetscFunctionReturn(0);
4022ed81e22dSJed Brown }
4023ed81e22dSJed Brown 
4024ed81e22dSJed Brown #undef __FUNCT__
4025ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4026ed81e22dSJed Brown /*@C
4027ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4028ed81e22dSJed Brown 
4029ed81e22dSJed Brown    Collective on TS
4030ed81e22dSJed Brown 
4031ed81e22dSJed Brown    Input Parameters:
4032ed81e22dSJed Brown +  ts - the TS context
4033ed81e22dSJed Brown .  step - current time-step
4034ed81e22dSJed Brown .  ptime - current time
40350910c330SBarry Smith .  u - current state
4036ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4037ed81e22dSJed Brown 
4038ed81e22dSJed Brown    Level: intermediate
4039ed81e22dSJed Brown 
4040ed81e22dSJed Brown    Notes:
4041ed81e22dSJed 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.
4042ed81e22dSJed Brown    These are named according to the file name template.
4043ed81e22dSJed Brown 
4044ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4045ed81e22dSJed Brown 
4046ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4047ed81e22dSJed Brown 
4048ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4049ed81e22dSJed Brown @*/
40500910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4051ed81e22dSJed Brown {
4052ed81e22dSJed Brown   PetscErrorCode ierr;
4053ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4054ed81e22dSJed Brown   PetscViewer    viewer;
4055ed81e22dSJed Brown 
4056ed81e22dSJed Brown   PetscFunctionBegin;
40578caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4058ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
40590910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4060ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4061ed81e22dSJed Brown   PetscFunctionReturn(0);
4062ed81e22dSJed Brown }
4063ed81e22dSJed Brown 
4064ed81e22dSJed Brown #undef __FUNCT__
4065ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4066ed81e22dSJed Brown /*@C
4067ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4068ed81e22dSJed Brown 
4069ed81e22dSJed Brown    Collective on TS
4070ed81e22dSJed Brown 
4071ed81e22dSJed Brown    Input Parameters:
4072ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4073ed81e22dSJed Brown 
4074ed81e22dSJed Brown    Level: intermediate
4075ed81e22dSJed Brown 
4076ed81e22dSJed Brown    Note:
4077ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4078ed81e22dSJed Brown 
4079ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4080ed81e22dSJed Brown 
4081ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4082ed81e22dSJed Brown @*/
4083ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4084ed81e22dSJed Brown {
4085ed81e22dSJed Brown   PetscErrorCode ierr;
4086ed81e22dSJed Brown 
4087ed81e22dSJed Brown   PetscFunctionBegin;
4088ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4089fb1732b5SBarry Smith   PetscFunctionReturn(0);
4090fb1732b5SBarry Smith }
4091fb1732b5SBarry Smith 
409284df9cb4SJed Brown #undef __FUNCT__
4093552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
409484df9cb4SJed Brown /*@
4095552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
409684df9cb4SJed Brown 
4097ed81e22dSJed Brown    Collective on TS if controller has not been created yet
409884df9cb4SJed Brown 
409984df9cb4SJed Brown    Input Arguments:
4100ed81e22dSJed Brown .  ts - time stepping context
410184df9cb4SJed Brown 
410284df9cb4SJed Brown    Output Arguments:
4103ed81e22dSJed Brown .  adapt - adaptive controller
410484df9cb4SJed Brown 
410584df9cb4SJed Brown    Level: intermediate
410684df9cb4SJed Brown 
4107ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
410884df9cb4SJed Brown @*/
4109552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
411084df9cb4SJed Brown {
411184df9cb4SJed Brown   PetscErrorCode ierr;
411284df9cb4SJed Brown 
411384df9cb4SJed Brown   PetscFunctionBegin;
411484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
411584df9cb4SJed Brown   PetscValidPointer(adapt,2);
411684df9cb4SJed Brown   if (!ts->adapt) {
4117ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
41183bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
41191c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
412084df9cb4SJed Brown   }
412184df9cb4SJed Brown   *adapt = ts->adapt;
412284df9cb4SJed Brown   PetscFunctionReturn(0);
412384df9cb4SJed Brown }
4124d6ebe24aSShri Abhyankar 
4125d6ebe24aSShri Abhyankar #undef __FUNCT__
41261c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
41271c3436cfSJed Brown /*@
41281c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
41291c3436cfSJed Brown 
41301c3436cfSJed Brown    Logically Collective
41311c3436cfSJed Brown 
41321c3436cfSJed Brown    Input Arguments:
41331c3436cfSJed Brown +  ts - time integration context
41341c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
41350298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
41361c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
41370298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
41381c3436cfSJed Brown 
41391c3436cfSJed Brown    Level: beginner
41401c3436cfSJed Brown 
4141c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
41421c3436cfSJed Brown @*/
41431c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
41441c3436cfSJed Brown {
41451c3436cfSJed Brown   PetscErrorCode ierr;
41461c3436cfSJed Brown 
41471c3436cfSJed Brown   PetscFunctionBegin;
4148c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
41491c3436cfSJed Brown   if (vatol) {
41501c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
41511c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4152bbd56ea5SKarl Rupp 
41531c3436cfSJed Brown     ts->vatol = vatol;
41541c3436cfSJed Brown   }
4155c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
41561c3436cfSJed Brown   if (vrtol) {
41571c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
41581c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4159bbd56ea5SKarl Rupp 
41601c3436cfSJed Brown     ts->vrtol = vrtol;
41611c3436cfSJed Brown   }
41621c3436cfSJed Brown   PetscFunctionReturn(0);
41631c3436cfSJed Brown }
41641c3436cfSJed Brown 
41651c3436cfSJed Brown #undef __FUNCT__
4166c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4167c5033834SJed Brown /*@
4168c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4169c5033834SJed Brown 
4170c5033834SJed Brown    Logically Collective
4171c5033834SJed Brown 
4172c5033834SJed Brown    Input Arguments:
4173c5033834SJed Brown .  ts - time integration context
4174c5033834SJed Brown 
4175c5033834SJed Brown    Output Arguments:
41760298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
41770298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
41780298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
41790298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4180c5033834SJed Brown 
4181c5033834SJed Brown    Level: beginner
4182c5033834SJed Brown 
4183c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4184c5033834SJed Brown @*/
4185c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4186c5033834SJed Brown {
4187c5033834SJed Brown   PetscFunctionBegin;
4188c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4189c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4190c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4191c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4192c5033834SJed Brown   PetscFunctionReturn(0);
4193c5033834SJed Brown }
4194c5033834SJed Brown 
4195c5033834SJed Brown #undef __FUNCT__
41961c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
41971c3436cfSJed Brown /*@
41981c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
41991c3436cfSJed Brown 
42001c3436cfSJed Brown    Collective on TS
42011c3436cfSJed Brown 
42021c3436cfSJed Brown    Input Arguments:
42031c3436cfSJed Brown +  ts - time stepping context
42041c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
42051c3436cfSJed Brown 
42061c3436cfSJed Brown    Output Arguments:
42071c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
42081c3436cfSJed Brown 
42091c3436cfSJed Brown    Level: developer
42101c3436cfSJed Brown 
42111c3436cfSJed Brown .seealso: TSSetTolerances()
42121c3436cfSJed Brown @*/
42131c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
42141c3436cfSJed Brown {
42151c3436cfSJed Brown   PetscErrorCode    ierr;
42161c3436cfSJed Brown   PetscInt          i,n,N;
42170910c330SBarry Smith   const PetscScalar *u,*y;
42180910c330SBarry Smith   Vec               U;
42191c3436cfSJed Brown   PetscReal         sum,gsum;
42201c3436cfSJed Brown 
42211c3436cfSJed Brown   PetscFunctionBegin;
42221c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42231c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
42241c3436cfSJed Brown   PetscValidPointer(norm,3);
42250910c330SBarry Smith   U = ts->vec_sol;
42260910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4227ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
42281c3436cfSJed Brown 
42290910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
42300910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
42310910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
42321c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
42331c3436cfSJed Brown   sum  = 0.;
4234ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4235ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4236ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4237ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4238ceefc113SJed Brown     for (i=0; i<n; i++) {
42390910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42400910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4241ceefc113SJed Brown     }
4242ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4243ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4244ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4245ceefc113SJed Brown     const PetscScalar *atol;
4246ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4247ceefc113SJed Brown     for (i=0; i<n; i++) {
42480910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42490910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4250ceefc113SJed Brown     }
4251ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4252ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4253ceefc113SJed Brown     const PetscScalar *rtol;
4254ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4255ceefc113SJed Brown     for (i=0; i<n; i++) {
42560910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42570910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4258ceefc113SJed Brown     }
4259ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4260ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
42611c3436cfSJed Brown     for (i=0; i<n; i++) {
42620910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42630910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
42641c3436cfSJed Brown     }
4265ceefc113SJed Brown   }
42660910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
42671c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
42681c3436cfSJed Brown 
4269ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
42701c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4271ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
42721c3436cfSJed Brown   PetscFunctionReturn(0);
42731c3436cfSJed Brown }
42741c3436cfSJed Brown 
42751c3436cfSJed Brown #undef __FUNCT__
42768d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
42778d59e960SJed Brown /*@
42788d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
42798d59e960SJed Brown 
42808d59e960SJed Brown    Logically Collective on TS
42818d59e960SJed Brown 
42828d59e960SJed Brown    Input Arguments:
42838d59e960SJed Brown +  ts - time stepping context
42848d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
42858d59e960SJed Brown 
42868d59e960SJed Brown    Note:
42878d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
42888d59e960SJed Brown 
42898d59e960SJed Brown    Level: intermediate
42908d59e960SJed Brown 
42918d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
42928d59e960SJed Brown @*/
42938d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
42948d59e960SJed Brown {
42958d59e960SJed Brown   PetscFunctionBegin;
42968d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42978d59e960SJed Brown   ts->cfltime_local = cfltime;
42988d59e960SJed Brown   ts->cfltime       = -1.;
42998d59e960SJed Brown   PetscFunctionReturn(0);
43008d59e960SJed Brown }
43018d59e960SJed Brown 
43028d59e960SJed Brown #undef __FUNCT__
43038d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
43048d59e960SJed Brown /*@
43058d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
43068d59e960SJed Brown 
43078d59e960SJed Brown    Collective on TS
43088d59e960SJed Brown 
43098d59e960SJed Brown    Input Arguments:
43108d59e960SJed Brown .  ts - time stepping context
43118d59e960SJed Brown 
43128d59e960SJed Brown    Output Arguments:
43138d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
43148d59e960SJed Brown 
43158d59e960SJed Brown    Level: advanced
43168d59e960SJed Brown 
43178d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
43188d59e960SJed Brown @*/
43198d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
43208d59e960SJed Brown {
43218d59e960SJed Brown   PetscErrorCode ierr;
43228d59e960SJed Brown 
43238d59e960SJed Brown   PetscFunctionBegin;
43248d59e960SJed Brown   if (ts->cfltime < 0) {
4325ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
43268d59e960SJed Brown   }
43278d59e960SJed Brown   *cfltime = ts->cfltime;
43288d59e960SJed Brown   PetscFunctionReturn(0);
43298d59e960SJed Brown }
43308d59e960SJed Brown 
43318d59e960SJed Brown #undef __FUNCT__
4332d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4333d6ebe24aSShri Abhyankar /*@
4334d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4335d6ebe24aSShri Abhyankar 
4336d6ebe24aSShri Abhyankar    Input Parameters:
4337d6ebe24aSShri Abhyankar .  ts   - the TS context.
4338d6ebe24aSShri Abhyankar .  xl   - lower bound.
4339d6ebe24aSShri Abhyankar .  xu   - upper bound.
4340d6ebe24aSShri Abhyankar 
4341d6ebe24aSShri Abhyankar    Notes:
4342d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
434333c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4344d6ebe24aSShri Abhyankar 
43452bd2b0e6SSatish Balay    Level: advanced
43462bd2b0e6SSatish Balay 
4347d6ebe24aSShri Abhyankar @*/
4348d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4349d6ebe24aSShri Abhyankar {
4350d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4351d6ebe24aSShri Abhyankar   SNES           snes;
4352d6ebe24aSShri Abhyankar 
4353d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4354d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4355d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4356d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4357d6ebe24aSShri Abhyankar }
4358d6ebe24aSShri Abhyankar 
4359325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4360c6db04a5SJed Brown #include <mex.h>
4361325fc9f4SBarry Smith 
4362325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4363325fc9f4SBarry Smith 
4364325fc9f4SBarry Smith #undef __FUNCT__
4365325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4366325fc9f4SBarry Smith /*
4367325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4368325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4369325fc9f4SBarry Smith 
4370325fc9f4SBarry Smith    Collective on TS
4371325fc9f4SBarry Smith 
4372325fc9f4SBarry Smith    Input Parameters:
4373325fc9f4SBarry Smith +  snes - the TS context
43740910c330SBarry Smith -  u - input vector
4375325fc9f4SBarry Smith 
4376325fc9f4SBarry Smith    Output Parameter:
4377325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4378325fc9f4SBarry Smith 
4379325fc9f4SBarry Smith    Notes:
4380325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4381325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4382325fc9f4SBarry Smith    themselves.
4383325fc9f4SBarry Smith 
4384325fc9f4SBarry Smith    Level: developer
4385325fc9f4SBarry Smith 
4386325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4387325fc9f4SBarry Smith 
4388325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4389325fc9f4SBarry Smith */
43900910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4391325fc9f4SBarry Smith {
4392325fc9f4SBarry Smith   PetscErrorCode  ierr;
4393325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4394325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4395325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4396325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4397325fc9f4SBarry Smith 
4398325fc9f4SBarry Smith   PetscFunctionBegin;
4399325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
44000910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
44010910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4402325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
44030910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4404325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4405325fc9f4SBarry Smith 
4406325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
44070910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
44080910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
44090910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4410bbd56ea5SKarl Rupp 
4411325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4412325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4413325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4414325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4415325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4416325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4417325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4418325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4419325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4420325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4421325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4422325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4423325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4424325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4425325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4426325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4427325fc9f4SBarry Smith   PetscFunctionReturn(0);
4428325fc9f4SBarry Smith }
4429325fc9f4SBarry Smith 
4430325fc9f4SBarry Smith 
4431325fc9f4SBarry Smith #undef __FUNCT__
4432325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4433325fc9f4SBarry Smith /*
4434325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4435325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4436e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4437325fc9f4SBarry Smith 
4438325fc9f4SBarry Smith    Logically Collective on TS
4439325fc9f4SBarry Smith 
4440325fc9f4SBarry Smith    Input Parameters:
4441325fc9f4SBarry Smith +  ts - the TS context
4442325fc9f4SBarry Smith -  func - function evaluation routine
4443325fc9f4SBarry Smith 
4444325fc9f4SBarry Smith    Calling sequence of func:
44450910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4446325fc9f4SBarry Smith 
4447325fc9f4SBarry Smith    Level: beginner
4448325fc9f4SBarry Smith 
4449325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4450325fc9f4SBarry Smith 
4451325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4452325fc9f4SBarry Smith */
4453cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4454325fc9f4SBarry Smith {
4455325fc9f4SBarry Smith   PetscErrorCode  ierr;
4456325fc9f4SBarry Smith   TSMatlabContext *sctx;
4457325fc9f4SBarry Smith 
4458325fc9f4SBarry Smith   PetscFunctionBegin;
4459325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4460325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4461325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4462325fc9f4SBarry Smith   /*
4463325fc9f4SBarry Smith      This should work, but it doesn't
4464325fc9f4SBarry Smith   sctx->ctx = ctx;
4465325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4466325fc9f4SBarry Smith   */
4467325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4468bbd56ea5SKarl Rupp 
44690298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4470325fc9f4SBarry Smith   PetscFunctionReturn(0);
4471325fc9f4SBarry Smith }
4472325fc9f4SBarry Smith 
4473325fc9f4SBarry Smith #undef __FUNCT__
4474325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4475325fc9f4SBarry Smith /*
4476325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4477325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4478325fc9f4SBarry Smith 
4479325fc9f4SBarry Smith    Collective on TS
4480325fc9f4SBarry Smith 
4481325fc9f4SBarry Smith    Input Parameters:
4482cdcf91faSSean Farley +  ts - the TS context
44830910c330SBarry Smith .  u - input vector
4484325fc9f4SBarry Smith .  A, B - the matrices
4485325fc9f4SBarry Smith -  ctx - user context
4486325fc9f4SBarry Smith 
4487325fc9f4SBarry Smith    Output Parameter:
4488325fc9f4SBarry Smith .  flag - structure of the matrix
4489325fc9f4SBarry Smith 
4490325fc9f4SBarry Smith    Level: developer
4491325fc9f4SBarry Smith 
4492325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4493325fc9f4SBarry Smith 
4494325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4495325fc9f4SBarry Smith @*/
44960910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4497325fc9f4SBarry Smith {
4498325fc9f4SBarry Smith   PetscErrorCode  ierr;
4499325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4500325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4501325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4502325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4503325fc9f4SBarry Smith 
4504325fc9f4SBarry Smith   PetscFunctionBegin;
4505cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45060910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4507325fc9f4SBarry Smith 
45080910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4509325fc9f4SBarry Smith 
4510cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
45110910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
45120910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
45130910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
45140910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4515bbd56ea5SKarl Rupp 
4516325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4517325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4518325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4519325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4520325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4521325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4522325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4523325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4524325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4525325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4526325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4527325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4528325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4529325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4530325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4531325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4532325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4533325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4534325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4535325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4536325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4537325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4538325fc9f4SBarry Smith   PetscFunctionReturn(0);
4539325fc9f4SBarry Smith }
4540325fc9f4SBarry Smith 
4541325fc9f4SBarry Smith 
4542325fc9f4SBarry Smith #undef __FUNCT__
4543325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4544325fc9f4SBarry Smith /*
4545325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4546e3c5b3baSBarry 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
4547325fc9f4SBarry Smith 
4548325fc9f4SBarry Smith    Logically Collective on TS
4549325fc9f4SBarry Smith 
4550325fc9f4SBarry Smith    Input Parameters:
4551cdcf91faSSean Farley +  ts - the TS context
4552325fc9f4SBarry Smith .  A,B - Jacobian matrices
4553325fc9f4SBarry Smith .  func - function evaluation routine
4554325fc9f4SBarry Smith -  ctx - user context
4555325fc9f4SBarry Smith 
4556325fc9f4SBarry Smith    Calling sequence of func:
45570910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4558325fc9f4SBarry Smith 
4559325fc9f4SBarry Smith 
4560325fc9f4SBarry Smith    Level: developer
4561325fc9f4SBarry Smith 
4562325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4563325fc9f4SBarry Smith 
4564325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4565325fc9f4SBarry Smith */
4566cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4567325fc9f4SBarry Smith {
4568325fc9f4SBarry Smith   PetscErrorCode  ierr;
4569325fc9f4SBarry Smith   TSMatlabContext *sctx;
4570325fc9f4SBarry Smith 
4571325fc9f4SBarry Smith   PetscFunctionBegin;
4572325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4573325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4574325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4575325fc9f4SBarry Smith   /*
4576325fc9f4SBarry Smith      This should work, but it doesn't
4577325fc9f4SBarry Smith   sctx->ctx = ctx;
4578325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4579325fc9f4SBarry Smith   */
4580325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4581bbd56ea5SKarl Rupp 
4582cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4583325fc9f4SBarry Smith   PetscFunctionReturn(0);
4584325fc9f4SBarry Smith }
4585325fc9f4SBarry Smith 
4586b5b1a830SBarry Smith #undef __FUNCT__
4587b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4588b5b1a830SBarry Smith /*
4589b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4590b5b1a830SBarry Smith 
4591b5b1a830SBarry Smith    Collective on TS
4592b5b1a830SBarry Smith 
4593b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4594b5b1a830SBarry Smith @*/
45950910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4596b5b1a830SBarry Smith {
4597b5b1a830SBarry Smith   PetscErrorCode  ierr;
4598b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4599a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4600b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4601b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4602b5b1a830SBarry Smith 
4603b5b1a830SBarry Smith   PetscFunctionBegin;
4604cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46050910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4606b5b1a830SBarry Smith 
4607cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
46080910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4609bbd56ea5SKarl Rupp 
4610b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4611b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4612b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4613b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4614b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4615b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4616b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4617b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4618b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4619b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4620b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4621b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4622b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4623b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4624b5b1a830SBarry Smith   PetscFunctionReturn(0);
4625b5b1a830SBarry Smith }
4626b5b1a830SBarry Smith 
4627b5b1a830SBarry Smith 
4628b5b1a830SBarry Smith #undef __FUNCT__
4629b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4630b5b1a830SBarry Smith /*
4631b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4632b5b1a830SBarry Smith 
4633b5b1a830SBarry Smith    Level: developer
4634b5b1a830SBarry Smith 
4635b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4636b5b1a830SBarry Smith 
4637b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4638b5b1a830SBarry Smith */
4639cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4640b5b1a830SBarry Smith {
4641b5b1a830SBarry Smith   PetscErrorCode  ierr;
4642b5b1a830SBarry Smith   TSMatlabContext *sctx;
4643b5b1a830SBarry Smith 
4644b5b1a830SBarry Smith   PetscFunctionBegin;
4645b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4646b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4647b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4648b5b1a830SBarry Smith   /*
4649b5b1a830SBarry Smith      This should work, but it doesn't
4650b5b1a830SBarry Smith   sctx->ctx = ctx;
4651b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4652b5b1a830SBarry Smith   */
4653b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4654bbd56ea5SKarl Rupp 
46550298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4656b5b1a830SBarry Smith   PetscFunctionReturn(0);
4657b5b1a830SBarry Smith }
4658325fc9f4SBarry Smith #endif
4659b3603a34SBarry Smith 
46600b039ecaSBarry Smith 
46610b039ecaSBarry Smith 
4662b3603a34SBarry Smith #undef __FUNCT__
46634f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4664b3603a34SBarry Smith /*@C
46654f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4666b3603a34SBarry Smith        in a time based line graph
4667b3603a34SBarry Smith 
4668b3603a34SBarry Smith    Collective on TS
4669b3603a34SBarry Smith 
4670b3603a34SBarry Smith    Input Parameters:
4671b3603a34SBarry Smith +  ts - the TS context
4672b3603a34SBarry Smith .  step - current time-step
4673b3603a34SBarry Smith .  ptime - current time
4674b3603a34SBarry Smith -  lg - a line graph object
4675b3603a34SBarry Smith 
4676b3603a34SBarry Smith    Level: intermediate
4677b3603a34SBarry Smith 
46780b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4679b3603a34SBarry Smith 
4680b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4681b3603a34SBarry Smith 
4682b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4683b3603a34SBarry Smith @*/
46840910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4685b3603a34SBarry Smith {
4686b3603a34SBarry Smith   PetscErrorCode    ierr;
46870b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4688b3603a34SBarry Smith   const PetscScalar *yy;
468958ff32f7SBarry Smith   PetscInt          dim;
4690b3603a34SBarry Smith 
4691b3603a34SBarry Smith   PetscFunctionBegin;
469258ff32f7SBarry Smith   if (!step) {
4693a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4694a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4695a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
46960910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
46970b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
46980b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
469958ff32f7SBarry Smith   }
47000910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4701e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4702e3efe391SJed Brown   {
4703e3efe391SJed Brown     PetscReal *yreal;
4704e3efe391SJed Brown     PetscInt  i,n;
47050910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4706e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4707e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
47080b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4709e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4710e3efe391SJed Brown   }
4711e3efe391SJed Brown #else
47120b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4713e3efe391SJed Brown #endif
47140910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
4715b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
47160b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
47173923b477SBarry Smith   }
4718b3603a34SBarry Smith   PetscFunctionReturn(0);
4719b3603a34SBarry Smith }
4720b3603a34SBarry Smith 
4721b3603a34SBarry Smith #undef __FUNCT__
47224f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4723ef20d060SBarry Smith /*@C
47244f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4725ef20d060SBarry Smith        in a time based line graph
4726ef20d060SBarry Smith 
4727ef20d060SBarry Smith    Collective on TS
4728ef20d060SBarry Smith 
4729ef20d060SBarry Smith    Input Parameters:
4730ef20d060SBarry Smith +  ts - the TS context
4731ef20d060SBarry Smith .  step - current time-step
4732ef20d060SBarry Smith .  ptime - current time
4733ef20d060SBarry Smith -  lg - a line graph object
4734ef20d060SBarry Smith 
4735ef20d060SBarry Smith    Level: intermediate
4736ef20d060SBarry Smith 
4737abd5a294SJed Brown    Notes:
4738abd5a294SJed Brown    Only for sequential solves.
4739abd5a294SJed Brown 
4740abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4741abd5a294SJed Brown 
4742abd5a294SJed Brown    Options Database Keys:
47434f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4744ef20d060SBarry Smith 
4745ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4746ef20d060SBarry Smith 
4747abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4748ef20d060SBarry Smith @*/
47490910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4750ef20d060SBarry Smith {
4751ef20d060SBarry Smith   PetscErrorCode    ierr;
47520b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4753ef20d060SBarry Smith   const PetscScalar *yy;
4754ef20d060SBarry Smith   Vec               y;
4755a9f9c1f6SBarry Smith   PetscInt          dim;
4756ef20d060SBarry Smith 
4757ef20d060SBarry Smith   PetscFunctionBegin;
4758a9f9c1f6SBarry Smith   if (!step) {
4759a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4760a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
47611ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
47620910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4763a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4764a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4765a9f9c1f6SBarry Smith   }
47660910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4767ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
47680910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4769ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4770e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4771e3efe391SJed Brown   {
4772e3efe391SJed Brown     PetscReal *yreal;
4773e3efe391SJed Brown     PetscInt  i,n;
4774e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4775e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4776e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
47770b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4778e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4779e3efe391SJed Brown   }
4780e3efe391SJed Brown #else
47810b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4782e3efe391SJed Brown #endif
4783ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4784ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
4785b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
47860b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
47873923b477SBarry Smith   }
4788ef20d060SBarry Smith   PetscFunctionReturn(0);
4789ef20d060SBarry Smith }
4790ef20d060SBarry Smith 
4791201da799SBarry Smith #undef __FUNCT__
4792201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4793201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4794201da799SBarry Smith {
4795201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4796201da799SBarry Smith   PetscReal      x   = ptime,y;
4797201da799SBarry Smith   PetscErrorCode ierr;
4798201da799SBarry Smith   PetscInt       its;
4799201da799SBarry Smith 
4800201da799SBarry Smith   PetscFunctionBegin;
4801201da799SBarry Smith   if (!n) {
4802201da799SBarry Smith     PetscDrawAxis axis;
4803bbd56ea5SKarl Rupp 
4804201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4805201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4806201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4807bbd56ea5SKarl Rupp 
4808201da799SBarry Smith     ctx->snes_its = 0;
4809201da799SBarry Smith   }
4810201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4811201da799SBarry Smith   y    = its - ctx->snes_its;
4812201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
48133fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4814201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4815201da799SBarry Smith   }
4816201da799SBarry Smith   ctx->snes_its = its;
4817201da799SBarry Smith   PetscFunctionReturn(0);
4818201da799SBarry Smith }
4819201da799SBarry Smith 
4820201da799SBarry Smith #undef __FUNCT__
4821201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4822201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4823201da799SBarry Smith {
4824201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4825201da799SBarry Smith   PetscReal      x   = ptime,y;
4826201da799SBarry Smith   PetscErrorCode ierr;
4827201da799SBarry Smith   PetscInt       its;
4828201da799SBarry Smith 
4829201da799SBarry Smith   PetscFunctionBegin;
4830201da799SBarry Smith   if (!n) {
4831201da799SBarry Smith     PetscDrawAxis axis;
4832bbd56ea5SKarl Rupp 
4833201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4834201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4835201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4836bbd56ea5SKarl Rupp 
4837201da799SBarry Smith     ctx->ksp_its = 0;
4838201da799SBarry Smith   }
4839201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4840201da799SBarry Smith   y    = its - ctx->ksp_its;
4841201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
484299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4843201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4844201da799SBarry Smith   }
4845201da799SBarry Smith   ctx->ksp_its = its;
4846201da799SBarry Smith   PetscFunctionReturn(0);
4847201da799SBarry Smith }
4848f9c1d6abSBarry Smith 
4849f9c1d6abSBarry Smith #undef __FUNCT__
4850f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4851f9c1d6abSBarry Smith /*@
4852f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4853f9c1d6abSBarry Smith 
4854f9c1d6abSBarry Smith    Collective on TS and Vec
4855f9c1d6abSBarry Smith 
4856f9c1d6abSBarry Smith    Input Parameters:
4857f9c1d6abSBarry Smith +  ts - the TS context
4858f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4859f9c1d6abSBarry Smith 
4860f9c1d6abSBarry Smith    Output Parameters:
4861f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4862f9c1d6abSBarry Smith 
4863f9c1d6abSBarry Smith    Level: developer
4864f9c1d6abSBarry Smith 
4865f9c1d6abSBarry Smith .keywords: TS, compute
4866f9c1d6abSBarry Smith 
4867f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4868f9c1d6abSBarry Smith @*/
4869f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4870f9c1d6abSBarry Smith {
4871f9c1d6abSBarry Smith   PetscErrorCode ierr;
4872f9c1d6abSBarry Smith 
4873f9c1d6abSBarry Smith   PetscFunctionBegin;
4874f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4875ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4876f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4877f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4878f9c1d6abSBarry Smith }
4879