xref: /petsc/src/ts/interface/ts.c (revision 7f52daa22071f496267835f6cb5d2fa14cd3101e)
163dd3a1aSKris Buschelman 
2af0996ceSBarry Smith #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 
142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
152d5ee99bSBarry Smith   PetscViewer   viewer;
164b363babSBarry Smith   PetscDrawAxis axis;
172d5ee99bSBarry Smith   Vec           initialsolution;
182d5ee99bSBarry Smith   PetscBool     showinitial;
192d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
202d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
212d5ee99bSBarry Smith   int           color;
222d5ee99bSBarry Smith };
232d5ee99bSBarry Smith 
24bdad233fSMatthew Knepley #undef __FUNCT__
25bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
26bdad233fSMatthew Knepley /*@
27bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
28bdad233fSMatthew Knepley 
29bdad233fSMatthew Knepley    Collective on TS
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley    Input Parameter:
32bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
33bdad233fSMatthew Knepley 
34bdad233fSMatthew Knepley    Options Database Keys:
354d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
36ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
373e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
383e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
393e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
40a3cdaa26SBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
41a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
42a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
43a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
44a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
45a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
46ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
47847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
48bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
49de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
50de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
51de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
52de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
53de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
54de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
55de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
563e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
573e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
5891b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
59b3d3934dSBarry Smith .  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
60110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
61110eb670SHong Zhang .  -ts_adjoint_monitor - print information at each adjoint time step
6253ea634cSHong Zhang -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically
6353ea634cSHong Zhang 
6491b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
65bdad233fSMatthew Knepley 
66bdad233fSMatthew Knepley    Level: beginner
67bdad233fSMatthew Knepley 
68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
69bdad233fSMatthew Knepley 
70a313700dSBarry Smith .seealso: TSGetType()
71bdad233fSMatthew Knepley @*/
727087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
73bdad233fSMatthew Knepley {
74bc952696SBarry Smith   PetscBool              opt,flg,tflg;
75dfbe8321SBarry Smith   PetscErrorCode         ierr;
76649052a6SBarry Smith   PetscViewer            monviewer;
77eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
78089b2837SJed Brown   SNES                   snes;
791c3436cfSJed Brown   TSAdapt                adapt;
8031748224SBarry Smith   PetscReal              time_step;
8149354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
82d1212d36SBarry Smith   char                   dir[16];
836991f827SBarry Smith   const char             *defaultType;
846991f827SBarry Smith   char                   typeName[256];
85bdad233fSMatthew Knepley 
86bdad233fSMatthew Knepley   PetscFunctionBegin;
870700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
883194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
896991f827SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
906991f827SBarry Smith   else defaultType = TSEULER;
916991f827SBarry Smith 
926991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
936991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
946991f827SBarry Smith   if (opt) {
956991f827SBarry Smith     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
966991f827SBarry Smith   } else {
976991f827SBarry Smith     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
986991f827SBarry Smith   }
99bdad233fSMatthew Knepley 
100bdad233fSMatthew Knepley   /* Handle generic TS options */
101844594baSBarry Smith   if (ts->trajectory) tflg = PETSC_TRUE;
102844594baSBarry Smith   else tflg = PETSC_FALSE;
103920aabf2SBarry Smith   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
104bc952696SBarry Smith   if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);}
105ef222394SBarry Smith   if (ts->adjoint_solve) tflg = PETSC_TRUE;
106ef222394SBarry Smith   else tflg = PETSC_FALSE;
107ef222394SBarry Smith   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
108ef222394SBarry Smith   if (flg) {
109ef222394SBarry Smith     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
110e87fd094SBarry Smith     ts->adjoint_solve = tflg;
111ef222394SBarry Smith   }
1120298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1130298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1140298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11531748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
11631748224SBarry Smith   if (flg) {
11731748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
11831748224SBarry Smith   }
11949354f04SShri 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);
12049354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1210298fd71SBarry 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);
1220298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1230298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1240298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1250298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
126bdad233fSMatthew Knepley 
12756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
12856f85f32SBarry Smith   {
12956f85f32SBarry Smith   PetscBool set;
13056f85f32SBarry Smith   flg  = PETSC_FALSE;
13156f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
13256f85f32SBarry Smith   if (set) {
13356f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
13456f85f32SBarry Smith   }
13556f85f32SBarry Smith   }
13656f85f32SBarry Smith #endif
13756f85f32SBarry Smith 
138bdad233fSMatthew Knepley   /* Monitor options */
139a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
140eabae89aSBarry Smith   if (flg) {
141ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
142649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
143bdad233fSMatthew Knepley   }
1445180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1455180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1465180491cSLisandro Dalcin 
1474f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
148a7cc72afSBarry Smith   if (opt) {
1490b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1503923b477SBarry Smith     PetscInt       howoften = 1;
151a80ad3e0SBarry Smith 
1520298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
153*7f52daa2SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1544f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
155b3603a34SBarry Smith   }
1564f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
157b3603a34SBarry Smith   if (opt) {
1580b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1593923b477SBarry Smith     PetscInt       howoften = 1;
160b3603a34SBarry Smith 
1610298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
16222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1634f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
164bdad233fSMatthew Knepley   }
1654f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
166ef20d060SBarry Smith   if (opt) {
1670b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1683923b477SBarry Smith     PetscInt       howoften = 1;
169ef20d060SBarry Smith 
1700298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
17122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1724f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
173ef20d060SBarry Smith   }
174201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
175201da799SBarry Smith   if (opt) {
176201da799SBarry Smith     TSMonitorLGCtx ctx;
177201da799SBarry Smith     PetscInt       howoften = 1;
178201da799SBarry Smith 
1790298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
180*7f52daa2SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
181201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
182201da799SBarry Smith   }
183201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
184201da799SBarry Smith   if (opt) {
185201da799SBarry Smith     TSMonitorLGCtx ctx;
186201da799SBarry Smith     PetscInt       howoften = 1;
187201da799SBarry Smith 
1880298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
189*7f52daa2SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
190201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
191201da799SBarry Smith   }
1928189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1938189c53fSBarry Smith   if (opt) {
1948189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1958189c53fSBarry Smith     PetscInt          howoften = 1;
1968189c53fSBarry Smith 
1970298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
19822d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1998189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2008189c53fSBarry Smith   }
201ef20d060SBarry Smith   opt  = PETSC_FALSE;
2020dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
203a7cc72afSBarry Smith   if (opt) {
20483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
20583a4ac43SBarry Smith     PetscInt         howoften = 1;
206a80ad3e0SBarry Smith 
2070298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
208ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
20983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
210bdad233fSMatthew Knepley   }
211fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2129110b2e7SHong Zhang   ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr);
2139110b2e7SHong Zhang   if (opt) {
2149110b2e7SHong Zhang     TSMonitorDrawCtx ctx;
2159110b2e7SHong Zhang     PetscInt         howoften = 1;
2169110b2e7SHong Zhang 
2179110b2e7SHong Zhang     ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr);
2189110b2e7SHong Zhang     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2199110b2e7SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2209110b2e7SHong Zhang   }
2219110b2e7SHong Zhang   opt  = PETSC_FALSE;
2222d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2232d5ee99bSBarry Smith   if (opt) {
2242d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2252d5ee99bSBarry Smith     PetscReal        bounds[4];
2262d5ee99bSBarry Smith     PetscInt         n = 4;
2272d5ee99bSBarry Smith     PetscDraw        draw;
2282d5ee99bSBarry Smith 
2292d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2302d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2312d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2322d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2332d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2344b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2354b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
236f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2374b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
23807995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2392d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2402d5ee99bSBarry Smith   }
2412d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2420dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2433a471f94SBarry Smith   if (opt) {
24483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
24583a4ac43SBarry Smith     PetscInt         howoften = 1;
2463a471f94SBarry Smith 
2470298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
248ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
24983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2503a471f94SBarry Smith   }
2513a471f94SBarry Smith   opt  = PETSC_FALSE;
25291b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
253fb1732b5SBarry Smith   if (flg) {
254fb1732b5SBarry Smith     PetscViewer ctx;
255fb1732b5SBarry Smith     if (monfilename[0]) {
256ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
257c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
258fb1732b5SBarry Smith     } else {
259ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2600298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
261fb1732b5SBarry Smith     }
262fb1732b5SBarry Smith   }
263ed81e22dSJed Brown   opt  = PETSC_FALSE;
26491b97e58SBarry 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);
265ed81e22dSJed Brown   if (flg) {
266ed81e22dSJed Brown     const char *ptr,*ptr2;
267ed81e22dSJed Brown     char       *filetemplate;
268ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
269ed81e22dSJed Brown     /* Do some cursory validation of the input. */
270ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
271ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
272ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
273ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
274ce94432eSBarry 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");
275ed81e22dSJed Brown       if (ptr2) break;
276ed81e22dSJed Brown     }
277ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
278ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
279ed81e22dSJed Brown   }
280bdad233fSMatthew Knepley 
281d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
282d1212d36SBarry Smith   if (flg) {
283d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
284d1212d36SBarry Smith     int                  ray = 0;
285d1212d36SBarry Smith     DMDADirection        ddir;
286d1212d36SBarry Smith     DM                   da;
287d1212d36SBarry Smith     PetscMPIInt          rank;
288d1212d36SBarry Smith 
289ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
290d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
291d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
292ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
293d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
294d1212d36SBarry Smith 
295d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
296b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
297d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
298d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
299ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
300d1212d36SBarry Smith     if (!rank) {
301d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
302d1212d36SBarry Smith     }
30351b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
304d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
305d1212d36SBarry Smith   }
30651b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
30751b4a12fSMatthew G. Knepley   if (flg) {
30851b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
30951b4a12fSMatthew G. Knepley     int                 ray = 0;
31051b4a12fSMatthew G. Knepley     DMDADirection       ddir;
31151b4a12fSMatthew G. Knepley     DM                  da;
31251b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
313d1212d36SBarry Smith 
31451b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
31551b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
31651b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
31751b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
31851b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3191c3436cfSJed Brown 
32051b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
321b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
32251b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
32351b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
32451b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
32551b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
32651b4a12fSMatthew G. Knepley   }
327a7a1495cSBarry Smith 
328b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
329b3d3934dSBarry Smith   if (opt) {
330b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
331b3d3934dSBarry Smith 
332b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
333b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
334b3d3934dSBarry Smith   }
335b3d3934dSBarry Smith 
336847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
337847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
338847ff0e1SMatthew G. Knepley   if (flg) {
339847ff0e1SMatthew G. Knepley     DM   dm;
340847ff0e1SMatthew G. Knepley     DMTS tdm;
341847ff0e1SMatthew G. Knepley 
342847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
343847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
344847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
345847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
346847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
347847ff0e1SMatthew G. Knepley   }
348847ff0e1SMatthew G. Knepley 
349110eb670SHong Zhang   ierr = PetscOptionsString("-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
350110eb670SHong Zhang   if (flg) {
351110eb670SHong Zhang     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
352110eb670SHong Zhang     ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
353110eb670SHong Zhang   }
354110eb670SHong Zhang 
3557781c08eSBarry Smith   /*
3567781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3577781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3587781c08eSBarry Smith   */
359552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
360e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
361d763cef2SBarry Smith 
362d763cef2SBarry Smith     /* Handle specific TS options */
363d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3646991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
365d763cef2SBarry Smith   }
3666991f827SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
367d763cef2SBarry Smith 
368d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
369d763cef2SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
370d763cef2SBarry Smith 
371bc952696SBarry Smith   if (ts->trajectory) {
372bc952696SBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
373bc952696SBarry Smith   }
374bc952696SBarry Smith 
3756991f827SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3766991f827SBarry Smith   if (snes) {
3776991f827SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
3786991f827SBarry Smith     ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
379d763cef2SBarry Smith   }
380d763cef2SBarry Smith   PetscFunctionReturn(0);
381d763cef2SBarry Smith }
382d763cef2SBarry Smith 
383d763cef2SBarry Smith #undef __FUNCT__
384bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
385d2daff3dSHong Zhang /*@
386bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
387d2daff3dSHong Zhang 
388d2daff3dSHong Zhang    Collective on TS
389d2daff3dSHong Zhang 
390d2daff3dSHong Zhang    Input Parameters:
391bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
392bc952696SBarry Smith 
393d2daff3dSHong Zhang 
394d2daff3dSHong Zhang    Level: intermediate
395d2daff3dSHong Zhang 
396bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
397d2daff3dSHong Zhang 
398bc952696SBarry Smith .keywords: TS, set, checkpoint,
399d2daff3dSHong Zhang @*/
400bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
401d2daff3dSHong Zhang {
402bc952696SBarry Smith   PetscErrorCode ierr;
403bc952696SBarry Smith 
404d2daff3dSHong Zhang   PetscFunctionBegin;
405d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
406bc952696SBarry Smith   if (!ts->trajectory) {
407bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
408920aabf2SBarry Smith     ierr = TSTrajectorySetType(ts->trajectory,TSTRAJECTORYBASIC);CHKERRQ(ierr);
409648b50deSBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
410bc952696SBarry Smith   }
411d2daff3dSHong Zhang   PetscFunctionReturn(0);
412d2daff3dSHong Zhang }
413d2daff3dSHong Zhang 
414a7a1495cSBarry Smith #undef __FUNCT__
415a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
416a7a1495cSBarry Smith /*@
417a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
418a7a1495cSBarry Smith       set with TSSetRHSJacobian().
419a7a1495cSBarry Smith 
420a7a1495cSBarry Smith    Collective on TS and Vec
421a7a1495cSBarry Smith 
422a7a1495cSBarry Smith    Input Parameters:
423316643e7SJed Brown +  ts - the TS context
424a7a1495cSBarry Smith .  t - current timestep
4250910c330SBarry Smith -  U - input vector
426a7a1495cSBarry Smith 
427a7a1495cSBarry Smith    Output Parameters:
428a7a1495cSBarry Smith +  A - Jacobian matrix
429a7a1495cSBarry Smith .  B - optional preconditioning matrix
430a7a1495cSBarry Smith -  flag - flag indicating matrix structure
431a7a1495cSBarry Smith 
432a7a1495cSBarry Smith    Notes:
433a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
434a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
435a7a1495cSBarry Smith 
43694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
437a7a1495cSBarry Smith    flag parameter.
438a7a1495cSBarry Smith 
439a7a1495cSBarry Smith    Level: developer
440a7a1495cSBarry Smith 
441a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
442a7a1495cSBarry Smith 
44394b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
444a7a1495cSBarry Smith @*/
445d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
446a7a1495cSBarry Smith {
447dfbe8321SBarry Smith   PetscErrorCode ierr;
448270bf2e7SJed Brown   PetscObjectState Ustate;
44924989b8cSPeter Brune   DM             dm;
450942e3340SBarry Smith   DMTS           tsdm;
45124989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
45224989b8cSPeter Brune   void           *ctx;
45324989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
454b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
455a7a1495cSBarry Smith 
456a7a1495cSBarry Smith   PetscFunctionBegin;
4570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4580910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4590910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
46024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
461942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
46224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4630298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
464b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
46559e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
466b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
4670e4ef248SJed Brown     PetscFunctionReturn(0);
468f8ede8e7SJed Brown   }
469d90be118SSean Farley 
470ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
471d90be118SSean Farley 
472e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
47394ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
47494ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
47594ab13aaSBarry Smith     if (A != B) {
47694ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
47794ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
478e1244c69SJed Brown     }
479e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
480e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
481e1244c69SJed Brown   }
482e1244c69SJed Brown 
48324989b8cSPeter Brune   if (rhsjacobianfunc) {
48494ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
485a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
486d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
487a7a1495cSBarry Smith     PetscStackPop;
48894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
489a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
49094ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
49194ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
492ef66eb69SBarry Smith   } else {
49394ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
49494ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
495ef66eb69SBarry Smith   }
4960e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4970910c330SBarry Smith   ts->rhsjacobian.X          = U;
49859e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
499a7a1495cSBarry Smith   PetscFunctionReturn(0);
500a7a1495cSBarry Smith }
501a7a1495cSBarry Smith 
5024a2ae208SSatish Balay #undef __FUNCT__
5034a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
504316643e7SJed Brown /*@
505d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
506d763cef2SBarry Smith 
507316643e7SJed Brown    Collective on TS and Vec
508316643e7SJed Brown 
509316643e7SJed Brown    Input Parameters:
510316643e7SJed Brown +  ts - the TS context
511316643e7SJed Brown .  t - current time
5120910c330SBarry Smith -  U - state vector
513316643e7SJed Brown 
514316643e7SJed Brown    Output Parameter:
515316643e7SJed Brown .  y - right hand side
516316643e7SJed Brown 
517316643e7SJed Brown    Note:
518316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
519316643e7SJed Brown    is used internally within the nonlinear solvers.
520316643e7SJed Brown 
521316643e7SJed Brown    Level: developer
522316643e7SJed Brown 
523316643e7SJed Brown .keywords: TS, compute
524316643e7SJed Brown 
525316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
526316643e7SJed Brown @*/
5270910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
528d763cef2SBarry Smith {
529dfbe8321SBarry Smith   PetscErrorCode ierr;
53024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
53124989b8cSPeter Brune   TSIFunction    ifunction;
53224989b8cSPeter Brune   void           *ctx;
53324989b8cSPeter Brune   DM             dm;
53424989b8cSPeter Brune 
535d763cef2SBarry Smith   PetscFunctionBegin;
5360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5370910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5380700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
53924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
54024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5410298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
542d763cef2SBarry Smith 
543ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
544d763cef2SBarry Smith 
5450910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
54624989b8cSPeter Brune   if (rhsfunction) {
547d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5480910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
549d763cef2SBarry Smith     PetscStackPop;
550214bc6a2SJed Brown   } else {
551214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
552b2cd27e8SJed Brown   }
55344a41b28SSean Farley 
5540910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
555d763cef2SBarry Smith   PetscFunctionReturn(0);
556d763cef2SBarry Smith }
557d763cef2SBarry Smith 
5584a2ae208SSatish Balay #undef __FUNCT__
559ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
560ef20d060SBarry Smith /*@
561ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
562ef20d060SBarry Smith 
563ef20d060SBarry Smith    Collective on TS and Vec
564ef20d060SBarry Smith 
565ef20d060SBarry Smith    Input Parameters:
566ef20d060SBarry Smith +  ts - the TS context
567ef20d060SBarry Smith -  t - current time
568ef20d060SBarry Smith 
569ef20d060SBarry Smith    Output Parameter:
5700910c330SBarry Smith .  U - the solution
571ef20d060SBarry Smith 
572ef20d060SBarry Smith    Note:
573ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
574ef20d060SBarry Smith    is used internally within the nonlinear solvers.
575ef20d060SBarry Smith 
576ef20d060SBarry Smith    Level: developer
577ef20d060SBarry Smith 
578ef20d060SBarry Smith .keywords: TS, compute
579ef20d060SBarry Smith 
580abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
581ef20d060SBarry Smith @*/
5820910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
583ef20d060SBarry Smith {
584ef20d060SBarry Smith   PetscErrorCode     ierr;
585ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
586ef20d060SBarry Smith   void               *ctx;
587ef20d060SBarry Smith   DM                 dm;
588ef20d060SBarry Smith 
589ef20d060SBarry Smith   PetscFunctionBegin;
590ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5910910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
592ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
593ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
594ef20d060SBarry Smith 
595ef20d060SBarry Smith   if (solutionfunction) {
5969b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5970910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
598ef20d060SBarry Smith     PetscStackPop;
599ef20d060SBarry Smith   }
600ef20d060SBarry Smith   PetscFunctionReturn(0);
601ef20d060SBarry Smith }
6029b7cd975SBarry Smith #undef __FUNCT__
6039b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
6049b7cd975SBarry Smith /*@
6059b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6069b7cd975SBarry Smith 
6079b7cd975SBarry Smith    Collective on TS and Vec
6089b7cd975SBarry Smith 
6099b7cd975SBarry Smith    Input Parameters:
6109b7cd975SBarry Smith +  ts - the TS context
6119b7cd975SBarry Smith -  t - current time
6129b7cd975SBarry Smith 
6139b7cd975SBarry Smith    Output Parameter:
6149b7cd975SBarry Smith .  U - the function value
6159b7cd975SBarry Smith 
6169b7cd975SBarry Smith    Note:
6179b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6189b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6199b7cd975SBarry Smith 
6209b7cd975SBarry Smith    Level: developer
6219b7cd975SBarry Smith 
6229b7cd975SBarry Smith .keywords: TS, compute
6239b7cd975SBarry Smith 
6249b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6259b7cd975SBarry Smith @*/
6269b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6279b7cd975SBarry Smith {
6289b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6299b7cd975SBarry Smith   void               *ctx;
6309b7cd975SBarry Smith   DM                 dm;
6319b7cd975SBarry Smith 
6329b7cd975SBarry Smith   PetscFunctionBegin;
6339b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6349b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6359b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6369b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6379b7cd975SBarry Smith 
6389b7cd975SBarry Smith   if (forcing) {
6399b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6409b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6419b7cd975SBarry Smith     PetscStackPop;
6429b7cd975SBarry Smith   }
6439b7cd975SBarry Smith   PetscFunctionReturn(0);
6449b7cd975SBarry Smith }
645ef20d060SBarry Smith 
646ef20d060SBarry Smith #undef __FUNCT__
647214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
648214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
649214bc6a2SJed Brown {
6502dd45cf8SJed Brown   Vec            F;
651214bc6a2SJed Brown   PetscErrorCode ierr;
652214bc6a2SJed Brown 
653214bc6a2SJed Brown   PetscFunctionBegin;
6540298fd71SBarry Smith   *Frhs = NULL;
6550298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
656214bc6a2SJed Brown   if (!ts->Frhs) {
6572dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
658214bc6a2SJed Brown   }
659214bc6a2SJed Brown   *Frhs = ts->Frhs;
660214bc6a2SJed Brown   PetscFunctionReturn(0);
661214bc6a2SJed Brown }
662214bc6a2SJed Brown 
663214bc6a2SJed Brown #undef __FUNCT__
664214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
665214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
666214bc6a2SJed Brown {
667214bc6a2SJed Brown   Mat            A,B;
6682dd45cf8SJed Brown   PetscErrorCode ierr;
669214bc6a2SJed Brown 
670214bc6a2SJed Brown   PetscFunctionBegin;
671c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
672c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6730298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
674214bc6a2SJed Brown   if (Arhs) {
675214bc6a2SJed Brown     if (!ts->Arhs) {
676214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
677214bc6a2SJed Brown     }
678214bc6a2SJed Brown     *Arhs = ts->Arhs;
679214bc6a2SJed Brown   }
680214bc6a2SJed Brown   if (Brhs) {
681214bc6a2SJed Brown     if (!ts->Brhs) {
682bdb70873SJed Brown       if (A != B) {
683214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
684bdb70873SJed Brown       } else {
685bdb70873SJed Brown         ts->Brhs = ts->Arhs;
686bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
687bdb70873SJed Brown       }
688214bc6a2SJed Brown     }
689214bc6a2SJed Brown     *Brhs = ts->Brhs;
690214bc6a2SJed Brown   }
691214bc6a2SJed Brown   PetscFunctionReturn(0);
692214bc6a2SJed Brown }
693214bc6a2SJed Brown 
694214bc6a2SJed Brown #undef __FUNCT__
695316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
696316643e7SJed Brown /*@
6970910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
698316643e7SJed Brown 
699316643e7SJed Brown    Collective on TS and Vec
700316643e7SJed Brown 
701316643e7SJed Brown    Input Parameters:
702316643e7SJed Brown +  ts - the TS context
703316643e7SJed Brown .  t - current time
7040910c330SBarry Smith .  U - state vector
7050910c330SBarry Smith .  Udot - time derivative of state vector
706214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
707316643e7SJed Brown 
708316643e7SJed Brown    Output Parameter:
709316643e7SJed Brown .  Y - right hand side
710316643e7SJed Brown 
711316643e7SJed Brown    Note:
712316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
713316643e7SJed Brown    is used internally within the nonlinear solvers.
714316643e7SJed Brown 
715316643e7SJed Brown    If the user did did not write their equations in implicit form, this
716316643e7SJed Brown    function recasts them in implicit form.
717316643e7SJed Brown 
718316643e7SJed Brown    Level: developer
719316643e7SJed Brown 
720316643e7SJed Brown .keywords: TS, compute
721316643e7SJed Brown 
722316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
723316643e7SJed Brown @*/
7240910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
725316643e7SJed Brown {
726316643e7SJed Brown   PetscErrorCode ierr;
72724989b8cSPeter Brune   TSIFunction    ifunction;
72824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
72924989b8cSPeter Brune   void           *ctx;
73024989b8cSPeter Brune   DM             dm;
731316643e7SJed Brown 
732316643e7SJed Brown   PetscFunctionBegin;
7330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7340910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7350910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7360700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
737316643e7SJed Brown 
73824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7400298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
74124989b8cSPeter Brune 
742ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
743d90be118SSean Farley 
7440910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
74524989b8cSPeter Brune   if (ifunction) {
746316643e7SJed Brown     PetscStackPush("TS user implicit function");
7470910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
748316643e7SJed Brown     PetscStackPop;
749214bc6a2SJed Brown   }
750214bc6a2SJed Brown   if (imex) {
75124989b8cSPeter Brune     if (!ifunction) {
7520910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7532dd45cf8SJed Brown     }
75424989b8cSPeter Brune   } else if (rhsfunction) {
75524989b8cSPeter Brune     if (ifunction) {
756214bc6a2SJed Brown       Vec Frhs;
757214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7580910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
759214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7602dd45cf8SJed Brown     } else {
7610910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7620910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
763316643e7SJed Brown     }
7644a6899ffSJed Brown   }
7650910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
766316643e7SJed Brown   PetscFunctionReturn(0);
767316643e7SJed Brown }
768316643e7SJed Brown 
769316643e7SJed Brown #undef __FUNCT__
770316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
771316643e7SJed Brown /*@
772316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
773316643e7SJed Brown 
774316643e7SJed Brown    Collective on TS and Vec
775316643e7SJed Brown 
776316643e7SJed Brown    Input
777316643e7SJed Brown       Input Parameters:
778316643e7SJed Brown +  ts - the TS context
779316643e7SJed Brown .  t - current timestep
7800910c330SBarry Smith .  U - state vector
7810910c330SBarry Smith .  Udot - time derivative of state vector
782214bc6a2SJed Brown .  shift - shift to apply, see note below
783214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
784316643e7SJed Brown 
785316643e7SJed Brown    Output Parameters:
786316643e7SJed Brown +  A - Jacobian matrix
787316643e7SJed Brown .  B - optional preconditioning matrix
788316643e7SJed Brown -  flag - flag indicating matrix structure
789316643e7SJed Brown 
790316643e7SJed Brown    Notes:
7910910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
792316643e7SJed Brown 
7930910c330SBarry Smith    dF/dU + shift*dF/dUdot
794316643e7SJed Brown 
795316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
796316643e7SJed Brown    is used internally within the nonlinear solvers.
797316643e7SJed Brown 
798316643e7SJed Brown    Level: developer
799316643e7SJed Brown 
800316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
801316643e7SJed Brown 
802316643e7SJed Brown .seealso:  TSSetIJacobian()
80363495f91SJed Brown @*/
804d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
805316643e7SJed Brown {
806316643e7SJed Brown   PetscErrorCode ierr;
80724989b8cSPeter Brune   TSIJacobian    ijacobian;
80824989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
80924989b8cSPeter Brune   DM             dm;
81024989b8cSPeter Brune   void           *ctx;
811316643e7SJed Brown 
812316643e7SJed Brown   PetscFunctionBegin;
8130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8140910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8150910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
816316643e7SJed Brown   PetscValidPointer(A,6);
81794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
818316643e7SJed Brown   PetscValidPointer(B,7);
81994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
82024989b8cSPeter Brune 
82124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8230298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
82424989b8cSPeter Brune 
825ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
826316643e7SJed Brown 
82794ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
82824989b8cSPeter Brune   if (ijacobian) {
829316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
830d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
831316643e7SJed Brown     PetscStackPop;
832214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
83394ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
83494ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
8354a6899ffSJed Brown   }
836214bc6a2SJed Brown   if (imex) {
837b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
83894ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
83994ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
84094ab13aaSBarry Smith       if (A != B) {
84194ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
84294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
843214bc6a2SJed Brown       }
844214bc6a2SJed Brown     }
845214bc6a2SJed Brown   } else {
846e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
847e1244c69SJed Brown     if (rhsjacobian) {
848bd373395SBarry Smith       if (ijacobian) {
849214bc6a2SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
850bd373395SBarry Smith       } else {
851bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
852214bc6a2SJed Brown       }
853d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
854e1244c69SJed Brown     }
85594ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
856e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
857e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
85894ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
85994ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
86094ab13aaSBarry Smith       if (A != B) {
86194ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
86294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
863316643e7SJed Brown       }
864e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
865e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
866e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
86794ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
86894ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
86994ab13aaSBarry Smith         if (A != B) {
87094ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
87194ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
872214bc6a2SJed Brown         }
873316643e7SJed Brown       }
87494ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
87594ab13aaSBarry Smith       if (A != B) {
87694ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
877316643e7SJed Brown       }
878316643e7SJed Brown     }
879316643e7SJed Brown   }
88094ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
881316643e7SJed Brown   PetscFunctionReturn(0);
882316643e7SJed Brown }
883316643e7SJed Brown 
884316643e7SJed Brown #undef __FUNCT__
8854a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
886d763cef2SBarry Smith /*@C
887d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
888b5abc632SBarry Smith     where U_t = G(t,u).
889d763cef2SBarry Smith 
8903f9fe445SBarry Smith     Logically Collective on TS
891d763cef2SBarry Smith 
892d763cef2SBarry Smith     Input Parameters:
893d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8940298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
895d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
896d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8970298fd71SBarry Smith           function evaluation routine (may be NULL)
898d763cef2SBarry Smith 
899d763cef2SBarry Smith     Calling sequence of func:
90087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
901d763cef2SBarry Smith 
902d763cef2SBarry Smith +   t - current timestep
903d763cef2SBarry Smith .   u - input vector
904d763cef2SBarry Smith .   F - function vector
905d763cef2SBarry Smith -   ctx - [optional] user-defined function context
906d763cef2SBarry Smith 
907d763cef2SBarry Smith     Level: beginner
908d763cef2SBarry Smith 
9092bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
9102bbac0d3SBarry Smith 
911d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
912d763cef2SBarry Smith 
913ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
914d763cef2SBarry Smith @*/
915089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
916d763cef2SBarry Smith {
917089b2837SJed Brown   PetscErrorCode ierr;
918089b2837SJed Brown   SNES           snes;
9190298fd71SBarry Smith   Vec            ralloc = NULL;
92024989b8cSPeter Brune   DM             dm;
921d763cef2SBarry Smith 
922089b2837SJed Brown   PetscFunctionBegin;
9230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
924ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
92524989b8cSPeter Brune 
92624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
92724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
928089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
929e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
930e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
931e856ceecSJed Brown     r    = ralloc;
932e856ceecSJed Brown   }
933089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
934e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
935d763cef2SBarry Smith   PetscFunctionReturn(0);
936d763cef2SBarry Smith }
937d763cef2SBarry Smith 
9384a2ae208SSatish Balay #undef __FUNCT__
939ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
940ef20d060SBarry Smith /*@C
941abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
942ef20d060SBarry Smith 
943ef20d060SBarry Smith     Logically Collective on TS
944ef20d060SBarry Smith 
945ef20d060SBarry Smith     Input Parameters:
946ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
947ef20d060SBarry Smith .   f - routine for evaluating the solution
948ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9490298fd71SBarry Smith           function evaluation routine (may be NULL)
950ef20d060SBarry Smith 
951ef20d060SBarry Smith     Calling sequence of func:
952ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
953ef20d060SBarry Smith 
954ef20d060SBarry Smith +   t - current timestep
955ef20d060SBarry Smith .   u - output vector
956ef20d060SBarry Smith -   ctx - [optional] user-defined function context
957ef20d060SBarry Smith 
958abd5a294SJed Brown     Notes:
959abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
960abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
961abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
962abd5a294SJed Brown 
9634f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
964abd5a294SJed Brown 
965ef20d060SBarry Smith     Level: beginner
966ef20d060SBarry Smith 
967ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
968ef20d060SBarry Smith 
9699b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
970ef20d060SBarry Smith @*/
971ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
972ef20d060SBarry Smith {
973ef20d060SBarry Smith   PetscErrorCode ierr;
974ef20d060SBarry Smith   DM             dm;
975ef20d060SBarry Smith 
976ef20d060SBarry Smith   PetscFunctionBegin;
977ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
978ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
979ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
980ef20d060SBarry Smith   PetscFunctionReturn(0);
981ef20d060SBarry Smith }
982ef20d060SBarry Smith 
983ef20d060SBarry Smith #undef __FUNCT__
9849b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9859b7cd975SBarry Smith /*@C
9869b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9879b7cd975SBarry Smith 
9889b7cd975SBarry Smith     Logically Collective on TS
9899b7cd975SBarry Smith 
9909b7cd975SBarry Smith     Input Parameters:
9919b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9929b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9939b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9940298fd71SBarry Smith           function evaluation routine (may be NULL)
9959b7cd975SBarry Smith 
9969b7cd975SBarry Smith     Calling sequence of func:
9979b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9989b7cd975SBarry Smith 
9999b7cd975SBarry Smith +   t - current timestep
10009b7cd975SBarry Smith .   u - output vector
10019b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10029b7cd975SBarry Smith 
10039b7cd975SBarry Smith     Notes:
10049b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
10059b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
10069b7cd975SBarry Smith 
10079b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
10089b7cd975SBarry Smith 
10099b7cd975SBarry Smith     Level: beginner
10109b7cd975SBarry Smith 
10119b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
10129b7cd975SBarry Smith 
10139b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
10149b7cd975SBarry Smith @*/
10159b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
10169b7cd975SBarry Smith {
10179b7cd975SBarry Smith   PetscErrorCode ierr;
10189b7cd975SBarry Smith   DM             dm;
10199b7cd975SBarry Smith 
10209b7cd975SBarry Smith   PetscFunctionBegin;
10219b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10229b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
10239b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
10249b7cd975SBarry Smith   PetscFunctionReturn(0);
10259b7cd975SBarry Smith }
10269b7cd975SBarry Smith 
10279b7cd975SBarry Smith #undef __FUNCT__
10284a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1029d763cef2SBarry Smith /*@C
1030f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1031b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1032d763cef2SBarry Smith 
10333f9fe445SBarry Smith    Logically Collective on TS
1034d763cef2SBarry Smith 
1035d763cef2SBarry Smith    Input Parameters:
1036d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1037e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1038e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1039d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1040d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10410298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1042d763cef2SBarry Smith 
1043f7ab8db6SBarry Smith    Calling sequence of f:
1044ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1045d763cef2SBarry Smith 
1046d763cef2SBarry Smith +  t - current timestep
1047d763cef2SBarry Smith .  u - input vector
1048e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1049e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1050d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1051d763cef2SBarry Smith 
1052d763cef2SBarry Smith 
1053d763cef2SBarry Smith    Level: beginner
1054d763cef2SBarry Smith 
1055d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1056d763cef2SBarry Smith 
1057ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1058d763cef2SBarry Smith 
1059d763cef2SBarry Smith @*/
1060e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1061d763cef2SBarry Smith {
1062277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1063089b2837SJed Brown   SNES           snes;
106424989b8cSPeter Brune   DM             dm;
106524989b8cSPeter Brune   TSIJacobian    ijacobian;
1066277b19d0SLisandro Dalcin 
1067d763cef2SBarry Smith   PetscFunctionBegin;
10680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1069e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1070e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1071e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1072e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1073d763cef2SBarry Smith 
107424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
107524989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1076e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1077e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1078e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1079e1244c69SJed Brown   }
10800298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1081089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10825f659677SPeter Brune   if (!ijacobian) {
1083e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10840e4ef248SJed Brown   }
1085e5d3d808SBarry Smith   if (Amat) {
1086e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10870e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1088bbd56ea5SKarl Rupp 
1089e5d3d808SBarry Smith     ts->Arhs = Amat;
10900e4ef248SJed Brown   }
1091e5d3d808SBarry Smith   if (Pmat) {
1092e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10930e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1094bbd56ea5SKarl Rupp 
1095e5d3d808SBarry Smith     ts->Brhs = Pmat;
10960e4ef248SJed Brown   }
1097d763cef2SBarry Smith   PetscFunctionReturn(0);
1098d763cef2SBarry Smith }
1099d763cef2SBarry Smith 
1100316643e7SJed Brown 
1101316643e7SJed Brown #undef __FUNCT__
1102316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1103316643e7SJed Brown /*@C
1104b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1105316643e7SJed Brown 
11063f9fe445SBarry Smith    Logically Collective on TS
1107316643e7SJed Brown 
1108316643e7SJed Brown    Input Parameters:
1109316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
11100298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1111316643e7SJed Brown .  f   - the function evaluation routine
11120298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1113316643e7SJed Brown 
1114316643e7SJed Brown    Calling sequence of f:
1115316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1116316643e7SJed Brown 
1117316643e7SJed Brown +  t   - time at step/stage being solved
1118316643e7SJed Brown .  u   - state vector
1119316643e7SJed Brown .  u_t - time derivative of state vector
1120316643e7SJed Brown .  F   - function vector
1121316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1122316643e7SJed Brown 
1123316643e7SJed Brown    Important:
11242bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1125316643e7SJed Brown 
1126316643e7SJed Brown    Level: beginner
1127316643e7SJed Brown 
1128316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1129316643e7SJed Brown 
1130d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1131316643e7SJed Brown @*/
1132089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1133316643e7SJed Brown {
1134089b2837SJed Brown   PetscErrorCode ierr;
1135089b2837SJed Brown   SNES           snes;
11360298fd71SBarry Smith   Vec            resalloc = NULL;
113724989b8cSPeter Brune   DM             dm;
1138316643e7SJed Brown 
1139316643e7SJed Brown   PetscFunctionBegin;
11400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1141ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
114224989b8cSPeter Brune 
114324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
114524989b8cSPeter Brune 
1146089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1147e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1148e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1149e856ceecSJed Brown     res  = resalloc;
1150e856ceecSJed Brown   }
1151089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1152e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1153089b2837SJed Brown   PetscFunctionReturn(0);
1154089b2837SJed Brown }
1155089b2837SJed Brown 
1156089b2837SJed Brown #undef __FUNCT__
1157089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1158089b2837SJed Brown /*@C
1159089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1160089b2837SJed Brown 
1161089b2837SJed Brown    Not Collective
1162089b2837SJed Brown 
1163089b2837SJed Brown    Input Parameter:
1164089b2837SJed Brown .  ts - the TS context
1165089b2837SJed Brown 
1166089b2837SJed Brown    Output Parameter:
11670298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11680298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11690298fd71SBarry Smith -  ctx - the function context (or NULL)
1170089b2837SJed Brown 
1171089b2837SJed Brown    Level: advanced
1172089b2837SJed Brown 
1173089b2837SJed Brown .keywords: TS, nonlinear, get, function
1174089b2837SJed Brown 
1175089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1176089b2837SJed Brown @*/
1177089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1178089b2837SJed Brown {
1179089b2837SJed Brown   PetscErrorCode ierr;
1180089b2837SJed Brown   SNES           snes;
118124989b8cSPeter Brune   DM             dm;
1182089b2837SJed Brown 
1183089b2837SJed Brown   PetscFunctionBegin;
1184089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1185089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11860298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
118724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1189089b2837SJed Brown   PetscFunctionReturn(0);
1190089b2837SJed Brown }
1191089b2837SJed Brown 
1192089b2837SJed Brown #undef __FUNCT__
1193089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1194089b2837SJed Brown /*@C
1195089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1196089b2837SJed Brown 
1197089b2837SJed Brown    Not Collective
1198089b2837SJed Brown 
1199089b2837SJed Brown    Input Parameter:
1200089b2837SJed Brown .  ts - the TS context
1201089b2837SJed Brown 
1202089b2837SJed Brown    Output Parameter:
12030298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12040298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
12050298fd71SBarry Smith -  ctx - the function context (or NULL)
1206089b2837SJed Brown 
1207089b2837SJed Brown    Level: advanced
1208089b2837SJed Brown 
1209089b2837SJed Brown .keywords: TS, nonlinear, get, function
1210089b2837SJed Brown 
12112bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1212089b2837SJed Brown @*/
1213089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1214089b2837SJed Brown {
1215089b2837SJed Brown   PetscErrorCode ierr;
1216089b2837SJed Brown   SNES           snes;
121724989b8cSPeter Brune   DM             dm;
1218089b2837SJed Brown 
1219089b2837SJed Brown   PetscFunctionBegin;
1220089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1221089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12220298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
122324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1225316643e7SJed Brown   PetscFunctionReturn(0);
1226316643e7SJed Brown }
1227316643e7SJed Brown 
1228316643e7SJed Brown #undef __FUNCT__
1229316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1230316643e7SJed Brown /*@C
1231a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1232ae8867d6SBarry Smith         provided with TSSetIFunction().
1233316643e7SJed Brown 
12343f9fe445SBarry Smith    Logically Collective on TS
1235316643e7SJed Brown 
1236316643e7SJed Brown    Input Parameters:
1237316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1238e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1239e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1240316643e7SJed Brown .  f   - the Jacobian evaluation routine
12410298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1242316643e7SJed Brown 
1243316643e7SJed Brown    Calling sequence of f:
1244ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1245316643e7SJed Brown 
1246316643e7SJed Brown +  t    - time at step/stage being solved
12471b4a444bSJed Brown .  U    - state vector
12481b4a444bSJed Brown .  U_t  - time derivative of state vector
1249316643e7SJed Brown .  a    - shift
1250e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1251e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1252316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1253316643e7SJed Brown 
1254316643e7SJed Brown    Notes:
1255e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1256316643e7SJed Brown 
1257895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1258895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1259895c21f2SBarry Smith 
1260a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1261b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1262a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1263a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1264a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1265a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1266a4f0a591SBarry Smith 
1267316643e7SJed Brown    Level: beginner
1268316643e7SJed Brown 
1269316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1270316643e7SJed Brown 
1271ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1272316643e7SJed Brown 
1273316643e7SJed Brown @*/
1274e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1275316643e7SJed Brown {
1276316643e7SJed Brown   PetscErrorCode ierr;
1277089b2837SJed Brown   SNES           snes;
127824989b8cSPeter Brune   DM             dm;
1279316643e7SJed Brown 
1280316643e7SJed Brown   PetscFunctionBegin;
12810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1282e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1283e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1284e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1285e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
128624989b8cSPeter Brune 
128724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128824989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
128924989b8cSPeter Brune 
1290089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1291e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1292316643e7SJed Brown   PetscFunctionReturn(0);
1293316643e7SJed Brown }
1294316643e7SJed Brown 
12954a2ae208SSatish Balay #undef __FUNCT__
1296e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1297e1244c69SJed Brown /*@
1298e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1299e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1300e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1301e1244c69SJed Brown    not been changed by the TS.
1302e1244c69SJed Brown 
1303e1244c69SJed Brown    Logically Collective
1304e1244c69SJed Brown 
1305e1244c69SJed Brown    Input Arguments:
1306e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1307e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1308e1244c69SJed Brown 
1309e1244c69SJed Brown    Level: intermediate
1310e1244c69SJed Brown 
1311e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1312e1244c69SJed Brown @*/
1313e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1314e1244c69SJed Brown {
1315e1244c69SJed Brown   PetscFunctionBegin;
1316e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1317e1244c69SJed Brown   PetscFunctionReturn(0);
1318e1244c69SJed Brown }
1319e1244c69SJed Brown 
1320e1244c69SJed Brown #undef __FUNCT__
132155849f57SBarry Smith #define __FUNCT__ "TSLoad"
132255849f57SBarry Smith /*@C
132355849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
132455849f57SBarry Smith 
132555849f57SBarry Smith   Collective on PetscViewer
132655849f57SBarry Smith 
132755849f57SBarry Smith   Input Parameters:
132855849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
132955849f57SBarry Smith            some related function before a call to TSLoad().
133055849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
133155849f57SBarry Smith 
133255849f57SBarry Smith    Level: intermediate
133355849f57SBarry Smith 
133455849f57SBarry Smith   Notes:
133555849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
133655849f57SBarry Smith 
133755849f57SBarry Smith   Notes for advanced users:
133855849f57SBarry Smith   Most users should not need to know the details of the binary storage
133955849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
134055849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
134155849f57SBarry Smith   format is
134255849f57SBarry Smith .vb
134355849f57SBarry Smith      has not yet been determined
134455849f57SBarry Smith .ve
134555849f57SBarry Smith 
134655849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
134755849f57SBarry Smith @*/
1348f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
134955849f57SBarry Smith {
135055849f57SBarry Smith   PetscErrorCode ierr;
135155849f57SBarry Smith   PetscBool      isbinary;
135255849f57SBarry Smith   PetscInt       classid;
135355849f57SBarry Smith   char           type[256];
13542d53ad75SBarry Smith   DMTS           sdm;
1355ad6bc421SBarry Smith   DM             dm;
135655849f57SBarry Smith 
135755849f57SBarry Smith   PetscFunctionBegin;
1358f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
135955849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
136055849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
136155849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
136255849f57SBarry Smith 
1363060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1364ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1365060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1366f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1367f2c2a1b9SBarry Smith   if (ts->ops->load) {
1368f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1369f2c2a1b9SBarry Smith   }
1370ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1371ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1372ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1373f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1374f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13752d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13762d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
137755849f57SBarry Smith   PetscFunctionReturn(0);
137855849f57SBarry Smith }
137955849f57SBarry Smith 
13809804daf3SBarry Smith #include <petscdraw.h>
1381e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1382e04113cfSBarry Smith #include <petscviewersaws.h>
1383f05ece33SBarry Smith #endif
138455849f57SBarry Smith #undef __FUNCT__
13854a2ae208SSatish Balay #define __FUNCT__ "TSView"
13867e2c5f70SBarry Smith /*@C
1387d763cef2SBarry Smith     TSView - Prints the TS data structure.
1388d763cef2SBarry Smith 
13894c49b128SBarry Smith     Collective on TS
1390d763cef2SBarry Smith 
1391d763cef2SBarry Smith     Input Parameters:
1392d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1393d763cef2SBarry Smith -   viewer - visualization context
1394d763cef2SBarry Smith 
1395d763cef2SBarry Smith     Options Database Key:
1396d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1397d763cef2SBarry Smith 
1398d763cef2SBarry Smith     Notes:
1399d763cef2SBarry Smith     The available visualization contexts include
1400b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1401b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1402d763cef2SBarry Smith          output where only the first processor opens
1403d763cef2SBarry Smith          the file.  All other processors send their
1404d763cef2SBarry Smith          data to the first processor to print.
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith     The user can open an alternative visualization context with
1407b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1408d763cef2SBarry Smith 
1409d763cef2SBarry Smith     Level: beginner
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith .keywords: TS, timestep, view
1412d763cef2SBarry Smith 
1413b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1414d763cef2SBarry Smith @*/
14157087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1416d763cef2SBarry Smith {
1417dfbe8321SBarry Smith   PetscErrorCode ierr;
141819fd82e9SBarry Smith   TSType         type;
14192b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
14202d53ad75SBarry Smith   DMTS           sdm;
1421e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1422536b137fSBarry Smith   PetscBool      issaws;
1423f05ece33SBarry Smith #endif
1424d763cef2SBarry Smith 
1425d763cef2SBarry Smith   PetscFunctionBegin;
14260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14273050cee2SBarry Smith   if (!viewer) {
1428ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
14293050cee2SBarry Smith   }
14300700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1431c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1432fd16b177SBarry Smith 
1433251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1434251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
143555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
14362b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1437e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1438536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1439f05ece33SBarry Smith #endif
144032077d6dSBarry Smith   if (iascii) {
1441dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
144277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14437c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1444d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14455ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1446c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1447d763cef2SBarry Smith     }
14485ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1449193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14502d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14512d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1452d52bd9f3SBarry Smith     if (ts->ops->view) {
1453d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1454d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1455d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1456d52bd9f3SBarry Smith     }
14570f5bd95cSBarry Smith   } else if (isstring) {
1458a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1459b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
146055849f57SBarry Smith   } else if (isbinary) {
146155849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
146255849f57SBarry Smith     MPI_Comm    comm;
146355849f57SBarry Smith     PetscMPIInt rank;
146455849f57SBarry Smith     char        type[256];
146555849f57SBarry Smith 
146655849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
146755849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
146855849f57SBarry Smith     if (!rank) {
146955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
147055849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
147155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
147255849f57SBarry Smith     }
147355849f57SBarry Smith     if (ts->ops->view) {
147455849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
147555849f57SBarry Smith     }
1476f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1477f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14782d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14792d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14802b0a91c0SBarry Smith   } else if (isdraw) {
14812b0a91c0SBarry Smith     PetscDraw draw;
14822b0a91c0SBarry Smith     char      str[36];
148389fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14842b0a91c0SBarry Smith 
14852b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14862b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14872b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14882b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
148951fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
149089fd9fafSBarry Smith     bottom = y - h;
14912b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14922b0a91c0SBarry Smith     if (ts->ops->view) {
14932b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14942b0a91c0SBarry Smith     }
14952b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1496e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1497536b137fSBarry Smith   } else if (issaws) {
1498d45a07a7SBarry Smith     PetscMPIInt rank;
14992657e9d9SBarry Smith     const char  *name;
15002657e9d9SBarry Smith 
15012657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1502d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1503d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1504d45a07a7SBarry Smith       char       dir[1024];
1505d45a07a7SBarry Smith 
1506e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1507a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
15082657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
15092657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
15102657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1511d763cef2SBarry Smith     }
15120acecf5bSBarry Smith     if (ts->ops->view) {
15130acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
15140acecf5bSBarry Smith     }
1515f05ece33SBarry Smith #endif
1516f05ece33SBarry Smith   }
1517f05ece33SBarry Smith 
1518b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1519251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1520b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1521d763cef2SBarry Smith   PetscFunctionReturn(0);
1522d763cef2SBarry Smith }
1523d763cef2SBarry Smith 
1524d763cef2SBarry Smith 
15254a2ae208SSatish Balay #undef __FUNCT__
15264a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1527b07ff414SBarry Smith /*@
1528d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1529d763cef2SBarry Smith    the timesteppers.
1530d763cef2SBarry Smith 
15313f9fe445SBarry Smith    Logically Collective on TS
1532d763cef2SBarry Smith 
1533d763cef2SBarry Smith    Input Parameters:
1534d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1535d763cef2SBarry Smith -  usrP - optional user context
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith    Level: intermediate
1538d763cef2SBarry Smith 
1539d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1542d763cef2SBarry Smith @*/
15437087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1544d763cef2SBarry Smith {
1545d763cef2SBarry Smith   PetscFunctionBegin;
15460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1547d763cef2SBarry Smith   ts->user = usrP;
1548d763cef2SBarry Smith   PetscFunctionReturn(0);
1549d763cef2SBarry Smith }
1550d763cef2SBarry Smith 
15514a2ae208SSatish Balay #undef __FUNCT__
15524a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1553b07ff414SBarry Smith /*@
1554d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1555d763cef2SBarry Smith     timestepper.
1556d763cef2SBarry Smith 
1557d763cef2SBarry Smith     Not Collective
1558d763cef2SBarry Smith 
1559d763cef2SBarry Smith     Input Parameter:
1560d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1561d763cef2SBarry Smith 
1562d763cef2SBarry Smith     Output Parameter:
1563d763cef2SBarry Smith .   usrP - user context
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith     Level: intermediate
1566d763cef2SBarry Smith 
1567d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1568d763cef2SBarry Smith 
1569d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1570d763cef2SBarry Smith @*/
1571e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1572d763cef2SBarry Smith {
1573d763cef2SBarry Smith   PetscFunctionBegin;
15740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1575e71120c6SJed Brown   *(void**)usrP = ts->user;
1576d763cef2SBarry Smith   PetscFunctionReturn(0);
1577d763cef2SBarry Smith }
1578d763cef2SBarry Smith 
15794a2ae208SSatish Balay #undef __FUNCT__
15804a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1581d763cef2SBarry Smith /*@
1582b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1583d763cef2SBarry Smith 
1584d763cef2SBarry Smith    Not Collective
1585d763cef2SBarry Smith 
1586d763cef2SBarry Smith    Input Parameter:
1587d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1588d763cef2SBarry Smith 
1589d763cef2SBarry Smith    Output Parameter:
1590b8123daeSJed Brown .  iter - number of steps completed so far
1591d763cef2SBarry Smith 
1592d763cef2SBarry Smith    Level: intermediate
1593d763cef2SBarry Smith 
1594d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15959be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1596d763cef2SBarry Smith @*/
15977087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1598d763cef2SBarry Smith {
1599d763cef2SBarry Smith   PetscFunctionBegin;
16000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16014482741eSBarry Smith   PetscValidIntPointer(iter,2);
1602d763cef2SBarry Smith   *iter = ts->steps;
1603d763cef2SBarry Smith   PetscFunctionReturn(0);
1604d763cef2SBarry Smith }
1605d763cef2SBarry Smith 
16064a2ae208SSatish Balay #undef __FUNCT__
16074a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1608d763cef2SBarry Smith /*@
1609d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1610d763cef2SBarry Smith    as well as the initial time.
1611d763cef2SBarry Smith 
16123f9fe445SBarry Smith    Logically Collective on TS
1613d763cef2SBarry Smith 
1614d763cef2SBarry Smith    Input Parameters:
1615d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1616d763cef2SBarry Smith .  initial_time - the initial time
1617d763cef2SBarry Smith -  time_step - the size of the timestep
1618d763cef2SBarry Smith 
1619d763cef2SBarry Smith    Level: intermediate
1620d763cef2SBarry Smith 
1621d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1622d763cef2SBarry Smith 
1623d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1624d763cef2SBarry Smith @*/
16257087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1626d763cef2SBarry Smith {
1627e144a568SJed Brown   PetscErrorCode ierr;
1628e144a568SJed Brown 
1629d763cef2SBarry Smith   PetscFunctionBegin;
16300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1631e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1632d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1633d763cef2SBarry Smith   PetscFunctionReturn(0);
1634d763cef2SBarry Smith }
1635d763cef2SBarry Smith 
16364a2ae208SSatish Balay #undef __FUNCT__
16374a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1638d763cef2SBarry Smith /*@
1639d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1640d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1641d763cef2SBarry Smith 
16423f9fe445SBarry Smith    Logically Collective on TS
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith    Input Parameters:
1645d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1646d763cef2SBarry Smith -  time_step - the size of the timestep
1647d763cef2SBarry Smith 
1648d763cef2SBarry Smith    Level: intermediate
1649d763cef2SBarry Smith 
1650d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith .keywords: TS, set, timestep
1653d763cef2SBarry Smith @*/
16547087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1655d763cef2SBarry Smith {
1656d763cef2SBarry Smith   PetscFunctionBegin;
16570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1658c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1659d763cef2SBarry Smith   ts->time_step      = time_step;
166031748224SBarry Smith   ts->time_step_orig = time_step;
1661d763cef2SBarry Smith   PetscFunctionReturn(0);
1662d763cef2SBarry Smith }
1663d763cef2SBarry Smith 
16644a2ae208SSatish Balay #undef __FUNCT__
1665a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1666a43b19c4SJed Brown /*@
166749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
166849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
166949354f04SShri Abhyankar      or just return at the final time TS computed.
1670a43b19c4SJed Brown 
1671a43b19c4SJed Brown   Logically Collective on TS
1672a43b19c4SJed Brown 
1673a43b19c4SJed Brown    Input Parameter:
1674a43b19c4SJed Brown +   ts - the time-step context
167549354f04SShri Abhyankar -   eftopt - exact final time option
1676a43b19c4SJed Brown 
1677a43b19c4SJed Brown    Level: beginner
1678a43b19c4SJed Brown 
1679a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1680a43b19c4SJed Brown @*/
168149354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1682a43b19c4SJed Brown {
1683a43b19c4SJed Brown   PetscFunctionBegin;
1684a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
168549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
168649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1687a43b19c4SJed Brown   PetscFunctionReturn(0);
1688a43b19c4SJed Brown }
1689a43b19c4SJed Brown 
1690a43b19c4SJed Brown #undef __FUNCT__
16914a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1692d763cef2SBarry Smith /*@
1693d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1694d763cef2SBarry Smith 
1695d763cef2SBarry Smith    Not Collective
1696d763cef2SBarry Smith 
1697d763cef2SBarry Smith    Input Parameter:
1698d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1699d763cef2SBarry Smith 
1700d763cef2SBarry Smith    Output Parameter:
1701d763cef2SBarry Smith .  dt - the current timestep size
1702d763cef2SBarry Smith 
1703d763cef2SBarry Smith    Level: intermediate
1704d763cef2SBarry Smith 
1705d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1706d763cef2SBarry Smith 
1707d763cef2SBarry Smith .keywords: TS, get, timestep
1708d763cef2SBarry Smith @*/
17097087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1710d763cef2SBarry Smith {
1711d763cef2SBarry Smith   PetscFunctionBegin;
17120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1713f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1714d763cef2SBarry Smith   *dt = ts->time_step;
1715d763cef2SBarry Smith   PetscFunctionReturn(0);
1716d763cef2SBarry Smith }
1717d763cef2SBarry Smith 
17184a2ae208SSatish Balay #undef __FUNCT__
17194a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1720d8e5e3e6SSatish Balay /*@
1721d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1722d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1723d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1724d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1725d763cef2SBarry Smith 
1726d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1727d763cef2SBarry Smith 
1728d763cef2SBarry Smith    Input Parameter:
1729d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1730d763cef2SBarry Smith 
1731d763cef2SBarry Smith    Output Parameter:
1732d763cef2SBarry Smith .  v - the vector containing the solution
1733d763cef2SBarry Smith 
1734d763cef2SBarry Smith    Level: intermediate
1735d763cef2SBarry Smith 
1736d763cef2SBarry Smith .seealso: TSGetTimeStep()
1737d763cef2SBarry Smith 
1738d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1739d763cef2SBarry Smith @*/
17407087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1741d763cef2SBarry Smith {
1742d763cef2SBarry Smith   PetscFunctionBegin;
17430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17444482741eSBarry Smith   PetscValidPointer(v,2);
17458737fe31SLisandro Dalcin   *v = ts->vec_sol;
1746d763cef2SBarry Smith   PetscFunctionReturn(0);
1747d763cef2SBarry Smith }
1748d763cef2SBarry Smith 
1749c235aa8dSHong Zhang #undef __FUNCT__
1750dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
1751c235aa8dSHong Zhang /*@
1752dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
1753c235aa8dSHong Zhang 
1754c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1755c235aa8dSHong Zhang 
1756c235aa8dSHong Zhang    Input Parameter:
1757c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1758c235aa8dSHong Zhang 
1759c235aa8dSHong Zhang    Output Parameter:
1760abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1761abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
1762c235aa8dSHong Zhang 
1763c235aa8dSHong Zhang    Level: intermediate
1764c235aa8dSHong Zhang 
1765c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1766c235aa8dSHong Zhang 
1767c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1768c235aa8dSHong Zhang @*/
1769dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
1770c235aa8dSHong Zhang {
1771c235aa8dSHong Zhang   PetscFunctionBegin;
1772c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1773abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
1774abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
1775abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
1776c235aa8dSHong Zhang   PetscFunctionReturn(0);
1777c235aa8dSHong Zhang }
1778c235aa8dSHong Zhang 
1779bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17804a2ae208SSatish Balay #undef __FUNCT__
1781bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1782d8e5e3e6SSatish Balay /*@
1783bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1784d763cef2SBarry Smith 
1785bdad233fSMatthew Knepley   Not collective
1786d763cef2SBarry Smith 
1787bdad233fSMatthew Knepley   Input Parameters:
1788bdad233fSMatthew Knepley + ts   - The TS
1789bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1790d763cef2SBarry Smith .vb
17910910c330SBarry Smith          U_t - A U = 0      (linear)
17920910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17930910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1794d763cef2SBarry Smith .ve
1795d763cef2SBarry Smith 
1796d763cef2SBarry Smith    Level: beginner
1797d763cef2SBarry Smith 
1798bdad233fSMatthew Knepley .keywords: TS, problem type
1799bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1800d763cef2SBarry Smith @*/
18017087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1802a7cc72afSBarry Smith {
18039e2a6581SJed Brown   PetscErrorCode ierr;
18049e2a6581SJed Brown 
1805d763cef2SBarry Smith   PetscFunctionBegin;
18060700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1807bdad233fSMatthew Knepley   ts->problem_type = type;
18089e2a6581SJed Brown   if (type == TS_LINEAR) {
18099e2a6581SJed Brown     SNES snes;
18109e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
18119e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
18129e2a6581SJed Brown   }
1813d763cef2SBarry Smith   PetscFunctionReturn(0);
1814d763cef2SBarry Smith }
1815d763cef2SBarry Smith 
1816bdad233fSMatthew Knepley #undef __FUNCT__
1817bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1818bdad233fSMatthew Knepley /*@C
1819bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1820bdad233fSMatthew Knepley 
1821bdad233fSMatthew Knepley   Not collective
1822bdad233fSMatthew Knepley 
1823bdad233fSMatthew Knepley   Input Parameter:
1824bdad233fSMatthew Knepley . ts   - The TS
1825bdad233fSMatthew Knepley 
1826bdad233fSMatthew Knepley   Output Parameter:
1827bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1828bdad233fSMatthew Knepley .vb
1829089b2837SJed Brown          M U_t = A U
1830089b2837SJed Brown          M(t) U_t = A(t) U
1831b5abc632SBarry Smith          F(t,U,U_t)
1832bdad233fSMatthew Knepley .ve
1833bdad233fSMatthew Knepley 
1834bdad233fSMatthew Knepley    Level: beginner
1835bdad233fSMatthew Knepley 
1836bdad233fSMatthew Knepley .keywords: TS, problem type
1837bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1838bdad233fSMatthew Knepley @*/
18397087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1840a7cc72afSBarry Smith {
1841bdad233fSMatthew Knepley   PetscFunctionBegin;
18420700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18434482741eSBarry Smith   PetscValidIntPointer(type,2);
1844bdad233fSMatthew Knepley   *type = ts->problem_type;
1845bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1846bdad233fSMatthew Knepley }
1847d763cef2SBarry Smith 
18484a2ae208SSatish Balay #undef __FUNCT__
18494a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1850d763cef2SBarry Smith /*@
1851d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1852d763cef2SBarry Smith    of a timestepper.
1853d763cef2SBarry Smith 
1854d763cef2SBarry Smith    Collective on TS
1855d763cef2SBarry Smith 
1856d763cef2SBarry Smith    Input Parameter:
1857d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1858d763cef2SBarry Smith 
1859d763cef2SBarry Smith    Notes:
1860d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1861d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1862d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1863d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1864d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1865d763cef2SBarry Smith 
1866d763cef2SBarry Smith    Level: advanced
1867d763cef2SBarry Smith 
1868d763cef2SBarry Smith .keywords: TS, timestep, setup
1869d763cef2SBarry Smith 
1870d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1871d763cef2SBarry Smith @*/
18727087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1873d763cef2SBarry Smith {
1874dfbe8321SBarry Smith   PetscErrorCode ierr;
18756c6b9e74SPeter Brune   DM             dm;
18766c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1877d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18786c6b9e74SPeter Brune   TSIJacobian    ijac;
18796c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1880d763cef2SBarry Smith 
1881d763cef2SBarry Smith   PetscFunctionBegin;
18820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1883277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1884277b19d0SLisandro Dalcin 
18852c18e0fdSBarry Smith   ts->total_steps = 0;
18867adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18879596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1888d763cef2SBarry Smith   }
1889277b19d0SLisandro Dalcin 
1890277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1891277b19d0SLisandro Dalcin 
18921c3436cfSJed Brown 
1893552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
1894277b19d0SLisandro Dalcin 
1895e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1896e1244c69SJed Brown     Mat Amat,Pmat;
1897e1244c69SJed Brown     SNES snes;
1898e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1899e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1900e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1901e1244c69SJed Brown      * have displaced the RHS matrix */
1902e1244c69SJed Brown     if (Amat == ts->Arhs) {
1903e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1904e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1905e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1906e1244c69SJed Brown     }
1907e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1908e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1909e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1910e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1911e1244c69SJed Brown     }
1912e1244c69SJed Brown   }
1913277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1914000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1915277b19d0SLisandro Dalcin   }
1916277b19d0SLisandro Dalcin 
19176c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
19186c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
19196c6b9e74SPeter Brune    */
19206c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19210298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
19226c6b9e74SPeter Brune   if (!func) {
19236c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
19246c6b9e74SPeter Brune   }
19256c6b9e74SPeter 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.
19266c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
19276c6b9e74SPeter Brune    */
19280298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
19290298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
19300298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
19316c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
19326c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
19336c6b9e74SPeter Brune   }
1934277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1935277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1936277b19d0SLisandro Dalcin }
1937277b19d0SLisandro Dalcin 
1938277b19d0SLisandro Dalcin #undef __FUNCT__
1939f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1940f6a906c0SBarry Smith /*@
1941f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1942f6a906c0SBarry Smith    of an adjoint solver
1943f6a906c0SBarry Smith 
1944f6a906c0SBarry Smith    Collective on TS
1945f6a906c0SBarry Smith 
1946f6a906c0SBarry Smith    Input Parameter:
1947f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1948f6a906c0SBarry Smith 
1949f6a906c0SBarry Smith    Level: advanced
1950f6a906c0SBarry Smith 
1951f6a906c0SBarry Smith .keywords: TS, timestep, setup
1952f6a906c0SBarry Smith 
1953947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
1954f6a906c0SBarry Smith @*/
1955f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1956f6a906c0SBarry Smith {
1957f6a906c0SBarry Smith   PetscErrorCode ierr;
1958f6a906c0SBarry Smith 
1959f6a906c0SBarry Smith   PetscFunctionBegin;
1960f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1961f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
1962dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
1963d8151eeaSBarry Smith 
1964947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
1965d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
1966d8151eeaSBarry Smith     if (ts->vecs_sensip){
1967d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
1968d8151eeaSBarry Smith     }
1969947abb85SHong Zhang   }
1970d8151eeaSBarry Smith 
197142f2b339SBarry Smith   if (ts->ops->adjointsetup) {
197242f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
1973f6a906c0SBarry Smith   }
1974f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1975f6a906c0SBarry Smith   PetscFunctionReturn(0);
1976f6a906c0SBarry Smith }
1977f6a906c0SBarry Smith 
1978f6a906c0SBarry Smith #undef __FUNCT__
1979277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1980277b19d0SLisandro Dalcin /*@
1981277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1982277b19d0SLisandro Dalcin 
1983277b19d0SLisandro Dalcin    Collective on TS
1984277b19d0SLisandro Dalcin 
1985277b19d0SLisandro Dalcin    Input Parameter:
1986277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1987277b19d0SLisandro Dalcin 
1988277b19d0SLisandro Dalcin    Level: beginner
1989277b19d0SLisandro Dalcin 
1990277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1991277b19d0SLisandro Dalcin 
1992277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1993277b19d0SLisandro Dalcin @*/
1994277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1995277b19d0SLisandro Dalcin {
1996277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1997277b19d0SLisandro Dalcin 
1998277b19d0SLisandro Dalcin   PetscFunctionBegin;
1999277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2000b18ea86cSHong Zhang 
2001277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2002277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2003277b19d0SLisandro Dalcin   }
2004277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2005e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2006bbd56ea5SKarl Rupp 
20074e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
20084e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2009214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
20106bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2011e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2012e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
201338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2014bbd56ea5SKarl Rupp 
2015947abb85SHong Zhang  if (ts->vec_costintegral) {
2016d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2017d8151eeaSBarry Smith     if (ts->vecs_drdp){
2018d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2019d8151eeaSBarry Smith     }
2020947abb85SHong Zhang   }
2021d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2022d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2023ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
20242c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
202536eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2026277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2027d763cef2SBarry Smith   PetscFunctionReturn(0);
2028d763cef2SBarry Smith }
2029d763cef2SBarry Smith 
20304a2ae208SSatish Balay #undef __FUNCT__
20314a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2032d8e5e3e6SSatish Balay /*@
2033d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2034d763cef2SBarry Smith    with TSCreate().
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith    Collective on TS
2037d763cef2SBarry Smith 
2038d763cef2SBarry Smith    Input Parameter:
2039d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith    Level: beginner
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2044d763cef2SBarry Smith 
2045d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2046d763cef2SBarry Smith @*/
20476bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2048d763cef2SBarry Smith {
20496849ba73SBarry Smith   PetscErrorCode ierr;
2050d763cef2SBarry Smith 
2051d763cef2SBarry Smith   PetscFunctionBegin;
20526bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
20536bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
20546bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2055d763cef2SBarry Smith 
20566bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2057277b19d0SLisandro Dalcin 
2058e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2059e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20606bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20616d4c513bSLisandro Dalcin 
2062bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2063bc952696SBarry Smith 
206484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2065aeb4809dSShri Abhyankar   if ((*ts)->event) {
2066aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2067aeb4809dSShri Abhyankar   }
20686bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20696bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20706bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20710dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
20726d4c513bSLisandro Dalcin 
2073a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2074d763cef2SBarry Smith   PetscFunctionReturn(0);
2075d763cef2SBarry Smith }
2076d763cef2SBarry Smith 
20774a2ae208SSatish Balay #undef __FUNCT__
20784a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2079d8e5e3e6SSatish Balay /*@
2080d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2081d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2082d763cef2SBarry Smith 
2083d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2084d763cef2SBarry Smith 
2085d763cef2SBarry Smith    Input Parameter:
2086d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith    Output Parameter:
2089d763cef2SBarry Smith .  snes - the nonlinear solver context
2090d763cef2SBarry Smith 
2091d763cef2SBarry Smith    Notes:
2092d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2093d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
209494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2095d763cef2SBarry Smith 
2096d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20970298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2098d763cef2SBarry Smith 
2099d763cef2SBarry Smith    Level: beginner
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith .keywords: timestep, get, SNES
2102d763cef2SBarry Smith @*/
21037087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2104d763cef2SBarry Smith {
2105d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2106d372ba47SLisandro Dalcin 
2107d763cef2SBarry Smith   PetscFunctionBegin;
21080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21094482741eSBarry Smith   PetscValidPointer(snes,2);
2110d372ba47SLisandro Dalcin   if (!ts->snes) {
2111ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
21120298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21133bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2114d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2115496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
21169e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
21179e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
21189e2a6581SJed Brown     }
2119d372ba47SLisandro Dalcin   }
2120d763cef2SBarry Smith   *snes = ts->snes;
2121d763cef2SBarry Smith   PetscFunctionReturn(0);
2122d763cef2SBarry Smith }
2123d763cef2SBarry Smith 
21244a2ae208SSatish Balay #undef __FUNCT__
2125deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2126deb2cd25SJed Brown /*@
2127deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2128deb2cd25SJed Brown 
2129deb2cd25SJed Brown    Collective
2130deb2cd25SJed Brown 
2131deb2cd25SJed Brown    Input Parameter:
2132deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2133deb2cd25SJed Brown -  snes - the nonlinear solver context
2134deb2cd25SJed Brown 
2135deb2cd25SJed Brown    Notes:
2136deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2137deb2cd25SJed Brown 
2138deb2cd25SJed Brown    Level: developer
2139deb2cd25SJed Brown 
2140deb2cd25SJed Brown .keywords: timestep, set, SNES
2141deb2cd25SJed Brown @*/
2142deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2143deb2cd25SJed Brown {
2144deb2cd25SJed Brown   PetscErrorCode ierr;
2145d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2146deb2cd25SJed Brown 
2147deb2cd25SJed Brown   PetscFunctionBegin;
2148deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2149deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2150deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2151deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2152bbd56ea5SKarl Rupp 
2153deb2cd25SJed Brown   ts->snes = snes;
2154bbd56ea5SKarl Rupp 
21550298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21560298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2157740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21580298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2159740132f1SEmil Constantinescu   }
2160deb2cd25SJed Brown   PetscFunctionReturn(0);
2161deb2cd25SJed Brown }
2162deb2cd25SJed Brown 
2163deb2cd25SJed Brown #undef __FUNCT__
216494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2165d8e5e3e6SSatish Balay /*@
216694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2167d763cef2SBarry Smith    a TS (timestepper) context.
2168d763cef2SBarry Smith 
216994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2170d763cef2SBarry Smith 
2171d763cef2SBarry Smith    Input Parameter:
2172d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Output Parameter:
217594b7f48cSBarry Smith .  ksp - the nonlinear solver context
2176d763cef2SBarry Smith 
2177d763cef2SBarry Smith    Notes:
217894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2179d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2180d763cef2SBarry Smith    KSP and PC contexts as well.
2181d763cef2SBarry Smith 
218294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21830298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith    Level: beginner
2186d763cef2SBarry Smith 
218794b7f48cSBarry Smith .keywords: timestep, get, KSP
2188d763cef2SBarry Smith @*/
21897087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2190d763cef2SBarry Smith {
2191d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2192089b2837SJed Brown   SNES           snes;
2193d372ba47SLisandro Dalcin 
2194d763cef2SBarry Smith   PetscFunctionBegin;
21950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21964482741eSBarry Smith   PetscValidPointer(ksp,2);
219717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2198e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2199089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2200089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2201d763cef2SBarry Smith   PetscFunctionReturn(0);
2202d763cef2SBarry Smith }
2203d763cef2SBarry Smith 
2204d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2205d763cef2SBarry Smith 
22064a2ae208SSatish Balay #undef __FUNCT__
2207adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2208adb62b0dSMatthew Knepley /*@
2209adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2210adb62b0dSMatthew Knepley    maximum time for iteration.
2211adb62b0dSMatthew Knepley 
22123f9fe445SBarry Smith    Not Collective
2213adb62b0dSMatthew Knepley 
2214adb62b0dSMatthew Knepley    Input Parameters:
2215adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
22160298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
22170298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2218adb62b0dSMatthew Knepley 
2219adb62b0dSMatthew Knepley    Level: intermediate
2220adb62b0dSMatthew Knepley 
2221adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2222adb62b0dSMatthew Knepley @*/
22237087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2224adb62b0dSMatthew Knepley {
2225adb62b0dSMatthew Knepley   PetscFunctionBegin;
22260700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2227abc0a331SBarry Smith   if (maxsteps) {
22284482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2229adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2230adb62b0dSMatthew Knepley   }
2231abc0a331SBarry Smith   if (maxtime) {
22324482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2233adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2234adb62b0dSMatthew Knepley   }
2235adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2236adb62b0dSMatthew Knepley }
2237adb62b0dSMatthew Knepley 
2238adb62b0dSMatthew Knepley #undef __FUNCT__
22394a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2240d763cef2SBarry Smith /*@
2241d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2242d763cef2SBarry Smith    maximum time for iteration.
2243d763cef2SBarry Smith 
22443f9fe445SBarry Smith    Logically Collective on TS
2245d763cef2SBarry Smith 
2246d763cef2SBarry Smith    Input Parameters:
2247d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2248d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2249d763cef2SBarry Smith -  maxtime - final time to iterate to
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith    Options Database Keys:
2252d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
22533bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2254d763cef2SBarry Smith 
2255d763cef2SBarry Smith    Notes:
2256d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith    Level: intermediate
2259d763cef2SBarry Smith 
2260d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2261a43b19c4SJed Brown 
2262a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2263d763cef2SBarry Smith @*/
22647087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2265d763cef2SBarry Smith {
2266d763cef2SBarry Smith   PetscFunctionBegin;
22670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2268c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2269c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
227039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
227139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2272d763cef2SBarry Smith   PetscFunctionReturn(0);
2273d763cef2SBarry Smith }
2274d763cef2SBarry Smith 
22754a2ae208SSatish Balay #undef __FUNCT__
22764a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2277d763cef2SBarry Smith /*@
2278d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2279d763cef2SBarry Smith    for use by the TS routines.
2280d763cef2SBarry Smith 
22813f9fe445SBarry Smith    Logically Collective on TS and Vec
2282d763cef2SBarry Smith 
2283d763cef2SBarry Smith    Input Parameters:
2284d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22850910c330SBarry Smith -  u - the solution vector
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Level: beginner
2288d763cef2SBarry Smith 
2289d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2290d763cef2SBarry Smith @*/
22910910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2292d763cef2SBarry Smith {
22938737fe31SLisandro Dalcin   PetscErrorCode ierr;
2294496e6a7aSJed Brown   DM             dm;
22958737fe31SLisandro Dalcin 
2296d763cef2SBarry Smith   PetscFunctionBegin;
22970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22980910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22990910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
23006bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2301bbd56ea5SKarl Rupp 
23020910c330SBarry Smith   ts->vec_sol = u;
2303bbd56ea5SKarl Rupp 
2304496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
23050910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2306d763cef2SBarry Smith   PetscFunctionReturn(0);
2307d763cef2SBarry Smith }
2308d763cef2SBarry Smith 
2309e74ef692SMatthew Knepley #undef __FUNCT__
231073b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
231173b18844SBarry Smith /*@
231273b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
231373b18844SBarry Smith 
231473b18844SBarry Smith    Logically Collective on TS
231573b18844SBarry Smith 
231673b18844SBarry Smith    Input Parameters:
231773b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
231873b18844SBarry Smith .  steps - number of steps to use
231973b18844SBarry Smith 
232073b18844SBarry Smith    Level: intermediate
232173b18844SBarry Smith 
232273b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
232373b18844SBarry Smith           so as to integrate back to less than the original timestep
232473b18844SBarry Smith 
232573b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
232673b18844SBarry Smith 
232773b18844SBarry Smith .seealso: TSSetExactFinalTime()
232873b18844SBarry Smith @*/
232973b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
233073b18844SBarry Smith {
233173b18844SBarry Smith   PetscFunctionBegin;
233273b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
233373b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
233473b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
233573b18844SBarry Smith   if (steps > ts->total_steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
233673b18844SBarry Smith   ts->adjoint_max_steps = steps;
233773b18844SBarry Smith   PetscFunctionReturn(0);
233873b18844SBarry Smith }
233973b18844SBarry Smith 
234073b18844SBarry Smith #undef __FUNCT__
2341dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2342c235aa8dSHong Zhang /*@
2343dfb21088SHong Zhang    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters
23440cf82b59SBarry Smith       for use by the TSAdjoint routines.
2345c235aa8dSHong Zhang 
2346c235aa8dSHong Zhang    Logically Collective on TS and Vec
2347c235aa8dSHong Zhang 
2348c235aa8dSHong Zhang    Input Parameters:
2349c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2350abc2977eSBarry Smith .  lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
2351abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2352c235aa8dSHong Zhang 
2353c235aa8dSHong Zhang    Level: beginner
2354c235aa8dSHong Zhang 
2355abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
23560cf82b59SBarry Smith 
2357c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2358c235aa8dSHong Zhang @*/
2359dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2360c235aa8dSHong Zhang {
2361c235aa8dSHong Zhang   PetscFunctionBegin;
2362c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2363abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2364abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2365abc2977eSBarry Smith   ts->vecs_sensip = mu;
2366dfb21088SHong Zhang   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand");
2367abc2977eSBarry Smith   ts->numcost  = numcost;
2368ad8e2604SHong Zhang   PetscFunctionReturn(0);
2369ad8e2604SHong Zhang }
2370ad8e2604SHong Zhang 
2371ad8e2604SHong Zhang #undef __FUNCT__
23725bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2373ad8e2604SHong Zhang /*@C
23740cf82b59SBarry Smith   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix.
2375ad8e2604SHong Zhang 
2376ad8e2604SHong Zhang   Logically Collective on TS
2377ad8e2604SHong Zhang 
2378ad8e2604SHong Zhang   Input Parameters:
2379ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2380ad8e2604SHong Zhang - func - The function
2381ad8e2604SHong Zhang 
2382ad8e2604SHong Zhang   Calling sequence of func:
23830cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
238405755b9cSHong Zhang +   t - current timestep
23850cf82b59SBarry Smith .   y - input vector (current ODE solution)
238605755b9cSHong Zhang .   A - output matrix
238705755b9cSHong Zhang -   ctx - [optional] user-defined function context
2388ad8e2604SHong Zhang 
2389ad8e2604SHong Zhang   Level: intermediate
2390ad8e2604SHong Zhang 
23910cf82b59SBarry Smith   Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p
23920cf82b59SBarry Smith 
2393ad8e2604SHong Zhang .keywords: TS, sensitivity
2394ad8e2604SHong Zhang .seealso:
2395ad8e2604SHong Zhang @*/
23965bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2397ad8e2604SHong Zhang {
2398ad8e2604SHong Zhang   PetscErrorCode ierr;
2399ad8e2604SHong Zhang 
2400ad8e2604SHong Zhang   PetscFunctionBegin;
2401ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2402ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2403ad8e2604SHong Zhang 
2404ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2405ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2406ad8e2604SHong Zhang   if(Amat) {
2407ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2408ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2409ad8e2604SHong Zhang     ts->Jacp = Amat;
2410ad8e2604SHong Zhang   }
2411ad8e2604SHong Zhang   PetscFunctionReturn(0);
2412ad8e2604SHong Zhang }
2413ad8e2604SHong Zhang 
2414ad8e2604SHong Zhang #undef __FUNCT__
24155bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
24160cf82b59SBarry Smith /*@C
24175bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2418ad8e2604SHong Zhang 
2419ad8e2604SHong Zhang   Collective on TS
2420ad8e2604SHong Zhang 
2421ad8e2604SHong Zhang   Input Parameters:
2422ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2423ad8e2604SHong Zhang 
2424ad8e2604SHong Zhang   Level: developer
2425ad8e2604SHong Zhang 
2426ad8e2604SHong Zhang .keywords: TS, sensitivity
24275bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2428ad8e2604SHong Zhang @*/
24295bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2430ad8e2604SHong Zhang {
2431ad8e2604SHong Zhang   PetscErrorCode ierr;
2432ad8e2604SHong Zhang 
2433ad8e2604SHong Zhang   PetscFunctionBegin;
2434ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2435ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2436ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2437ad8e2604SHong Zhang 
2438ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2439ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2440ad8e2604SHong Zhang   PetscStackPop;
2441c235aa8dSHong Zhang   PetscFunctionReturn(0);
2442c235aa8dSHong Zhang }
2443c235aa8dSHong Zhang 
2444c235aa8dSHong Zhang #undef __FUNCT__
2445dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
24466fd0887fSHong Zhang /*@C
2447dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
24486fd0887fSHong Zhang 
24496fd0887fSHong Zhang     Logically Collective on TS
24506fd0887fSHong Zhang 
24516fd0887fSHong Zhang     Input Parameters:
24526fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2453abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
24540cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2455b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2456b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2457b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
24586fd0887fSHong Zhang 
24590cf82b59SBarry Smith     Calling sequence of rf:
24600cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
24616fd0887fSHong Zhang 
24626fd0887fSHong Zhang +   t - current timestep
24630cf82b59SBarry Smith .   y - input vector
24640cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
24656fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24666fd0887fSHong Zhang 
2467b612ec54SBarry Smith    Calling sequence of drdyf:
24680cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
2469b612ec54SBarry Smith 
2470b612ec54SBarry Smith    Calling sequence of drdpf:
24710cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
2472b612ec54SBarry Smith 
2473b612ec54SBarry Smith     Level: intermediate
24746fd0887fSHong Zhang 
2475abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
24760cf82b59SBarry Smith 
24776fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24786fd0887fSHong Zhang 
2479dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
24806fd0887fSHong Zhang @*/
2481dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
2482d8151eeaSBarry Smith                                                                   PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2483d8151eeaSBarry Smith                                                                   PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
24846fd0887fSHong Zhang {
24856fd0887fSHong Zhang   PetscErrorCode ierr;
24866fd0887fSHong Zhang 
24876fd0887fSHong Zhang   PetscFunctionBegin;
24886fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2489dfb21088SHong Zhang   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()");
2490c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
24916fd0887fSHong Zhang 
2492abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
24932c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
24940cf82b59SBarry Smith   ts->costintegrand    = rf;
249505755b9cSHong Zhang   ts->costintegrandctx = ctx;
2496b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
2497b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
24986fd0887fSHong Zhang   PetscFunctionReturn(0);
24996fd0887fSHong Zhang }
25006fd0887fSHong Zhang 
25016fd0887fSHong Zhang #undef __FUNCT__
2502dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
250336eaed60SHong Zhang /*@
2504dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
250536eaed60SHong Zhang    It is valid to call the routine after a backward run.
250636eaed60SHong Zhang 
250736eaed60SHong Zhang    Not Collective
250836eaed60SHong Zhang 
250936eaed60SHong Zhang    Input Parameter:
251036eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
251136eaed60SHong Zhang 
251236eaed60SHong Zhang    Output Parameter:
25132c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
251436eaed60SHong Zhang 
251536eaed60SHong Zhang    Level: intermediate
251636eaed60SHong Zhang 
2517dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
251836eaed60SHong Zhang 
251936eaed60SHong Zhang .keywords: TS, sensitivity analysis
252036eaed60SHong Zhang @*/
2521dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
252236eaed60SHong Zhang {
252336eaed60SHong Zhang   PetscFunctionBegin;
252436eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
252536eaed60SHong Zhang   PetscValidPointer(v,2);
25262c39e106SBarry Smith   *v = ts->vec_costintegral;
252736eaed60SHong Zhang   PetscFunctionReturn(0);
252836eaed60SHong Zhang }
252936eaed60SHong Zhang 
253036eaed60SHong Zhang #undef __FUNCT__
2531d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
25326fd0887fSHong Zhang /*@
25332c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
25346fd0887fSHong Zhang 
25356fd0887fSHong Zhang    Input Parameters:
25366fd0887fSHong Zhang +  ts - the TS context
25376fd0887fSHong Zhang .  t - current time
25380cf82b59SBarry Smith -  y - state vector, i.e. current solution
25396fd0887fSHong Zhang 
25406fd0887fSHong Zhang    Output Parameter:
2541abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
25426fd0887fSHong Zhang 
25436fd0887fSHong Zhang    Note:
25446fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
25456fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
25466fd0887fSHong Zhang 
25476fd0887fSHong Zhang    Level: developer
25486fd0887fSHong Zhang 
25496fd0887fSHong Zhang .keywords: TS, compute
25506fd0887fSHong Zhang 
2551dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
25526fd0887fSHong Zhang @*/
25530cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
25546fd0887fSHong Zhang {
25556fd0887fSHong Zhang   PetscErrorCode ierr;
25566fd0887fSHong Zhang 
25576fd0887fSHong Zhang   PetscFunctionBegin;
25586fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25590cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
25606fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
25616fd0887fSHong Zhang 
25620cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
2563e4680132SHong Zhang   if (ts->costintegrand) {
2564e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
25650cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
25666fd0887fSHong Zhang     PetscStackPop;
25676fd0887fSHong Zhang   } else {
25686fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25696fd0887fSHong Zhang   }
25706fd0887fSHong Zhang 
25710cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
25726fd0887fSHong Zhang   PetscFunctionReturn(0);
25736fd0887fSHong Zhang }
25746fd0887fSHong Zhang 
25756fd0887fSHong Zhang #undef __FUNCT__
2576d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
257705755b9cSHong Zhang /*@
2578d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
257905755b9cSHong Zhang 
258005755b9cSHong Zhang   Collective on TS
258105755b9cSHong Zhang 
258205755b9cSHong Zhang   Input Parameters:
258305755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
258405755b9cSHong Zhang 
258505755b9cSHong Zhang   Notes:
2586d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
258705755b9cSHong Zhang   so most users would not generally call this routine themselves.
258805755b9cSHong Zhang 
258905755b9cSHong Zhang   Level: developer
259005755b9cSHong Zhang 
259105755b9cSHong Zhang .keywords: TS, sensitivity
2592d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
259305755b9cSHong Zhang @*/
25940cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
259505755b9cSHong Zhang {
259605755b9cSHong Zhang   PetscErrorCode ierr;
259705755b9cSHong Zhang 
259805755b9cSHong Zhang   PetscFunctionBegin;
259905755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26000cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
260105755b9cSHong Zhang 
260205755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
26030cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
260405755b9cSHong Zhang   PetscStackPop;
260505755b9cSHong Zhang   PetscFunctionReturn(0);
260605755b9cSHong Zhang }
260705755b9cSHong Zhang 
260805755b9cSHong Zhang #undef __FUNCT__
2609d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
261005755b9cSHong Zhang /*@
2611d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
261205755b9cSHong Zhang 
261305755b9cSHong Zhang   Collective on TS
261405755b9cSHong Zhang 
261505755b9cSHong Zhang   Input Parameters:
261605755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
261705755b9cSHong Zhang 
261805755b9cSHong Zhang   Notes:
261905755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
262005755b9cSHong Zhang   so most users would not generally call this routine themselves.
262105755b9cSHong Zhang 
262205755b9cSHong Zhang   Level: developer
262305755b9cSHong Zhang 
262405755b9cSHong Zhang .keywords: TS, sensitivity
2625d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
262605755b9cSHong Zhang @*/
26270cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
262805755b9cSHong Zhang {
262905755b9cSHong Zhang   PetscErrorCode ierr;
263005755b9cSHong Zhang 
263105755b9cSHong Zhang   PetscFunctionBegin;
263205755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26330cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
263405755b9cSHong Zhang 
263505755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
26360cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
263705755b9cSHong Zhang   PetscStackPop;
263805755b9cSHong Zhang   PetscFunctionReturn(0);
263905755b9cSHong Zhang }
264005755b9cSHong Zhang 
264105755b9cSHong Zhang #undef __FUNCT__
2642e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2643ac226902SBarry Smith /*@C
2644000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
26453f2090d5SJed Brown   called once at the beginning of each time step.
2646000e7ae3SMatthew Knepley 
26473f9fe445SBarry Smith   Logically Collective on TS
2648000e7ae3SMatthew Knepley 
2649000e7ae3SMatthew Knepley   Input Parameters:
2650000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2651000e7ae3SMatthew Knepley - func - The function
2652000e7ae3SMatthew Knepley 
2653000e7ae3SMatthew Knepley   Calling sequence of func:
2654000e7ae3SMatthew Knepley . func (TS ts);
2655000e7ae3SMatthew Knepley 
2656000e7ae3SMatthew Knepley   Level: intermediate
2657000e7ae3SMatthew Knepley 
2658b8123daeSJed Brown   Note:
2659b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2660b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2661b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2662b8123daeSJed Brown 
2663000e7ae3SMatthew Knepley .keywords: TS, timestep
26649be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2665000e7ae3SMatthew Knepley @*/
26667087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2667000e7ae3SMatthew Knepley {
2668000e7ae3SMatthew Knepley   PetscFunctionBegin;
26690700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2670ae60f76fSBarry Smith   ts->prestep = func;
2671000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2672000e7ae3SMatthew Knepley }
2673000e7ae3SMatthew Knepley 
2674e74ef692SMatthew Knepley #undef __FUNCT__
26753f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
267609ee8438SJed Brown /*@
26773f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26783f2090d5SJed Brown 
26793f2090d5SJed Brown   Collective on TS
26803f2090d5SJed Brown 
26813f2090d5SJed Brown   Input Parameters:
26823f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26833f2090d5SJed Brown 
26843f2090d5SJed Brown   Notes:
26853f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26863f2090d5SJed Brown   so most users would not generally call this routine themselves.
26873f2090d5SJed Brown 
26883f2090d5SJed Brown   Level: developer
26893f2090d5SJed Brown 
26903f2090d5SJed Brown .keywords: TS, timestep
26919be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26923f2090d5SJed Brown @*/
26937087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26943f2090d5SJed Brown {
26953f2090d5SJed Brown   PetscErrorCode ierr;
26963f2090d5SJed Brown 
26973f2090d5SJed Brown   PetscFunctionBegin;
26980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2699ae60f76fSBarry Smith   if (ts->prestep) {
2700ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2701312ce896SJed Brown   }
27023f2090d5SJed Brown   PetscFunctionReturn(0);
27033f2090d5SJed Brown }
27043f2090d5SJed Brown 
27053f2090d5SJed Brown #undef __FUNCT__
2706b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2707b8123daeSJed Brown /*@C
2708b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2709b8123daeSJed Brown   called once at the beginning of each stage.
2710b8123daeSJed Brown 
2711b8123daeSJed Brown   Logically Collective on TS
2712b8123daeSJed Brown 
2713b8123daeSJed Brown   Input Parameters:
2714b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2715b8123daeSJed Brown - func - The function
2716b8123daeSJed Brown 
2717b8123daeSJed Brown   Calling sequence of func:
2718b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2719b8123daeSJed Brown 
2720b8123daeSJed Brown   Level: intermediate
2721b8123daeSJed Brown 
2722b8123daeSJed Brown   Note:
2723b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2724b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2725b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2726b8123daeSJed Brown 
2727b8123daeSJed Brown .keywords: TS, timestep
27289be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2729b8123daeSJed Brown @*/
2730b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2731b8123daeSJed Brown {
2732b8123daeSJed Brown   PetscFunctionBegin;
2733b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2734ae60f76fSBarry Smith   ts->prestage = func;
2735b8123daeSJed Brown   PetscFunctionReturn(0);
2736b8123daeSJed Brown }
2737b8123daeSJed Brown 
2738b8123daeSJed Brown #undef __FUNCT__
27399be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
27409be3e283SDebojyoti Ghosh /*@C
27419be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
27429be3e283SDebojyoti Ghosh   called once at the end of each stage.
27439be3e283SDebojyoti Ghosh 
27449be3e283SDebojyoti Ghosh   Logically Collective on TS
27459be3e283SDebojyoti Ghosh 
27469be3e283SDebojyoti Ghosh   Input Parameters:
27479be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
27489be3e283SDebojyoti Ghosh - func - The function
27499be3e283SDebojyoti Ghosh 
27509be3e283SDebojyoti Ghosh   Calling sequence of func:
27519be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27529be3e283SDebojyoti Ghosh 
27539be3e283SDebojyoti Ghosh   Level: intermediate
27549be3e283SDebojyoti Ghosh 
27559be3e283SDebojyoti Ghosh   Note:
27569be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27579be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27589be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27599be3e283SDebojyoti Ghosh 
27609be3e283SDebojyoti Ghosh .keywords: TS, timestep
27619be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27629be3e283SDebojyoti Ghosh @*/
27639be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27649be3e283SDebojyoti Ghosh {
27659be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27669be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27679be3e283SDebojyoti Ghosh   ts->poststage = func;
27689be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27699be3e283SDebojyoti Ghosh }
27709be3e283SDebojyoti Ghosh 
27719be3e283SDebojyoti Ghosh #undef __FUNCT__
2772b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2773b8123daeSJed Brown /*@
2774b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2775b8123daeSJed Brown 
2776b8123daeSJed Brown   Collective on TS
2777b8123daeSJed Brown 
2778b8123daeSJed Brown   Input Parameters:
2779b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27809be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2781b8123daeSJed Brown 
2782b8123daeSJed Brown   Notes:
2783b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2784b8123daeSJed Brown   most users would not generally call this routine themselves.
2785b8123daeSJed Brown 
2786b8123daeSJed Brown   Level: developer
2787b8123daeSJed Brown 
2788b8123daeSJed Brown .keywords: TS, timestep
27899be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2790b8123daeSJed Brown @*/
2791b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2792b8123daeSJed Brown {
2793b8123daeSJed Brown   PetscErrorCode ierr;
2794b8123daeSJed Brown 
2795b8123daeSJed Brown   PetscFunctionBegin;
2796b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2797ae60f76fSBarry Smith   if (ts->prestage) {
2798ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2799b8123daeSJed Brown   }
2800b8123daeSJed Brown   PetscFunctionReturn(0);
2801b8123daeSJed Brown }
2802b8123daeSJed Brown 
2803b8123daeSJed Brown #undef __FUNCT__
28049be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
28059be3e283SDebojyoti Ghosh /*@
28069be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
28079be3e283SDebojyoti Ghosh 
28089be3e283SDebojyoti Ghosh   Collective on TS
28099be3e283SDebojyoti Ghosh 
28109be3e283SDebojyoti Ghosh   Input Parameters:
28119be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
28129be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
28139be3e283SDebojyoti Ghosh   stageindex  - Stage number
28149be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
28159be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
28169be3e283SDebojyoti Ghosh 
28179be3e283SDebojyoti Ghosh   Notes:
28189be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
28199be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
28209be3e283SDebojyoti Ghosh 
28219be3e283SDebojyoti Ghosh   Level: developer
28229be3e283SDebojyoti Ghosh 
28239be3e283SDebojyoti Ghosh .keywords: TS, timestep
28249be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
28259be3e283SDebojyoti Ghosh @*/
28269be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
28279be3e283SDebojyoti Ghosh {
28289be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
28299be3e283SDebojyoti Ghosh 
28309be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28319be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28324beae5d8SLisandro Dalcin   if (ts->poststage) {
28339be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
28349be3e283SDebojyoti Ghosh   }
28359be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28369be3e283SDebojyoti Ghosh }
28379be3e283SDebojyoti Ghosh 
28389be3e283SDebojyoti Ghosh #undef __FUNCT__
2839e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2840ac226902SBarry Smith /*@C
2841000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
28423f2090d5SJed Brown   called once at the end of each time step.
2843000e7ae3SMatthew Knepley 
28443f9fe445SBarry Smith   Logically Collective on TS
2845000e7ae3SMatthew Knepley 
2846000e7ae3SMatthew Knepley   Input Parameters:
2847000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2848000e7ae3SMatthew Knepley - func - The function
2849000e7ae3SMatthew Knepley 
2850000e7ae3SMatthew Knepley   Calling sequence of func:
2851b8123daeSJed Brown $ func (TS ts);
2852000e7ae3SMatthew Knepley 
2853000e7ae3SMatthew Knepley   Level: intermediate
2854000e7ae3SMatthew Knepley 
2855000e7ae3SMatthew Knepley .keywords: TS, timestep
2856b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2857000e7ae3SMatthew Knepley @*/
28587087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2859000e7ae3SMatthew Knepley {
2860000e7ae3SMatthew Knepley   PetscFunctionBegin;
28610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2862ae60f76fSBarry Smith   ts->poststep = func;
2863000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2864000e7ae3SMatthew Knepley }
2865000e7ae3SMatthew Knepley 
2866e74ef692SMatthew Knepley #undef __FUNCT__
28673f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
286809ee8438SJed Brown /*@
28693f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28703f2090d5SJed Brown 
28713f2090d5SJed Brown   Collective on TS
28723f2090d5SJed Brown 
28733f2090d5SJed Brown   Input Parameters:
28743f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28753f2090d5SJed Brown 
28763f2090d5SJed Brown   Notes:
28773f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28783f2090d5SJed Brown   so most users would not generally call this routine themselves.
28793f2090d5SJed Brown 
28803f2090d5SJed Brown   Level: developer
28813f2090d5SJed Brown 
28823f2090d5SJed Brown .keywords: TS, timestep
28833f2090d5SJed Brown @*/
28847087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28853f2090d5SJed Brown {
28863f2090d5SJed Brown   PetscErrorCode ierr;
28873f2090d5SJed Brown 
28883f2090d5SJed Brown   PetscFunctionBegin;
28890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2890ae60f76fSBarry Smith   if (ts->poststep) {
2891ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
289272ac3e02SJed Brown   }
28933f2090d5SJed Brown   PetscFunctionReturn(0);
28943f2090d5SJed Brown }
28953f2090d5SJed Brown 
2896d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2897d763cef2SBarry Smith 
28984a2ae208SSatish Balay #undef __FUNCT__
2899a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2900d763cef2SBarry Smith /*@C
2901a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2902d763cef2SBarry Smith    timestep to display the iteration's  progress.
2903d763cef2SBarry Smith 
29043f9fe445SBarry Smith    Logically Collective on TS
2905d763cef2SBarry Smith 
2906d763cef2SBarry Smith    Input Parameters:
2907d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2908e213d8f1SJed Brown .  monitor - monitoring routine
2909329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
29100298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2911b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
29120298fd71SBarry Smith           (may be NULL)
2913d763cef2SBarry Smith 
2914e213d8f1SJed Brown    Calling sequence of monitor:
29150910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2916d763cef2SBarry Smith 
2917d763cef2SBarry Smith +    ts - the TS context
291888c05cc5SBarry 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
291988c05cc5SBarry Smith                                been interpolated to)
29201f06c33eSBarry Smith .    time - current time
29210910c330SBarry Smith .    u - current iterate
2922d763cef2SBarry Smith -    mctx - [optional] monitoring context
2923d763cef2SBarry Smith 
2924d763cef2SBarry Smith    Notes:
2925d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2926d763cef2SBarry Smith    already has been loaded.
2927d763cef2SBarry Smith 
2928025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2929025f1a04SBarry Smith 
2930d763cef2SBarry Smith    Level: intermediate
2931d763cef2SBarry Smith 
2932d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2933d763cef2SBarry Smith 
2934a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2935d763cef2SBarry Smith @*/
2936c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2937d763cef2SBarry Smith {
2938d763cef2SBarry Smith   PetscFunctionBegin;
29390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
294017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2941d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
29428704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2943d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2944d763cef2SBarry Smith   PetscFunctionReturn(0);
2945d763cef2SBarry Smith }
2946d763cef2SBarry Smith 
29474a2ae208SSatish Balay #undef __FUNCT__
2948a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2949d763cef2SBarry Smith /*@C
2950a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2951d763cef2SBarry Smith 
29523f9fe445SBarry Smith    Logically Collective on TS
2953d763cef2SBarry Smith 
2954d763cef2SBarry Smith    Input Parameters:
2955d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2956d763cef2SBarry Smith 
2957d763cef2SBarry Smith    Notes:
2958d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2959d763cef2SBarry Smith 
2960d763cef2SBarry Smith    Level: intermediate
2961d763cef2SBarry Smith 
2962d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2963d763cef2SBarry Smith 
2964a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2965d763cef2SBarry Smith @*/
29667087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2967d763cef2SBarry Smith {
2968d952e501SBarry Smith   PetscErrorCode ierr;
2969d952e501SBarry Smith   PetscInt       i;
2970d952e501SBarry Smith 
2971d763cef2SBarry Smith   PetscFunctionBegin;
29720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2973d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29748704b422SBarry Smith     if (ts->monitordestroy[i]) {
29758704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2976d952e501SBarry Smith     }
2977d952e501SBarry Smith   }
2978d763cef2SBarry Smith   ts->numbermonitors = 0;
2979d763cef2SBarry Smith   PetscFunctionReturn(0);
2980d763cef2SBarry Smith }
2981d763cef2SBarry Smith 
29824a2ae208SSatish Balay #undef __FUNCT__
2983a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2984d8e5e3e6SSatish Balay /*@
2985a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29865516499fSSatish Balay 
29875516499fSSatish Balay    Level: intermediate
298841251cbbSSatish Balay 
29895516499fSSatish Balay .keywords: TS, set, monitor
29905516499fSSatish Balay 
299141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
299241251cbbSSatish Balay @*/
2993649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2994d763cef2SBarry Smith {
2995dfbe8321SBarry Smith   PetscErrorCode ierr;
29964d4332d5SBarry Smith   PetscViewer    viewer =  (PetscViewer) dummy;
2997d132466eSBarry Smith 
2998d763cef2SBarry Smith   PetscFunctionBegin;
29994d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3000649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
30018392e04aSShri Abhyankar   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
3002649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3003d763cef2SBarry Smith   PetscFunctionReturn(0);
3004d763cef2SBarry Smith }
3005d763cef2SBarry Smith 
30064a2ae208SSatish Balay #undef __FUNCT__
30079110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet"
30089110b2e7SHong Zhang /*@C
30099110b2e7SHong Zhang    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
30109110b2e7SHong Zhang    timestep to display the iteration's  progress.
30119110b2e7SHong Zhang 
30129110b2e7SHong Zhang    Logically Collective on TS
30139110b2e7SHong Zhang 
30149110b2e7SHong Zhang    Input Parameters:
30159110b2e7SHong Zhang +  ts - the TS context obtained from TSCreate()
30169110b2e7SHong Zhang .  adjointmonitor - monitoring routine
30179110b2e7SHong Zhang .  adjointmctx - [optional] user-defined context for private data for the
30189110b2e7SHong Zhang              monitor routine (use NULL if no context is desired)
30199110b2e7SHong Zhang -  adjointmonitordestroy - [optional] routine that frees monitor context
30209110b2e7SHong Zhang           (may be NULL)
30219110b2e7SHong Zhang 
30229110b2e7SHong Zhang    Calling sequence of monitor:
30239110b2e7SHong Zhang $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)
30249110b2e7SHong Zhang 
30259110b2e7SHong Zhang +    ts - the TS context
30269110b2e7SHong Zhang .    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
30279110b2e7SHong Zhang                                been interpolated to)
30289110b2e7SHong Zhang .    time - current time
30299110b2e7SHong Zhang .    u - current iterate
30309110b2e7SHong Zhang .    numcost - number of cost functionos
30319110b2e7SHong Zhang .    lambda - sensitivities to initial conditions
30329110b2e7SHong Zhang .    mu - sensitivities to parameters
30339110b2e7SHong Zhang -    adjointmctx - [optional] adjoint monitoring context
30349110b2e7SHong Zhang 
30359110b2e7SHong Zhang    Notes:
30369110b2e7SHong Zhang    This routine adds an additional monitor to the list of monitors that
30379110b2e7SHong Zhang    already has been loaded.
30389110b2e7SHong Zhang 
30399110b2e7SHong Zhang    Fortran notes: Only a single monitor function can be set for each TS object
30409110b2e7SHong Zhang 
30419110b2e7SHong Zhang    Level: intermediate
30429110b2e7SHong Zhang 
30439110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
30449110b2e7SHong Zhang 
30459110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel()
30469110b2e7SHong Zhang @*/
30479110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
30489110b2e7SHong Zhang {
30499110b2e7SHong Zhang   PetscFunctionBegin;
30509110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30519110b2e7SHong Zhang   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
30529110b2e7SHong Zhang   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
30539110b2e7SHong Zhang   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
30549110b2e7SHong Zhang   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
30559110b2e7SHong Zhang   PetscFunctionReturn(0);
30569110b2e7SHong Zhang }
30579110b2e7SHong Zhang 
30589110b2e7SHong Zhang #undef __FUNCT__
30599110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel"
30609110b2e7SHong Zhang /*@C
30619110b2e7SHong Zhang    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.
30629110b2e7SHong Zhang 
30639110b2e7SHong Zhang    Logically Collective on TS
30649110b2e7SHong Zhang 
30659110b2e7SHong Zhang    Input Parameters:
30669110b2e7SHong Zhang .  ts - the TS context obtained from TSCreate()
30679110b2e7SHong Zhang 
30689110b2e7SHong Zhang    Notes:
30699110b2e7SHong Zhang    There is no way to remove a single, specific monitor.
30709110b2e7SHong Zhang 
30719110b2e7SHong Zhang    Level: intermediate
30729110b2e7SHong Zhang 
30739110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor
30749110b2e7SHong Zhang 
30759110b2e7SHong Zhang .seealso: TSAdjointMonitorSet()
30769110b2e7SHong Zhang @*/
30779110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorCancel(TS ts)
30789110b2e7SHong Zhang {
30799110b2e7SHong Zhang   PetscErrorCode ierr;
30809110b2e7SHong Zhang   PetscInt       i;
30819110b2e7SHong Zhang 
30829110b2e7SHong Zhang   PetscFunctionBegin;
30839110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30849110b2e7SHong Zhang   for (i=0; i<ts->numberadjointmonitors; i++) {
30859110b2e7SHong Zhang     if (ts->adjointmonitordestroy[i]) {
30869110b2e7SHong Zhang       ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
30879110b2e7SHong Zhang     }
30889110b2e7SHong Zhang   }
30899110b2e7SHong Zhang   ts->numberadjointmonitors = 0;
30909110b2e7SHong Zhang   PetscFunctionReturn(0);
30919110b2e7SHong Zhang }
30929110b2e7SHong Zhang 
30939110b2e7SHong Zhang #undef __FUNCT__
3094110eb670SHong Zhang #define __FUNCT__ "TSAdjointMonitorDefault"
3095110eb670SHong Zhang /*@
3096110eb670SHong Zhang    TSAdjointMonitorDefault - Sets the Default monitor
3097110eb670SHong Zhang 
3098110eb670SHong Zhang    Level: intermediate
3099110eb670SHong Zhang 
3100110eb670SHong Zhang .keywords: TS, set, monitor
3101110eb670SHong Zhang 
3102110eb670SHong Zhang .seealso: TSAdjointMonitorSet()
3103110eb670SHong Zhang @*/
3104110eb670SHong Zhang PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
3105110eb670SHong Zhang {
3106110eb670SHong Zhang   PetscErrorCode ierr;
3107110eb670SHong Zhang   PetscViewer    viewer =  (PetscViewer) dummy;
3108110eb670SHong Zhang 
3109110eb670SHong Zhang   PetscFunctionBegin;
3110110eb670SHong Zhang   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3111110eb670SHong Zhang   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3112110eb670SHong Zhang   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
3113110eb670SHong Zhang   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3114110eb670SHong Zhang   PetscFunctionReturn(0);
3115110eb670SHong Zhang }
3116110eb670SHong Zhang 
3117110eb670SHong Zhang #undef __FUNCT__
3118cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
3119cd652676SJed Brown /*@
3120cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
3121cd652676SJed Brown 
3122cd652676SJed Brown    Logically Collective on TS
3123cd652676SJed Brown 
3124cd652676SJed Brown    Input Argument:
3125cd652676SJed Brown .  ts - time stepping context
3126cd652676SJed Brown 
3127cd652676SJed Brown    Output Argument:
3128cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
3129cd652676SJed Brown 
3130cd652676SJed Brown    Level: intermediate
3131cd652676SJed Brown 
3132cd652676SJed Brown .keywords: TS, set
3133cd652676SJed Brown 
3134cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3135cd652676SJed Brown @*/
3136cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3137cd652676SJed Brown {
3138cd652676SJed Brown   PetscFunctionBegin;
3139cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3140cd652676SJed Brown   ts->retain_stages = flg;
3141cd652676SJed Brown   PetscFunctionReturn(0);
3142cd652676SJed Brown }
3143cd652676SJed Brown 
3144cd652676SJed Brown #undef __FUNCT__
3145cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3146cd652676SJed Brown /*@
3147cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3148cd652676SJed Brown 
3149cd652676SJed Brown    Collective on TS
3150cd652676SJed Brown 
3151cd652676SJed Brown    Input Argument:
3152cd652676SJed Brown +  ts - time stepping context
3153cd652676SJed Brown -  t - time to interpolate to
3154cd652676SJed Brown 
3155cd652676SJed Brown    Output Argument:
31560910c330SBarry Smith .  U - state at given time
3157cd652676SJed Brown 
3158cd652676SJed Brown    Notes:
3159cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3160cd652676SJed Brown 
3161cd652676SJed Brown    Level: intermediate
3162cd652676SJed Brown 
3163cd652676SJed Brown    Developer Notes:
3164cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3165cd652676SJed Brown 
3166cd652676SJed Brown .keywords: TS, set
3167cd652676SJed Brown 
3168cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3169cd652676SJed Brown @*/
31700910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3171cd652676SJed Brown {
3172cd652676SJed Brown   PetscErrorCode ierr;
3173cd652676SJed Brown 
3174cd652676SJed Brown   PetscFunctionBegin;
3175cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3176b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
31776712e2f1SBarry 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,(double)(ts->ptime-ts->time_step_prev),(double)ts->ptime);
3178ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
31790910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3180cd652676SJed Brown   PetscFunctionReturn(0);
3181cd652676SJed Brown }
3182cd652676SJed Brown 
3183cd652676SJed Brown #undef __FUNCT__
31844a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3185d763cef2SBarry Smith /*@
31866d9e5789SSean Farley    TSStep - Steps one time step
3187d763cef2SBarry Smith 
3188d763cef2SBarry Smith    Collective on TS
3189d763cef2SBarry Smith 
3190d763cef2SBarry Smith    Input Parameter:
3191d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3192d763cef2SBarry Smith 
319327829d71SBarry Smith    Level: developer
3194d763cef2SBarry Smith 
3195b8123daeSJed Brown    Notes:
319627829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
319727829d71SBarry Smith 
3198b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3199b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3200b8123daeSJed Brown 
320125cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
320225cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
320325cb2221SBarry Smith 
3204d763cef2SBarry Smith .keywords: TS, timestep, solve
3205d763cef2SBarry Smith 
32069be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3207d763cef2SBarry Smith @*/
3208193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3209d763cef2SBarry Smith {
32102fb8446cSMatthew G. Knepley   DM               dm;
3211dfbe8321SBarry Smith   PetscErrorCode   ierr;
3212fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3213d763cef2SBarry Smith 
3214d763cef2SBarry Smith   PetscFunctionBegin;
32150700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3216fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3217fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3218fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3219fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3220fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3221fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3222302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3223fffbeea8SBarry Smith 
32242fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3225d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3226d405a339SMatthew Knepley 
3227362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
322824655328SShri   ts->ptime_prev = ts->ptime;
3229cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3230bbd56ea5SKarl Rupp 
3231e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3232d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3233193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3234d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3235bbd56ea5SKarl Rupp 
323624655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3237cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3238362cd11cSLisandro Dalcin 
3239362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3240cef5090cSJed Brown     if (ts->errorifstepfailed) {
324108c7845fSBarry Smith       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
324208c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3243d2daff3dSHong Zhang     }
324408c7845fSBarry Smith   } else if (!ts->reason) {
324508c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
324608c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
324708c7845fSBarry Smith   }
32482c18e0fdSBarry Smith   ts->total_steps++;
32498392e04aSShri Abhyankar   ts->steprollback = PETSC_FALSE;
325008c7845fSBarry Smith   PetscFunctionReturn(0);
325108c7845fSBarry Smith }
325208c7845fSBarry Smith 
325308c7845fSBarry Smith #undef __FUNCT__
325408c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
325508c7845fSBarry Smith /*@
3256b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
325708c7845fSBarry Smith 
325808c7845fSBarry Smith    Collective on TS
325908c7845fSBarry Smith 
326008c7845fSBarry Smith    Input Parameter:
326108c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
326208c7845fSBarry Smith 
32636a4d4014SLisandro Dalcin    Level: intermediate
32646a4d4014SLisandro Dalcin 
3265b957a604SHong Zhang .keywords: TS, adjoint, step
32666a4d4014SLisandro Dalcin 
3267b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
32686a4d4014SLisandro Dalcin @*/
326908c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
32706a4d4014SLisandro Dalcin {
327108c7845fSBarry Smith   DM               dm;
32726a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
32736a4d4014SLisandro Dalcin 
32746a4d4014SLisandro Dalcin   PetscFunctionBegin;
32754a2ae208SSatish Balay   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
327608c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
327708c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3278d763cef2SBarry Smith 
3279d763cef2SBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
328008c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
328108c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3282685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr);
3283d763cef2SBarry Smith 
32846849ba73SBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
328542f2b339SBarry Smith   if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of  %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name);
328642f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
3287a6570f20SBarry Smith   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3288d763cef2SBarry Smith 
3289cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3290cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3291362cd11cSLisandro Dalcin 
3292362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3293cef5090cSJed Brown     if (ts->errorifstepfailed) {
3294cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3295ce94432eSBarry 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]);
32962a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
32972a3a10ceSLisandro Dalcin         SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
3298ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3299cef5090cSJed Brown     }
3300362cd11cSLisandro Dalcin   } else if (!ts->reason) {
330173b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3302db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3303362cd11cSLisandro Dalcin   }
33042c18e0fdSBarry Smith   ts->total_steps--;
3305d763cef2SBarry Smith   PetscFunctionReturn(0);
3306d763cef2SBarry Smith }
3307d763cef2SBarry Smith 
3308d763cef2SBarry Smith #undef __FUNCT__
330905175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
331005175c85SJed Brown /*@
331105175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
331205175c85SJed Brown 
33131c3436cfSJed Brown    Collective on TS
331405175c85SJed Brown 
331505175c85SJed Brown    Input Arguments:
33161c3436cfSJed Brown +  ts - time stepping context
33171c3436cfSJed Brown .  order - desired order of accuracy
33180298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
331905175c85SJed Brown 
332005175c85SJed Brown    Output Arguments:
33210910c330SBarry Smith .  U - state at the end of the current step
332205175c85SJed Brown 
332305175c85SJed Brown    Level: advanced
332405175c85SJed Brown 
3325108c343cSJed Brown    Notes:
3326108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3327108c343cSJed 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.
3328108c343cSJed Brown 
33291c3436cfSJed Brown .seealso: TSStep(), TSAdapt
333005175c85SJed Brown @*/
33310910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
333205175c85SJed Brown {
333305175c85SJed Brown   PetscErrorCode ierr;
333405175c85SJed Brown 
333505175c85SJed Brown   PetscFunctionBegin;
333605175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
333705175c85SJed Brown   PetscValidType(ts,1);
33380910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3339ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
33400910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
334105175c85SJed Brown   PetscFunctionReturn(0);
334205175c85SJed Brown }
334305175c85SJed Brown 
3344d67e68b7SBarry Smith 
334505175c85SJed Brown #undef __FUNCT__
3346d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
3347d763cef2SBarry Smith /*@
3348d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3349d763cef2SBarry Smith 
3350d763cef2SBarry Smith    Collective on TS
3351d763cef2SBarry Smith 
3352d763cef2SBarry Smith    Input Parameter:
3353d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3354cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
33555a3a76d0SJed Brown 
3356d763cef2SBarry Smith    Level: beginner
3357d763cef2SBarry Smith 
33585a3a76d0SJed Brown    Notes:
33595a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
33605a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
33615a3a76d0SJed Brown    stepped over the final time.
33625a3a76d0SJed Brown 
3363d763cef2SBarry Smith .keywords: TS, timestep, solve
3364d763cef2SBarry Smith 
3365d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3366d763cef2SBarry Smith @*/
3367cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
3368d763cef2SBarry Smith {
3369b06615a5SLisandro Dalcin   Vec               solution;
3370d763cef2SBarry Smith   PetscErrorCode    ierr;
3371d763cef2SBarry Smith 
3372d763cef2SBarry Smith   PetscFunctionBegin;
3373d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3374f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
337549354f04SShri 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 */
3376b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
33770910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3378b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3379b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3380b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
33815a3a76d0SJed Brown     }
33820910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3383bbd56ea5SKarl Rupp   } else if (u) {
33840910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
33855a3a76d0SJed Brown   }
3386b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3387d763cef2SBarry Smith   /* reset time step and iteration counters */
3388193ac0bcSJed Brown   ts->steps             = 0;
33895ef26d82SJed Brown   ts->ksp_its           = 0;
33905ef26d82SJed Brown   ts->snes_its          = 0;
3391c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3392c610991cSLisandro Dalcin   ts->reject            = 0;
3393193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3394193ac0bcSJed Brown 
3395ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3396ec8db0baSMatthew G. Knepley   {
3397ec8db0baSMatthew G. Knepley     DM dm;
3398ec8db0baSMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3399ec8db0baSMatthew G. Knepley     ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3400ec8db0baSMatthew G. Knepley   }
3401f05ece33SBarry Smith 
3402193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3403193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
34040910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3405cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3406193ac0bcSJed Brown   } else {
3407d763cef2SBarry Smith     /* steps the requested number of timesteps. */
3408db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3409db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3410cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
341111b05f00SHong Zhang     if (ts->vec_costintegral) ts->costintegralfwd=PETSC_TRUE;
34126fea3669SShri Abhyankar     if(ts->event) {
34136fea3669SShri Abhyankar       ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr);
34146fea3669SShri Abhyankar     }
3415e1a7a14fSJed Brown     while (!ts->reason) {
3416193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3417193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3418aeb4809dSShri Abhyankar       if (ts->event) {
3419aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
34201eda64f1SShri Abhyankar       }
34218392e04aSShri Abhyankar       if(!ts->steprollback) {
3422cdf0de3dSShri Abhyankar 	ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
34231eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3424aeb4809dSShri Abhyankar       }
3425193ac0bcSJed Brown     }
342649354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
34270910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3428cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3429b06615a5SLisandro Dalcin       solution = u;
34300574a7fbSJed Brown     } else {
3431ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3432cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3433b06615a5SLisandro Dalcin       solution = ts->vec_sol;
34340574a7fbSJed Brown     }
3435b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3436685405a1SBarry Smith     ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr);
3437193ac0bcSJed Brown   }
3438d2daff3dSHong Zhang 
3439ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
3440dc1cb503SEmil Constantinescu   ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr);
344156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3442ef222394SBarry Smith   if (ts->adjoint_solve) {
3443ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
34442b0a91c0SBarry Smith   }
3445d763cef2SBarry Smith   PetscFunctionReturn(0);
3446d763cef2SBarry Smith }
3447d763cef2SBarry Smith 
3448d763cef2SBarry Smith #undef __FUNCT__
3449c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3450c88f2d44SBarry Smith /*@
3451c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3452c88f2d44SBarry Smith 
3453c88f2d44SBarry Smith    Collective on TS
3454c88f2d44SBarry Smith 
3455c88f2d44SBarry Smith    Input Parameter:
34562c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3457c88f2d44SBarry Smith 
3458e87fd094SBarry Smith    Options Database:
3459e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
3460e87fd094SBarry Smith 
3461c88f2d44SBarry Smith    Level: intermediate
3462c88f2d44SBarry Smith 
3463c88f2d44SBarry Smith    Notes:
3464c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3465c88f2d44SBarry Smith 
346673b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
346773b18844SBarry Smith 
3468c88f2d44SBarry Smith .keywords: TS, timestep, solve
3469c88f2d44SBarry Smith 
3470b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
3471c88f2d44SBarry Smith @*/
34722c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3473c88f2d44SBarry Smith {
3474c88f2d44SBarry Smith   PetscErrorCode    ierr;
3475c88f2d44SBarry Smith 
3476c88f2d44SBarry Smith   PetscFunctionBegin;
3477c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3478c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3479c88f2d44SBarry Smith   /* reset time step and iteration counters */
3480c88f2d44SBarry Smith   ts->steps             = 0;
3481c88f2d44SBarry Smith   ts->ksp_its           = 0;
3482c88f2d44SBarry Smith   ts->snes_its          = 0;
3483c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3484c88f2d44SBarry Smith   ts->reject            = 0;
3485c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3486c88f2d44SBarry Smith 
348773b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3488c88f2d44SBarry Smith 
348973b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3490c88f2d44SBarry Smith   while (!ts->reason) {
349155c52731SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
34929110b2e7SHong Zhang     ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
3493c88f2d44SBarry Smith     if (ts->event) {
3494d0578d90SShri Abhyankar       ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr);
3495d0578d90SShri Abhyankar     }
3496616946c1SHong Zhang     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3497c88f2d44SBarry Smith   }
349826656371SHong Zhang   ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr);
349926656371SHong Zhang   ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr);
3500c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3501685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
3502c88f2d44SBarry Smith   PetscFunctionReturn(0);
3503c88f2d44SBarry Smith }
3504c88f2d44SBarry Smith 
3505c88f2d44SBarry Smith #undef __FUNCT__
3506d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
3507228d79bcSJed Brown /*@
3508228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3509228d79bcSJed Brown 
3510228d79bcSJed Brown    Collective on TS
3511228d79bcSJed Brown 
3512228d79bcSJed Brown    Input Parameters:
3513228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3514228d79bcSJed Brown .  step - step number that has just completed
3515228d79bcSJed Brown .  ptime - model time of the state
35160910c330SBarry Smith -  u - state at the current model time
3517228d79bcSJed Brown 
3518228d79bcSJed Brown    Notes:
3519228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3520228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3521228d79bcSJed Brown 
3522228d79bcSJed Brown    Level: advanced
3523228d79bcSJed Brown 
3524228d79bcSJed Brown .keywords: TS, timestep
3525228d79bcSJed Brown @*/
35260910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3527d763cef2SBarry Smith {
3528d763cef2SBarry Smith   PetscErrorCode ierr;
3529d763cef2SBarry Smith   PetscInt       i,n = ts->numbermonitors;
3530d763cef2SBarry Smith 
3531d763cef2SBarry Smith   PetscFunctionBegin;
3532b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3533b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
35345edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3535d763cef2SBarry Smith   for (i=0; i<n; i++) {
35360910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3537d763cef2SBarry Smith   }
35385edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3539d763cef2SBarry Smith   PetscFunctionReturn(0);
3540d763cef2SBarry Smith }
3541d763cef2SBarry Smith 
35429110b2e7SHong Zhang #undef __FUNCT__
35439110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor"
35449110b2e7SHong Zhang /*@
35459110b2e7SHong Zhang    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()
35469110b2e7SHong Zhang 
35479110b2e7SHong Zhang    Collective on TS
35489110b2e7SHong Zhang 
35499110b2e7SHong Zhang    Input Parameters:
35509110b2e7SHong Zhang +  ts - time stepping context obtained from TSCreate()
35519110b2e7SHong Zhang .  step - step number that has just completed
35529110b2e7SHong Zhang .  ptime - model time of the state
35539110b2e7SHong Zhang .  u - state at the current model time
35549110b2e7SHong Zhang .  numcost - number of cost functions (dimension of lambda  or mu)
35559110b2e7SHong Zhang .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
35569110b2e7SHong Zhang -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
35579110b2e7SHong Zhang 
35589110b2e7SHong Zhang    Notes:
35599110b2e7SHong Zhang    TSAdjointMonitor() is typically used within the adjoint implementations.
35609110b2e7SHong Zhang    Users might call this function when using the TSAdjointStep() interface instead of TSAdjointSolve().
35619110b2e7SHong Zhang 
35629110b2e7SHong Zhang    Level: advanced
35639110b2e7SHong Zhang 
35649110b2e7SHong Zhang .keywords: TS, timestep
35659110b2e7SHong Zhang @*/
35669110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
35679110b2e7SHong Zhang {
35689110b2e7SHong Zhang   PetscErrorCode ierr;
35699110b2e7SHong Zhang   PetscInt       i,n = ts->numberadjointmonitors;
35709110b2e7SHong Zhang 
35719110b2e7SHong Zhang   PetscFunctionBegin;
35729110b2e7SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
35739110b2e7SHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
35749110b2e7SHong Zhang   ierr = VecLockPush(u);CHKERRQ(ierr);
35759110b2e7SHong Zhang   for (i=0; i<n; i++) {
35769110b2e7SHong Zhang     ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr);
35779110b2e7SHong Zhang   }
35789110b2e7SHong Zhang   ierr = VecLockPop(u);CHKERRQ(ierr);
35799110b2e7SHong Zhang   PetscFunctionReturn(0);
35809110b2e7SHong Zhang }
35819110b2e7SHong Zhang 
3582d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3583d763cef2SBarry Smith #undef __FUNCT__
3584a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3585d763cef2SBarry Smith /*@C
3586a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3587a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3588d763cef2SBarry Smith 
3589d763cef2SBarry Smith    Collective on TS
3590d763cef2SBarry Smith 
3591d763cef2SBarry Smith    Input Parameters:
3592d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3593d763cef2SBarry Smith .  label - the title to put in the title bar
35947c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3595a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3596a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3597d763cef2SBarry Smith 
3598d763cef2SBarry Smith    Output Parameter:
35990b039ecaSBarry Smith .  ctx - the context
3600d763cef2SBarry Smith 
3601d763cef2SBarry Smith    Options Database Key:
36024f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
36034f09c107SBarry Smith .  -ts_monitor_lg_solution -
360499fdda47SBarry Smith .  -ts_monitor_lg_error -
360599fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
360699fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
3607b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3608d763cef2SBarry Smith 
3609d763cef2SBarry Smith    Notes:
3610a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3611d763cef2SBarry Smith 
3612d763cef2SBarry Smith    Level: intermediate
3613d763cef2SBarry Smith 
36147c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3615d763cef2SBarry Smith 
36164f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
36177c922b88SBarry Smith 
3618d763cef2SBarry Smith @*/
3619a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3620d763cef2SBarry Smith {
3621*7f52daa2SLisandro Dalcin   PetscDraw      draw;
3622dfbe8321SBarry Smith   PetscErrorCode ierr;
3623d763cef2SBarry Smith 
3624d763cef2SBarry Smith   PetscFunctionBegin;
3625b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3626*7f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
3627*7f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
3628*7f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3629b6fe0379SLisandro Dalcin   ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3630287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3631*7f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3632a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3633d763cef2SBarry Smith   PetscFunctionReturn(0);
3634d763cef2SBarry Smith }
3635d763cef2SBarry Smith 
36364a2ae208SSatish Balay #undef __FUNCT__
36374f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3638b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3639d763cef2SBarry Smith {
36400b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3641c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3642dfbe8321SBarry Smith   PetscErrorCode ierr;
3643d763cef2SBarry Smith 
3644d763cef2SBarry Smith   PetscFunctionBegin;
3645b06615a5SLisandro Dalcin   if (!step) {
3646a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3647a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3648a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3649a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3650a9f9c1f6SBarry Smith   }
3651c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
36520b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3653b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
36540b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
36553923b477SBarry Smith   }
3656d763cef2SBarry Smith   PetscFunctionReturn(0);
3657d763cef2SBarry Smith }
3658d763cef2SBarry Smith 
36594a2ae208SSatish Balay #undef __FUNCT__
3660a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3661d763cef2SBarry Smith /*@C
3662a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3663a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3664d763cef2SBarry Smith 
36650b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3666d763cef2SBarry Smith 
3667d763cef2SBarry Smith    Input Parameter:
36680b039ecaSBarry Smith .  ctx - the monitor context
3669d763cef2SBarry Smith 
3670d763cef2SBarry Smith    Level: intermediate
3671d763cef2SBarry Smith 
3672d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3673d763cef2SBarry Smith 
36744f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3675d763cef2SBarry Smith @*/
3676a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3677d763cef2SBarry Smith {
3678dfbe8321SBarry Smith   PetscErrorCode ierr;
3679d763cef2SBarry Smith 
3680d763cef2SBarry Smith   PetscFunctionBegin;
36817684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
36827684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
36837684fa3eSBarry Smith   }
36840b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
368531152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3686387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3687387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3688387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
36890b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3690d763cef2SBarry Smith   PetscFunctionReturn(0);
3691d763cef2SBarry Smith }
3692d763cef2SBarry Smith 
36934a2ae208SSatish Balay #undef __FUNCT__
36944a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3695d763cef2SBarry Smith /*@
3696b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3697d763cef2SBarry Smith 
3698d763cef2SBarry Smith    Not Collective
3699d763cef2SBarry Smith 
3700d763cef2SBarry Smith    Input Parameter:
3701d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3702d763cef2SBarry Smith 
3703d763cef2SBarry Smith    Output Parameter:
3704d763cef2SBarry Smith .  t  - the current time
3705d763cef2SBarry Smith 
3706d763cef2SBarry Smith    Level: beginner
3707d763cef2SBarry Smith 
3708b8123daeSJed Brown    Note:
3709b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
37109be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3711b8123daeSJed Brown 
3712d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3713d763cef2SBarry Smith 
3714d763cef2SBarry Smith .keywords: TS, get, time
3715d763cef2SBarry Smith @*/
37167087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3717d763cef2SBarry Smith {
3718d763cef2SBarry Smith   PetscFunctionBegin;
37190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3720f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3721d763cef2SBarry Smith   *t = ts->ptime;
3722d763cef2SBarry Smith   PetscFunctionReturn(0);
3723d763cef2SBarry Smith }
3724d763cef2SBarry Smith 
37254a2ae208SSatish Balay #undef __FUNCT__
3726e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3727e5e524a1SHong Zhang /*@
3728e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3729e5e524a1SHong Zhang 
3730e5e524a1SHong Zhang    Not Collective
3731e5e524a1SHong Zhang 
3732e5e524a1SHong Zhang    Input Parameter:
3733e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3734e5e524a1SHong Zhang 
3735e5e524a1SHong Zhang    Output Parameter:
3736e5e524a1SHong Zhang .  t  - the previous time
3737e5e524a1SHong Zhang 
3738e5e524a1SHong Zhang    Level: beginner
3739e5e524a1SHong Zhang 
3740e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3741e5e524a1SHong Zhang 
3742e5e524a1SHong Zhang .keywords: TS, get, time
3743e5e524a1SHong Zhang @*/
3744e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3745e5e524a1SHong Zhang {
3746e5e524a1SHong Zhang   PetscFunctionBegin;
3747e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3748e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3749e5e524a1SHong Zhang   *t = ts->ptime_prev;
3750e5e524a1SHong Zhang   PetscFunctionReturn(0);
3751e5e524a1SHong Zhang }
3752e5e524a1SHong Zhang 
3753e5e524a1SHong Zhang #undef __FUNCT__
37546a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
37556a4d4014SLisandro Dalcin /*@
37566a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
37576a4d4014SLisandro Dalcin 
37583f9fe445SBarry Smith    Logically Collective on TS
37596a4d4014SLisandro Dalcin 
37606a4d4014SLisandro Dalcin    Input Parameters:
37616a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
37626a4d4014SLisandro Dalcin -  time - the time
37636a4d4014SLisandro Dalcin 
37646a4d4014SLisandro Dalcin    Level: intermediate
37656a4d4014SLisandro Dalcin 
37666a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
37676a4d4014SLisandro Dalcin 
37686a4d4014SLisandro Dalcin .keywords: TS, set, time
37696a4d4014SLisandro Dalcin @*/
37707087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
37716a4d4014SLisandro Dalcin {
37726a4d4014SLisandro Dalcin   PetscFunctionBegin;
37730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3774c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
37756a4d4014SLisandro Dalcin   ts->ptime = t;
37766a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
37776a4d4014SLisandro Dalcin }
37786a4d4014SLisandro Dalcin 
37796a4d4014SLisandro Dalcin #undef __FUNCT__
37804a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3781d763cef2SBarry Smith /*@C
3782d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3783d763cef2SBarry Smith    TS options in the database.
3784d763cef2SBarry Smith 
37853f9fe445SBarry Smith    Logically Collective on TS
3786d763cef2SBarry Smith 
3787d763cef2SBarry Smith    Input Parameter:
3788d763cef2SBarry Smith +  ts     - The TS context
3789d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3790d763cef2SBarry Smith 
3791d763cef2SBarry Smith    Notes:
3792d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3793d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3794d763cef2SBarry Smith    hyphen.
3795d763cef2SBarry Smith 
3796d763cef2SBarry Smith    Level: advanced
3797d763cef2SBarry Smith 
3798d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3799d763cef2SBarry Smith 
3800d763cef2SBarry Smith .seealso: TSSetFromOptions()
3801d763cef2SBarry Smith 
3802d763cef2SBarry Smith @*/
38037087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3804d763cef2SBarry Smith {
3805dfbe8321SBarry Smith   PetscErrorCode ierr;
3806089b2837SJed Brown   SNES           snes;
3807d763cef2SBarry Smith 
3808d763cef2SBarry Smith   PetscFunctionBegin;
38090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3810d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3811089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3812089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3813d763cef2SBarry Smith   PetscFunctionReturn(0);
3814d763cef2SBarry Smith }
3815d763cef2SBarry Smith 
3816d763cef2SBarry Smith 
38174a2ae208SSatish Balay #undef __FUNCT__
38184a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3819d763cef2SBarry Smith /*@C
3820d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3821d763cef2SBarry Smith    TS options in the database.
3822d763cef2SBarry Smith 
38233f9fe445SBarry Smith    Logically Collective on TS
3824d763cef2SBarry Smith 
3825d763cef2SBarry Smith    Input Parameter:
3826d763cef2SBarry Smith +  ts     - The TS context
3827d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3828d763cef2SBarry Smith 
3829d763cef2SBarry Smith    Notes:
3830d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3831d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3832d763cef2SBarry Smith    hyphen.
3833d763cef2SBarry Smith 
3834d763cef2SBarry Smith    Level: advanced
3835d763cef2SBarry Smith 
3836d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3837d763cef2SBarry Smith 
3838d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3839d763cef2SBarry Smith 
3840d763cef2SBarry Smith @*/
38417087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3842d763cef2SBarry Smith {
3843dfbe8321SBarry Smith   PetscErrorCode ierr;
3844089b2837SJed Brown   SNES           snes;
3845d763cef2SBarry Smith 
3846d763cef2SBarry Smith   PetscFunctionBegin;
38470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3848d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3849089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3850089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3851d763cef2SBarry Smith   PetscFunctionReturn(0);
3852d763cef2SBarry Smith }
3853d763cef2SBarry Smith 
38544a2ae208SSatish Balay #undef __FUNCT__
38554a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3856d763cef2SBarry Smith /*@C
3857d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3858d763cef2SBarry Smith    TS options in the database.
3859d763cef2SBarry Smith 
3860d763cef2SBarry Smith    Not Collective
3861d763cef2SBarry Smith 
3862d763cef2SBarry Smith    Input Parameter:
3863d763cef2SBarry Smith .  ts - The TS context
3864d763cef2SBarry Smith 
3865d763cef2SBarry Smith    Output Parameter:
3866d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3867d763cef2SBarry Smith 
3868d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3869d763cef2SBarry Smith    sufficient length to hold the prefix.
3870d763cef2SBarry Smith 
3871d763cef2SBarry Smith    Level: intermediate
3872d763cef2SBarry Smith 
3873d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3874d763cef2SBarry Smith 
3875d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3876d763cef2SBarry Smith @*/
38777087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3878d763cef2SBarry Smith {
3879dfbe8321SBarry Smith   PetscErrorCode ierr;
3880d763cef2SBarry Smith 
3881d763cef2SBarry Smith   PetscFunctionBegin;
38820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38834482741eSBarry Smith   PetscValidPointer(prefix,2);
3884d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3885d763cef2SBarry Smith   PetscFunctionReturn(0);
3886d763cef2SBarry Smith }
3887d763cef2SBarry Smith 
38884a2ae208SSatish Balay #undef __FUNCT__
38894a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3890d763cef2SBarry Smith /*@C
3891d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3892d763cef2SBarry Smith 
3893d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3894d763cef2SBarry Smith 
3895d763cef2SBarry Smith    Input Parameter:
3896d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3897d763cef2SBarry Smith 
3898d763cef2SBarry Smith    Output Parameters:
3899e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3900e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3901e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3902e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3903d763cef2SBarry Smith 
39040298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3905d763cef2SBarry Smith 
3906d763cef2SBarry Smith    Level: intermediate
3907d763cef2SBarry Smith 
390826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3909d763cef2SBarry Smith 
3910d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3911d763cef2SBarry Smith @*/
3912e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3913d763cef2SBarry Smith {
3914089b2837SJed Brown   PetscErrorCode ierr;
3915089b2837SJed Brown   SNES           snes;
391624989b8cSPeter Brune   DM             dm;
3917089b2837SJed Brown 
3918d763cef2SBarry Smith   PetscFunctionBegin;
3919089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3920e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
392124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
392224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3923d763cef2SBarry Smith   PetscFunctionReturn(0);
3924d763cef2SBarry Smith }
3925d763cef2SBarry Smith 
39261713a123SBarry Smith #undef __FUNCT__
39272eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
39282eca1d9cSJed Brown /*@C
39292eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
39302eca1d9cSJed Brown 
39312eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
39322eca1d9cSJed Brown 
39332eca1d9cSJed Brown    Input Parameter:
39342eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
39352eca1d9cSJed Brown 
39362eca1d9cSJed Brown    Output Parameters:
3937e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3938e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
39392eca1d9cSJed Brown .  f   - The function to compute the matrices
39402eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
39412eca1d9cSJed Brown 
39420298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
39432eca1d9cSJed Brown 
39442eca1d9cSJed Brown    Level: advanced
39452eca1d9cSJed Brown 
39462eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
39472eca1d9cSJed Brown 
39482eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
39492eca1d9cSJed Brown @*/
3950e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
39512eca1d9cSJed Brown {
3952089b2837SJed Brown   PetscErrorCode ierr;
3953089b2837SJed Brown   SNES           snes;
395424989b8cSPeter Brune   DM             dm;
39550910c330SBarry Smith 
39562eca1d9cSJed Brown   PetscFunctionBegin;
3957089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3958f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3959e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
396024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
396124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
39622eca1d9cSJed Brown   PetscFunctionReturn(0);
39632eca1d9cSJed Brown }
39642eca1d9cSJed Brown 
39656083293cSBarry Smith 
39662eca1d9cSJed Brown #undef __FUNCT__
396783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
39681713a123SBarry Smith /*@C
396983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
39701713a123SBarry Smith    VecView() for the solution at each timestep
39711713a123SBarry Smith 
39721713a123SBarry Smith    Collective on TS
39731713a123SBarry Smith 
39741713a123SBarry Smith    Input Parameters:
39751713a123SBarry Smith +  ts - the TS context
39761713a123SBarry Smith .  step - current time-step
3977142b95e3SSatish Balay .  ptime - current time
39780298fd71SBarry Smith -  dummy - either a viewer or NULL
39791713a123SBarry Smith 
398099fdda47SBarry Smith    Options Database:
398199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
398299fdda47SBarry Smith 
3983387f4636SBarry Smith    Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
398499fdda47SBarry Smith        will look bad
398599fdda47SBarry Smith 
39861713a123SBarry Smith    Level: intermediate
39871713a123SBarry Smith 
39881713a123SBarry Smith .keywords: TS,  vector, monitor, view
39891713a123SBarry Smith 
3990a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39911713a123SBarry Smith @*/
39920910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39931713a123SBarry Smith {
3994dfbe8321SBarry Smith   PetscErrorCode   ierr;
399583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3996473a3ab2SBarry Smith   PetscDraw        draw;
39971713a123SBarry Smith 
39981713a123SBarry Smith   PetscFunctionBegin;
39996083293cSBarry Smith   if (!step && ictx->showinitial) {
40006083293cSBarry Smith     if (!ictx->initialsolution) {
40010910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
40021713a123SBarry Smith     }
40030910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
40046083293cSBarry Smith   }
4005b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
40060dcf80beSBarry Smith 
40076083293cSBarry Smith   if (ictx->showinitial) {
40086083293cSBarry Smith     PetscReal pause;
40096083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
40106083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
40116083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
40126083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
40136083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
40146083293cSBarry Smith   }
40150910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4016473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
401751fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4018473a3ab2SBarry Smith     char      time[32];
4019473a3ab2SBarry Smith 
4020473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
402185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4022473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4023473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
402451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4025473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4026473a3ab2SBarry Smith   }
4027473a3ab2SBarry Smith 
40286083293cSBarry Smith   if (ictx->showinitial) {
40296083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
40306083293cSBarry Smith   }
40311713a123SBarry Smith   PetscFunctionReturn(0);
40321713a123SBarry Smith }
40331713a123SBarry Smith 
40342d5ee99bSBarry Smith #undef __FUNCT__
40359110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi"
40369110b2e7SHong Zhang /*@C
40379110b2e7SHong Zhang    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
40389110b2e7SHong Zhang    VecView() for the sensitivities to initial states at each timestep
40399110b2e7SHong Zhang 
40409110b2e7SHong Zhang    Collective on TS
40419110b2e7SHong Zhang 
40429110b2e7SHong Zhang    Input Parameters:
40439110b2e7SHong Zhang +  ts - the TS context
40449110b2e7SHong Zhang .  step - current time-step
40459110b2e7SHong Zhang .  ptime - current time
40469110b2e7SHong Zhang .  u - current state
40479110b2e7SHong Zhang .  numcost - number of cost functions
40489110b2e7SHong Zhang .  lambda - sensitivities to initial conditions
40499110b2e7SHong Zhang .  mu - sensitivities to parameters
40509110b2e7SHong Zhang -  dummy - either a viewer or NULL
40519110b2e7SHong Zhang 
40529110b2e7SHong Zhang    Level: intermediate
40539110b2e7SHong Zhang 
40549110b2e7SHong Zhang .keywords: TS,  vector, adjoint, monitor, view
40559110b2e7SHong Zhang 
40569110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
40579110b2e7SHong Zhang @*/
40589110b2e7SHong Zhang PetscErrorCode  TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
40599110b2e7SHong Zhang {
40609110b2e7SHong Zhang   PetscErrorCode   ierr;
40619110b2e7SHong Zhang   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
40629110b2e7SHong Zhang   PetscDraw        draw;
4063a0046e2cSSatish Balay   PetscReal        xl,yl,xr,yr,h;
4064a0046e2cSSatish Balay   char             time[32];
40659110b2e7SHong Zhang 
40669110b2e7SHong Zhang   PetscFunctionBegin;
40679110b2e7SHong Zhang   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
40689110b2e7SHong Zhang 
40699110b2e7SHong Zhang   ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr);
40709110b2e7SHong Zhang   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
40719110b2e7SHong Zhang   ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
40729110b2e7SHong Zhang   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
40739110b2e7SHong Zhang   h    = yl + .95*(yr - yl);
40749110b2e7SHong Zhang   ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
40759110b2e7SHong Zhang   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
40769110b2e7SHong Zhang 
40779110b2e7SHong Zhang   PetscFunctionReturn(0);
40789110b2e7SHong Zhang }
40799110b2e7SHong Zhang 
40809110b2e7SHong Zhang #undef __FUNCT__
40812d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
40822d5ee99bSBarry Smith /*@C
40832d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
40842d5ee99bSBarry Smith 
40852d5ee99bSBarry Smith    Collective on TS
40862d5ee99bSBarry Smith 
40872d5ee99bSBarry Smith    Input Parameters:
40882d5ee99bSBarry Smith +  ts - the TS context
40892d5ee99bSBarry Smith .  step - current time-step
40902d5ee99bSBarry Smith .  ptime - current time
40912d5ee99bSBarry Smith -  dummy - either a viewer or NULL
40922d5ee99bSBarry Smith 
40932d5ee99bSBarry Smith    Level: intermediate
40942d5ee99bSBarry Smith 
40952d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
40962d5ee99bSBarry Smith 
40972d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
40982d5ee99bSBarry Smith @*/
40992d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
41002d5ee99bSBarry Smith {
41012d5ee99bSBarry Smith   PetscErrorCode    ierr;
41022d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
41032d5ee99bSBarry Smith   PetscDraw         draw;
41042d5ee99bSBarry Smith   MPI_Comm          comm;
41052d5ee99bSBarry Smith   PetscInt          n;
41062d5ee99bSBarry Smith   PetscMPIInt       size;
410751fa3d41SBarry Smith   PetscReal         xl,yl,xr,yr,h;
41082d5ee99bSBarry Smith   char              time[32];
41092d5ee99bSBarry Smith   const PetscScalar *U;
41102d5ee99bSBarry Smith 
41112d5ee99bSBarry Smith   PetscFunctionBegin;
41122d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
41132d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
41142d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
41152d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
41162d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
41172d5ee99bSBarry Smith 
41182d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
41192d5ee99bSBarry Smith 
41202d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
41214b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
4122ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
4123a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
4124a4494fc1SBarry Smith       PetscFunctionReturn(0);
4125a4494fc1SBarry Smith   }
41262d5ee99bSBarry Smith   if (!step) ictx->color++;
41272d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
41282d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
41292d5ee99bSBarry Smith 
41302d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
41314b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
413285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
41332d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
413451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
41352d5ee99bSBarry Smith   }
41362d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
41372d5ee99bSBarry Smith   PetscFunctionReturn(0);
41382d5ee99bSBarry Smith }
41392d5ee99bSBarry Smith 
41401713a123SBarry Smith 
41416c699258SBarry Smith #undef __FUNCT__
414283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
41436083293cSBarry Smith /*@C
414483a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
41456083293cSBarry Smith 
41466083293cSBarry Smith    Collective on TS
41476083293cSBarry Smith 
41486083293cSBarry Smith    Input Parameters:
41496083293cSBarry Smith .    ctx - the monitor context
41506083293cSBarry Smith 
41516083293cSBarry Smith    Level: intermediate
41526083293cSBarry Smith 
41536083293cSBarry Smith .keywords: TS,  vector, monitor, view
41546083293cSBarry Smith 
415583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
41566083293cSBarry Smith @*/
415783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
41586083293cSBarry Smith {
41596083293cSBarry Smith   PetscErrorCode ierr;
41606083293cSBarry Smith 
41616083293cSBarry Smith   PetscFunctionBegin;
41624b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
416383a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
416483a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
416583a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
41666083293cSBarry Smith   PetscFunctionReturn(0);
41676083293cSBarry Smith }
41686083293cSBarry Smith 
41696083293cSBarry Smith #undef __FUNCT__
417083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
41716083293cSBarry Smith /*@C
417283a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
41736083293cSBarry Smith 
41746083293cSBarry Smith    Collective on TS
41756083293cSBarry Smith 
41766083293cSBarry Smith    Input Parameter:
41776083293cSBarry Smith .    ts - time-step context
41786083293cSBarry Smith 
41796083293cSBarry Smith    Output Patameter:
41806083293cSBarry Smith .    ctx - the monitor context
41816083293cSBarry Smith 
418299fdda47SBarry Smith    Options Database:
418399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
418499fdda47SBarry Smith 
41856083293cSBarry Smith    Level: intermediate
41866083293cSBarry Smith 
41876083293cSBarry Smith .keywords: TS,  vector, monitor, view
41886083293cSBarry Smith 
418983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
41906083293cSBarry Smith @*/
419183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
41926083293cSBarry Smith {
41936083293cSBarry Smith   PetscErrorCode   ierr;
41946083293cSBarry Smith 
41956083293cSBarry Smith   PetscFunctionBegin;
4196b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
419783a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4198e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4199bbd56ea5SKarl Rupp 
420083a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4201473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
42020298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4203473a3ab2SBarry Smith 
4204473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
42050298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
42062d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
42076083293cSBarry Smith   PetscFunctionReturn(0);
42086083293cSBarry Smith }
42096083293cSBarry Smith 
42106083293cSBarry Smith #undef __FUNCT__
421183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
42123a471f94SBarry Smith /*@C
421383a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
42143a471f94SBarry Smith    VecView() for the error at each timestep
42153a471f94SBarry Smith 
42163a471f94SBarry Smith    Collective on TS
42173a471f94SBarry Smith 
42183a471f94SBarry Smith    Input Parameters:
42193a471f94SBarry Smith +  ts - the TS context
42203a471f94SBarry Smith .  step - current time-step
42213a471f94SBarry Smith .  ptime - current time
42220298fd71SBarry Smith -  dummy - either a viewer or NULL
42233a471f94SBarry Smith 
42243a471f94SBarry Smith    Level: intermediate
42253a471f94SBarry Smith 
42263a471f94SBarry Smith .keywords: TS,  vector, monitor, view
42273a471f94SBarry Smith 
42283a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42293a471f94SBarry Smith @*/
42300910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42313a471f94SBarry Smith {
42323a471f94SBarry Smith   PetscErrorCode   ierr;
423383a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
423483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
42353a471f94SBarry Smith   Vec              work;
42363a471f94SBarry Smith 
42373a471f94SBarry Smith   PetscFunctionBegin;
4238b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42390910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
42403a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
42410910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
42423a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
42433a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
42443a471f94SBarry Smith   PetscFunctionReturn(0);
42453a471f94SBarry Smith }
42463a471f94SBarry Smith 
4247af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
42483a471f94SBarry Smith #undef __FUNCT__
42496c699258SBarry Smith #define __FUNCT__ "TSSetDM"
42506c699258SBarry Smith /*@
42516c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
42526c699258SBarry Smith 
42533f9fe445SBarry Smith    Logically Collective on TS and DM
42546c699258SBarry Smith 
42556c699258SBarry Smith    Input Parameters:
42566c699258SBarry Smith +  ts - the preconditioner context
42576c699258SBarry Smith -  dm - the dm
42586c699258SBarry Smith 
42596c699258SBarry Smith    Level: intermediate
42606c699258SBarry Smith 
42616c699258SBarry Smith 
42626c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
42636c699258SBarry Smith @*/
42647087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
42656c699258SBarry Smith {
42666c699258SBarry Smith   PetscErrorCode ierr;
4267089b2837SJed Brown   SNES           snes;
4268942e3340SBarry Smith   DMTS           tsdm;
42696c699258SBarry Smith 
42706c699258SBarry Smith   PetscFunctionBegin;
42710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
427270663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4273942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
42742a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4275942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4276942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
427724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
427824989b8cSPeter Brune         tsdm->originaldm = dm;
427924989b8cSPeter Brune       }
428024989b8cSPeter Brune     }
42816bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
428224989b8cSPeter Brune   }
42836c699258SBarry Smith   ts->dm = dm;
4284bbd56ea5SKarl Rupp 
4285089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4286089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
42876c699258SBarry Smith   PetscFunctionReturn(0);
42886c699258SBarry Smith }
42896c699258SBarry Smith 
42906c699258SBarry Smith #undef __FUNCT__
42916c699258SBarry Smith #define __FUNCT__ "TSGetDM"
42926c699258SBarry Smith /*@
42936c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
42946c699258SBarry Smith 
42953f9fe445SBarry Smith    Not Collective
42966c699258SBarry Smith 
42976c699258SBarry Smith    Input Parameter:
42986c699258SBarry Smith . ts - the preconditioner context
42996c699258SBarry Smith 
43006c699258SBarry Smith    Output Parameter:
43016c699258SBarry Smith .  dm - the dm
43026c699258SBarry Smith 
43036c699258SBarry Smith    Level: intermediate
43046c699258SBarry Smith 
43056c699258SBarry Smith 
43066c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
43076c699258SBarry Smith @*/
43087087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
43096c699258SBarry Smith {
4310496e6a7aSJed Brown   PetscErrorCode ierr;
4311496e6a7aSJed Brown 
43126c699258SBarry Smith   PetscFunctionBegin;
43130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4314496e6a7aSJed Brown   if (!ts->dm) {
4315ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4316496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4317496e6a7aSJed Brown   }
43186c699258SBarry Smith   *dm = ts->dm;
43196c699258SBarry Smith   PetscFunctionReturn(0);
43206c699258SBarry Smith }
43211713a123SBarry Smith 
43220f5c6efeSJed Brown #undef __FUNCT__
43230f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
43240f5c6efeSJed Brown /*@
43250f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
43260f5c6efeSJed Brown 
43273f9fe445SBarry Smith    Logically Collective on SNES
43280f5c6efeSJed Brown 
43290f5c6efeSJed Brown    Input Parameter:
4330d42a1c89SJed Brown + snes - nonlinear solver
43310910c330SBarry Smith . U - the current state at which to evaluate the residual
4332d42a1c89SJed Brown - ctx - user context, must be a TS
43330f5c6efeSJed Brown 
43340f5c6efeSJed Brown    Output Parameter:
43350f5c6efeSJed Brown . F - the nonlinear residual
43360f5c6efeSJed Brown 
43370f5c6efeSJed Brown    Notes:
43380f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
43390f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
43400f5c6efeSJed Brown 
43410f5c6efeSJed Brown    Level: advanced
43420f5c6efeSJed Brown 
43430f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
43440f5c6efeSJed Brown @*/
43450910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
43460f5c6efeSJed Brown {
43470f5c6efeSJed Brown   TS             ts = (TS)ctx;
43480f5c6efeSJed Brown   PetscErrorCode ierr;
43490f5c6efeSJed Brown 
43500f5c6efeSJed Brown   PetscFunctionBegin;
43510f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43520910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43530f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
43540f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
43550910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
43560f5c6efeSJed Brown   PetscFunctionReturn(0);
43570f5c6efeSJed Brown }
43580f5c6efeSJed Brown 
43590f5c6efeSJed Brown #undef __FUNCT__
43600f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
43610f5c6efeSJed Brown /*@
43620f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
43630f5c6efeSJed Brown 
43640f5c6efeSJed Brown    Collective on SNES
43650f5c6efeSJed Brown 
43660f5c6efeSJed Brown    Input Parameter:
43670f5c6efeSJed Brown + snes - nonlinear solver
43680910c330SBarry Smith . U - the current state at which to evaluate the residual
43690f5c6efeSJed Brown - ctx - user context, must be a TS
43700f5c6efeSJed Brown 
43710f5c6efeSJed Brown    Output Parameter:
43720f5c6efeSJed Brown + A - the Jacobian
43730f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
43740f5c6efeSJed Brown - flag - indicates any structure change in the matrix
43750f5c6efeSJed Brown 
43760f5c6efeSJed Brown    Notes:
43770f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
43780f5c6efeSJed Brown 
43790f5c6efeSJed Brown    Level: developer
43800f5c6efeSJed Brown 
43810f5c6efeSJed Brown .seealso: SNESSetJacobian()
43820f5c6efeSJed Brown @*/
4383d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
43840f5c6efeSJed Brown {
43850f5c6efeSJed Brown   TS             ts = (TS)ctx;
43860f5c6efeSJed Brown   PetscErrorCode ierr;
43870f5c6efeSJed Brown 
43880f5c6efeSJed Brown   PetscFunctionBegin;
43890f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
43900910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
43910f5c6efeSJed Brown   PetscValidPointer(A,3);
439294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
43930f5c6efeSJed Brown   PetscValidPointer(B,4);
439494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
43950f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4396d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
43970f5c6efeSJed Brown   PetscFunctionReturn(0);
43980f5c6efeSJed Brown }
4399325fc9f4SBarry Smith 
44000e4ef248SJed Brown #undef __FUNCT__
44010e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
44020e4ef248SJed Brown /*@C
44030e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
44040e4ef248SJed Brown 
44050e4ef248SJed Brown    Collective on TS
44060e4ef248SJed Brown 
44070e4ef248SJed Brown    Input Arguments:
44080e4ef248SJed Brown +  ts - time stepping context
44090e4ef248SJed Brown .  t - time at which to evaluate
44100910c330SBarry Smith .  U - state at which to evaluate
44110e4ef248SJed Brown -  ctx - context
44120e4ef248SJed Brown 
44130e4ef248SJed Brown    Output Arguments:
44140e4ef248SJed Brown .  F - right hand side
44150e4ef248SJed Brown 
44160e4ef248SJed Brown    Level: intermediate
44170e4ef248SJed Brown 
44180e4ef248SJed Brown    Notes:
44190e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
44200e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
44210e4ef248SJed Brown 
44220e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
44230e4ef248SJed Brown @*/
44240910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
44250e4ef248SJed Brown {
44260e4ef248SJed Brown   PetscErrorCode ierr;
44270e4ef248SJed Brown   Mat            Arhs,Brhs;
44280e4ef248SJed Brown 
44290e4ef248SJed Brown   PetscFunctionBegin;
44300e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4431d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
44320910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
44330e4ef248SJed Brown   PetscFunctionReturn(0);
44340e4ef248SJed Brown }
44350e4ef248SJed Brown 
44360e4ef248SJed Brown #undef __FUNCT__
44370e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
44380e4ef248SJed Brown /*@C
44390e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
44400e4ef248SJed Brown 
44410e4ef248SJed Brown    Collective on TS
44420e4ef248SJed Brown 
44430e4ef248SJed Brown    Input Arguments:
44440e4ef248SJed Brown +  ts - time stepping context
44450e4ef248SJed Brown .  t - time at which to evaluate
44460910c330SBarry Smith .  U - state at which to evaluate
44470e4ef248SJed Brown -  ctx - context
44480e4ef248SJed Brown 
44490e4ef248SJed Brown    Output Arguments:
44500e4ef248SJed Brown +  A - pointer to operator
44510e4ef248SJed Brown .  B - pointer to preconditioning matrix
44520e4ef248SJed Brown -  flg - matrix structure flag
44530e4ef248SJed Brown 
44540e4ef248SJed Brown    Level: intermediate
44550e4ef248SJed Brown 
44560e4ef248SJed Brown    Notes:
44570e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
44580e4ef248SJed Brown 
44590e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
44600e4ef248SJed Brown @*/
4461d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
44620e4ef248SJed Brown {
44630e4ef248SJed Brown   PetscFunctionBegin;
44640e4ef248SJed Brown   PetscFunctionReturn(0);
44650e4ef248SJed Brown }
44660e4ef248SJed Brown 
44670026cea9SSean Farley #undef __FUNCT__
44680026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
44690026cea9SSean Farley /*@C
44700026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
44710026cea9SSean Farley 
44720026cea9SSean Farley    Collective on TS
44730026cea9SSean Farley 
44740026cea9SSean Farley    Input Arguments:
44750026cea9SSean Farley +  ts - time stepping context
44760026cea9SSean Farley .  t - time at which to evaluate
44770910c330SBarry Smith .  U - state at which to evaluate
44780910c330SBarry Smith .  Udot - time derivative of state vector
44790026cea9SSean Farley -  ctx - context
44800026cea9SSean Farley 
44810026cea9SSean Farley    Output Arguments:
44820026cea9SSean Farley .  F - left hand side
44830026cea9SSean Farley 
44840026cea9SSean Farley    Level: intermediate
44850026cea9SSean Farley 
44860026cea9SSean Farley    Notes:
44870910c330SBarry 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
44880026cea9SSean Farley    user is required to write their own TSComputeIFunction.
44890026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
44900026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
44910026cea9SSean Farley 
44920026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
44930026cea9SSean Farley @*/
44940910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
44950026cea9SSean Farley {
44960026cea9SSean Farley   PetscErrorCode ierr;
44970026cea9SSean Farley   Mat            A,B;
44980026cea9SSean Farley 
44990026cea9SSean Farley   PetscFunctionBegin;
45000298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4501d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
45020910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
45030026cea9SSean Farley   PetscFunctionReturn(0);
45040026cea9SSean Farley }
45050026cea9SSean Farley 
45060026cea9SSean Farley #undef __FUNCT__
45070026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
45080026cea9SSean Farley /*@C
4509b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
45100026cea9SSean Farley 
45110026cea9SSean Farley    Collective on TS
45120026cea9SSean Farley 
45130026cea9SSean Farley    Input Arguments:
45140026cea9SSean Farley +  ts - time stepping context
45150026cea9SSean Farley .  t - time at which to evaluate
45160910c330SBarry Smith .  U - state at which to evaluate
45170910c330SBarry Smith .  Udot - time derivative of state vector
45180026cea9SSean Farley .  shift - shift to apply
45190026cea9SSean Farley -  ctx - context
45200026cea9SSean Farley 
45210026cea9SSean Farley    Output Arguments:
45220026cea9SSean Farley +  A - pointer to operator
45230026cea9SSean Farley .  B - pointer to preconditioning matrix
45240026cea9SSean Farley -  flg - matrix structure flag
45250026cea9SSean Farley 
4526b41af12eSJed Brown    Level: advanced
45270026cea9SSean Farley 
45280026cea9SSean Farley    Notes:
45290026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
45300026cea9SSean Farley 
4531b41af12eSJed Brown    It is only appropriate for problems of the form
4532b41af12eSJed Brown 
4533b41af12eSJed Brown $     M Udot = F(U,t)
4534b41af12eSJed Brown 
4535b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4536b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4537b41af12eSJed Brown   an implicit operator of the form
4538b41af12eSJed Brown 
4539b41af12eSJed Brown $    shift*M + J
4540b41af12eSJed Brown 
4541b41af12eSJed 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
4542b41af12eSJed Brown   a copy of M or reassemble it when requested.
4543b41af12eSJed Brown 
45440026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
45450026cea9SSean Farley @*/
4546d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
45470026cea9SSean Farley {
4548b41af12eSJed Brown   PetscErrorCode ierr;
4549b41af12eSJed Brown 
45500026cea9SSean Farley   PetscFunctionBegin;
455194ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4552b41af12eSJed Brown   ts->ijacobian.shift = shift;
45530026cea9SSean Farley   PetscFunctionReturn(0);
45540026cea9SSean Farley }
4555b41af12eSJed Brown 
4556e817cc15SEmil Constantinescu #undef __FUNCT__
4557e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4558e817cc15SEmil Constantinescu /*@
4559e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4560e817cc15SEmil Constantinescu 
4561e817cc15SEmil Constantinescu    Not Collective
4562e817cc15SEmil Constantinescu 
4563e817cc15SEmil Constantinescu    Input Parameter:
4564e817cc15SEmil Constantinescu .  ts - the TS context
4565e817cc15SEmil Constantinescu 
4566e817cc15SEmil Constantinescu    Output Parameter:
45674e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4568e817cc15SEmil Constantinescu 
4569e817cc15SEmil Constantinescu    Level: beginner
4570e817cc15SEmil Constantinescu 
4571e817cc15SEmil Constantinescu .keywords: TS, equation type
4572e817cc15SEmil Constantinescu 
4573e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4574e817cc15SEmil Constantinescu @*/
4575e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4576e817cc15SEmil Constantinescu {
4577e817cc15SEmil Constantinescu   PetscFunctionBegin;
4578e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4579e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4580e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4581e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4582e817cc15SEmil Constantinescu }
4583e817cc15SEmil Constantinescu 
4584e817cc15SEmil Constantinescu #undef __FUNCT__
4585e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4586e817cc15SEmil Constantinescu /*@
4587e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4588e817cc15SEmil Constantinescu 
4589e817cc15SEmil Constantinescu    Not Collective
4590e817cc15SEmil Constantinescu 
4591e817cc15SEmil Constantinescu    Input Parameter:
4592e817cc15SEmil Constantinescu +  ts - the TS context
45931297b224SEmil Constantinescu -  equation_type - see TSEquationType
4594e817cc15SEmil Constantinescu 
4595e817cc15SEmil Constantinescu    Level: advanced
4596e817cc15SEmil Constantinescu 
4597e817cc15SEmil Constantinescu .keywords: TS, equation type
4598e817cc15SEmil Constantinescu 
4599e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4600e817cc15SEmil Constantinescu @*/
4601e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4602e817cc15SEmil Constantinescu {
4603e817cc15SEmil Constantinescu   PetscFunctionBegin;
4604e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4605e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4606e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4607e817cc15SEmil Constantinescu }
46080026cea9SSean Farley 
46094af1b03aSJed Brown #undef __FUNCT__
46104af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
46114af1b03aSJed Brown /*@
46124af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
46134af1b03aSJed Brown 
46144af1b03aSJed Brown    Not Collective
46154af1b03aSJed Brown 
46164af1b03aSJed Brown    Input Parameter:
46174af1b03aSJed Brown .  ts - the TS context
46184af1b03aSJed Brown 
46194af1b03aSJed Brown    Output Parameter:
46204af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
46214af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
46224af1b03aSJed Brown 
4623487e0bb9SJed Brown    Level: beginner
46244af1b03aSJed Brown 
4625cd652676SJed Brown    Notes:
4626cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
46274af1b03aSJed Brown 
46284af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
46294af1b03aSJed Brown 
46304af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
46314af1b03aSJed Brown @*/
46324af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
46334af1b03aSJed Brown {
46344af1b03aSJed Brown   PetscFunctionBegin;
46354af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46364af1b03aSJed Brown   PetscValidPointer(reason,2);
46374af1b03aSJed Brown   *reason = ts->reason;
46384af1b03aSJed Brown   PetscFunctionReturn(0);
46394af1b03aSJed Brown }
46404af1b03aSJed Brown 
4641fb1732b5SBarry Smith #undef __FUNCT__
4642d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4643d6ad946cSShri Abhyankar /*@
4644d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4645d6ad946cSShri Abhyankar 
4646d6ad946cSShri Abhyankar    Not Collective
4647d6ad946cSShri Abhyankar 
4648d6ad946cSShri Abhyankar    Input Parameter:
4649d6ad946cSShri Abhyankar +  ts - the TS context
4650d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4651d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4652d6ad946cSShri Abhyankar 
4653f5abba47SShri Abhyankar    Level: advanced
4654d6ad946cSShri Abhyankar 
4655d6ad946cSShri Abhyankar    Notes:
4656d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4657d6ad946cSShri Abhyankar 
4658d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4659d6ad946cSShri Abhyankar 
4660d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4661d6ad946cSShri Abhyankar @*/
4662d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4663d6ad946cSShri Abhyankar {
4664d6ad946cSShri Abhyankar   PetscFunctionBegin;
4665d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4666d6ad946cSShri Abhyankar   ts->reason = reason;
4667d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4668d6ad946cSShri Abhyankar }
4669d6ad946cSShri Abhyankar 
4670d6ad946cSShri Abhyankar #undef __FUNCT__
4671cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4672cc708dedSBarry Smith /*@
4673cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4674cc708dedSBarry Smith 
4675cc708dedSBarry Smith    Not Collective
4676cc708dedSBarry Smith 
4677cc708dedSBarry Smith    Input Parameter:
4678cc708dedSBarry Smith .  ts - the TS context
4679cc708dedSBarry Smith 
4680cc708dedSBarry Smith    Output Parameter:
4681cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4682cc708dedSBarry Smith 
4683487e0bb9SJed Brown    Level: beginner
4684cc708dedSBarry Smith 
4685cc708dedSBarry Smith    Notes:
4686cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4687cc708dedSBarry Smith 
4688cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4689cc708dedSBarry Smith 
4690cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4691cc708dedSBarry Smith @*/
4692cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4693cc708dedSBarry Smith {
4694cc708dedSBarry Smith   PetscFunctionBegin;
4695cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4696cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4697cc708dedSBarry Smith   *ftime = ts->solvetime;
4698cc708dedSBarry Smith   PetscFunctionReturn(0);
4699cc708dedSBarry Smith }
4700cc708dedSBarry Smith 
4701cc708dedSBarry Smith #undef __FUNCT__
47022c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
47032c18e0fdSBarry Smith /*@
47042c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
47052c18e0fdSBarry Smith 
47062c18e0fdSBarry Smith    Not Collective
47072c18e0fdSBarry Smith 
47082c18e0fdSBarry Smith    Input Parameter:
47092c18e0fdSBarry Smith .  ts - the TS context
47102c18e0fdSBarry Smith 
47112c18e0fdSBarry Smith    Output Parameter:
47122c18e0fdSBarry Smith .  steps - the number of steps
47132c18e0fdSBarry Smith 
47142c18e0fdSBarry Smith    Level: beginner
47152c18e0fdSBarry Smith 
47162c18e0fdSBarry Smith    Notes:
47172c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
47182c18e0fdSBarry Smith 
47192c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
47202c18e0fdSBarry Smith 
47212c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
47222c18e0fdSBarry Smith @*/
47232c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
47242c18e0fdSBarry Smith {
47252c18e0fdSBarry Smith   PetscFunctionBegin;
47262c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47272c18e0fdSBarry Smith   PetscValidPointer(steps,2);
47282c18e0fdSBarry Smith   *steps = ts->total_steps;
47292c18e0fdSBarry Smith   PetscFunctionReturn(0);
47302c18e0fdSBarry Smith }
47312c18e0fdSBarry Smith 
47322c18e0fdSBarry Smith #undef __FUNCT__
47335ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
47349f67acb7SJed Brown /*@
47355ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
47369f67acb7SJed Brown    used by the time integrator.
47379f67acb7SJed Brown 
47389f67acb7SJed Brown    Not Collective
47399f67acb7SJed Brown 
47409f67acb7SJed Brown    Input Parameter:
47419f67acb7SJed Brown .  ts - TS context
47429f67acb7SJed Brown 
47439f67acb7SJed Brown    Output Parameter:
47449f67acb7SJed Brown .  nits - number of nonlinear iterations
47459f67acb7SJed Brown 
47469f67acb7SJed Brown    Notes:
47479f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
47489f67acb7SJed Brown 
47499f67acb7SJed Brown    Level: intermediate
47509f67acb7SJed Brown 
47519f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
47529f67acb7SJed Brown 
47535ef26d82SJed Brown .seealso:  TSGetKSPIterations()
47549f67acb7SJed Brown @*/
47555ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
47569f67acb7SJed Brown {
47579f67acb7SJed Brown   PetscFunctionBegin;
47589f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47599f67acb7SJed Brown   PetscValidIntPointer(nits,2);
47605ef26d82SJed Brown   *nits = ts->snes_its;
47619f67acb7SJed Brown   PetscFunctionReturn(0);
47629f67acb7SJed Brown }
47639f67acb7SJed Brown 
47649f67acb7SJed Brown #undef __FUNCT__
47655ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
47669f67acb7SJed Brown /*@
47675ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
47689f67acb7SJed Brown    used by the time integrator.
47699f67acb7SJed Brown 
47709f67acb7SJed Brown    Not Collective
47719f67acb7SJed Brown 
47729f67acb7SJed Brown    Input Parameter:
47739f67acb7SJed Brown .  ts - TS context
47749f67acb7SJed Brown 
47759f67acb7SJed Brown    Output Parameter:
47769f67acb7SJed Brown .  lits - number of linear iterations
47779f67acb7SJed Brown 
47789f67acb7SJed Brown    Notes:
47799f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
47809f67acb7SJed Brown 
47819f67acb7SJed Brown    Level: intermediate
47829f67acb7SJed Brown 
47839f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
47849f67acb7SJed Brown 
47855ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
47869f67acb7SJed Brown @*/
47875ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
47889f67acb7SJed Brown {
47899f67acb7SJed Brown   PetscFunctionBegin;
47909f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47919f67acb7SJed Brown   PetscValidIntPointer(lits,2);
47925ef26d82SJed Brown   *lits = ts->ksp_its;
47939f67acb7SJed Brown   PetscFunctionReturn(0);
47949f67acb7SJed Brown }
47959f67acb7SJed Brown 
47969f67acb7SJed Brown #undef __FUNCT__
4797cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4798cef5090cSJed Brown /*@
4799cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4800cef5090cSJed Brown 
4801cef5090cSJed Brown    Not Collective
4802cef5090cSJed Brown 
4803cef5090cSJed Brown    Input Parameter:
4804cef5090cSJed Brown .  ts - TS context
4805cef5090cSJed Brown 
4806cef5090cSJed Brown    Output Parameter:
4807cef5090cSJed Brown .  rejects - number of steps rejected
4808cef5090cSJed Brown 
4809cef5090cSJed Brown    Notes:
4810cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4811cef5090cSJed Brown 
4812cef5090cSJed Brown    Level: intermediate
4813cef5090cSJed Brown 
4814cef5090cSJed Brown .keywords: TS, get, number
4815cef5090cSJed Brown 
48165ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4817cef5090cSJed Brown @*/
4818cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4819cef5090cSJed Brown {
4820cef5090cSJed Brown   PetscFunctionBegin;
4821cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4822cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4823cef5090cSJed Brown   *rejects = ts->reject;
4824cef5090cSJed Brown   PetscFunctionReturn(0);
4825cef5090cSJed Brown }
4826cef5090cSJed Brown 
4827cef5090cSJed Brown #undef __FUNCT__
4828cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4829cef5090cSJed Brown /*@
4830cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4831cef5090cSJed Brown 
4832cef5090cSJed Brown    Not Collective
4833cef5090cSJed Brown 
4834cef5090cSJed Brown    Input Parameter:
4835cef5090cSJed Brown .  ts - TS context
4836cef5090cSJed Brown 
4837cef5090cSJed Brown    Output Parameter:
4838cef5090cSJed Brown .  fails - number of failed nonlinear solves
4839cef5090cSJed Brown 
4840cef5090cSJed Brown    Notes:
4841cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4842cef5090cSJed Brown 
4843cef5090cSJed Brown    Level: intermediate
4844cef5090cSJed Brown 
4845cef5090cSJed Brown .keywords: TS, get, number
4846cef5090cSJed Brown 
48475ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4848cef5090cSJed Brown @*/
4849cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4850cef5090cSJed Brown {
4851cef5090cSJed Brown   PetscFunctionBegin;
4852cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4853cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4854cef5090cSJed Brown   *fails = ts->num_snes_failures;
4855cef5090cSJed Brown   PetscFunctionReturn(0);
4856cef5090cSJed Brown }
4857cef5090cSJed Brown 
4858cef5090cSJed Brown #undef __FUNCT__
4859cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4860cef5090cSJed Brown /*@
4861cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4862cef5090cSJed Brown 
4863cef5090cSJed Brown    Not Collective
4864cef5090cSJed Brown 
4865cef5090cSJed Brown    Input Parameter:
4866cef5090cSJed Brown +  ts - TS context
4867cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4868cef5090cSJed Brown 
4869cef5090cSJed Brown    Notes:
4870cef5090cSJed Brown    The counter is reset to zero for each step
4871cef5090cSJed Brown 
4872cef5090cSJed Brown    Options Database Key:
4873cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4874cef5090cSJed Brown 
4875cef5090cSJed Brown    Level: intermediate
4876cef5090cSJed Brown 
4877cef5090cSJed Brown .keywords: TS, set, maximum, number
4878cef5090cSJed Brown 
48795ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4880cef5090cSJed Brown @*/
4881cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4882cef5090cSJed Brown {
4883cef5090cSJed Brown   PetscFunctionBegin;
4884cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4885cef5090cSJed Brown   ts->max_reject = rejects;
4886cef5090cSJed Brown   PetscFunctionReturn(0);
4887cef5090cSJed Brown }
4888cef5090cSJed Brown 
4889cef5090cSJed Brown #undef __FUNCT__
4890cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4891cef5090cSJed Brown /*@
4892cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4893cef5090cSJed Brown 
4894cef5090cSJed Brown    Not Collective
4895cef5090cSJed Brown 
4896cef5090cSJed Brown    Input Parameter:
4897cef5090cSJed Brown +  ts - TS context
4898cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4899cef5090cSJed Brown 
4900cef5090cSJed Brown    Notes:
4901cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4902cef5090cSJed Brown 
4903cef5090cSJed Brown    Options Database Key:
4904cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4905cef5090cSJed Brown 
4906cef5090cSJed Brown    Level: intermediate
4907cef5090cSJed Brown 
4908cef5090cSJed Brown .keywords: TS, set, maximum, number
4909cef5090cSJed Brown 
49105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4911cef5090cSJed Brown @*/
4912cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4913cef5090cSJed Brown {
4914cef5090cSJed Brown   PetscFunctionBegin;
4915cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4916cef5090cSJed Brown   ts->max_snes_failures = fails;
4917cef5090cSJed Brown   PetscFunctionReturn(0);
4918cef5090cSJed Brown }
4919cef5090cSJed Brown 
4920cef5090cSJed Brown #undef __FUNCT__
49214e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4922cef5090cSJed Brown /*@
4923cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4924cef5090cSJed Brown 
4925cef5090cSJed Brown    Not Collective
4926cef5090cSJed Brown 
4927cef5090cSJed Brown    Input Parameter:
4928cef5090cSJed Brown +  ts - TS context
4929cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4930cef5090cSJed Brown 
4931cef5090cSJed Brown    Options Database Key:
4932cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4933cef5090cSJed Brown 
4934cef5090cSJed Brown    Level: intermediate
4935cef5090cSJed Brown 
4936cef5090cSJed Brown .keywords: TS, set, error
4937cef5090cSJed Brown 
49385ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4939cef5090cSJed Brown @*/
4940cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4941cef5090cSJed Brown {
4942cef5090cSJed Brown   PetscFunctionBegin;
4943cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4944cef5090cSJed Brown   ts->errorifstepfailed = err;
4945cef5090cSJed Brown   PetscFunctionReturn(0);
4946cef5090cSJed Brown }
4947cef5090cSJed Brown 
4948cef5090cSJed Brown #undef __FUNCT__
4949fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4950fb1732b5SBarry Smith /*@C
4951fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4952fb1732b5SBarry Smith 
4953fb1732b5SBarry Smith    Collective on TS
4954fb1732b5SBarry Smith 
4955fb1732b5SBarry Smith    Input Parameters:
4956fb1732b5SBarry Smith +  ts - the TS context
4957fb1732b5SBarry Smith .  step - current time-step
4958fb1732b5SBarry Smith .  ptime - current time
49590910c330SBarry Smith .  u - current state
4960fb1732b5SBarry Smith -  viewer - binary viewer
4961fb1732b5SBarry Smith 
4962fb1732b5SBarry Smith    Level: intermediate
4963fb1732b5SBarry Smith 
4964fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4965fb1732b5SBarry Smith 
4966fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4967fb1732b5SBarry Smith @*/
49680910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4969fb1732b5SBarry Smith {
4970fb1732b5SBarry Smith   PetscErrorCode ierr;
4971ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4972fb1732b5SBarry Smith 
4973fb1732b5SBarry Smith   PetscFunctionBegin;
49740910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4975ed81e22dSJed Brown   PetscFunctionReturn(0);
4976ed81e22dSJed Brown }
4977ed81e22dSJed Brown 
4978ed81e22dSJed Brown #undef __FUNCT__
4979ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4980ed81e22dSJed Brown /*@C
4981ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4982ed81e22dSJed Brown 
4983ed81e22dSJed Brown    Collective on TS
4984ed81e22dSJed Brown 
4985ed81e22dSJed Brown    Input Parameters:
4986ed81e22dSJed Brown +  ts - the TS context
4987ed81e22dSJed Brown .  step - current time-step
4988ed81e22dSJed Brown .  ptime - current time
49890910c330SBarry Smith .  u - current state
4990ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4991ed81e22dSJed Brown 
4992ed81e22dSJed Brown    Level: intermediate
4993ed81e22dSJed Brown 
4994ed81e22dSJed Brown    Notes:
4995ed81e22dSJed 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.
4996ed81e22dSJed Brown    These are named according to the file name template.
4997ed81e22dSJed Brown 
4998ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4999ed81e22dSJed Brown 
5000ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5001ed81e22dSJed Brown 
5002ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5003ed81e22dSJed Brown @*/
50040910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5005ed81e22dSJed Brown {
5006ed81e22dSJed Brown   PetscErrorCode ierr;
5007ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5008ed81e22dSJed Brown   PetscViewer    viewer;
5009ed81e22dSJed Brown 
5010ed81e22dSJed Brown   PetscFunctionBegin;
50118caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5012ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
50130910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5014ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5015ed81e22dSJed Brown   PetscFunctionReturn(0);
5016ed81e22dSJed Brown }
5017ed81e22dSJed Brown 
5018ed81e22dSJed Brown #undef __FUNCT__
5019ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
5020ed81e22dSJed Brown /*@C
5021ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5022ed81e22dSJed Brown 
5023ed81e22dSJed Brown    Collective on TS
5024ed81e22dSJed Brown 
5025ed81e22dSJed Brown    Input Parameters:
5026ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5027ed81e22dSJed Brown 
5028ed81e22dSJed Brown    Level: intermediate
5029ed81e22dSJed Brown 
5030ed81e22dSJed Brown    Note:
5031ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5032ed81e22dSJed Brown 
5033ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5034ed81e22dSJed Brown 
5035ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5036ed81e22dSJed Brown @*/
5037ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5038ed81e22dSJed Brown {
5039ed81e22dSJed Brown   PetscErrorCode ierr;
5040ed81e22dSJed Brown 
5041ed81e22dSJed Brown   PetscFunctionBegin;
5042ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5043fb1732b5SBarry Smith   PetscFunctionReturn(0);
5044fb1732b5SBarry Smith }
5045fb1732b5SBarry Smith 
504684df9cb4SJed Brown #undef __FUNCT__
5047552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
504884df9cb4SJed Brown /*@
5049552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
505084df9cb4SJed Brown 
5051ed81e22dSJed Brown    Collective on TS if controller has not been created yet
505284df9cb4SJed Brown 
505384df9cb4SJed Brown    Input Arguments:
5054ed81e22dSJed Brown .  ts - time stepping context
505584df9cb4SJed Brown 
505684df9cb4SJed Brown    Output Arguments:
5057ed81e22dSJed Brown .  adapt - adaptive controller
505884df9cb4SJed Brown 
505984df9cb4SJed Brown    Level: intermediate
506084df9cb4SJed Brown 
5061ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
506284df9cb4SJed Brown @*/
5063552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
506484df9cb4SJed Brown {
506584df9cb4SJed Brown   PetscErrorCode ierr;
506684df9cb4SJed Brown 
506784df9cb4SJed Brown   PetscFunctionBegin;
506884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
506984df9cb4SJed Brown   PetscValidPointer(adapt,2);
507084df9cb4SJed Brown   if (!ts->adapt) {
5071ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
50723bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
50731c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
507484df9cb4SJed Brown   }
507584df9cb4SJed Brown   *adapt = ts->adapt;
507684df9cb4SJed Brown   PetscFunctionReturn(0);
507784df9cb4SJed Brown }
5078d6ebe24aSShri Abhyankar 
5079d6ebe24aSShri Abhyankar #undef __FUNCT__
50801c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
50811c3436cfSJed Brown /*@
50821c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
50831c3436cfSJed Brown 
50841c3436cfSJed Brown    Logically Collective
50851c3436cfSJed Brown 
50861c3436cfSJed Brown    Input Arguments:
50871c3436cfSJed Brown +  ts - time integration context
50881c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
50890298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
50901c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
50910298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
50921c3436cfSJed Brown 
5093a3cdaa26SBarry Smith    Options Database keys:
5094a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5095a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5096a3cdaa26SBarry Smith 
50973ff766beSShri Abhyankar    Notes:
50983ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
50993ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
51003ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
51013ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
51023ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
51033ff766beSShri Abhyankar    differential variables.
51043ff766beSShri Abhyankar 
51051c3436cfSJed Brown    Level: beginner
51061c3436cfSJed Brown 
5107c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
51081c3436cfSJed Brown @*/
51091c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
51101c3436cfSJed Brown {
51111c3436cfSJed Brown   PetscErrorCode ierr;
51121c3436cfSJed Brown 
51131c3436cfSJed Brown   PetscFunctionBegin;
5114c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
51151c3436cfSJed Brown   if (vatol) {
51161c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
51171c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
5118bbd56ea5SKarl Rupp 
51191c3436cfSJed Brown     ts->vatol = vatol;
51201c3436cfSJed Brown   }
5121c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
51221c3436cfSJed Brown   if (vrtol) {
51231c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
51241c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
5125bbd56ea5SKarl Rupp 
51261c3436cfSJed Brown     ts->vrtol = vrtol;
51271c3436cfSJed Brown   }
51281c3436cfSJed Brown   PetscFunctionReturn(0);
51291c3436cfSJed Brown }
51301c3436cfSJed Brown 
51311c3436cfSJed Brown #undef __FUNCT__
5132c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
5133c5033834SJed Brown /*@
5134c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5135c5033834SJed Brown 
5136c5033834SJed Brown    Logically Collective
5137c5033834SJed Brown 
5138c5033834SJed Brown    Input Arguments:
5139c5033834SJed Brown .  ts - time integration context
5140c5033834SJed Brown 
5141c5033834SJed Brown    Output Arguments:
51420298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
51430298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
51440298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
51450298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5146c5033834SJed Brown 
5147c5033834SJed Brown    Level: beginner
5148c5033834SJed Brown 
5149c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5150c5033834SJed Brown @*/
5151c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5152c5033834SJed Brown {
5153c5033834SJed Brown   PetscFunctionBegin;
5154c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5155c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5156c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5157c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5158c5033834SJed Brown   PetscFunctionReturn(0);
5159c5033834SJed Brown }
5160c5033834SJed Brown 
5161c5033834SJed Brown #undef __FUNCT__
51629c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
51639c6b16b5SShri Abhyankar /*@
5164a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
51659c6b16b5SShri Abhyankar 
51669c6b16b5SShri Abhyankar    Collective on TS
51679c6b16b5SShri Abhyankar 
51689c6b16b5SShri Abhyankar    Input Arguments:
51699c6b16b5SShri Abhyankar +  ts - time stepping context
5170a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5171a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
51729c6b16b5SShri Abhyankar 
51739c6b16b5SShri Abhyankar    Output Arguments:
51749c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
51759c6b16b5SShri Abhyankar 
51769c6b16b5SShri Abhyankar    Level: developer
51779c6b16b5SShri Abhyankar 
5178deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
51799c6b16b5SShri Abhyankar @*/
5180a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm)
51819c6b16b5SShri Abhyankar {
51829c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
51839c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
51849c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
51859c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
51869c6b16b5SShri Abhyankar   PetscReal         tol;
51879c6b16b5SShri Abhyankar 
51889c6b16b5SShri Abhyankar   PetscFunctionBegin;
51899c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5190a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5191a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5192a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5193a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5194a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5195a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5196a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
51979c6b16b5SShri Abhyankar 
51989c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
51999c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
52009c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
52019c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
52029c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
52039c6b16b5SShri Abhyankar   sum  = 0.;
52049c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
52059c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
52069c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52079c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52089c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
52099c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52109c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
52119c6b16b5SShri Abhyankar     }
52129c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52139c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52149c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
52159c6b16b5SShri Abhyankar     const PetscScalar *atol;
52169c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52179c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
52189c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52199c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
52209c6b16b5SShri Abhyankar     }
52219c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52229c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
52239c6b16b5SShri Abhyankar     const PetscScalar *rtol;
52249c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52259c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
52269c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52279c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
52289c6b16b5SShri Abhyankar     }
52299c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52309c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
52319c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
52329c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52339c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
52349c6b16b5SShri Abhyankar     }
52359c6b16b5SShri Abhyankar   }
52369c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
52379c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
52389c6b16b5SShri Abhyankar 
52399c6b16b5SShri Abhyankar   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
52409c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
52419c6b16b5SShri Abhyankar 
52429c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
52439c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
52449c6b16b5SShri Abhyankar }
52459c6b16b5SShri Abhyankar 
52469c6b16b5SShri Abhyankar #undef __FUNCT__
52479c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
52489c6b16b5SShri Abhyankar /*@
5249a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
52509c6b16b5SShri Abhyankar 
52519c6b16b5SShri Abhyankar    Collective on TS
52529c6b16b5SShri Abhyankar 
52539c6b16b5SShri Abhyankar    Input Arguments:
52549c6b16b5SShri Abhyankar +  ts - time stepping context
5255a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5256a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
52579c6b16b5SShri Abhyankar 
52589c6b16b5SShri Abhyankar    Output Arguments:
52599c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
52609c6b16b5SShri Abhyankar 
52619c6b16b5SShri Abhyankar    Level: developer
52629c6b16b5SShri Abhyankar 
5263deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
52649c6b16b5SShri Abhyankar @*/
5265a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm)
52669c6b16b5SShri Abhyankar {
52679c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
52689c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
52699c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
52709c6b16b5SShri Abhyankar   PetscReal         max,gmax;
52719c6b16b5SShri Abhyankar   PetscReal         tol;
52729c6b16b5SShri Abhyankar 
52739c6b16b5SShri Abhyankar   PetscFunctionBegin;
52749c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5275a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5276a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5277a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5278a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5279a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5280a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5281a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
52829c6b16b5SShri Abhyankar 
52839c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
52849c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
52859c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
52869c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
52879c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
52889c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
52899c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
52909c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52919c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52929c6b16b5SShri Abhyankar     k = 0;
52939c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52949c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
52959c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
52969c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52979c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
52989c6b16b5SShri Abhyankar     }
52999c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53009c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53019c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
53029c6b16b5SShri Abhyankar     const PetscScalar *atol;
53039c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53049c6b16b5SShri Abhyankar     k = 0;
53059c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
53069c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
53079c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
53089c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53099c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
53109c6b16b5SShri Abhyankar     }
53119c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
53129c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
53139c6b16b5SShri Abhyankar     const PetscScalar *rtol;
53149c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53159c6b16b5SShri Abhyankar     k = 0;
53169c6b16b5SShri Abhyankar     tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
53179c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
53189c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
53199c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53209c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
53219c6b16b5SShri Abhyankar     }
53229c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
53239c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
53249c6b16b5SShri Abhyankar     k = 0;
53259c6b16b5SShri Abhyankar     tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
53269c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
53279c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
53289c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
53299c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
53309c6b16b5SShri Abhyankar     }
53319c6b16b5SShri Abhyankar   }
53329c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
53339c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
53349c6b16b5SShri Abhyankar 
53359c6b16b5SShri Abhyankar   ierr  = MPI_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
53369c6b16b5SShri Abhyankar   *norm = gmax;
53379c6b16b5SShri Abhyankar 
53389c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
53399c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
53409c6b16b5SShri Abhyankar }
53419c6b16b5SShri Abhyankar 
53429c6b16b5SShri Abhyankar #undef __FUNCT__
53437619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
53441c3436cfSJed Brown /*@
5345a4868fbcSLisandro Dalcin    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors
53461c3436cfSJed Brown 
53471c3436cfSJed Brown    Collective on TS
53481c3436cfSJed Brown 
53491c3436cfSJed Brown    Input Arguments:
53501c3436cfSJed Brown +  ts - time stepping context
5351a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5352a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5353a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
53547619abb3SShri 
53551c3436cfSJed Brown    Output Arguments:
53561c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
53571c3436cfSJed Brown 
5358a4868fbcSLisandro Dalcin 
5359a4868fbcSLisandro Dalcin    Options Database Keys:
5360a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5361a4868fbcSLisandro Dalcin 
53621c3436cfSJed Brown    Level: developer
53631c3436cfSJed Brown 
5364deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
53651c3436cfSJed Brown @*/
5366a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm)
53671c3436cfSJed Brown {
53688beabaa1SBarry Smith   PetscErrorCode ierr;
53691c3436cfSJed Brown 
53701c3436cfSJed Brown   PetscFunctionBegin;
5371a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
5372a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr);
5373a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
5374a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr);
5375a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
53761c3436cfSJed Brown   PetscFunctionReturn(0);
53771c3436cfSJed Brown }
53781c3436cfSJed Brown 
53791c3436cfSJed Brown #undef __FUNCT__
53808d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
53818d59e960SJed Brown /*@
53828d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
53838d59e960SJed Brown 
53848d59e960SJed Brown    Logically Collective on TS
53858d59e960SJed Brown 
53868d59e960SJed Brown    Input Arguments:
53878d59e960SJed Brown +  ts - time stepping context
53888d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
53898d59e960SJed Brown 
53908d59e960SJed Brown    Note:
53918d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
53928d59e960SJed Brown 
53938d59e960SJed Brown    Level: intermediate
53948d59e960SJed Brown 
53958d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
53968d59e960SJed Brown @*/
53978d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
53988d59e960SJed Brown {
53998d59e960SJed Brown   PetscFunctionBegin;
54008d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54018d59e960SJed Brown   ts->cfltime_local = cfltime;
54028d59e960SJed Brown   ts->cfltime       = -1.;
54038d59e960SJed Brown   PetscFunctionReturn(0);
54048d59e960SJed Brown }
54058d59e960SJed Brown 
54068d59e960SJed Brown #undef __FUNCT__
54078d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
54088d59e960SJed Brown /*@
54098d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
54108d59e960SJed Brown 
54118d59e960SJed Brown    Collective on TS
54128d59e960SJed Brown 
54138d59e960SJed Brown    Input Arguments:
54148d59e960SJed Brown .  ts - time stepping context
54158d59e960SJed Brown 
54168d59e960SJed Brown    Output Arguments:
54178d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
54188d59e960SJed Brown 
54198d59e960SJed Brown    Level: advanced
54208d59e960SJed Brown 
54218d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
54228d59e960SJed Brown @*/
54238d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
54248d59e960SJed Brown {
54258d59e960SJed Brown   PetscErrorCode ierr;
54268d59e960SJed Brown 
54278d59e960SJed Brown   PetscFunctionBegin;
54288d59e960SJed Brown   if (ts->cfltime < 0) {
5429ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
54308d59e960SJed Brown   }
54318d59e960SJed Brown   *cfltime = ts->cfltime;
54328d59e960SJed Brown   PetscFunctionReturn(0);
54338d59e960SJed Brown }
54348d59e960SJed Brown 
54358d59e960SJed Brown #undef __FUNCT__
5436d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5437d6ebe24aSShri Abhyankar /*@
5438d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5439d6ebe24aSShri Abhyankar 
5440d6ebe24aSShri Abhyankar    Input Parameters:
5441d6ebe24aSShri Abhyankar .  ts   - the TS context.
5442d6ebe24aSShri Abhyankar .  xl   - lower bound.
5443d6ebe24aSShri Abhyankar .  xu   - upper bound.
5444d6ebe24aSShri Abhyankar 
5445d6ebe24aSShri Abhyankar    Notes:
5446d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5447e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5448d6ebe24aSShri Abhyankar 
54492bd2b0e6SSatish Balay    Level: advanced
54502bd2b0e6SSatish Balay 
5451d6ebe24aSShri Abhyankar @*/
5452d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5453d6ebe24aSShri Abhyankar {
5454d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5455d6ebe24aSShri Abhyankar   SNES           snes;
5456d6ebe24aSShri Abhyankar 
5457d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5458d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5459d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5460d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5461d6ebe24aSShri Abhyankar }
5462d6ebe24aSShri Abhyankar 
5463325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5464c6db04a5SJed Brown #include <mex.h>
5465325fc9f4SBarry Smith 
5466325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5467325fc9f4SBarry Smith 
5468325fc9f4SBarry Smith #undef __FUNCT__
5469325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5470325fc9f4SBarry Smith /*
5471325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5472325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5473325fc9f4SBarry Smith 
5474325fc9f4SBarry Smith    Collective on TS
5475325fc9f4SBarry Smith 
5476325fc9f4SBarry Smith    Input Parameters:
5477325fc9f4SBarry Smith +  snes - the TS context
54780910c330SBarry Smith -  u - input vector
5479325fc9f4SBarry Smith 
5480325fc9f4SBarry Smith    Output Parameter:
5481325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5482325fc9f4SBarry Smith 
5483325fc9f4SBarry Smith    Notes:
5484325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5485325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5486325fc9f4SBarry Smith    themselves.
5487325fc9f4SBarry Smith 
5488325fc9f4SBarry Smith    Level: developer
5489325fc9f4SBarry Smith 
5490325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5491325fc9f4SBarry Smith 
5492325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5493325fc9f4SBarry Smith */
54940910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5495325fc9f4SBarry Smith {
5496325fc9f4SBarry Smith   PetscErrorCode  ierr;
5497325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5498325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5499325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5500325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5501325fc9f4SBarry Smith 
5502325fc9f4SBarry Smith   PetscFunctionBegin;
5503325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
55040910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
55050910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5506325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
55070910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5508325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5509325fc9f4SBarry Smith 
5510325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
55110910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
55120910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
55130910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5514bbd56ea5SKarl Rupp 
5515325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5516325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5517325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5518325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5519325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5520325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5521325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5522325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5523325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5524325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5525325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5526325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5527325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5528325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5529325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5530325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5531325fc9f4SBarry Smith   PetscFunctionReturn(0);
5532325fc9f4SBarry Smith }
5533325fc9f4SBarry Smith 
5534325fc9f4SBarry Smith 
5535325fc9f4SBarry Smith #undef __FUNCT__
5536325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5537325fc9f4SBarry Smith /*
5538325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5539325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5540e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5541325fc9f4SBarry Smith 
5542325fc9f4SBarry Smith    Logically Collective on TS
5543325fc9f4SBarry Smith 
5544325fc9f4SBarry Smith    Input Parameters:
5545325fc9f4SBarry Smith +  ts - the TS context
5546325fc9f4SBarry Smith -  func - function evaluation routine
5547325fc9f4SBarry Smith 
5548325fc9f4SBarry Smith    Calling sequence of func:
55490910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5550325fc9f4SBarry Smith 
5551325fc9f4SBarry Smith    Level: beginner
5552325fc9f4SBarry Smith 
5553325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5554325fc9f4SBarry Smith 
5555325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5556325fc9f4SBarry Smith */
5557cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5558325fc9f4SBarry Smith {
5559325fc9f4SBarry Smith   PetscErrorCode  ierr;
5560325fc9f4SBarry Smith   TSMatlabContext *sctx;
5561325fc9f4SBarry Smith 
5562325fc9f4SBarry Smith   PetscFunctionBegin;
5563325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5564325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5565325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5566325fc9f4SBarry Smith   /*
5567325fc9f4SBarry Smith      This should work, but it doesn't
5568325fc9f4SBarry Smith   sctx->ctx = ctx;
5569325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5570325fc9f4SBarry Smith   */
5571325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5572bbd56ea5SKarl Rupp 
55730298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5574325fc9f4SBarry Smith   PetscFunctionReturn(0);
5575325fc9f4SBarry Smith }
5576325fc9f4SBarry Smith 
5577325fc9f4SBarry Smith #undef __FUNCT__
5578325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5579325fc9f4SBarry Smith /*
5580325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5581325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5582325fc9f4SBarry Smith 
5583325fc9f4SBarry Smith    Collective on TS
5584325fc9f4SBarry Smith 
5585325fc9f4SBarry Smith    Input Parameters:
5586cdcf91faSSean Farley +  ts - the TS context
55870910c330SBarry Smith .  u - input vector
5588325fc9f4SBarry Smith .  A, B - the matrices
5589325fc9f4SBarry Smith -  ctx - user context
5590325fc9f4SBarry Smith 
5591325fc9f4SBarry Smith    Level: developer
5592325fc9f4SBarry Smith 
5593325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5594325fc9f4SBarry Smith 
5595325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5596325fc9f4SBarry Smith @*/
5597f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5598325fc9f4SBarry Smith {
5599325fc9f4SBarry Smith   PetscErrorCode  ierr;
5600325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5601325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5602325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5603325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5604325fc9f4SBarry Smith 
5605325fc9f4SBarry Smith   PetscFunctionBegin;
5606cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56070910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5608325fc9f4SBarry Smith 
56090910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5610325fc9f4SBarry Smith 
5611cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
56120910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
56130910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
56140910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
56150910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5616bbd56ea5SKarl Rupp 
5617325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5618325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5619325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5620325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5621325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5622325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5623325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5624325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5625325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5626325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5627325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5628325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5629325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5630325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5631325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5632325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5633325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5634325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5635325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5636325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5637325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5638325fc9f4SBarry Smith   PetscFunctionReturn(0);
5639325fc9f4SBarry Smith }
5640325fc9f4SBarry Smith 
5641325fc9f4SBarry Smith 
5642325fc9f4SBarry Smith #undef __FUNCT__
5643325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5644325fc9f4SBarry Smith /*
5645325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5646e3c5b3baSBarry 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
5647325fc9f4SBarry Smith 
5648325fc9f4SBarry Smith    Logically Collective on TS
5649325fc9f4SBarry Smith 
5650325fc9f4SBarry Smith    Input Parameters:
5651cdcf91faSSean Farley +  ts - the TS context
5652325fc9f4SBarry Smith .  A,B - Jacobian matrices
5653325fc9f4SBarry Smith .  func - function evaluation routine
5654325fc9f4SBarry Smith -  ctx - user context
5655325fc9f4SBarry Smith 
5656325fc9f4SBarry Smith    Calling sequence of func:
56570910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5658325fc9f4SBarry Smith 
5659325fc9f4SBarry Smith 
5660325fc9f4SBarry Smith    Level: developer
5661325fc9f4SBarry Smith 
5662325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5663325fc9f4SBarry Smith 
5664325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5665325fc9f4SBarry Smith */
5666cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5667325fc9f4SBarry Smith {
5668325fc9f4SBarry Smith   PetscErrorCode  ierr;
5669325fc9f4SBarry Smith   TSMatlabContext *sctx;
5670325fc9f4SBarry Smith 
5671325fc9f4SBarry Smith   PetscFunctionBegin;
5672325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5673325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5674325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5675325fc9f4SBarry Smith   /*
5676325fc9f4SBarry Smith      This should work, but it doesn't
5677325fc9f4SBarry Smith   sctx->ctx = ctx;
5678325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5679325fc9f4SBarry Smith   */
5680325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5681bbd56ea5SKarl Rupp 
5682cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5683325fc9f4SBarry Smith   PetscFunctionReturn(0);
5684325fc9f4SBarry Smith }
5685325fc9f4SBarry Smith 
5686b5b1a830SBarry Smith #undef __FUNCT__
5687b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5688b5b1a830SBarry Smith /*
5689b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5690b5b1a830SBarry Smith 
5691b5b1a830SBarry Smith    Collective on TS
5692b5b1a830SBarry Smith 
5693b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5694b5b1a830SBarry Smith @*/
56950910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5696b5b1a830SBarry Smith {
5697b5b1a830SBarry Smith   PetscErrorCode  ierr;
5698b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5699a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5700b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5701b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5702b5b1a830SBarry Smith 
5703b5b1a830SBarry Smith   PetscFunctionBegin;
5704cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57050910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5706b5b1a830SBarry Smith 
5707cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
57080910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5709bbd56ea5SKarl Rupp 
5710b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5711b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5712b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5713b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5714b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5715b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5716b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5717b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5718b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5719b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5720b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5721b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5722b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5723b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5724b5b1a830SBarry Smith   PetscFunctionReturn(0);
5725b5b1a830SBarry Smith }
5726b5b1a830SBarry Smith 
5727b5b1a830SBarry Smith 
5728b5b1a830SBarry Smith #undef __FUNCT__
5729b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5730b5b1a830SBarry Smith /*
5731b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5732b5b1a830SBarry Smith 
5733b5b1a830SBarry Smith    Level: developer
5734b5b1a830SBarry Smith 
5735b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5736b5b1a830SBarry Smith 
5737b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5738b5b1a830SBarry Smith */
5739cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5740b5b1a830SBarry Smith {
5741b5b1a830SBarry Smith   PetscErrorCode  ierr;
5742b5b1a830SBarry Smith   TSMatlabContext *sctx;
5743b5b1a830SBarry Smith 
5744b5b1a830SBarry Smith   PetscFunctionBegin;
5745b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5746b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5747b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5748b5b1a830SBarry Smith   /*
5749b5b1a830SBarry Smith      This should work, but it doesn't
5750b5b1a830SBarry Smith   sctx->ctx = ctx;
5751b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5752b5b1a830SBarry Smith   */
5753b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5754bbd56ea5SKarl Rupp 
57550298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5756b5b1a830SBarry Smith   PetscFunctionReturn(0);
5757b5b1a830SBarry Smith }
5758325fc9f4SBarry Smith #endif
5759b3603a34SBarry Smith 
5760b3603a34SBarry Smith #undef __FUNCT__
57614f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5762b3603a34SBarry Smith /*@C
57634f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5764b3603a34SBarry Smith        in a time based line graph
5765b3603a34SBarry Smith 
5766b3603a34SBarry Smith    Collective on TS
5767b3603a34SBarry Smith 
5768b3603a34SBarry Smith    Input Parameters:
5769b3603a34SBarry Smith +  ts - the TS context
5770b3603a34SBarry Smith .  step - current time-step
5771b3603a34SBarry Smith .  ptime - current time
5772b3603a34SBarry Smith -  lg - a line graph object
5773b3603a34SBarry Smith 
5774b3d3934dSBarry Smith    Options Database:
57759ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
5776b3d3934dSBarry Smith 
5777b3603a34SBarry Smith    Level: intermediate
5778b3603a34SBarry Smith 
57790b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5780b3603a34SBarry Smith 
5781b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5782b3603a34SBarry Smith 
5783b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5784b3603a34SBarry Smith @*/
57850910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5786b3603a34SBarry Smith {
5787b3603a34SBarry Smith   PetscErrorCode    ierr;
57880b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5789b3603a34SBarry Smith   const PetscScalar *yy;
579058ff32f7SBarry Smith   PetscInt          dim;
579180666b62SBarry Smith   Vec               v;
5792b3603a34SBarry Smith 
5793b3603a34SBarry Smith   PetscFunctionBegin;
579458ff32f7SBarry Smith   if (!step) {
5795a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5796a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5797a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
5798387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
5799387f4636SBarry Smith       char      **displaynames;
5800387f4636SBarry Smith       PetscBool flg;
5801387f4636SBarry Smith 
5802387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5803387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
5804387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
58059ae14b6eSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
5806387f4636SBarry Smith       if (flg) {
5807a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
5808387f4636SBarry Smith       }
5809387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
5810387f4636SBarry Smith     }
5811387f4636SBarry Smith     if (ctx->displaynames) {
5812387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
5813387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
5814387f4636SBarry Smith     } else if (ctx->names) {
58150910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
58160b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5817387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
5818387f4636SBarry Smith     }
58190b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
582058ff32f7SBarry Smith   }
582180666b62SBarry Smith   if (ctx->transform) {
5822e673d494SBarry Smith     ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);
582380666b62SBarry Smith   } else {
582480666b62SBarry Smith     v = u;
582580666b62SBarry Smith   }
582680666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
5827e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5828e3efe391SJed Brown   {
5829e3efe391SJed Brown     PetscReal *yreal;
5830e3efe391SJed Brown     PetscInt  i,n;
583180666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
5832785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5833e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
58340b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5835e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5836e3efe391SJed Brown   }
5837e3efe391SJed Brown #else
5838387f4636SBarry Smith   if (ctx->displaynames) {
5839387f4636SBarry Smith     PetscInt i;
5840387f4636SBarry Smith     for (i=0; i<ctx->ndisplayvariables; i++) {
5841387f4636SBarry Smith       ctx->displayvalues[i] = yy[ctx->displayvariables[i]];
5842387f4636SBarry Smith     }
5843387f4636SBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
5844387f4636SBarry Smith   } else {
58450b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5846387f4636SBarry Smith   }
5847e3efe391SJed Brown #endif
584880666b62SBarry Smith   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
584980666b62SBarry Smith   if (ctx->transform) {
585080666b62SBarry Smith     ierr = VecDestroy(&v);CHKERRQ(ierr);
585180666b62SBarry Smith   }
5852b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
58530b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
58543923b477SBarry Smith   }
5855b3603a34SBarry Smith   PetscFunctionReturn(0);
5856b3603a34SBarry Smith }
5857b3603a34SBarry Smith 
5858387f4636SBarry Smith 
5859b3603a34SBarry Smith #undef __FUNCT__
586031152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
5861b037adc7SBarry Smith /*@C
586231152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
5863b037adc7SBarry Smith 
5864b037adc7SBarry Smith    Collective on TS
5865b037adc7SBarry Smith 
5866b037adc7SBarry Smith    Input Parameters:
5867b037adc7SBarry Smith +  ts - the TS context
5868b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
5869b037adc7SBarry Smith 
5870b037adc7SBarry Smith    Level: intermediate
5871b037adc7SBarry Smith 
5872b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
5873b037adc7SBarry Smith 
5874a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
5875b037adc7SBarry Smith @*/
587631152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
5877b037adc7SBarry Smith {
5878b037adc7SBarry Smith   PetscErrorCode    ierr;
5879b037adc7SBarry Smith   PetscInt          i;
5880b037adc7SBarry Smith 
5881b037adc7SBarry Smith   PetscFunctionBegin;
5882b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5883b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
58845537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
5885b3d3934dSBarry Smith       break;
5886b3d3934dSBarry Smith     }
5887b3d3934dSBarry Smith   }
5888b3d3934dSBarry Smith   PetscFunctionReturn(0);
5889b3d3934dSBarry Smith }
5890b3d3934dSBarry Smith 
5891b3d3934dSBarry Smith #undef __FUNCT__
5892e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
5893e673d494SBarry Smith /*@C
5894e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
5895e673d494SBarry Smith 
5896e673d494SBarry Smith    Collective on TS
5897e673d494SBarry Smith 
5898e673d494SBarry Smith    Input Parameters:
5899e673d494SBarry Smith +  ts - the TS context
5900e673d494SBarry Smith -  names - the names of the components, final string must be NULL
5901e673d494SBarry Smith 
5902e673d494SBarry Smith    Level: intermediate
5903e673d494SBarry Smith 
5904e673d494SBarry Smith .keywords: TS,  vector, monitor, view
5905e673d494SBarry Smith 
5906a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
5907e673d494SBarry Smith @*/
5908e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
5909e673d494SBarry Smith {
5910e673d494SBarry Smith   PetscErrorCode    ierr;
5911e673d494SBarry Smith 
5912e673d494SBarry Smith   PetscFunctionBegin;
5913e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
5914e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
5915e673d494SBarry Smith   PetscFunctionReturn(0);
5916e673d494SBarry Smith }
5917e673d494SBarry Smith 
5918e673d494SBarry Smith #undef __FUNCT__
5919b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
5920b3d3934dSBarry Smith /*@C
5921b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
5922b3d3934dSBarry Smith 
5923b3d3934dSBarry Smith    Collective on TS
5924b3d3934dSBarry Smith 
5925b3d3934dSBarry Smith    Input Parameter:
5926b3d3934dSBarry Smith .  ts - the TS context
5927b3d3934dSBarry Smith 
5928b3d3934dSBarry Smith    Output Parameter:
5929b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
5930b3d3934dSBarry Smith 
5931b3d3934dSBarry Smith    Level: intermediate
5932b3d3934dSBarry Smith 
5933b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
5934b3d3934dSBarry Smith 
5935b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
5936b3d3934dSBarry Smith @*/
5937b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
5938b3d3934dSBarry Smith {
5939b3d3934dSBarry Smith   PetscInt       i;
5940b3d3934dSBarry Smith 
5941b3d3934dSBarry Smith   PetscFunctionBegin;
5942b3d3934dSBarry Smith   *names = NULL;
5943b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5944b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
59455537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
5946b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
5947b3d3934dSBarry Smith       break;
5948387f4636SBarry Smith     }
5949387f4636SBarry Smith   }
5950387f4636SBarry Smith   PetscFunctionReturn(0);
5951387f4636SBarry Smith }
5952387f4636SBarry Smith 
5953387f4636SBarry Smith #undef __FUNCT__
5954a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
5955a66092f1SBarry Smith /*@C
5956a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
5957a66092f1SBarry Smith 
5958a66092f1SBarry Smith    Collective on TS
5959a66092f1SBarry Smith 
5960a66092f1SBarry Smith    Input Parameters:
5961a66092f1SBarry Smith +  ctx - the TSMonitorLG context
5962a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
5963a66092f1SBarry Smith 
5964a66092f1SBarry Smith    Level: intermediate
5965a66092f1SBarry Smith 
5966a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
5967a66092f1SBarry Smith 
5968a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
5969a66092f1SBarry Smith @*/
5970a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
5971a66092f1SBarry Smith {
5972a66092f1SBarry Smith   PetscInt          j = 0,k;
5973a66092f1SBarry Smith   PetscErrorCode    ierr;
5974a66092f1SBarry Smith 
5975a66092f1SBarry Smith   PetscFunctionBegin;
5976a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
5977a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
5978a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
5979a66092f1SBarry Smith   while (displaynames[j]) j++;
5980a66092f1SBarry Smith   ctx->ndisplayvariables = j;
5981a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
5982a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
5983a66092f1SBarry Smith   j = 0;
5984a66092f1SBarry Smith   while (displaynames[j]) {
5985a66092f1SBarry Smith     k = 0;
5986a66092f1SBarry Smith     while (ctx->names[k]) {
5987a66092f1SBarry Smith       PetscBool flg;
5988a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
5989a66092f1SBarry Smith       if (flg) {
5990a66092f1SBarry Smith         ctx->displayvariables[j] = k;
5991a66092f1SBarry Smith         break;
5992a66092f1SBarry Smith       }
5993a66092f1SBarry Smith       k++;
5994a66092f1SBarry Smith     }
5995a66092f1SBarry Smith     j++;
5996a66092f1SBarry Smith   }
5997a66092f1SBarry Smith   PetscFunctionReturn(0);
5998a66092f1SBarry Smith }
5999a66092f1SBarry Smith 
6000a66092f1SBarry Smith 
6001a66092f1SBarry Smith #undef __FUNCT__
6002387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
6003387f4636SBarry Smith /*@C
6004387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6005387f4636SBarry Smith 
6006387f4636SBarry Smith    Collective on TS
6007387f4636SBarry Smith 
6008387f4636SBarry Smith    Input Parameters:
6009387f4636SBarry Smith +  ts - the TS context
6010387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6011387f4636SBarry Smith 
6012387f4636SBarry Smith    Level: intermediate
6013387f4636SBarry Smith 
6014387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6015387f4636SBarry Smith 
6016387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6017387f4636SBarry Smith @*/
6018387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6019387f4636SBarry Smith {
6020a66092f1SBarry Smith   PetscInt          i;
6021387f4636SBarry Smith   PetscErrorCode    ierr;
6022387f4636SBarry Smith 
6023387f4636SBarry Smith   PetscFunctionBegin;
6024387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6025387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
60265537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6027b3d3934dSBarry Smith       break;
6028b037adc7SBarry Smith     }
6029b037adc7SBarry Smith   }
6030b037adc7SBarry Smith   PetscFunctionReturn(0);
6031b037adc7SBarry Smith }
6032b037adc7SBarry Smith 
6033b037adc7SBarry Smith #undef __FUNCT__
603480666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
603580666b62SBarry Smith /*@C
603680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
603780666b62SBarry Smith 
603880666b62SBarry Smith    Collective on TS
603980666b62SBarry Smith 
604080666b62SBarry Smith    Input Parameters:
604180666b62SBarry Smith +  ts - the TS context
604280666b62SBarry Smith .  transform - the transform function
60437684fa3eSBarry Smith .  destroy - function to destroy the optional context
604480666b62SBarry Smith -  ctx - optional context used by transform function
604580666b62SBarry Smith 
604680666b62SBarry Smith    Level: intermediate
604780666b62SBarry Smith 
604880666b62SBarry Smith .keywords: TS,  vector, monitor, view
604980666b62SBarry Smith 
6050a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
605180666b62SBarry Smith @*/
60527684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
605380666b62SBarry Smith {
605480666b62SBarry Smith   PetscInt          i;
6055a66092f1SBarry Smith   PetscErrorCode    ierr;
605680666b62SBarry Smith 
605780666b62SBarry Smith   PetscFunctionBegin;
605880666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
605980666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
60605537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
606180666b62SBarry Smith     }
606280666b62SBarry Smith   }
606380666b62SBarry Smith   PetscFunctionReturn(0);
606480666b62SBarry Smith }
606580666b62SBarry Smith 
606680666b62SBarry Smith #undef __FUNCT__
6067e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
6068e673d494SBarry Smith /*@C
6069e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6070e673d494SBarry Smith 
6071e673d494SBarry Smith    Collective on TSLGCtx
6072e673d494SBarry Smith 
6073e673d494SBarry Smith    Input Parameters:
6074e673d494SBarry Smith +  ts - the TS context
6075e673d494SBarry Smith .  transform - the transform function
60767684fa3eSBarry Smith .  destroy - function to destroy the optional context
6077e673d494SBarry Smith -  ctx - optional context used by transform function
6078e673d494SBarry Smith 
6079e673d494SBarry Smith    Level: intermediate
6080e673d494SBarry Smith 
6081e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6082e673d494SBarry Smith 
6083a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6084e673d494SBarry Smith @*/
60857684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6086e673d494SBarry Smith {
6087e673d494SBarry Smith   PetscFunctionBegin;
6088e673d494SBarry Smith   ctx->transform    = transform;
60897684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6090e673d494SBarry Smith   ctx->transformctx = tctx;
6091e673d494SBarry Smith   PetscFunctionReturn(0);
6092e673d494SBarry Smith }
6093e673d494SBarry Smith 
6094b3603a34SBarry Smith #undef __FUNCT__
60954f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
6096ef20d060SBarry Smith /*@C
60974f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
6098ef20d060SBarry Smith        in a time based line graph
6099ef20d060SBarry Smith 
6100ef20d060SBarry Smith    Collective on TS
6101ef20d060SBarry Smith 
6102ef20d060SBarry Smith    Input Parameters:
6103ef20d060SBarry Smith +  ts - the TS context
6104ef20d060SBarry Smith .  step - current time-step
6105ef20d060SBarry Smith .  ptime - current time
6106ef20d060SBarry Smith -  lg - a line graph object
6107ef20d060SBarry Smith 
6108ef20d060SBarry Smith    Level: intermediate
6109ef20d060SBarry Smith 
6110abd5a294SJed Brown    Notes:
6111abd5a294SJed Brown    Only for sequential solves.
6112abd5a294SJed Brown 
6113abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6114abd5a294SJed Brown 
6115abd5a294SJed Brown    Options Database Keys:
61164f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6117ef20d060SBarry Smith 
6118ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6119ef20d060SBarry Smith 
6120abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6121ef20d060SBarry Smith @*/
61220910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6123ef20d060SBarry Smith {
6124ef20d060SBarry Smith   PetscErrorCode    ierr;
61250b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6126ef20d060SBarry Smith   const PetscScalar *yy;
6127ef20d060SBarry Smith   Vec               y;
6128a9f9c1f6SBarry Smith   PetscInt          dim;
6129ef20d060SBarry Smith 
6130ef20d060SBarry Smith   PetscFunctionBegin;
6131a9f9c1f6SBarry Smith   if (!step) {
6132a9f9c1f6SBarry Smith     PetscDrawAxis axis;
6133a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
61341ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
61350910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6136a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6137a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6138a9f9c1f6SBarry Smith   }
61390910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6140ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
61410910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6142ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6143e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6144e3efe391SJed Brown   {
6145e3efe391SJed Brown     PetscReal *yreal;
6146e3efe391SJed Brown     PetscInt  i,n;
6147e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6148785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6149e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
61500b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6151e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6152e3efe391SJed Brown   }
6153e3efe391SJed Brown #else
61540b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6155e3efe391SJed Brown #endif
6156ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6157ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6158b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
61590b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
61603923b477SBarry Smith   }
6161ef20d060SBarry Smith   PetscFunctionReturn(0);
6162ef20d060SBarry Smith }
6163ef20d060SBarry Smith 
6164201da799SBarry Smith #undef __FUNCT__
6165201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
6166201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6167201da799SBarry Smith {
6168201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6169201da799SBarry Smith   PetscReal      x   = ptime,y;
6170201da799SBarry Smith   PetscErrorCode ierr;
6171201da799SBarry Smith   PetscInt       its;
6172201da799SBarry Smith 
6173201da799SBarry Smith   PetscFunctionBegin;
6174201da799SBarry Smith   if (!n) {
6175201da799SBarry Smith     PetscDrawAxis axis;
6176bbd56ea5SKarl Rupp 
6177201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6178201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6179201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6180bbd56ea5SKarl Rupp 
6181201da799SBarry Smith     ctx->snes_its = 0;
6182201da799SBarry Smith   }
6183201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6184201da799SBarry Smith   y    = its - ctx->snes_its;
6185201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
61863fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6187201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6188201da799SBarry Smith   }
6189201da799SBarry Smith   ctx->snes_its = its;
6190201da799SBarry Smith   PetscFunctionReturn(0);
6191201da799SBarry Smith }
6192201da799SBarry Smith 
6193201da799SBarry Smith #undef __FUNCT__
6194201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
6195201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6196201da799SBarry Smith {
6197201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6198201da799SBarry Smith   PetscReal      x   = ptime,y;
6199201da799SBarry Smith   PetscErrorCode ierr;
6200201da799SBarry Smith   PetscInt       its;
6201201da799SBarry Smith 
6202201da799SBarry Smith   PetscFunctionBegin;
6203201da799SBarry Smith   if (!n) {
6204201da799SBarry Smith     PetscDrawAxis axis;
6205bbd56ea5SKarl Rupp 
6206201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6207201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6208201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6209bbd56ea5SKarl Rupp 
6210201da799SBarry Smith     ctx->ksp_its = 0;
6211201da799SBarry Smith   }
6212201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6213201da799SBarry Smith   y    = its - ctx->ksp_its;
6214201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
621599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6216201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6217201da799SBarry Smith   }
6218201da799SBarry Smith   ctx->ksp_its = its;
6219201da799SBarry Smith   PetscFunctionReturn(0);
6220201da799SBarry Smith }
6221f9c1d6abSBarry Smith 
6222f9c1d6abSBarry Smith #undef __FUNCT__
6223f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
6224f9c1d6abSBarry Smith /*@
6225f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6226f9c1d6abSBarry Smith 
6227f9c1d6abSBarry Smith    Collective on TS and Vec
6228f9c1d6abSBarry Smith 
6229f9c1d6abSBarry Smith    Input Parameters:
6230f9c1d6abSBarry Smith +  ts - the TS context
6231f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6232f9c1d6abSBarry Smith 
6233f9c1d6abSBarry Smith    Output Parameters:
6234f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6235f9c1d6abSBarry Smith 
6236f9c1d6abSBarry Smith    Level: developer
6237f9c1d6abSBarry Smith 
6238f9c1d6abSBarry Smith .keywords: TS, compute
6239f9c1d6abSBarry Smith 
6240f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6241f9c1d6abSBarry Smith @*/
6242f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6243f9c1d6abSBarry Smith {
6244f9c1d6abSBarry Smith   PetscErrorCode ierr;
6245f9c1d6abSBarry Smith 
6246f9c1d6abSBarry Smith   PetscFunctionBegin;
6247f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6248ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6249f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6250f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6251f9c1d6abSBarry Smith }
625224655328SShri 
6253b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6254b3d3934dSBarry Smith #undef __FUNCT__
6255b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
6256b3d3934dSBarry Smith /*@C
6257b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6258b3d3934dSBarry Smith 
6259b3d3934dSBarry Smith    Collective on TS
6260b3d3934dSBarry Smith 
6261b3d3934dSBarry Smith    Input Parameters:
6262b3d3934dSBarry Smith .  ts  - the ODE solver object
6263b3d3934dSBarry Smith 
6264b3d3934dSBarry Smith    Output Parameter:
6265b3d3934dSBarry Smith .  ctx - the context
6266b3d3934dSBarry Smith 
6267b3d3934dSBarry Smith    Level: intermediate
6268b3d3934dSBarry Smith 
6269b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6270b3d3934dSBarry Smith 
6271b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6272b3d3934dSBarry Smith 
6273b3d3934dSBarry Smith @*/
6274b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6275b3d3934dSBarry Smith {
6276b3d3934dSBarry Smith   PetscErrorCode ierr;
6277b3d3934dSBarry Smith 
6278b3d3934dSBarry Smith   PetscFunctionBegin;
6279a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6280b3d3934dSBarry Smith   PetscFunctionReturn(0);
6281b3d3934dSBarry Smith }
6282b3d3934dSBarry Smith 
6283b3d3934dSBarry Smith #undef __FUNCT__
6284b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
6285b3d3934dSBarry Smith /*@C
6286b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6287b3d3934dSBarry Smith 
6288b3d3934dSBarry Smith    Collective on TS
6289b3d3934dSBarry Smith 
6290b3d3934dSBarry Smith    Input Parameters:
6291b3d3934dSBarry Smith +  ts - the TS context
6292b3d3934dSBarry Smith .  step - current time-step
6293b3d3934dSBarry Smith .  ptime - current time
6294b3d3934dSBarry Smith -  ctx - the envelope context
6295b3d3934dSBarry Smith 
6296b3d3934dSBarry Smith    Options Database:
6297b3d3934dSBarry Smith .  -ts_monitor_envelope
6298b3d3934dSBarry Smith 
6299b3d3934dSBarry Smith    Level: intermediate
6300b3d3934dSBarry Smith 
6301b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6302b3d3934dSBarry Smith 
6303b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6304b3d3934dSBarry Smith 
6305b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds()
6306b3d3934dSBarry Smith @*/
6307b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6308b3d3934dSBarry Smith {
6309b3d3934dSBarry Smith   PetscErrorCode       ierr;
6310b3d3934dSBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy;
6311b3d3934dSBarry Smith 
6312b3d3934dSBarry Smith   PetscFunctionBegin;
6313b3d3934dSBarry Smith   if (!ctx->max) {
6314b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6315b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6316b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6317b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6318b3d3934dSBarry Smith   } else {
6319b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6320b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6321b3d3934dSBarry Smith   }
6322b3d3934dSBarry Smith   PetscFunctionReturn(0);
6323b3d3934dSBarry Smith }
6324b3d3934dSBarry Smith 
6325b3d3934dSBarry Smith 
6326b3d3934dSBarry Smith #undef __FUNCT__
6327b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
6328b3d3934dSBarry Smith /*@C
6329b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6330b3d3934dSBarry Smith 
6331b3d3934dSBarry Smith    Collective on TS
6332b3d3934dSBarry Smith 
6333b3d3934dSBarry Smith    Input Parameter:
6334b3d3934dSBarry Smith .  ts - the TS context
6335b3d3934dSBarry Smith 
6336b3d3934dSBarry Smith    Output Parameter:
6337b3d3934dSBarry Smith +  max - the maximum values
6338b3d3934dSBarry Smith -  min - the minimum values
6339b3d3934dSBarry Smith 
6340b3d3934dSBarry Smith    Level: intermediate
6341b3d3934dSBarry Smith 
6342b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6343b3d3934dSBarry Smith 
6344b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6345b3d3934dSBarry Smith @*/
6346b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6347b3d3934dSBarry Smith {
6348b3d3934dSBarry Smith   PetscInt i;
6349b3d3934dSBarry Smith 
6350b3d3934dSBarry Smith   PetscFunctionBegin;
6351b3d3934dSBarry Smith   if (max) *max = NULL;
6352b3d3934dSBarry Smith   if (min) *min = NULL;
6353b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6354b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
63555537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6356b3d3934dSBarry Smith       if (max) *max = ctx->max;
6357b3d3934dSBarry Smith       if (min) *min = ctx->min;
6358b3d3934dSBarry Smith       break;
6359b3d3934dSBarry Smith     }
6360b3d3934dSBarry Smith   }
6361b3d3934dSBarry Smith   PetscFunctionReturn(0);
6362b3d3934dSBarry Smith }
6363b3d3934dSBarry Smith 
6364b3d3934dSBarry Smith #undef __FUNCT__
6365b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
6366b3d3934dSBarry Smith /*@C
6367b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6368b3d3934dSBarry Smith 
6369b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6370b3d3934dSBarry Smith 
6371b3d3934dSBarry Smith    Input Parameter:
6372b3d3934dSBarry Smith .  ctx - the monitor context
6373b3d3934dSBarry Smith 
6374b3d3934dSBarry Smith    Level: intermediate
6375b3d3934dSBarry Smith 
6376b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
6377b3d3934dSBarry Smith 
6378b3d3934dSBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
6379b3d3934dSBarry Smith @*/
6380b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6381b3d3934dSBarry Smith {
6382b3d3934dSBarry Smith   PetscErrorCode ierr;
6383b3d3934dSBarry Smith 
6384b3d3934dSBarry Smith   PetscFunctionBegin;
6385b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6386b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6387b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6388b3d3934dSBarry Smith   PetscFunctionReturn(0);
6389b3d3934dSBarry Smith }
6390f2dee214SBarry Smith 
639124655328SShri #undef __FUNCT__
639224655328SShri #define __FUNCT__ "TSRollBack"
639324655328SShri /*@
639424655328SShri    TSRollBack - Rolls back one time step
639524655328SShri 
639624655328SShri    Collective on TS
639724655328SShri 
639824655328SShri    Input Parameter:
639924655328SShri .  ts - the TS context obtained from TSCreate()
640024655328SShri 
640124655328SShri    Level: advanced
640224655328SShri 
640324655328SShri .keywords: TS, timestep, rollback
640424655328SShri 
640524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
640624655328SShri @*/
640724655328SShri PetscErrorCode  TSRollBack(TS ts)
640824655328SShri {
640924655328SShri   PetscErrorCode ierr;
641024655328SShri 
641124655328SShri   PetscFunctionBegin;
641224655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
641324655328SShri 
641424655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
641524655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
641624655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
641724655328SShri   ts->ptime = ts->ptime_prev;
64188392e04aSShri Abhyankar   ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */
641924655328SShri   PetscFunctionReturn(0);
642024655328SShri }
6421aeb4809dSShri Abhyankar 
6422ff22ae23SHong Zhang #undef __FUNCT__
6423ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
6424ff22ae23SHong Zhang /*@
6425ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6426ff22ae23SHong Zhang 
6427ff22ae23SHong Zhang    Input Parameter:
6428ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6429ff22ae23SHong Zhang 
6430ff22ae23SHong Zhang    Level: advanced
6431ff22ae23SHong Zhang 
6432ff22ae23SHong Zhang .keywords: TS, getstages
6433ff22ae23SHong Zhang 
6434ff22ae23SHong Zhang .seealso: TSCreate()
6435ff22ae23SHong Zhang @*/
6436ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
6437ff22ae23SHong Zhang {
6438ff22ae23SHong Zhang   PetscErrorCode ierr;
6439ff22ae23SHong Zhang 
6440ff22ae23SHong Zhang   PetscFunctionBegin;
6441ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6442ff22ae23SHong Zhang   PetscValidPointer(ns,2);
6443ff22ae23SHong Zhang 
6444ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
6445ff22ae23SHong Zhang   else {
6446ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6447ff22ae23SHong Zhang   }
6448ff22ae23SHong Zhang   PetscFunctionReturn(0);
6449ff22ae23SHong Zhang }
6450ff22ae23SHong Zhang 
6451847ff0e1SMatthew G. Knepley #undef __FUNCT__
6452847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
6453847ff0e1SMatthew G. Knepley /*@C
6454847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6455847ff0e1SMatthew G. Knepley 
6456847ff0e1SMatthew G. Knepley   Collective on SNES
6457847ff0e1SMatthew G. Knepley 
6458847ff0e1SMatthew G. Knepley   Input Parameters:
6459847ff0e1SMatthew G. Knepley + ts - the TS context
6460847ff0e1SMatthew G. Knepley . t - current timestep
6461847ff0e1SMatthew G. Knepley . U - state vector
6462847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6463847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6464847ff0e1SMatthew G. Knepley - ctx - an optional user context
6465847ff0e1SMatthew G. Knepley 
6466847ff0e1SMatthew G. Knepley   Output Parameters:
6467847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6468847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6469847ff0e1SMatthew G. Knepley 
6470847ff0e1SMatthew G. Knepley   Level: intermediate
6471847ff0e1SMatthew G. Knepley 
6472847ff0e1SMatthew G. Knepley   Notes:
6473847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6474847ff0e1SMatthew G. Knepley 
6475847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6476847ff0e1SMatthew G. Knepley 
6477847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6478847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6479847ff0e1SMatthew G. Knepley 
6480847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6481847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6482847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6483847ff0e1SMatthew G. Knepley 
6484847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
6485847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6486847ff0e1SMatthew G. Knepley @*/
6487847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6488847ff0e1SMatthew G. Knepley {
6489847ff0e1SMatthew G. Knepley   SNES           snes;
6490847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6491847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6492847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6493847ff0e1SMatthew G. Knepley 
6494847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6495847ff0e1SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6496847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6497847ff0e1SMatthew G. Knepley   if (!color) {
6498847ff0e1SMatthew G. Knepley     DM         dm;
6499847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6500847ff0e1SMatthew G. Knepley 
6501847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6502847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6503847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6504847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6505847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6506847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6507847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6508847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6509847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6510847ff0e1SMatthew G. Knepley     } else {
6511847ff0e1SMatthew G. Knepley       MatColoring mc;
6512847ff0e1SMatthew G. Knepley 
6513847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6514847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6515847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6516847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6517847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6518847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6519847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6520847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6521847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6522847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6523847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6524847ff0e1SMatthew G. Knepley     }
6525847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6526847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6527847ff0e1SMatthew G. Knepley   }
6528847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6529847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6530847ff0e1SMatthew G. Knepley   if (J != B) {
6531847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6532847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6533847ff0e1SMatthew G. Knepley   }
6534847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6535847ff0e1SMatthew G. Knepley }
653693b34091SDebojyoti Ghosh 
653793b34091SDebojyoti Ghosh #undef  __FUNCT__
6538e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
653993b34091SDebojyoti Ghosh /*@C
6540e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
654193b34091SDebojyoti Ghosh 
654293b34091SDebojyoti Ghosh   Collective on MPI_Comm
654393b34091SDebojyoti Ghosh 
654493b34091SDebojyoti Ghosh   Input Parameter:
654593b34091SDebojyoti Ghosh . tsin    - The input TS
654693b34091SDebojyoti Ghosh 
654793b34091SDebojyoti Ghosh   Output Parameter:
6548e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
654993b34091SDebojyoti Ghosh 
65505eca1a21SEmil Constantinescu   Notes:
65515eca1a21SEmil Constantinescu   This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly.
65525eca1a21SEmil Constantinescu 
6553baa10174SEmil Constantinescu   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup);
65545eca1a21SEmil Constantinescu 
65555eca1a21SEmil Constantinescu   Level: developer
655693b34091SDebojyoti Ghosh 
6557e5168f73SEmil Constantinescu .keywords: TS, clone
6558e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
655993b34091SDebojyoti Ghosh @*/
6560baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
656193b34091SDebojyoti Ghosh {
656293b34091SDebojyoti Ghosh   TS             t;
656393b34091SDebojyoti Ghosh   PetscErrorCode ierr;
6564dc846ba4SSatish Balay   SNES           snes_start;
6565dc846ba4SSatish Balay   DM             dm;
6566dc846ba4SSatish Balay   TSType         type;
656793b34091SDebojyoti Ghosh 
656893b34091SDebojyoti Ghosh   PetscFunctionBegin;
656993b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
657093b34091SDebojyoti Ghosh   *tsout = NULL;
657193b34091SDebojyoti Ghosh 
65727a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
657393b34091SDebojyoti Ghosh 
657493b34091SDebojyoti Ghosh   /* General TS description */
657593b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
657693b34091SDebojyoti Ghosh   t->setupcalled       = 0;
657793b34091SDebojyoti Ghosh   t->ksp_its           = 0;
657893b34091SDebojyoti Ghosh   t->snes_its          = 0;
657993b34091SDebojyoti Ghosh   t->nwork             = 0;
658093b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
658193b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
658293b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
658393b34091SDebojyoti Ghosh 
658434561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);                   CHKERRQ(ierr);
658534561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);                       CHKERRQ(ierr);
6586d15a3a53SEmil Constantinescu 
658793b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);                             CHKERRQ(ierr);
658893b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);                                 CHKERRQ(ierr);
658993b34091SDebojyoti Ghosh 
659093b34091SDebojyoti Ghosh   t->adapt=tsin->adapt;
659193b34091SDebojyoti Ghosh   PetscObjectReference((PetscObject)t->adapt);
659293b34091SDebojyoti Ghosh 
659393b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
659493b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
659593b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
659693b34091SDebojyoti Ghosh   t->time_step_orig    = tsin->time_step_orig;
659793b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
659893b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
659993b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
660093b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
660193b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
660293b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
660393b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
660493b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
660593b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
660693b34091SDebojyoti Ghosh 
660793b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type); CHKERRQ(ierr);
660893b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);     CHKERRQ(ierr);
660993b34091SDebojyoti Ghosh 
661093b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
661193b34091SDebojyoti Ghosh 
661293b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
661393b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
661493b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
661593b34091SDebojyoti Ghosh 
661693b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
661793b34091SDebojyoti Ghosh 
661893b34091SDebojyoti Ghosh   *tsout = t;
661993b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
662093b34091SDebojyoti Ghosh }
6621