xref: /petsc/src/ts/interface/ts.c (revision 51b4a12f587ec4cde580070b9fdf80b4184c8082)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
144a2ae208SSatish Balay #undef __FUNCT__
15bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
16bdad233fSMatthew Knepley /*
17bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Collective on TS
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Input Parameter:
22bdad233fSMatthew Knepley . ts - The ts
23bdad233fSMatthew Knepley 
24bdad233fSMatthew Knepley   Level: intermediate
25bdad233fSMatthew Knepley 
26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
28bdad233fSMatthew Knepley */
296849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
30bdad233fSMatthew Knepley {
31ace3abfcSBarry Smith   PetscBool      opt;
322fc52814SBarry Smith   const char     *defaultType;
33bdad233fSMatthew Knepley   char           typeName[256];
34dfbe8321SBarry Smith   PetscErrorCode ierr;
35bdad233fSMatthew Knepley 
36bdad233fSMatthew Knepley   PetscFunctionBegin;
37bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
38bbd56ea5SKarl Rupp   else defaultType = TSEULER;
39bdad233fSMatthew Knepley 
40607a6623SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);}
41bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
42a7cc72afSBarry Smith   if (opt) {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   } else {
45bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
46bdad233fSMatthew Knepley   }
47bdad233fSMatthew Knepley   PetscFunctionReturn(0);
48bdad233fSMatthew Knepley }
49bdad233fSMatthew Knepley 
502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
512d5ee99bSBarry Smith   PetscViewer   viewer;
524b363babSBarry Smith   PetscDrawAxis axis;
532d5ee99bSBarry Smith   Vec           initialsolution;
542d5ee99bSBarry Smith   PetscBool     showinitial;
552d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
562d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
572d5ee99bSBarry Smith   int           color;
582d5ee99bSBarry Smith };
592d5ee99bSBarry Smith 
60bdad233fSMatthew Knepley #undef __FUNCT__
61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
62bdad233fSMatthew Knepley /*@
63bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Collective on TS
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley    Input Parameter:
68bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
69bdad233fSMatthew Knepley 
70bdad233fSMatthew Knepley    Options Database Keys:
714d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
72bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
733bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
74bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
75bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
76de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
77de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
78de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
79de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
80de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
81de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
82de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
832d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
84de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
8591b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
8691b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
8791b97e58SBarry Smith 
8891b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
89bdad233fSMatthew Knepley 
90bdad233fSMatthew Knepley    Level: beginner
91bdad233fSMatthew Knepley 
92bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
93bdad233fSMatthew Knepley 
94a313700dSBarry Smith .seealso: TSGetType()
95bdad233fSMatthew Knepley @*/
967087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
97bdad233fSMatthew Knepley {
98ace3abfcSBarry Smith   PetscBool              opt,flg;
99dfbe8321SBarry Smith   PetscErrorCode         ierr;
100649052a6SBarry Smith   PetscViewer            monviewer;
101eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
102089b2837SJed Brown   SNES                   snes;
1031c3436cfSJed Brown   TSAdapt                adapt;
10431748224SBarry Smith   PetscReal              time_step;
10549354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
106d1212d36SBarry Smith   char                   dir[16];
107bdad233fSMatthew Knepley 
108bdad233fSMatthew Knepley   PetscFunctionBegin;
1090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1103194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
111a43b19c4SJed Brown   /* Handle TS type options */
112a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley   /* Handle generic TS options */
1150298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1160298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1170298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11831748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
11931748224SBarry Smith   if (flg) {
12031748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12131748224SBarry Smith   }
12249354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
12349354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1240298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1250298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1260298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1280298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
129bdad233fSMatthew Knepley 
130bdad233fSMatthew Knepley   /* Monitor options */
131a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
132eabae89aSBarry Smith   if (flg) {
133ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
134649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
135bdad233fSMatthew Knepley   }
1365180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1375180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1385180491cSLisandro Dalcin 
1394f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
140a7cc72afSBarry Smith   if (opt) {
1410b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1423923b477SBarry Smith     PetscInt       howoften = 1;
143a80ad3e0SBarry Smith 
1440298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
145ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1464f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
147b3603a34SBarry Smith   }
1484f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
149b3603a34SBarry Smith   if (opt) {
1500b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1513923b477SBarry Smith     PetscInt       howoften = 1;
152b3603a34SBarry Smith 
1530298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
15422d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1554f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
156bdad233fSMatthew Knepley   }
1574f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
158ef20d060SBarry Smith   if (opt) {
1590b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1603923b477SBarry Smith     PetscInt       howoften = 1;
161ef20d060SBarry Smith 
1620298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
16322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1644f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
165ef20d060SBarry Smith   }
166201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
167201da799SBarry Smith   if (opt) {
168201da799SBarry Smith     TSMonitorLGCtx ctx;
169201da799SBarry Smith     PetscInt       howoften = 1;
170201da799SBarry Smith 
1710298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
17222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
173201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
174201da799SBarry Smith   }
175201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
176201da799SBarry Smith   if (opt) {
177201da799SBarry Smith     TSMonitorLGCtx ctx;
178201da799SBarry Smith     PetscInt       howoften = 1;
179201da799SBarry Smith 
1800298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
18122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
182201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
183201da799SBarry Smith   }
1848189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1858189c53fSBarry Smith   if (opt) {
1868189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1878189c53fSBarry Smith     PetscInt          howoften = 1;
1888189c53fSBarry Smith 
1890298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
19022d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1918189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1928189c53fSBarry Smith   }
193ef20d060SBarry Smith   opt  = PETSC_FALSE;
1940dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
195a7cc72afSBarry Smith   if (opt) {
19683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19783a4ac43SBarry Smith     PetscInt         howoften = 1;
198a80ad3e0SBarry Smith 
1990298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
200ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
20183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
202bdad233fSMatthew Knepley   }
203fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2042d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2052d5ee99bSBarry Smith   if (opt) {
2062d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2072d5ee99bSBarry Smith     PetscReal        bounds[4];
2082d5ee99bSBarry Smith     PetscInt         n = 4;
2092d5ee99bSBarry Smith     PetscDraw        draw;
2102d5ee99bSBarry Smith 
2112d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2122d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2132d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2142d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2152d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2164b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2174b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
218f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2194b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
22007995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2212d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2222d5ee99bSBarry Smith   }
2232d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2240dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2253a471f94SBarry Smith   if (opt) {
22683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
22783a4ac43SBarry Smith     PetscInt         howoften = 1;
2283a471f94SBarry Smith 
2290298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
230ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
23183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2323a471f94SBarry Smith   }
2333a471f94SBarry Smith   opt  = PETSC_FALSE;
23491b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
235fb1732b5SBarry Smith   if (flg) {
236fb1732b5SBarry Smith     PetscViewer ctx;
237fb1732b5SBarry Smith     if (monfilename[0]) {
238ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
239c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
240fb1732b5SBarry Smith     } else {
241ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2420298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
243fb1732b5SBarry Smith     }
244fb1732b5SBarry Smith   }
245ed81e22dSJed Brown   opt  = PETSC_FALSE;
24691b97e58SBarry 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);
247ed81e22dSJed Brown   if (flg) {
248ed81e22dSJed Brown     const char *ptr,*ptr2;
249ed81e22dSJed Brown     char       *filetemplate;
250ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
251ed81e22dSJed Brown     /* Do some cursory validation of the input. */
252ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
253ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
254ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
255ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
256ce94432eSBarry 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");
257ed81e22dSJed Brown       if (ptr2) break;
258ed81e22dSJed Brown     }
259ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
260ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
261ed81e22dSJed Brown   }
262bdad233fSMatthew Knepley 
263d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
264d1212d36SBarry Smith   if (flg) {
265d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
266d1212d36SBarry Smith     int                  ray = 0;
267d1212d36SBarry Smith     DMDADirection        ddir;
268d1212d36SBarry Smith     DM                   da;
269d1212d36SBarry Smith     PetscMPIInt          rank;
270d1212d36SBarry Smith 
271ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
272d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
273d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
274ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
275d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
276d1212d36SBarry Smith 
277d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
278d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
279d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
280d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
281ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
282d1212d36SBarry Smith     if (!rank) {
283d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
284d1212d36SBarry Smith     }
285*51b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
286d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
287d1212d36SBarry Smith   }
288*51b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
289*51b4a12fSMatthew G. Knepley   if (flg) {
290*51b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
291*51b4a12fSMatthew G. Knepley     int                 ray = 0;
292*51b4a12fSMatthew G. Knepley     DMDADirection       ddir;
293*51b4a12fSMatthew G. Knepley     DM                  da;
294*51b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
295*51b4a12fSMatthew G. Knepley 
296*51b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
297*51b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
298*51b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
299*51b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
300*51b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
301*51b4a12fSMatthew G. Knepley 
302*51b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
303*51b4a12fSMatthew G. Knepley     ierr = PetscNew(TSMonitorDMDARayCtx, &rayctx);CHKERRQ(ierr);
304*51b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
305*51b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
306*51b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
307*51b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
308*51b4a12fSMatthew G. Knepley   }
309d1212d36SBarry Smith 
310552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
3111c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
3121c3436cfSJed Brown 
313d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
314d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
315d52bd9f3SBarry Smith 
316bdad233fSMatthew Knepley   /* Handle specific TS options */
317abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
318bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
319bdad233fSMatthew Knepley   }
3205d973c19SBarry Smith 
3215d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3225d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
323bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
324bdad233fSMatthew Knepley   PetscFunctionReturn(0);
325bdad233fSMatthew Knepley }
326bdad233fSMatthew Knepley 
327bdad233fSMatthew Knepley #undef __FUNCT__
328cdcf91faSSean Farley #undef __FUNCT__
3294a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
330a7a1495cSBarry Smith /*@
3318c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
332a7a1495cSBarry Smith       set with TSSetRHSJacobian().
333a7a1495cSBarry Smith 
334a7a1495cSBarry Smith    Collective on TS and Vec
335a7a1495cSBarry Smith 
336a7a1495cSBarry Smith    Input Parameters:
337316643e7SJed Brown +  ts - the TS context
338a7a1495cSBarry Smith .  t - current timestep
3390910c330SBarry Smith -  U - input vector
340a7a1495cSBarry Smith 
341a7a1495cSBarry Smith    Output Parameters:
342a7a1495cSBarry Smith +  A - Jacobian matrix
343a7a1495cSBarry Smith .  B - optional preconditioning matrix
344a7a1495cSBarry Smith -  flag - flag indicating matrix structure
345a7a1495cSBarry Smith 
346a7a1495cSBarry Smith    Notes:
347a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
348a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
349a7a1495cSBarry Smith 
35094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
351a7a1495cSBarry Smith    flag parameter.
352a7a1495cSBarry Smith 
353a7a1495cSBarry Smith    Level: developer
354a7a1495cSBarry Smith 
355a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
356a7a1495cSBarry Smith 
35794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
358a7a1495cSBarry Smith @*/
3590910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
360a7a1495cSBarry Smith {
361dfbe8321SBarry Smith   PetscErrorCode ierr;
362270bf2e7SJed Brown   PetscObjectState Ustate;
36324989b8cSPeter Brune   DM             dm;
364942e3340SBarry Smith   DMTS           tsdm;
36524989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
36624989b8cSPeter Brune   void           *ctx;
36724989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
368a7a1495cSBarry Smith 
369a7a1495cSBarry Smith   PetscFunctionBegin;
3700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3710910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3720910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
37324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
374942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
37524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3760298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
37759e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
3780910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3790e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3800e4ef248SJed Brown     PetscFunctionReturn(0);
381f8ede8e7SJed Brown   }
382d90be118SSean Farley 
383ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
384d90be118SSean Farley 
385e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
386e1244c69SJed Brown     ierr = MatShift(*A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
387e1244c69SJed Brown     ierr = MatScale(*A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
388e1244c69SJed Brown     if (*A != *B) {
389e1244c69SJed Brown       ierr = MatShift(*B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
390e1244c69SJed Brown       ierr = MatScale(*B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
391e1244c69SJed Brown     }
392e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
393e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
394e1244c69SJed Brown   }
395e1244c69SJed Brown 
39624989b8cSPeter Brune   if (rhsjacobianfunc) {
3970910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
398a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
399a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
4000910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
401a7a1495cSBarry Smith     PetscStackPop;
4020910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
403a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
4040700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
4050700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
406ef66eb69SBarry Smith   } else {
407214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
408214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
409214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
410ef66eb69SBarry Smith   }
4110e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4120910c330SBarry Smith   ts->rhsjacobian.X          = U;
41359e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
4140e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
415a7a1495cSBarry Smith   PetscFunctionReturn(0);
416a7a1495cSBarry Smith }
417a7a1495cSBarry Smith 
4184a2ae208SSatish Balay #undef __FUNCT__
4194a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
420316643e7SJed Brown /*@
421d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
422d763cef2SBarry Smith 
423316643e7SJed Brown    Collective on TS and Vec
424316643e7SJed Brown 
425316643e7SJed Brown    Input Parameters:
426316643e7SJed Brown +  ts - the TS context
427316643e7SJed Brown .  t - current time
4280910c330SBarry Smith -  U - state vector
429316643e7SJed Brown 
430316643e7SJed Brown    Output Parameter:
431316643e7SJed Brown .  y - right hand side
432316643e7SJed Brown 
433316643e7SJed Brown    Note:
434316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
435316643e7SJed Brown    is used internally within the nonlinear solvers.
436316643e7SJed Brown 
437316643e7SJed Brown    Level: developer
438316643e7SJed Brown 
439316643e7SJed Brown .keywords: TS, compute
440316643e7SJed Brown 
441316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
442316643e7SJed Brown @*/
4430910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
444d763cef2SBarry Smith {
445dfbe8321SBarry Smith   PetscErrorCode ierr;
44624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
44724989b8cSPeter Brune   TSIFunction    ifunction;
44824989b8cSPeter Brune   void           *ctx;
44924989b8cSPeter Brune   DM             dm;
45024989b8cSPeter Brune 
451d763cef2SBarry Smith   PetscFunctionBegin;
4520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4530910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4540700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
45524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
45624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4570298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
458d763cef2SBarry Smith 
459ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
460d763cef2SBarry Smith 
4610910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
46224989b8cSPeter Brune   if (rhsfunction) {
463d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4640910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
465d763cef2SBarry Smith     PetscStackPop;
466214bc6a2SJed Brown   } else {
467214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
468b2cd27e8SJed Brown   }
46944a41b28SSean Farley 
4700910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
471d763cef2SBarry Smith   PetscFunctionReturn(0);
472d763cef2SBarry Smith }
473d763cef2SBarry Smith 
4744a2ae208SSatish Balay #undef __FUNCT__
475ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
476ef20d060SBarry Smith /*@
477ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
478ef20d060SBarry Smith 
479ef20d060SBarry Smith    Collective on TS and Vec
480ef20d060SBarry Smith 
481ef20d060SBarry Smith    Input Parameters:
482ef20d060SBarry Smith +  ts - the TS context
483ef20d060SBarry Smith -  t - current time
484ef20d060SBarry Smith 
485ef20d060SBarry Smith    Output Parameter:
4860910c330SBarry Smith .  U - the solution
487ef20d060SBarry Smith 
488ef20d060SBarry Smith    Note:
489ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
490ef20d060SBarry Smith    is used internally within the nonlinear solvers.
491ef20d060SBarry Smith 
492ef20d060SBarry Smith    Level: developer
493ef20d060SBarry Smith 
494ef20d060SBarry Smith .keywords: TS, compute
495ef20d060SBarry Smith 
496abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
497ef20d060SBarry Smith @*/
4980910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
499ef20d060SBarry Smith {
500ef20d060SBarry Smith   PetscErrorCode     ierr;
501ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
502ef20d060SBarry Smith   void               *ctx;
503ef20d060SBarry Smith   DM                 dm;
504ef20d060SBarry Smith 
505ef20d060SBarry Smith   PetscFunctionBegin;
506ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5070910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
508ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
509ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
510ef20d060SBarry Smith 
511ef20d060SBarry Smith   if (solutionfunction) {
5129b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5130910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
514ef20d060SBarry Smith     PetscStackPop;
515ef20d060SBarry Smith   }
516ef20d060SBarry Smith   PetscFunctionReturn(0);
517ef20d060SBarry Smith }
5189b7cd975SBarry Smith #undef __FUNCT__
5199b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5209b7cd975SBarry Smith /*@
5219b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5229b7cd975SBarry Smith 
5239b7cd975SBarry Smith    Collective on TS and Vec
5249b7cd975SBarry Smith 
5259b7cd975SBarry Smith    Input Parameters:
5269b7cd975SBarry Smith +  ts - the TS context
5279b7cd975SBarry Smith -  t - current time
5289b7cd975SBarry Smith 
5299b7cd975SBarry Smith    Output Parameter:
5309b7cd975SBarry Smith .  U - the function value
5319b7cd975SBarry Smith 
5329b7cd975SBarry Smith    Note:
5339b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5349b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5359b7cd975SBarry Smith 
5369b7cd975SBarry Smith    Level: developer
5379b7cd975SBarry Smith 
5389b7cd975SBarry Smith .keywords: TS, compute
5399b7cd975SBarry Smith 
5409b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5419b7cd975SBarry Smith @*/
5429b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5439b7cd975SBarry Smith {
5449b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5459b7cd975SBarry Smith   void               *ctx;
5469b7cd975SBarry Smith   DM                 dm;
5479b7cd975SBarry Smith 
5489b7cd975SBarry Smith   PetscFunctionBegin;
5499b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5509b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5519b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5529b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5539b7cd975SBarry Smith 
5549b7cd975SBarry Smith   if (forcing) {
5559b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5569b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5579b7cd975SBarry Smith     PetscStackPop;
5589b7cd975SBarry Smith   }
5599b7cd975SBarry Smith   PetscFunctionReturn(0);
5609b7cd975SBarry Smith }
561ef20d060SBarry Smith 
562ef20d060SBarry Smith #undef __FUNCT__
563214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
564214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
565214bc6a2SJed Brown {
5662dd45cf8SJed Brown   Vec            F;
567214bc6a2SJed Brown   PetscErrorCode ierr;
568214bc6a2SJed Brown 
569214bc6a2SJed Brown   PetscFunctionBegin;
5700298fd71SBarry Smith   *Frhs = NULL;
5710298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
572214bc6a2SJed Brown   if (!ts->Frhs) {
5732dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
574214bc6a2SJed Brown   }
575214bc6a2SJed Brown   *Frhs = ts->Frhs;
576214bc6a2SJed Brown   PetscFunctionReturn(0);
577214bc6a2SJed Brown }
578214bc6a2SJed Brown 
579214bc6a2SJed Brown #undef __FUNCT__
580214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
581214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
582214bc6a2SJed Brown {
583214bc6a2SJed Brown   Mat            A,B;
5842dd45cf8SJed Brown   PetscErrorCode ierr;
585214bc6a2SJed Brown 
586214bc6a2SJed Brown   PetscFunctionBegin;
5870298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
588214bc6a2SJed Brown   if (Arhs) {
589214bc6a2SJed Brown     if (!ts->Arhs) {
590214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
591214bc6a2SJed Brown     }
592214bc6a2SJed Brown     *Arhs = ts->Arhs;
593214bc6a2SJed Brown   }
594214bc6a2SJed Brown   if (Brhs) {
595214bc6a2SJed Brown     if (!ts->Brhs) {
596214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
597214bc6a2SJed Brown     }
598214bc6a2SJed Brown     *Brhs = ts->Brhs;
599214bc6a2SJed Brown   }
600214bc6a2SJed Brown   PetscFunctionReturn(0);
601214bc6a2SJed Brown }
602214bc6a2SJed Brown 
603214bc6a2SJed Brown #undef __FUNCT__
604316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
605316643e7SJed Brown /*@
6060910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
607316643e7SJed Brown 
608316643e7SJed Brown    Collective on TS and Vec
609316643e7SJed Brown 
610316643e7SJed Brown    Input Parameters:
611316643e7SJed Brown +  ts - the TS context
612316643e7SJed Brown .  t - current time
6130910c330SBarry Smith .  U - state vector
6140910c330SBarry Smith .  Udot - time derivative of state vector
615214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
616316643e7SJed Brown 
617316643e7SJed Brown    Output Parameter:
618316643e7SJed Brown .  Y - right hand side
619316643e7SJed Brown 
620316643e7SJed Brown    Note:
621316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
622316643e7SJed Brown    is used internally within the nonlinear solvers.
623316643e7SJed Brown 
624316643e7SJed Brown    If the user did did not write their equations in implicit form, this
625316643e7SJed Brown    function recasts them in implicit form.
626316643e7SJed Brown 
627316643e7SJed Brown    Level: developer
628316643e7SJed Brown 
629316643e7SJed Brown .keywords: TS, compute
630316643e7SJed Brown 
631316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
632316643e7SJed Brown @*/
6330910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
634316643e7SJed Brown {
635316643e7SJed Brown   PetscErrorCode ierr;
63624989b8cSPeter Brune   TSIFunction    ifunction;
63724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
63824989b8cSPeter Brune   void           *ctx;
63924989b8cSPeter Brune   DM             dm;
640316643e7SJed Brown 
641316643e7SJed Brown   PetscFunctionBegin;
6420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6430910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6440910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6450700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
646316643e7SJed Brown 
64724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
64824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6490298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
65024989b8cSPeter Brune 
651ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
652d90be118SSean Farley 
6530910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
65424989b8cSPeter Brune   if (ifunction) {
655316643e7SJed Brown     PetscStackPush("TS user implicit function");
6560910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
657316643e7SJed Brown     PetscStackPop;
658214bc6a2SJed Brown   }
659214bc6a2SJed Brown   if (imex) {
66024989b8cSPeter Brune     if (!ifunction) {
6610910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6622dd45cf8SJed Brown     }
66324989b8cSPeter Brune   } else if (rhsfunction) {
66424989b8cSPeter Brune     if (ifunction) {
665214bc6a2SJed Brown       Vec Frhs;
666214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6670910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
668214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6692dd45cf8SJed Brown     } else {
6700910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6710910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
672316643e7SJed Brown     }
6734a6899ffSJed Brown   }
6740910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
675316643e7SJed Brown   PetscFunctionReturn(0);
676316643e7SJed Brown }
677316643e7SJed Brown 
678316643e7SJed Brown #undef __FUNCT__
679316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
680316643e7SJed Brown /*@
681316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
682316643e7SJed Brown 
683316643e7SJed Brown    Collective on TS and Vec
684316643e7SJed Brown 
685316643e7SJed Brown    Input
686316643e7SJed Brown       Input Parameters:
687316643e7SJed Brown +  ts - the TS context
688316643e7SJed Brown .  t - current timestep
6890910c330SBarry Smith .  U - state vector
6900910c330SBarry Smith .  Udot - time derivative of state vector
691214bc6a2SJed Brown .  shift - shift to apply, see note below
692214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
693316643e7SJed Brown 
694316643e7SJed Brown    Output Parameters:
695316643e7SJed Brown +  A - Jacobian matrix
696316643e7SJed Brown .  B - optional preconditioning matrix
697316643e7SJed Brown -  flag - flag indicating matrix structure
698316643e7SJed Brown 
699316643e7SJed Brown    Notes:
7000910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
701316643e7SJed Brown 
7020910c330SBarry Smith    dF/dU + shift*dF/dUdot
703316643e7SJed Brown 
704316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
705316643e7SJed Brown    is used internally within the nonlinear solvers.
706316643e7SJed Brown 
707316643e7SJed Brown    Level: developer
708316643e7SJed Brown 
709316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
710316643e7SJed Brown 
711316643e7SJed Brown .seealso:  TSSetIJacobian()
71263495f91SJed Brown @*/
7130910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
714316643e7SJed Brown {
715316643e7SJed Brown   PetscErrorCode ierr;
71624989b8cSPeter Brune   TSIJacobian    ijacobian;
71724989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
71824989b8cSPeter Brune   DM             dm;
71924989b8cSPeter Brune   void           *ctx;
720316643e7SJed Brown 
721316643e7SJed Brown   PetscFunctionBegin;
7220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7230910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7240910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
725316643e7SJed Brown   PetscValidPointer(A,6);
7260700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
727316643e7SJed Brown   PetscValidPointer(B,7);
7280700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
729316643e7SJed Brown   PetscValidPointer(flg,8);
73024989b8cSPeter Brune 
73124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7330298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
73424989b8cSPeter Brune 
735ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
736316643e7SJed Brown 
7374e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7380910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
73924989b8cSPeter Brune   if (ijacobian) {
7402dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
741316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7420910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
743316643e7SJed Brown     PetscStackPop;
744214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
745214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
746214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7474a6899ffSJed Brown   }
748214bc6a2SJed Brown   if (imex) {
749b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
750214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7512dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
752214bc6a2SJed Brown       if (*A != *B) {
753214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
754214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
755214bc6a2SJed Brown       }
756214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
757214bc6a2SJed Brown     }
758214bc6a2SJed Brown   } else {
759e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
760e1244c69SJed Brown     MatStructure flg2;
761e1244c69SJed Brown     if (rhsjacobian) {
762e1244c69SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
763e1244c69SJed Brown       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
764e1244c69SJed Brown     }
765e1244c69SJed Brown     if (Arhs == *A) {           /* No IJacobian, so we only have the RHS matrix */
766e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
767e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
768214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
769214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
770316643e7SJed Brown       if (*A != *B) {
771316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
772316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
773316643e7SJed Brown       }
774e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
775e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
776e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
777e1244c69SJed Brown         ierr = MatZeroEntries(*A);CHKERRQ(ierr);
778e1244c69SJed Brown         ierr = MatShift(*A,shift);CHKERRQ(ierr);
779e1244c69SJed Brown         if (*A != *B) {
780e1244c69SJed Brown           ierr = MatZeroEntries(*B);CHKERRQ(ierr);
781e1244c69SJed Brown           ierr = MatShift(*B,shift);CHKERRQ(ierr);
782e1244c69SJed Brown         }
783e1244c69SJed Brown       }
784214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
785214bc6a2SJed Brown       if (*A != *B) {
786214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
787214bc6a2SJed Brown       }
788214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
789214bc6a2SJed Brown     }
790316643e7SJed Brown   }
7910026cea9SSean Farley 
7920910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
793316643e7SJed Brown   PetscFunctionReturn(0);
794316643e7SJed Brown }
795316643e7SJed Brown 
796316643e7SJed Brown #undef __FUNCT__
7974a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
798d763cef2SBarry Smith /*@C
799d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
800b5abc632SBarry Smith     where U_t = G(t,u).
801d763cef2SBarry Smith 
8023f9fe445SBarry Smith     Logically Collective on TS
803d763cef2SBarry Smith 
804d763cef2SBarry Smith     Input Parameters:
805d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8060298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
807d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
808d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8090298fd71SBarry Smith           function evaluation routine (may be NULL)
810d763cef2SBarry Smith 
811d763cef2SBarry Smith     Calling sequence of func:
81287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
813d763cef2SBarry Smith 
814d763cef2SBarry Smith +   t - current timestep
815d763cef2SBarry Smith .   u - input vector
816d763cef2SBarry Smith .   F - function vector
817d763cef2SBarry Smith -   ctx - [optional] user-defined function context
818d763cef2SBarry Smith 
819d763cef2SBarry Smith     Level: beginner
820d763cef2SBarry Smith 
821d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
822d763cef2SBarry Smith 
823d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
824d763cef2SBarry Smith @*/
825089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
826d763cef2SBarry Smith {
827089b2837SJed Brown   PetscErrorCode ierr;
828089b2837SJed Brown   SNES           snes;
8290298fd71SBarry Smith   Vec            ralloc = NULL;
83024989b8cSPeter Brune   DM             dm;
831d763cef2SBarry Smith 
832089b2837SJed Brown   PetscFunctionBegin;
8330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
834ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
83524989b8cSPeter Brune 
83624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
83724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
838089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
839e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
840e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
841e856ceecSJed Brown     r    = ralloc;
842e856ceecSJed Brown   }
843089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
844e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
845d763cef2SBarry Smith   PetscFunctionReturn(0);
846d763cef2SBarry Smith }
847d763cef2SBarry Smith 
8484a2ae208SSatish Balay #undef __FUNCT__
849ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
850ef20d060SBarry Smith /*@C
851abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
852ef20d060SBarry Smith 
853ef20d060SBarry Smith     Logically Collective on TS
854ef20d060SBarry Smith 
855ef20d060SBarry Smith     Input Parameters:
856ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
857ef20d060SBarry Smith .   f - routine for evaluating the solution
858ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8590298fd71SBarry Smith           function evaluation routine (may be NULL)
860ef20d060SBarry Smith 
861ef20d060SBarry Smith     Calling sequence of func:
862ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
863ef20d060SBarry Smith 
864ef20d060SBarry Smith +   t - current timestep
865ef20d060SBarry Smith .   u - output vector
866ef20d060SBarry Smith -   ctx - [optional] user-defined function context
867ef20d060SBarry Smith 
868abd5a294SJed Brown     Notes:
869abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
870abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
871abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
872abd5a294SJed Brown 
8734f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
874abd5a294SJed Brown 
875ef20d060SBarry Smith     Level: beginner
876ef20d060SBarry Smith 
877ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
878ef20d060SBarry Smith 
8799b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
880ef20d060SBarry Smith @*/
881ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
882ef20d060SBarry Smith {
883ef20d060SBarry Smith   PetscErrorCode ierr;
884ef20d060SBarry Smith   DM             dm;
885ef20d060SBarry Smith 
886ef20d060SBarry Smith   PetscFunctionBegin;
887ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
888ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
889ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
890ef20d060SBarry Smith   PetscFunctionReturn(0);
891ef20d060SBarry Smith }
892ef20d060SBarry Smith 
893ef20d060SBarry Smith #undef __FUNCT__
8949b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8959b7cd975SBarry Smith /*@C
8969b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8979b7cd975SBarry Smith 
8989b7cd975SBarry Smith     Logically Collective on TS
8999b7cd975SBarry Smith 
9009b7cd975SBarry Smith     Input Parameters:
9019b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9029b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9039b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9040298fd71SBarry Smith           function evaluation routine (may be NULL)
9059b7cd975SBarry Smith 
9069b7cd975SBarry Smith     Calling sequence of func:
9079b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9089b7cd975SBarry Smith 
9099b7cd975SBarry Smith +   t - current timestep
9109b7cd975SBarry Smith .   u - output vector
9119b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9129b7cd975SBarry Smith 
9139b7cd975SBarry Smith     Notes:
9149b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9159b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9169b7cd975SBarry Smith 
9179b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9189b7cd975SBarry Smith 
9199b7cd975SBarry Smith     Level: beginner
9209b7cd975SBarry Smith 
9219b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9229b7cd975SBarry Smith 
9239b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9249b7cd975SBarry Smith @*/
9259b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9269b7cd975SBarry Smith {
9279b7cd975SBarry Smith   PetscErrorCode ierr;
9289b7cd975SBarry Smith   DM             dm;
9299b7cd975SBarry Smith 
9309b7cd975SBarry Smith   PetscFunctionBegin;
9319b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9329b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9339b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9349b7cd975SBarry Smith   PetscFunctionReturn(0);
9359b7cd975SBarry Smith }
9369b7cd975SBarry Smith 
9379b7cd975SBarry Smith #undef __FUNCT__
9384a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
939d763cef2SBarry Smith /*@C
940d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
941b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
942d763cef2SBarry Smith 
9433f9fe445SBarry Smith    Logically Collective on TS
944d763cef2SBarry Smith 
945d763cef2SBarry Smith    Input Parameters:
946d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
947e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
948e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
949d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
950d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9510298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
952d763cef2SBarry Smith 
953d763cef2SBarry Smith    Calling sequence of func:
95487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
955d763cef2SBarry Smith 
956d763cef2SBarry Smith +  t - current timestep
957d763cef2SBarry Smith .  u - input vector
958e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
959e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
960d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
96194b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
962d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
963d763cef2SBarry Smith 
964d763cef2SBarry Smith    Notes:
96594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
966d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
967d763cef2SBarry Smith 
968d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
969d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
97056335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
971d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
972d763cef2SBarry Smith    throughout the global iterations.
973d763cef2SBarry Smith 
974d763cef2SBarry Smith    Level: beginner
975d763cef2SBarry Smith 
976d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
977d763cef2SBarry Smith 
978e1244c69SJed Brown .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse()
979d763cef2SBarry Smith 
980d763cef2SBarry Smith @*/
981e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
982d763cef2SBarry Smith {
983277b19d0SLisandro Dalcin   PetscErrorCode ierr;
984089b2837SJed Brown   SNES           snes;
98524989b8cSPeter Brune   DM             dm;
98624989b8cSPeter Brune   TSIJacobian    ijacobian;
987277b19d0SLisandro Dalcin 
988d763cef2SBarry Smith   PetscFunctionBegin;
9890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
990e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
991e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
992e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
993e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
994d763cef2SBarry Smith 
99524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99624989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
997e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
998e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
999e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1000e1244c69SJed Brown   }
10010298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1002089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10035f659677SPeter Brune   if (!ijacobian) {
1004e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10050e4ef248SJed Brown   }
1006e5d3d808SBarry Smith   if (Amat) {
1007e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10080e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1009bbd56ea5SKarl Rupp 
1010e5d3d808SBarry Smith     ts->Arhs = Amat;
10110e4ef248SJed Brown   }
1012e5d3d808SBarry Smith   if (Pmat) {
1013e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10140e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1015bbd56ea5SKarl Rupp 
1016e5d3d808SBarry Smith     ts->Brhs = Pmat;
10170e4ef248SJed Brown   }
1018d763cef2SBarry Smith   PetscFunctionReturn(0);
1019d763cef2SBarry Smith }
1020d763cef2SBarry Smith 
1021316643e7SJed Brown 
1022316643e7SJed Brown #undef __FUNCT__
1023316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1024316643e7SJed Brown /*@C
1025b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1026316643e7SJed Brown 
10273f9fe445SBarry Smith    Logically Collective on TS
1028316643e7SJed Brown 
1029316643e7SJed Brown    Input Parameters:
1030316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10310298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1032316643e7SJed Brown .  f   - the function evaluation routine
10330298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1034316643e7SJed Brown 
1035316643e7SJed Brown    Calling sequence of f:
1036316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1037316643e7SJed Brown 
1038316643e7SJed Brown +  t   - time at step/stage being solved
1039316643e7SJed Brown .  u   - state vector
1040316643e7SJed Brown .  u_t - time derivative of state vector
1041316643e7SJed Brown .  F   - function vector
1042316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1043316643e7SJed Brown 
1044316643e7SJed Brown    Important:
1045d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1046316643e7SJed Brown 
1047316643e7SJed Brown    Level: beginner
1048316643e7SJed Brown 
1049316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1050316643e7SJed Brown 
1051d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1052316643e7SJed Brown @*/
1053089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1054316643e7SJed Brown {
1055089b2837SJed Brown   PetscErrorCode ierr;
1056089b2837SJed Brown   SNES           snes;
10570298fd71SBarry Smith   Vec            resalloc = NULL;
105824989b8cSPeter Brune   DM             dm;
1059316643e7SJed Brown 
1060316643e7SJed Brown   PetscFunctionBegin;
10610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1062ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
106324989b8cSPeter Brune 
106424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
106624989b8cSPeter Brune 
1067089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1068e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1069e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1070e856ceecSJed Brown     res  = resalloc;
1071e856ceecSJed Brown   }
1072089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1073e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1074089b2837SJed Brown   PetscFunctionReturn(0);
1075089b2837SJed Brown }
1076089b2837SJed Brown 
1077089b2837SJed Brown #undef __FUNCT__
1078089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1079089b2837SJed Brown /*@C
1080089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1081089b2837SJed Brown 
1082089b2837SJed Brown    Not Collective
1083089b2837SJed Brown 
1084089b2837SJed Brown    Input Parameter:
1085089b2837SJed Brown .  ts - the TS context
1086089b2837SJed Brown 
1087089b2837SJed Brown    Output Parameter:
10880298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10890298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10900298fd71SBarry Smith -  ctx - the function context (or NULL)
1091089b2837SJed Brown 
1092089b2837SJed Brown    Level: advanced
1093089b2837SJed Brown 
1094089b2837SJed Brown .keywords: TS, nonlinear, get, function
1095089b2837SJed Brown 
1096089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1097089b2837SJed Brown @*/
1098089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1099089b2837SJed Brown {
1100089b2837SJed Brown   PetscErrorCode ierr;
1101089b2837SJed Brown   SNES           snes;
110224989b8cSPeter Brune   DM             dm;
1103089b2837SJed Brown 
1104089b2837SJed Brown   PetscFunctionBegin;
1105089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1106089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11070298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
110824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1110089b2837SJed Brown   PetscFunctionReturn(0);
1111089b2837SJed Brown }
1112089b2837SJed Brown 
1113089b2837SJed Brown #undef __FUNCT__
1114089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1115089b2837SJed Brown /*@C
1116089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1117089b2837SJed Brown 
1118089b2837SJed Brown    Not Collective
1119089b2837SJed Brown 
1120089b2837SJed Brown    Input Parameter:
1121089b2837SJed Brown .  ts - the TS context
1122089b2837SJed Brown 
1123089b2837SJed Brown    Output Parameter:
11240298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11250298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11260298fd71SBarry Smith -  ctx - the function context (or NULL)
1127089b2837SJed Brown 
1128089b2837SJed Brown    Level: advanced
1129089b2837SJed Brown 
1130089b2837SJed Brown .keywords: TS, nonlinear, get, function
1131089b2837SJed Brown 
1132089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1133089b2837SJed Brown @*/
1134089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1135089b2837SJed Brown {
1136089b2837SJed Brown   PetscErrorCode ierr;
1137089b2837SJed Brown   SNES           snes;
113824989b8cSPeter Brune   DM             dm;
1139089b2837SJed Brown 
1140089b2837SJed Brown   PetscFunctionBegin;
1141089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1142089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11430298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
114424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1146316643e7SJed Brown   PetscFunctionReturn(0);
1147316643e7SJed Brown }
1148316643e7SJed Brown 
1149316643e7SJed Brown #undef __FUNCT__
1150316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1151316643e7SJed Brown /*@C
1152a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1153a4f0a591SBarry Smith         you provided with TSSetIFunction().
1154316643e7SJed Brown 
11553f9fe445SBarry Smith    Logically Collective on TS
1156316643e7SJed Brown 
1157316643e7SJed Brown    Input Parameters:
1158316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1159e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1160e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1161316643e7SJed Brown .  f   - the Jacobian evaluation routine
11620298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1163316643e7SJed Brown 
1164316643e7SJed Brown    Calling sequence of f:
1165e5d3d808SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx);
1166316643e7SJed Brown 
1167316643e7SJed Brown +  t    - time at step/stage being solved
11681b4a444bSJed Brown .  U    - state vector
11691b4a444bSJed Brown .  U_t  - time derivative of state vector
1170316643e7SJed Brown .  a    - shift
1171e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1172e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1173316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1174316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1175316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1176316643e7SJed Brown 
1177316643e7SJed Brown    Notes:
1178e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1179316643e7SJed Brown 
1180a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1181b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1182a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1183a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1184a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1185a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1186a4f0a591SBarry Smith 
1187316643e7SJed Brown    Level: beginner
1188316643e7SJed Brown 
1189316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1190316643e7SJed Brown 
11918d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault()
1192316643e7SJed Brown 
1193316643e7SJed Brown @*/
1194e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1195316643e7SJed Brown {
1196316643e7SJed Brown   PetscErrorCode ierr;
1197089b2837SJed Brown   SNES           snes;
119824989b8cSPeter Brune   DM             dm;
1199316643e7SJed Brown 
1200316643e7SJed Brown   PetscFunctionBegin;
12010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1202e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1203e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1204e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1205e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
120624989b8cSPeter Brune 
120724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120824989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
120924989b8cSPeter Brune 
1210089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1211e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1212316643e7SJed Brown   PetscFunctionReturn(0);
1213316643e7SJed Brown }
1214316643e7SJed Brown 
12154a2ae208SSatish Balay #undef __FUNCT__
1216e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1217e1244c69SJed Brown /*@
1218e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1219e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1220e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1221e1244c69SJed Brown    not been changed by the TS.
1222e1244c69SJed Brown 
1223e1244c69SJed Brown    Logically Collective
1224e1244c69SJed Brown 
1225e1244c69SJed Brown    Input Arguments:
1226e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1227e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1228e1244c69SJed Brown 
1229e1244c69SJed Brown    Level: intermediate
1230e1244c69SJed Brown 
1231e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1232e1244c69SJed Brown @*/
1233e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1234e1244c69SJed Brown {
1235e1244c69SJed Brown   PetscFunctionBegin;
1236e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1237e1244c69SJed Brown   PetscFunctionReturn(0);
1238e1244c69SJed Brown }
1239e1244c69SJed Brown 
1240e1244c69SJed Brown #undef __FUNCT__
124155849f57SBarry Smith #define __FUNCT__ "TSLoad"
124255849f57SBarry Smith /*@C
124355849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
124455849f57SBarry Smith 
124555849f57SBarry Smith   Collective on PetscViewer
124655849f57SBarry Smith 
124755849f57SBarry Smith   Input Parameters:
124855849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
124955849f57SBarry Smith            some related function before a call to TSLoad().
125055849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
125155849f57SBarry Smith 
125255849f57SBarry Smith    Level: intermediate
125355849f57SBarry Smith 
125455849f57SBarry Smith   Notes:
125555849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
125655849f57SBarry Smith 
125755849f57SBarry Smith   Notes for advanced users:
125855849f57SBarry Smith   Most users should not need to know the details of the binary storage
125955849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
126055849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
126155849f57SBarry Smith   format is
126255849f57SBarry Smith .vb
126355849f57SBarry Smith      has not yet been determined
126455849f57SBarry Smith .ve
126555849f57SBarry Smith 
126655849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
126755849f57SBarry Smith @*/
1268f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
126955849f57SBarry Smith {
127055849f57SBarry Smith   PetscErrorCode ierr;
127155849f57SBarry Smith   PetscBool      isbinary;
127255849f57SBarry Smith   PetscInt       classid;
127355849f57SBarry Smith   char           type[256];
12742d53ad75SBarry Smith   DMTS           sdm;
1275ad6bc421SBarry Smith   DM             dm;
127655849f57SBarry Smith 
127755849f57SBarry Smith   PetscFunctionBegin;
1278f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
127955849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
128055849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
128155849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
128255849f57SBarry Smith 
128355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1284ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
128555849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1286f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1287f2c2a1b9SBarry Smith   if (ts->ops->load) {
1288f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1289f2c2a1b9SBarry Smith   }
1290ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1291ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1292ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1293f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1294f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12952d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12962d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
129755849f57SBarry Smith   PetscFunctionReturn(0);
129855849f57SBarry Smith }
129955849f57SBarry Smith 
13009804daf3SBarry Smith #include <petscdraw.h>
1301f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1302f05ece33SBarry Smith #include <petscviewerams.h>
1303f05ece33SBarry Smith #endif
130455849f57SBarry Smith #undef __FUNCT__
13054a2ae208SSatish Balay #define __FUNCT__ "TSView"
13067e2c5f70SBarry Smith /*@C
1307d763cef2SBarry Smith     TSView - Prints the TS data structure.
1308d763cef2SBarry Smith 
13094c49b128SBarry Smith     Collective on TS
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith     Input Parameters:
1312d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1313d763cef2SBarry Smith -   viewer - visualization context
1314d763cef2SBarry Smith 
1315d763cef2SBarry Smith     Options Database Key:
1316d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1317d763cef2SBarry Smith 
1318d763cef2SBarry Smith     Notes:
1319d763cef2SBarry Smith     The available visualization contexts include
1320b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1321b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1322d763cef2SBarry Smith          output where only the first processor opens
1323d763cef2SBarry Smith          the file.  All other processors send their
1324d763cef2SBarry Smith          data to the first processor to print.
1325d763cef2SBarry Smith 
1326d763cef2SBarry Smith     The user can open an alternative visualization context with
1327b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith     Level: beginner
1330d763cef2SBarry Smith 
1331d763cef2SBarry Smith .keywords: TS, timestep, view
1332d763cef2SBarry Smith 
1333b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1334d763cef2SBarry Smith @*/
13357087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1336d763cef2SBarry Smith {
1337dfbe8321SBarry Smith   PetscErrorCode ierr;
133819fd82e9SBarry Smith   TSType         type;
13392b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13402d53ad75SBarry Smith   DMTS           sdm;
1341f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1342f05ece33SBarry Smith   PetscBool      isams;
1343f05ece33SBarry Smith #endif
1344d763cef2SBarry Smith 
1345d763cef2SBarry Smith   PetscFunctionBegin;
13460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13473050cee2SBarry Smith   if (!viewer) {
1348ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13493050cee2SBarry Smith   }
13500700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1351c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1352fd16b177SBarry Smith 
1353251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1354251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
135555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13562b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1357f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1358f05ece33SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERAMS,&isams);CHKERRQ(ierr);
1359f05ece33SBarry Smith #endif
136032077d6dSBarry Smith   if (iascii) {
1361dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
136277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1363a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1364d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13655ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1366c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1367d763cef2SBarry Smith     }
13685ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1369193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13702d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13712d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1372d52bd9f3SBarry Smith     if (ts->ops->view) {
1373d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1374d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1375d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1376d52bd9f3SBarry Smith     }
13770f5bd95cSBarry Smith   } else if (isstring) {
1378a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1379b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
138055849f57SBarry Smith   } else if (isbinary) {
138155849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
138255849f57SBarry Smith     MPI_Comm    comm;
138355849f57SBarry Smith     PetscMPIInt rank;
138455849f57SBarry Smith     char        type[256];
138555849f57SBarry Smith 
138655849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
138755849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
138855849f57SBarry Smith     if (!rank) {
138955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
139055849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
139155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
139255849f57SBarry Smith     }
139355849f57SBarry Smith     if (ts->ops->view) {
139455849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
139555849f57SBarry Smith     }
1396f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1397f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13982d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13992d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14002b0a91c0SBarry Smith   } else if (isdraw) {
14012b0a91c0SBarry Smith     PetscDraw draw;
14022b0a91c0SBarry Smith     char      str[36];
140389fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14042b0a91c0SBarry Smith 
14052b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14062b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14072b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14082b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14090298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
141089fd9fafSBarry Smith     bottom = y - h;
14112b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14122b0a91c0SBarry Smith     if (ts->ops->view) {
14132b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14142b0a91c0SBarry Smith     }
14152b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1416f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1417f05ece33SBarry Smith   } else if (isams) {
1418f05ece33SBarry Smith     if (((PetscObject)ts)->amsmem == -1) {
1419f05ece33SBarry Smith       ierr = PetscObjectViewAMS((PetscObject)ts,viewer);CHKERRQ(ierr);
1420f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_take_access,(((PetscObject)ts)->amsmem));
1421f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time step",&ts->steps,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1422f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time",&ts->ptime,1,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1423f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_grant_access,(((PetscObject)ts)->amsmem));
1424d763cef2SBarry Smith     }
14250acecf5bSBarry Smith     if (ts->ops->view) {
14260acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14270acecf5bSBarry Smith     }
1428f05ece33SBarry Smith #endif
1429f05ece33SBarry Smith   }
1430f05ece33SBarry Smith 
1431b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1432251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1433b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1434d763cef2SBarry Smith   PetscFunctionReturn(0);
1435d763cef2SBarry Smith }
1436d763cef2SBarry Smith 
1437d763cef2SBarry Smith 
14384a2ae208SSatish Balay #undef __FUNCT__
14394a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1440b07ff414SBarry Smith /*@
1441d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1442d763cef2SBarry Smith    the timesteppers.
1443d763cef2SBarry Smith 
14443f9fe445SBarry Smith    Logically Collective on TS
1445d763cef2SBarry Smith 
1446d763cef2SBarry Smith    Input Parameters:
1447d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1448d763cef2SBarry Smith -  usrP - optional user context
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Level: intermediate
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1455d763cef2SBarry Smith @*/
14567087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1457d763cef2SBarry Smith {
1458d763cef2SBarry Smith   PetscFunctionBegin;
14590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1460d763cef2SBarry Smith   ts->user = usrP;
1461d763cef2SBarry Smith   PetscFunctionReturn(0);
1462d763cef2SBarry Smith }
1463d763cef2SBarry Smith 
14644a2ae208SSatish Balay #undef __FUNCT__
14654a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1466b07ff414SBarry Smith /*@
1467d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1468d763cef2SBarry Smith     timestepper.
1469d763cef2SBarry Smith 
1470d763cef2SBarry Smith     Not Collective
1471d763cef2SBarry Smith 
1472d763cef2SBarry Smith     Input Parameter:
1473d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1474d763cef2SBarry Smith 
1475d763cef2SBarry Smith     Output Parameter:
1476d763cef2SBarry Smith .   usrP - user context
1477d763cef2SBarry Smith 
1478d763cef2SBarry Smith     Level: intermediate
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1483d763cef2SBarry Smith @*/
1484e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1485d763cef2SBarry Smith {
1486d763cef2SBarry Smith   PetscFunctionBegin;
14870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1488e71120c6SJed Brown   *(void**)usrP = ts->user;
1489d763cef2SBarry Smith   PetscFunctionReturn(0);
1490d763cef2SBarry Smith }
1491d763cef2SBarry Smith 
14924a2ae208SSatish Balay #undef __FUNCT__
14934a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1494d763cef2SBarry Smith /*@
1495b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1496d763cef2SBarry Smith 
1497d763cef2SBarry Smith    Not Collective
1498d763cef2SBarry Smith 
1499d763cef2SBarry Smith    Input Parameter:
1500d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith    Output Parameter:
1503b8123daeSJed Brown .  iter - number of steps completed so far
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith    Level: intermediate
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15089be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1509d763cef2SBarry Smith @*/
15107087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1511d763cef2SBarry Smith {
1512d763cef2SBarry Smith   PetscFunctionBegin;
15130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15144482741eSBarry Smith   PetscValidIntPointer(iter,2);
1515d763cef2SBarry Smith   *iter = ts->steps;
1516d763cef2SBarry Smith   PetscFunctionReturn(0);
1517d763cef2SBarry Smith }
1518d763cef2SBarry Smith 
15194a2ae208SSatish Balay #undef __FUNCT__
15204a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1521d763cef2SBarry Smith /*@
1522d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1523d763cef2SBarry Smith    as well as the initial time.
1524d763cef2SBarry Smith 
15253f9fe445SBarry Smith    Logically Collective on TS
1526d763cef2SBarry Smith 
1527d763cef2SBarry Smith    Input Parameters:
1528d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1529d763cef2SBarry Smith .  initial_time - the initial time
1530d763cef2SBarry Smith -  time_step - the size of the timestep
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith    Level: intermediate
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1535d763cef2SBarry Smith 
1536d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1537d763cef2SBarry Smith @*/
15387087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1539d763cef2SBarry Smith {
1540e144a568SJed Brown   PetscErrorCode ierr;
1541e144a568SJed Brown 
1542d763cef2SBarry Smith   PetscFunctionBegin;
15430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1544e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1545d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1546d763cef2SBarry Smith   PetscFunctionReturn(0);
1547d763cef2SBarry Smith }
1548d763cef2SBarry Smith 
15494a2ae208SSatish Balay #undef __FUNCT__
15504a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1551d763cef2SBarry Smith /*@
1552d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1553d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1554d763cef2SBarry Smith 
15553f9fe445SBarry Smith    Logically Collective on TS
1556d763cef2SBarry Smith 
1557d763cef2SBarry Smith    Input Parameters:
1558d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1559d763cef2SBarry Smith -  time_step - the size of the timestep
1560d763cef2SBarry Smith 
1561d763cef2SBarry Smith    Level: intermediate
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith .keywords: TS, set, timestep
1566d763cef2SBarry Smith @*/
15677087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1568d763cef2SBarry Smith {
1569d763cef2SBarry Smith   PetscFunctionBegin;
15700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1571c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1572d763cef2SBarry Smith   ts->time_step      = time_step;
157331748224SBarry Smith   ts->time_step_orig = time_step;
1574d763cef2SBarry Smith   PetscFunctionReturn(0);
1575d763cef2SBarry Smith }
1576d763cef2SBarry Smith 
15774a2ae208SSatish Balay #undef __FUNCT__
1578a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1579a43b19c4SJed Brown /*@
158049354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
158149354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
158249354f04SShri Abhyankar      or just return at the final time TS computed.
1583a43b19c4SJed Brown 
1584a43b19c4SJed Brown   Logically Collective on TS
1585a43b19c4SJed Brown 
1586a43b19c4SJed Brown    Input Parameter:
1587a43b19c4SJed Brown +   ts - the time-step context
158849354f04SShri Abhyankar -   eftopt - exact final time option
1589a43b19c4SJed Brown 
1590a43b19c4SJed Brown    Level: beginner
1591a43b19c4SJed Brown 
1592a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1593a43b19c4SJed Brown @*/
159449354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1595a43b19c4SJed Brown {
1596a43b19c4SJed Brown   PetscFunctionBegin;
1597a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
159849354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
159949354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1600a43b19c4SJed Brown   PetscFunctionReturn(0);
1601a43b19c4SJed Brown }
1602a43b19c4SJed Brown 
1603a43b19c4SJed Brown #undef __FUNCT__
16044a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1605d763cef2SBarry Smith /*@
1606d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1607d763cef2SBarry Smith 
1608d763cef2SBarry Smith    Not Collective
1609d763cef2SBarry Smith 
1610d763cef2SBarry Smith    Input Parameter:
1611d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1612d763cef2SBarry Smith 
1613d763cef2SBarry Smith    Output Parameter:
1614d763cef2SBarry Smith .  dt - the current timestep size
1615d763cef2SBarry Smith 
1616d763cef2SBarry Smith    Level: intermediate
1617d763cef2SBarry Smith 
1618d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1619d763cef2SBarry Smith 
1620d763cef2SBarry Smith .keywords: TS, get, timestep
1621d763cef2SBarry Smith @*/
16227087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1623d763cef2SBarry Smith {
1624d763cef2SBarry Smith   PetscFunctionBegin;
16250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1626f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1627d763cef2SBarry Smith   *dt = ts->time_step;
1628d763cef2SBarry Smith   PetscFunctionReturn(0);
1629d763cef2SBarry Smith }
1630d763cef2SBarry Smith 
16314a2ae208SSatish Balay #undef __FUNCT__
16324a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1633d8e5e3e6SSatish Balay /*@
1634d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1635d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1636d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1637d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1638d763cef2SBarry Smith 
1639d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1640d763cef2SBarry Smith 
1641d763cef2SBarry Smith    Input Parameter:
1642d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1643d763cef2SBarry Smith 
1644d763cef2SBarry Smith    Output Parameter:
1645d763cef2SBarry Smith .  v - the vector containing the solution
1646d763cef2SBarry Smith 
1647d763cef2SBarry Smith    Level: intermediate
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith .seealso: TSGetTimeStep()
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1652d763cef2SBarry Smith @*/
16537087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1654d763cef2SBarry Smith {
1655d763cef2SBarry Smith   PetscFunctionBegin;
16560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16574482741eSBarry Smith   PetscValidPointer(v,2);
16588737fe31SLisandro Dalcin   *v = ts->vec_sol;
1659d763cef2SBarry Smith   PetscFunctionReturn(0);
1660d763cef2SBarry Smith }
1661d763cef2SBarry Smith 
1662bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
16634a2ae208SSatish Balay #undef __FUNCT__
1664bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1665d8e5e3e6SSatish Balay /*@
1666bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1667d763cef2SBarry Smith 
1668bdad233fSMatthew Knepley   Not collective
1669d763cef2SBarry Smith 
1670bdad233fSMatthew Knepley   Input Parameters:
1671bdad233fSMatthew Knepley + ts   - The TS
1672bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1673d763cef2SBarry Smith .vb
16740910c330SBarry Smith          U_t - A U = 0      (linear)
16750910c330SBarry Smith          U_t - A(t) U = 0   (linear)
16760910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1677d763cef2SBarry Smith .ve
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Level: beginner
1680d763cef2SBarry Smith 
1681bdad233fSMatthew Knepley .keywords: TS, problem type
1682bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1683d763cef2SBarry Smith @*/
16847087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1685a7cc72afSBarry Smith {
16869e2a6581SJed Brown   PetscErrorCode ierr;
16879e2a6581SJed Brown 
1688d763cef2SBarry Smith   PetscFunctionBegin;
16890700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1690bdad233fSMatthew Knepley   ts->problem_type = type;
16919e2a6581SJed Brown   if (type == TS_LINEAR) {
16929e2a6581SJed Brown     SNES snes;
16939e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
16949e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
16959e2a6581SJed Brown   }
1696d763cef2SBarry Smith   PetscFunctionReturn(0);
1697d763cef2SBarry Smith }
1698d763cef2SBarry Smith 
1699bdad233fSMatthew Knepley #undef __FUNCT__
1700bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1701bdad233fSMatthew Knepley /*@C
1702bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1703bdad233fSMatthew Knepley 
1704bdad233fSMatthew Knepley   Not collective
1705bdad233fSMatthew Knepley 
1706bdad233fSMatthew Knepley   Input Parameter:
1707bdad233fSMatthew Knepley . ts   - The TS
1708bdad233fSMatthew Knepley 
1709bdad233fSMatthew Knepley   Output Parameter:
1710bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1711bdad233fSMatthew Knepley .vb
1712089b2837SJed Brown          M U_t = A U
1713089b2837SJed Brown          M(t) U_t = A(t) U
1714b5abc632SBarry Smith          F(t,U,U_t)
1715bdad233fSMatthew Knepley .ve
1716bdad233fSMatthew Knepley 
1717bdad233fSMatthew Knepley    Level: beginner
1718bdad233fSMatthew Knepley 
1719bdad233fSMatthew Knepley .keywords: TS, problem type
1720bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1721bdad233fSMatthew Knepley @*/
17227087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1723a7cc72afSBarry Smith {
1724bdad233fSMatthew Knepley   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17264482741eSBarry Smith   PetscValidIntPointer(type,2);
1727bdad233fSMatthew Knepley   *type = ts->problem_type;
1728bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1729bdad233fSMatthew Knepley }
1730d763cef2SBarry Smith 
17314a2ae208SSatish Balay #undef __FUNCT__
17324a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1733d763cef2SBarry Smith /*@
1734d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1735d763cef2SBarry Smith    of a timestepper.
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith    Collective on TS
1738d763cef2SBarry Smith 
1739d763cef2SBarry Smith    Input Parameter:
1740d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1741d763cef2SBarry Smith 
1742d763cef2SBarry Smith    Notes:
1743d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1744d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1745d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1746d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1747d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1748d763cef2SBarry Smith 
1749d763cef2SBarry Smith    Level: advanced
1750d763cef2SBarry Smith 
1751d763cef2SBarry Smith .keywords: TS, timestep, setup
1752d763cef2SBarry Smith 
1753d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1754d763cef2SBarry Smith @*/
17557087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1756d763cef2SBarry Smith {
1757dfbe8321SBarry Smith   PetscErrorCode ierr;
17586c6b9e74SPeter Brune   DM             dm;
17596c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17606c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
17616c6b9e74SPeter Brune   TSIJacobian    ijac;
17626c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1763d763cef2SBarry Smith 
1764d763cef2SBarry Smith   PetscFunctionBegin;
17650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1766277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1767277b19d0SLisandro Dalcin 
17687adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
17699596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1770d763cef2SBarry Smith   }
1771277b19d0SLisandro Dalcin 
1772277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1773277b19d0SLisandro Dalcin 
1774552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
17751c3436cfSJed Brown 
1776e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1777e1244c69SJed Brown     Mat Amat,Pmat;
1778e1244c69SJed Brown     SNES snes;
1779e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1780e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1781e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1782e1244c69SJed Brown      * have displaced the RHS matrix */
1783e1244c69SJed Brown     if (Amat == ts->Arhs) {
1784e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1785e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1786e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1787e1244c69SJed Brown     }
1788e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1789e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1790e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1791e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1792e1244c69SJed Brown     }
1793e1244c69SJed Brown   }
1794e1244c69SJed Brown 
1795277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1796000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1797277b19d0SLisandro Dalcin   }
1798277b19d0SLisandro Dalcin 
17996c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18006c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18016c6b9e74SPeter Brune    */
18026c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18030298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18046c6b9e74SPeter Brune   if (!func) {
18056c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18066c6b9e74SPeter Brune   }
18076c6b9e74SPeter Brune   /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
18086c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18096c6b9e74SPeter Brune    */
18100298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18110298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18120298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18136c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18146c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18156c6b9e74SPeter Brune   }
1816277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1817277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1818277b19d0SLisandro Dalcin }
1819277b19d0SLisandro Dalcin 
1820277b19d0SLisandro Dalcin #undef __FUNCT__
1821277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1822277b19d0SLisandro Dalcin /*@
1823277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1824277b19d0SLisandro Dalcin 
1825277b19d0SLisandro Dalcin    Collective on TS
1826277b19d0SLisandro Dalcin 
1827277b19d0SLisandro Dalcin    Input Parameter:
1828277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1829277b19d0SLisandro Dalcin 
1830277b19d0SLisandro Dalcin    Level: beginner
1831277b19d0SLisandro Dalcin 
1832277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1833277b19d0SLisandro Dalcin 
1834277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1835277b19d0SLisandro Dalcin @*/
1836277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1837277b19d0SLisandro Dalcin {
1838277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1839277b19d0SLisandro Dalcin 
1840277b19d0SLisandro Dalcin   PetscFunctionBegin;
1841277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1842277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1843277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1844277b19d0SLisandro Dalcin   }
1845277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1846bbd56ea5SKarl Rupp 
18474e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
18484e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1849214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
18506bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1851e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1852e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
185338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1854bbd56ea5SKarl Rupp 
1855277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1856d763cef2SBarry Smith   PetscFunctionReturn(0);
1857d763cef2SBarry Smith }
1858d763cef2SBarry Smith 
18594a2ae208SSatish Balay #undef __FUNCT__
18604a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1861d8e5e3e6SSatish Balay /*@
1862d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1863d763cef2SBarry Smith    with TSCreate().
1864d763cef2SBarry Smith 
1865d763cef2SBarry Smith    Collective on TS
1866d763cef2SBarry Smith 
1867d763cef2SBarry Smith    Input Parameter:
1868d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1869d763cef2SBarry Smith 
1870d763cef2SBarry Smith    Level: beginner
1871d763cef2SBarry Smith 
1872d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1873d763cef2SBarry Smith 
1874d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1875d763cef2SBarry Smith @*/
18766bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1877d763cef2SBarry Smith {
18786849ba73SBarry Smith   PetscErrorCode ierr;
1879d763cef2SBarry Smith 
1880d763cef2SBarry Smith   PetscFunctionBegin;
18816bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
18826bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
18836bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1884d763cef2SBarry Smith 
18856bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1886277b19d0SLisandro Dalcin 
1887be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
1888f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*ts);CHKERRQ(ierr);
18896bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
18906d4c513bSLisandro Dalcin 
189184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
18926bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
18936bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
18946bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
18956d4c513bSLisandro Dalcin 
1896a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1897d763cef2SBarry Smith   PetscFunctionReturn(0);
1898d763cef2SBarry Smith }
1899d763cef2SBarry Smith 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1902d8e5e3e6SSatish Balay /*@
1903d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1904d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1905d763cef2SBarry Smith 
1906d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1907d763cef2SBarry Smith 
1908d763cef2SBarry Smith    Input Parameter:
1909d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1910d763cef2SBarry Smith 
1911d763cef2SBarry Smith    Output Parameter:
1912d763cef2SBarry Smith .  snes - the nonlinear solver context
1913d763cef2SBarry Smith 
1914d763cef2SBarry Smith    Notes:
1915d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1916d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
191794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1918d763cef2SBarry Smith 
1919d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
19200298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1921d763cef2SBarry Smith 
1922d763cef2SBarry Smith    Level: beginner
1923d763cef2SBarry Smith 
1924d763cef2SBarry Smith .keywords: timestep, get, SNES
1925d763cef2SBarry Smith @*/
19267087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1927d763cef2SBarry Smith {
1928d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1929d372ba47SLisandro Dalcin 
1930d763cef2SBarry Smith   PetscFunctionBegin;
19310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19324482741eSBarry Smith   PetscValidPointer(snes,2);
1933d372ba47SLisandro Dalcin   if (!ts->snes) {
1934ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
19350298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19363bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
1937d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1938496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
19399e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
19409e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
19419e2a6581SJed Brown     }
1942d372ba47SLisandro Dalcin   }
1943d763cef2SBarry Smith   *snes = ts->snes;
1944d763cef2SBarry Smith   PetscFunctionReturn(0);
1945d763cef2SBarry Smith }
1946d763cef2SBarry Smith 
19474a2ae208SSatish Balay #undef __FUNCT__
1948deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1949deb2cd25SJed Brown /*@
1950deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1951deb2cd25SJed Brown 
1952deb2cd25SJed Brown    Collective
1953deb2cd25SJed Brown 
1954deb2cd25SJed Brown    Input Parameter:
1955deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1956deb2cd25SJed Brown -  snes - the nonlinear solver context
1957deb2cd25SJed Brown 
1958deb2cd25SJed Brown    Notes:
1959deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1960deb2cd25SJed Brown 
1961deb2cd25SJed Brown    Level: developer
1962deb2cd25SJed Brown 
1963deb2cd25SJed Brown .keywords: timestep, set, SNES
1964deb2cd25SJed Brown @*/
1965deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1966deb2cd25SJed Brown {
1967deb2cd25SJed Brown   PetscErrorCode ierr;
1968740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1969deb2cd25SJed Brown 
1970deb2cd25SJed Brown   PetscFunctionBegin;
1971deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1972deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1973deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1974deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1975bbd56ea5SKarl Rupp 
1976deb2cd25SJed Brown   ts->snes = snes;
1977bbd56ea5SKarl Rupp 
19780298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19790298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1980740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
19810298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1982740132f1SEmil Constantinescu   }
1983deb2cd25SJed Brown   PetscFunctionReturn(0);
1984deb2cd25SJed Brown }
1985deb2cd25SJed Brown 
1986deb2cd25SJed Brown #undef __FUNCT__
198794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1988d8e5e3e6SSatish Balay /*@
198994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1990d763cef2SBarry Smith    a TS (timestepper) context.
1991d763cef2SBarry Smith 
199294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1993d763cef2SBarry Smith 
1994d763cef2SBarry Smith    Input Parameter:
1995d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith    Output Parameter:
199894b7f48cSBarry Smith .  ksp - the nonlinear solver context
1999d763cef2SBarry Smith 
2000d763cef2SBarry Smith    Notes:
200194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2002d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2003d763cef2SBarry Smith    KSP and PC contexts as well.
2004d763cef2SBarry Smith 
200594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
20060298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2007d763cef2SBarry Smith 
2008d763cef2SBarry Smith    Level: beginner
2009d763cef2SBarry Smith 
201094b7f48cSBarry Smith .keywords: timestep, get, KSP
2011d763cef2SBarry Smith @*/
20127087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2013d763cef2SBarry Smith {
2014d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2015089b2837SJed Brown   SNES           snes;
2016d372ba47SLisandro Dalcin 
2017d763cef2SBarry Smith   PetscFunctionBegin;
20180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20194482741eSBarry Smith   PetscValidPointer(ksp,2);
202017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2021e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2022089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2023089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2024d763cef2SBarry Smith   PetscFunctionReturn(0);
2025d763cef2SBarry Smith }
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2028d763cef2SBarry Smith 
20294a2ae208SSatish Balay #undef __FUNCT__
2030adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2031adb62b0dSMatthew Knepley /*@
2032adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2033adb62b0dSMatthew Knepley    maximum time for iteration.
2034adb62b0dSMatthew Knepley 
20353f9fe445SBarry Smith    Not Collective
2036adb62b0dSMatthew Knepley 
2037adb62b0dSMatthew Knepley    Input Parameters:
2038adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
20390298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
20400298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2041adb62b0dSMatthew Knepley 
2042adb62b0dSMatthew Knepley    Level: intermediate
2043adb62b0dSMatthew Knepley 
2044adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2045adb62b0dSMatthew Knepley @*/
20467087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2047adb62b0dSMatthew Knepley {
2048adb62b0dSMatthew Knepley   PetscFunctionBegin;
20490700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2050abc0a331SBarry Smith   if (maxsteps) {
20514482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2052adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2053adb62b0dSMatthew Knepley   }
2054abc0a331SBarry Smith   if (maxtime) {
20554482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2056adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2057adb62b0dSMatthew Knepley   }
2058adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2059adb62b0dSMatthew Knepley }
2060adb62b0dSMatthew Knepley 
2061adb62b0dSMatthew Knepley #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2063d763cef2SBarry Smith /*@
2064d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2065d763cef2SBarry Smith    maximum time for iteration.
2066d763cef2SBarry Smith 
20673f9fe445SBarry Smith    Logically Collective on TS
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith    Input Parameters:
2070d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2071d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2072d763cef2SBarry Smith -  maxtime - final time to iterate to
2073d763cef2SBarry Smith 
2074d763cef2SBarry Smith    Options Database Keys:
2075d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
20763bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith    Notes:
2079d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith    Level: intermediate
2082d763cef2SBarry Smith 
2083d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2084a43b19c4SJed Brown 
2085a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2086d763cef2SBarry Smith @*/
20877087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2088d763cef2SBarry Smith {
2089d763cef2SBarry Smith   PetscFunctionBegin;
20900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2091c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2092c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
209339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
209439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2095d763cef2SBarry Smith   PetscFunctionReturn(0);
2096d763cef2SBarry Smith }
2097d763cef2SBarry Smith 
20984a2ae208SSatish Balay #undef __FUNCT__
20994a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2100d763cef2SBarry Smith /*@
2101d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2102d763cef2SBarry Smith    for use by the TS routines.
2103d763cef2SBarry Smith 
21043f9fe445SBarry Smith    Logically Collective on TS and Vec
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith    Input Parameters:
2107d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
21080910c330SBarry Smith -  u - the solution vector
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Level: beginner
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2113d763cef2SBarry Smith @*/
21140910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2115d763cef2SBarry Smith {
21168737fe31SLisandro Dalcin   PetscErrorCode ierr;
2117496e6a7aSJed Brown   DM             dm;
21188737fe31SLisandro Dalcin 
2119d763cef2SBarry Smith   PetscFunctionBegin;
21200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21210910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
21220910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
21236bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2124bbd56ea5SKarl Rupp 
21250910c330SBarry Smith   ts->vec_sol = u;
2126bbd56ea5SKarl Rupp 
2127496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
21280910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2129d763cef2SBarry Smith   PetscFunctionReturn(0);
2130d763cef2SBarry Smith }
2131d763cef2SBarry Smith 
2132e74ef692SMatthew Knepley #undef __FUNCT__
2133e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2134ac226902SBarry Smith /*@C
2135000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
21363f2090d5SJed Brown   called once at the beginning of each time step.
2137000e7ae3SMatthew Knepley 
21383f9fe445SBarry Smith   Logically Collective on TS
2139000e7ae3SMatthew Knepley 
2140000e7ae3SMatthew Knepley   Input Parameters:
2141000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2142000e7ae3SMatthew Knepley - func - The function
2143000e7ae3SMatthew Knepley 
2144000e7ae3SMatthew Knepley   Calling sequence of func:
2145000e7ae3SMatthew Knepley . func (TS ts);
2146000e7ae3SMatthew Knepley 
2147000e7ae3SMatthew Knepley   Level: intermediate
2148000e7ae3SMatthew Knepley 
2149b8123daeSJed Brown   Note:
2150b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2151b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2152b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2153b8123daeSJed Brown 
2154000e7ae3SMatthew Knepley .keywords: TS, timestep
21559be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2156000e7ae3SMatthew Knepley @*/
21577087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2158000e7ae3SMatthew Knepley {
2159000e7ae3SMatthew Knepley   PetscFunctionBegin;
21600700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2161ae60f76fSBarry Smith   ts->prestep = func;
2162000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2163000e7ae3SMatthew Knepley }
2164000e7ae3SMatthew Knepley 
2165e74ef692SMatthew Knepley #undef __FUNCT__
21663f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
216709ee8438SJed Brown /*@
21683f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
21693f2090d5SJed Brown 
21703f2090d5SJed Brown   Collective on TS
21713f2090d5SJed Brown 
21723f2090d5SJed Brown   Input Parameters:
21733f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21743f2090d5SJed Brown 
21753f2090d5SJed Brown   Notes:
21763f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
21773f2090d5SJed Brown   so most users would not generally call this routine themselves.
21783f2090d5SJed Brown 
21793f2090d5SJed Brown   Level: developer
21803f2090d5SJed Brown 
21813f2090d5SJed Brown .keywords: TS, timestep
21829be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
21833f2090d5SJed Brown @*/
21847087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
21853f2090d5SJed Brown {
21863f2090d5SJed Brown   PetscErrorCode ierr;
21873f2090d5SJed Brown 
21883f2090d5SJed Brown   PetscFunctionBegin;
21890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2190ae60f76fSBarry Smith   if (ts->prestep) {
2191ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2192312ce896SJed Brown   }
21933f2090d5SJed Brown   PetscFunctionReturn(0);
21943f2090d5SJed Brown }
21953f2090d5SJed Brown 
21963f2090d5SJed Brown #undef __FUNCT__
2197b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2198b8123daeSJed Brown /*@C
2199b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2200b8123daeSJed Brown   called once at the beginning of each stage.
2201b8123daeSJed Brown 
2202b8123daeSJed Brown   Logically Collective on TS
2203b8123daeSJed Brown 
2204b8123daeSJed Brown   Input Parameters:
2205b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2206b8123daeSJed Brown - func - The function
2207b8123daeSJed Brown 
2208b8123daeSJed Brown   Calling sequence of func:
2209b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2210b8123daeSJed Brown 
2211b8123daeSJed Brown   Level: intermediate
2212b8123daeSJed Brown 
2213b8123daeSJed Brown   Note:
2214b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2215b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2216b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2217b8123daeSJed Brown 
2218b8123daeSJed Brown .keywords: TS, timestep
22199be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2220b8123daeSJed Brown @*/
2221b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2222b8123daeSJed Brown {
2223b8123daeSJed Brown   PetscFunctionBegin;
2224b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2225ae60f76fSBarry Smith   ts->prestage = func;
2226b8123daeSJed Brown   PetscFunctionReturn(0);
2227b8123daeSJed Brown }
2228b8123daeSJed Brown 
2229b8123daeSJed Brown #undef __FUNCT__
22309be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
22319be3e283SDebojyoti Ghosh /*@C
22329be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
22339be3e283SDebojyoti Ghosh   called once at the end of each stage.
22349be3e283SDebojyoti Ghosh 
22359be3e283SDebojyoti Ghosh   Logically Collective on TS
22369be3e283SDebojyoti Ghosh 
22379be3e283SDebojyoti Ghosh   Input Parameters:
22389be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
22399be3e283SDebojyoti Ghosh - func - The function
22409be3e283SDebojyoti Ghosh 
22419be3e283SDebojyoti Ghosh   Calling sequence of func:
22429be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
22439be3e283SDebojyoti Ghosh 
22449be3e283SDebojyoti Ghosh   Level: intermediate
22459be3e283SDebojyoti Ghosh 
22469be3e283SDebojyoti Ghosh   Note:
22479be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
22489be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
22499be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
22509be3e283SDebojyoti Ghosh 
22519be3e283SDebojyoti Ghosh .keywords: TS, timestep
22529be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
22539be3e283SDebojyoti Ghosh @*/
22549be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
22559be3e283SDebojyoti Ghosh {
22569be3e283SDebojyoti Ghosh   PetscFunctionBegin;
22579be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
22589be3e283SDebojyoti Ghosh   ts->poststage = func;
22599be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
22609be3e283SDebojyoti Ghosh }
22619be3e283SDebojyoti Ghosh 
22629be3e283SDebojyoti Ghosh #undef __FUNCT__
2263b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2264b8123daeSJed Brown /*@
2265b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2266b8123daeSJed Brown 
2267b8123daeSJed Brown   Collective on TS
2268b8123daeSJed Brown 
2269b8123daeSJed Brown   Input Parameters:
2270b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
22719be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2272b8123daeSJed Brown 
2273b8123daeSJed Brown   Notes:
2274b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2275b8123daeSJed Brown   most users would not generally call this routine themselves.
2276b8123daeSJed Brown 
2277b8123daeSJed Brown   Level: developer
2278b8123daeSJed Brown 
2279b8123daeSJed Brown .keywords: TS, timestep
22809be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2281b8123daeSJed Brown @*/
2282b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2283b8123daeSJed Brown {
2284b8123daeSJed Brown   PetscErrorCode ierr;
2285b8123daeSJed Brown 
2286b8123daeSJed Brown   PetscFunctionBegin;
2287b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2288ae60f76fSBarry Smith   if (ts->prestage) {
2289ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2290b8123daeSJed Brown   }
2291b8123daeSJed Brown   PetscFunctionReturn(0);
2292b8123daeSJed Brown }
2293b8123daeSJed Brown 
2294b8123daeSJed Brown #undef __FUNCT__
22959be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
22969be3e283SDebojyoti Ghosh /*@
22979be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
22989be3e283SDebojyoti Ghosh 
22999be3e283SDebojyoti Ghosh   Collective on TS
23009be3e283SDebojyoti Ghosh 
23019be3e283SDebojyoti Ghosh   Input Parameters:
23029be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
23039be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
23049be3e283SDebojyoti Ghosh   stageindex  - Stage number
23059be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
23069be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
23079be3e283SDebojyoti Ghosh 
23089be3e283SDebojyoti Ghosh   Notes:
23099be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
23109be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
23119be3e283SDebojyoti Ghosh 
23129be3e283SDebojyoti Ghosh   Level: developer
23139be3e283SDebojyoti Ghosh 
23149be3e283SDebojyoti Ghosh .keywords: TS, timestep
23159be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
23169be3e283SDebojyoti Ghosh @*/
23179be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
23189be3e283SDebojyoti Ghosh {
23199be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
23209be3e283SDebojyoti Ghosh 
23219be3e283SDebojyoti Ghosh   PetscFunctionBegin;
23229be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23239be3e283SDebojyoti Ghosh   if (ts->prestage) {
23249be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
23259be3e283SDebojyoti Ghosh   }
23269be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
23279be3e283SDebojyoti Ghosh }
23289be3e283SDebojyoti Ghosh 
23299be3e283SDebojyoti Ghosh #undef __FUNCT__
2330e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2331ac226902SBarry Smith /*@C
2332000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
23333f2090d5SJed Brown   called once at the end of each time step.
2334000e7ae3SMatthew Knepley 
23353f9fe445SBarry Smith   Logically Collective on TS
2336000e7ae3SMatthew Knepley 
2337000e7ae3SMatthew Knepley   Input Parameters:
2338000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2339000e7ae3SMatthew Knepley - func - The function
2340000e7ae3SMatthew Knepley 
2341000e7ae3SMatthew Knepley   Calling sequence of func:
2342b8123daeSJed Brown $ func (TS ts);
2343000e7ae3SMatthew Knepley 
2344000e7ae3SMatthew Knepley   Level: intermediate
2345000e7ae3SMatthew Knepley 
2346000e7ae3SMatthew Knepley .keywords: TS, timestep
2347b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2348000e7ae3SMatthew Knepley @*/
23497087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2350000e7ae3SMatthew Knepley {
2351000e7ae3SMatthew Knepley   PetscFunctionBegin;
23520700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2353ae60f76fSBarry Smith   ts->poststep = func;
2354000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2355000e7ae3SMatthew Knepley }
2356000e7ae3SMatthew Knepley 
2357e74ef692SMatthew Knepley #undef __FUNCT__
23583f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
235909ee8438SJed Brown /*@
23603f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
23613f2090d5SJed Brown 
23623f2090d5SJed Brown   Collective on TS
23633f2090d5SJed Brown 
23643f2090d5SJed Brown   Input Parameters:
23653f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
23663f2090d5SJed Brown 
23673f2090d5SJed Brown   Notes:
23683f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
23693f2090d5SJed Brown   so most users would not generally call this routine themselves.
23703f2090d5SJed Brown 
23713f2090d5SJed Brown   Level: developer
23723f2090d5SJed Brown 
23733f2090d5SJed Brown .keywords: TS, timestep
23743f2090d5SJed Brown @*/
23757087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
23763f2090d5SJed Brown {
23773f2090d5SJed Brown   PetscErrorCode ierr;
23783f2090d5SJed Brown 
23793f2090d5SJed Brown   PetscFunctionBegin;
23800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2381ae60f76fSBarry Smith   if (ts->poststep) {
2382ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
238372ac3e02SJed Brown   }
23843f2090d5SJed Brown   PetscFunctionReturn(0);
23853f2090d5SJed Brown }
23863f2090d5SJed Brown 
2387d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2388d763cef2SBarry Smith 
23894a2ae208SSatish Balay #undef __FUNCT__
2390a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2391d763cef2SBarry Smith /*@C
2392a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2393d763cef2SBarry Smith    timestep to display the iteration's  progress.
2394d763cef2SBarry Smith 
23953f9fe445SBarry Smith    Logically Collective on TS
2396d763cef2SBarry Smith 
2397d763cef2SBarry Smith    Input Parameters:
2398d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2399e213d8f1SJed Brown .  monitor - monitoring routine
2400329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
24010298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2402b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
24030298fd71SBarry Smith           (may be NULL)
2404d763cef2SBarry Smith 
2405e213d8f1SJed Brown    Calling sequence of monitor:
24060910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2407d763cef2SBarry Smith 
2408d763cef2SBarry Smith +    ts - the TS context
240988c05cc5SBarry 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
241088c05cc5SBarry Smith                                been interpolated to)
24111f06c33eSBarry Smith .    time - current time
24120910c330SBarry Smith .    u - current iterate
2413d763cef2SBarry Smith -    mctx - [optional] monitoring context
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Notes:
2416d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2417d763cef2SBarry Smith    already has been loaded.
2418d763cef2SBarry Smith 
2419025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2420025f1a04SBarry Smith 
2421d763cef2SBarry Smith    Level: intermediate
2422d763cef2SBarry Smith 
2423d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2424d763cef2SBarry Smith 
2425a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2426d763cef2SBarry Smith @*/
2427c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2428d763cef2SBarry Smith {
2429d763cef2SBarry Smith   PetscFunctionBegin;
24300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
243117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2432d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
24338704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2434d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2435d763cef2SBarry Smith   PetscFunctionReturn(0);
2436d763cef2SBarry Smith }
2437d763cef2SBarry Smith 
24384a2ae208SSatish Balay #undef __FUNCT__
2439a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2440d763cef2SBarry Smith /*@C
2441a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2442d763cef2SBarry Smith 
24433f9fe445SBarry Smith    Logically Collective on TS
2444d763cef2SBarry Smith 
2445d763cef2SBarry Smith    Input Parameters:
2446d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2447d763cef2SBarry Smith 
2448d763cef2SBarry Smith    Notes:
2449d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2450d763cef2SBarry Smith 
2451d763cef2SBarry Smith    Level: intermediate
2452d763cef2SBarry Smith 
2453d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2454d763cef2SBarry Smith 
2455a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2456d763cef2SBarry Smith @*/
24577087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2458d763cef2SBarry Smith {
2459d952e501SBarry Smith   PetscErrorCode ierr;
2460d952e501SBarry Smith   PetscInt       i;
2461d952e501SBarry Smith 
2462d763cef2SBarry Smith   PetscFunctionBegin;
24630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2464d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
24658704b422SBarry Smith     if (ts->monitordestroy[i]) {
24668704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2467d952e501SBarry Smith     }
2468d952e501SBarry Smith   }
2469d763cef2SBarry Smith   ts->numbermonitors = 0;
2470d763cef2SBarry Smith   PetscFunctionReturn(0);
2471d763cef2SBarry Smith }
2472d763cef2SBarry Smith 
24734a2ae208SSatish Balay #undef __FUNCT__
2474a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2475d8e5e3e6SSatish Balay /*@
2476a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
24775516499fSSatish Balay 
24785516499fSSatish Balay    Level: intermediate
247941251cbbSSatish Balay 
24805516499fSSatish Balay .keywords: TS, set, monitor
24815516499fSSatish Balay 
248241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
248341251cbbSSatish Balay @*/
2484649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2485d763cef2SBarry Smith {
2486dfbe8321SBarry Smith   PetscErrorCode ierr;
2487ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2488d132466eSBarry Smith 
2489d763cef2SBarry Smith   PetscFunctionBegin;
2490649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2491971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2492649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2493d763cef2SBarry Smith   PetscFunctionReturn(0);
2494d763cef2SBarry Smith }
2495d763cef2SBarry Smith 
24964a2ae208SSatish Balay #undef __FUNCT__
2497cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2498cd652676SJed Brown /*@
2499cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2500cd652676SJed Brown 
2501cd652676SJed Brown    Logically Collective on TS
2502cd652676SJed Brown 
2503cd652676SJed Brown    Input Argument:
2504cd652676SJed Brown .  ts - time stepping context
2505cd652676SJed Brown 
2506cd652676SJed Brown    Output Argument:
2507cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2508cd652676SJed Brown 
2509cd652676SJed Brown    Level: intermediate
2510cd652676SJed Brown 
2511cd652676SJed Brown .keywords: TS, set
2512cd652676SJed Brown 
2513cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2514cd652676SJed Brown @*/
2515cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2516cd652676SJed Brown {
2517cd652676SJed Brown   PetscFunctionBegin;
2518cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2519cd652676SJed Brown   ts->retain_stages = flg;
2520cd652676SJed Brown   PetscFunctionReturn(0);
2521cd652676SJed Brown }
2522cd652676SJed Brown 
2523cd652676SJed Brown #undef __FUNCT__
2524cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2525cd652676SJed Brown /*@
2526cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2527cd652676SJed Brown 
2528cd652676SJed Brown    Collective on TS
2529cd652676SJed Brown 
2530cd652676SJed Brown    Input Argument:
2531cd652676SJed Brown +  ts - time stepping context
2532cd652676SJed Brown -  t - time to interpolate to
2533cd652676SJed Brown 
2534cd652676SJed Brown    Output Argument:
25350910c330SBarry Smith .  U - state at given time
2536cd652676SJed Brown 
2537cd652676SJed Brown    Notes:
2538cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2539cd652676SJed Brown 
2540cd652676SJed Brown    Level: intermediate
2541cd652676SJed Brown 
2542cd652676SJed Brown    Developer Notes:
2543cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2544cd652676SJed Brown 
2545cd652676SJed Brown .keywords: TS, set
2546cd652676SJed Brown 
2547cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2548cd652676SJed Brown @*/
25490910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2550cd652676SJed Brown {
2551cd652676SJed Brown   PetscErrorCode ierr;
2552cd652676SJed Brown 
2553cd652676SJed Brown   PetscFunctionBegin;
2554cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2555b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2556ce94432eSBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
2557ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
25580910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2559cd652676SJed Brown   PetscFunctionReturn(0);
2560cd652676SJed Brown }
2561cd652676SJed Brown 
2562cd652676SJed Brown #undef __FUNCT__
25634a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2564d763cef2SBarry Smith /*@
25656d9e5789SSean Farley    TSStep - Steps one time step
2566d763cef2SBarry Smith 
2567d763cef2SBarry Smith    Collective on TS
2568d763cef2SBarry Smith 
2569d763cef2SBarry Smith    Input Parameter:
2570d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2571d763cef2SBarry Smith 
25726d9e5789SSean Farley    Level: intermediate
2573d763cef2SBarry Smith 
2574b8123daeSJed Brown    Notes:
2575b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2576b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2577b8123daeSJed Brown 
257825cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
257925cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
258025cb2221SBarry Smith 
2581d763cef2SBarry Smith .keywords: TS, timestep, solve
2582d763cef2SBarry Smith 
25839be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
2584d763cef2SBarry Smith @*/
2585193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2586d763cef2SBarry Smith {
2587362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2588dfbe8321SBarry Smith   PetscErrorCode ierr;
2589d763cef2SBarry Smith 
2590d763cef2SBarry Smith   PetscFunctionBegin;
25910700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2592d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2593d405a339SMatthew Knepley 
2594362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2595362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2596bbd56ea5SKarl Rupp 
2597d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2598193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2599d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2600bbd56ea5SKarl Rupp 
2601362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2602362cd11cSLisandro Dalcin 
2603362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2604cef5090cSJed Brown     if (ts->errorifstepfailed) {
2605cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2606ce94432eSBarry 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]);
2607ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2608cef5090cSJed Brown     }
2609362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2610db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2611db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2612362cd11cSLisandro Dalcin   }
2613d763cef2SBarry Smith   PetscFunctionReturn(0);
2614d763cef2SBarry Smith }
2615d763cef2SBarry Smith 
26164a2ae208SSatish Balay #undef __FUNCT__
261705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
261805175c85SJed Brown /*@
261905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
262005175c85SJed Brown 
26211c3436cfSJed Brown    Collective on TS
262205175c85SJed Brown 
262305175c85SJed Brown    Input Arguments:
26241c3436cfSJed Brown +  ts - time stepping context
26251c3436cfSJed Brown .  order - desired order of accuracy
26260298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
262705175c85SJed Brown 
262805175c85SJed Brown    Output Arguments:
26290910c330SBarry Smith .  U - state at the end of the current step
263005175c85SJed Brown 
263105175c85SJed Brown    Level: advanced
263205175c85SJed Brown 
2633108c343cSJed Brown    Notes:
2634108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2635108c343cSJed 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.
2636108c343cSJed Brown 
26371c3436cfSJed Brown .seealso: TSStep(), TSAdapt
263805175c85SJed Brown @*/
26390910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
264005175c85SJed Brown {
264105175c85SJed Brown   PetscErrorCode ierr;
264205175c85SJed Brown 
264305175c85SJed Brown   PetscFunctionBegin;
264405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
264505175c85SJed Brown   PetscValidType(ts,1);
26460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2647ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
26480910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
264905175c85SJed Brown   PetscFunctionReturn(0);
265005175c85SJed Brown }
265105175c85SJed Brown 
265205175c85SJed Brown #undef __FUNCT__
26536a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
26546a4d4014SLisandro Dalcin /*@
26556a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
26566a4d4014SLisandro Dalcin 
26576a4d4014SLisandro Dalcin    Collective on TS
26586a4d4014SLisandro Dalcin 
26596a4d4014SLisandro Dalcin    Input Parameter:
26606a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2661cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
26625a3a76d0SJed Brown 
26636a4d4014SLisandro Dalcin    Level: beginner
26646a4d4014SLisandro Dalcin 
26655a3a76d0SJed Brown    Notes:
26665a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
26675a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
26685a3a76d0SJed Brown    stepped over the final time.
26695a3a76d0SJed Brown 
26706a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
26716a4d4014SLisandro Dalcin 
26726a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
26736a4d4014SLisandro Dalcin @*/
2674cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
26756a4d4014SLisandro Dalcin {
26764d7d938eSLisandro Dalcin   PetscBool         flg;
26774d7d938eSLisandro Dalcin   PetscViewer       viewer;
2678b06615a5SLisandro Dalcin   Vec               solution;
26796a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2680cffb1e40SBarry Smith   PetscViewerFormat format;
2681f22f69f0SBarry Smith 
26826a4d4014SLisandro Dalcin   PetscFunctionBegin;
26830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2684f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
268549354f04SShri 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 */
2686b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
26870910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
2688b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
2689b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
2690b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
26915a3a76d0SJed Brown     }
26920910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2693bbd56ea5SKarl Rupp   } else if (u) {
26940910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
26955a3a76d0SJed Brown   }
2696b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
26976a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2698193ac0bcSJed Brown   ts->steps             = 0;
26995ef26d82SJed Brown   ts->ksp_its           = 0;
27005ef26d82SJed Brown   ts->snes_its          = 0;
2701c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2702c610991cSLisandro Dalcin   ts->reject            = 0;
2703193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2704193ac0bcSJed Brown 
2705f05ece33SBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view_pre",&viewer,&format,&flg);CHKERRQ(ierr);
2706f05ece33SBarry Smith   if (flg && !PetscPreLoadingOn) {
2707f05ece33SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
2708f05ece33SBarry Smith     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2709f05ece33SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2710f05ece33SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2711f05ece33SBarry Smith   }
2712f05ece33SBarry Smith 
2713193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2714193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
27150910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2716cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2717193ac0bcSJed Brown   } else {
27186a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2719db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2720db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2721e1a7a14fSJed Brown     while (!ts->reason) {
2722b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2723193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2724193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2725193ac0bcSJed Brown     }
272649354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
27270910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2728cc708dedSBarry Smith       ts->solvetime = ts->max_time;
2729b06615a5SLisandro Dalcin       solution = u;
27300574a7fbSJed Brown     } else {
2731ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2732cc708dedSBarry Smith       ts->solvetime = ts->ptime;
2733b06615a5SLisandro Dalcin       solution = ts->vec_sol;
27340574a7fbSJed Brown     }
2735b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
2736193ac0bcSJed Brown   }
2737ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
27384d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2739cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
27404d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2741cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2742cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
27432b0a91c0SBarry Smith   }
27446a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
27456a4d4014SLisandro Dalcin }
27466a4d4014SLisandro Dalcin 
27476a4d4014SLisandro Dalcin #undef __FUNCT__
27484a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2749228d79bcSJed Brown /*@
2750228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2751228d79bcSJed Brown 
2752228d79bcSJed Brown    Collective on TS
2753228d79bcSJed Brown 
2754228d79bcSJed Brown    Input Parameters:
2755228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2756228d79bcSJed Brown .  step - step number that has just completed
2757228d79bcSJed Brown .  ptime - model time of the state
27580910c330SBarry Smith -  u - state at the current model time
2759228d79bcSJed Brown 
2760228d79bcSJed Brown    Notes:
2761228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2762228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2763228d79bcSJed Brown 
2764228d79bcSJed Brown    Level: advanced
2765228d79bcSJed Brown 
2766228d79bcSJed Brown .keywords: TS, timestep
2767228d79bcSJed Brown @*/
27680910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2769d763cef2SBarry Smith {
27706849ba73SBarry Smith   PetscErrorCode ierr;
2771a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2772d763cef2SBarry Smith 
2773d763cef2SBarry Smith   PetscFunctionBegin;
2774b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2775b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
2776d763cef2SBarry Smith   for (i=0; i<n; i++) {
27770910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2778d763cef2SBarry Smith   }
2779d763cef2SBarry Smith   PetscFunctionReturn(0);
2780d763cef2SBarry Smith }
2781d763cef2SBarry Smith 
2782d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
27834a2ae208SSatish Balay #undef __FUNCT__
2784a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2785d763cef2SBarry Smith /*@C
2786a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2787a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2788d763cef2SBarry Smith 
2789d763cef2SBarry Smith    Collective on TS
2790d763cef2SBarry Smith 
2791d763cef2SBarry Smith    Input Parameters:
2792d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2793d763cef2SBarry Smith .  label - the title to put in the title bar
27947c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2795a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2796a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2797d763cef2SBarry Smith 
2798d763cef2SBarry Smith    Output Parameter:
27990b039ecaSBarry Smith .  ctx - the context
2800d763cef2SBarry Smith 
2801d763cef2SBarry Smith    Options Database Key:
28024f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
28034f09c107SBarry Smith .  -ts_monitor_lg_solution -
280499fdda47SBarry Smith .  -ts_monitor_lg_error -
280599fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
280699fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
280799fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith    Notes:
2810a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2811d763cef2SBarry Smith 
2812d763cef2SBarry Smith    Level: intermediate
2813d763cef2SBarry Smith 
28147c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2815d763cef2SBarry Smith 
28164f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
28177c922b88SBarry Smith 
2818d763cef2SBarry Smith @*/
2819a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2820d763cef2SBarry Smith {
2821b0a32e0cSBarry Smith   PetscDraw      win;
2822dfbe8321SBarry Smith   PetscErrorCode ierr;
282399fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2824d763cef2SBarry Smith 
2825d763cef2SBarry Smith   PetscFunctionBegin;
2826201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2827a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
28283fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
28290b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
28300298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
283199fdda47SBarry Smith   if (flg) {
28320b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
283399fdda47SBarry Smith   }
28343bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
2835a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2836d763cef2SBarry Smith   PetscFunctionReturn(0);
2837d763cef2SBarry Smith }
2838d763cef2SBarry Smith 
28394a2ae208SSatish Balay #undef __FUNCT__
28404f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
2841b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
2842d763cef2SBarry Smith {
28430b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2844c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2845dfbe8321SBarry Smith   PetscErrorCode ierr;
2846d763cef2SBarry Smith 
2847d763cef2SBarry Smith   PetscFunctionBegin;
2848b06615a5SLisandro Dalcin   if (!step) {
2849a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2850a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2851a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2852a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2853a9f9c1f6SBarry Smith   }
2854c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
28550b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
2856b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
28570b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
28583923b477SBarry Smith   }
2859d763cef2SBarry Smith   PetscFunctionReturn(0);
2860d763cef2SBarry Smith }
2861d763cef2SBarry Smith 
28624a2ae208SSatish Balay #undef __FUNCT__
2863a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2864d763cef2SBarry Smith /*@C
2865a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2866a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2867d763cef2SBarry Smith 
28680b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2869d763cef2SBarry Smith 
2870d763cef2SBarry Smith    Input Parameter:
28710b039ecaSBarry Smith .  ctx - the monitor context
2872d763cef2SBarry Smith 
2873d763cef2SBarry Smith    Level: intermediate
2874d763cef2SBarry Smith 
2875d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2876d763cef2SBarry Smith 
28774f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2878d763cef2SBarry Smith @*/
2879a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2880d763cef2SBarry Smith {
2881b0a32e0cSBarry Smith   PetscDraw      draw;
2882dfbe8321SBarry Smith   PetscErrorCode ierr;
2883d763cef2SBarry Smith 
2884d763cef2SBarry Smith   PetscFunctionBegin;
28850b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
28866bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
28870b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
28880b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2889d763cef2SBarry Smith   PetscFunctionReturn(0);
2890d763cef2SBarry Smith }
2891d763cef2SBarry Smith 
28924a2ae208SSatish Balay #undef __FUNCT__
28934a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2894d763cef2SBarry Smith /*@
2895b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2896d763cef2SBarry Smith 
2897d763cef2SBarry Smith    Not Collective
2898d763cef2SBarry Smith 
2899d763cef2SBarry Smith    Input Parameter:
2900d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2901d763cef2SBarry Smith 
2902d763cef2SBarry Smith    Output Parameter:
2903d763cef2SBarry Smith .  t  - the current time
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith    Level: beginner
2906d763cef2SBarry Smith 
2907b8123daeSJed Brown    Note:
2908b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
29099be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2910b8123daeSJed Brown 
2911d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2912d763cef2SBarry Smith 
2913d763cef2SBarry Smith .keywords: TS, get, time
2914d763cef2SBarry Smith @*/
29157087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2916d763cef2SBarry Smith {
2917d763cef2SBarry Smith   PetscFunctionBegin;
29180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2919f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2920d763cef2SBarry Smith   *t = ts->ptime;
2921d763cef2SBarry Smith   PetscFunctionReturn(0);
2922d763cef2SBarry Smith }
2923d763cef2SBarry Smith 
29244a2ae208SSatish Balay #undef __FUNCT__
29256a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
29266a4d4014SLisandro Dalcin /*@
29276a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
29286a4d4014SLisandro Dalcin 
29293f9fe445SBarry Smith    Logically Collective on TS
29306a4d4014SLisandro Dalcin 
29316a4d4014SLisandro Dalcin    Input Parameters:
29326a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
29336a4d4014SLisandro Dalcin -  time - the time
29346a4d4014SLisandro Dalcin 
29356a4d4014SLisandro Dalcin    Level: intermediate
29366a4d4014SLisandro Dalcin 
29376a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
29386a4d4014SLisandro Dalcin 
29396a4d4014SLisandro Dalcin .keywords: TS, set, time
29406a4d4014SLisandro Dalcin @*/
29417087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
29426a4d4014SLisandro Dalcin {
29436a4d4014SLisandro Dalcin   PetscFunctionBegin;
29440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2945c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
29466a4d4014SLisandro Dalcin   ts->ptime = t;
29476a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
29486a4d4014SLisandro Dalcin }
29496a4d4014SLisandro Dalcin 
29506a4d4014SLisandro Dalcin #undef __FUNCT__
29514a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2952d763cef2SBarry Smith /*@C
2953d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2954d763cef2SBarry Smith    TS options in the database.
2955d763cef2SBarry Smith 
29563f9fe445SBarry Smith    Logically Collective on TS
2957d763cef2SBarry Smith 
2958d763cef2SBarry Smith    Input Parameter:
2959d763cef2SBarry Smith +  ts     - The TS context
2960d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2961d763cef2SBarry Smith 
2962d763cef2SBarry Smith    Notes:
2963d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2964d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2965d763cef2SBarry Smith    hyphen.
2966d763cef2SBarry Smith 
2967d763cef2SBarry Smith    Level: advanced
2968d763cef2SBarry Smith 
2969d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2970d763cef2SBarry Smith 
2971d763cef2SBarry Smith .seealso: TSSetFromOptions()
2972d763cef2SBarry Smith 
2973d763cef2SBarry Smith @*/
29747087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2975d763cef2SBarry Smith {
2976dfbe8321SBarry Smith   PetscErrorCode ierr;
2977089b2837SJed Brown   SNES           snes;
2978d763cef2SBarry Smith 
2979d763cef2SBarry Smith   PetscFunctionBegin;
29800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2981d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2982089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2983089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2984d763cef2SBarry Smith   PetscFunctionReturn(0);
2985d763cef2SBarry Smith }
2986d763cef2SBarry Smith 
2987d763cef2SBarry Smith 
29884a2ae208SSatish Balay #undef __FUNCT__
29894a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2990d763cef2SBarry Smith /*@C
2991d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2992d763cef2SBarry Smith    TS options in the database.
2993d763cef2SBarry Smith 
29943f9fe445SBarry Smith    Logically Collective on TS
2995d763cef2SBarry Smith 
2996d763cef2SBarry Smith    Input Parameter:
2997d763cef2SBarry Smith +  ts     - The TS context
2998d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2999d763cef2SBarry Smith 
3000d763cef2SBarry Smith    Notes:
3001d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3002d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3003d763cef2SBarry Smith    hyphen.
3004d763cef2SBarry Smith 
3005d763cef2SBarry Smith    Level: advanced
3006d763cef2SBarry Smith 
3007d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3008d763cef2SBarry Smith 
3009d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3010d763cef2SBarry Smith 
3011d763cef2SBarry Smith @*/
30127087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3013d763cef2SBarry Smith {
3014dfbe8321SBarry Smith   PetscErrorCode ierr;
3015089b2837SJed Brown   SNES           snes;
3016d763cef2SBarry Smith 
3017d763cef2SBarry Smith   PetscFunctionBegin;
30180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3019d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3020089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3021089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3022d763cef2SBarry Smith   PetscFunctionReturn(0);
3023d763cef2SBarry Smith }
3024d763cef2SBarry Smith 
30254a2ae208SSatish Balay #undef __FUNCT__
30264a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3027d763cef2SBarry Smith /*@C
3028d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3029d763cef2SBarry Smith    TS options in the database.
3030d763cef2SBarry Smith 
3031d763cef2SBarry Smith    Not Collective
3032d763cef2SBarry Smith 
3033d763cef2SBarry Smith    Input Parameter:
3034d763cef2SBarry Smith .  ts - The TS context
3035d763cef2SBarry Smith 
3036d763cef2SBarry Smith    Output Parameter:
3037d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3038d763cef2SBarry Smith 
3039d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3040d763cef2SBarry Smith    sufficient length to hold the prefix.
3041d763cef2SBarry Smith 
3042d763cef2SBarry Smith    Level: intermediate
3043d763cef2SBarry Smith 
3044d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3045d763cef2SBarry Smith 
3046d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3047d763cef2SBarry Smith @*/
30487087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3049d763cef2SBarry Smith {
3050dfbe8321SBarry Smith   PetscErrorCode ierr;
3051d763cef2SBarry Smith 
3052d763cef2SBarry Smith   PetscFunctionBegin;
30530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30544482741eSBarry Smith   PetscValidPointer(prefix,2);
3055d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3056d763cef2SBarry Smith   PetscFunctionReturn(0);
3057d763cef2SBarry Smith }
3058d763cef2SBarry Smith 
30594a2ae208SSatish Balay #undef __FUNCT__
30604a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3061d763cef2SBarry Smith /*@C
3062d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3063d763cef2SBarry Smith 
3064d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3065d763cef2SBarry Smith 
3066d763cef2SBarry Smith    Input Parameter:
3067d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3068d763cef2SBarry Smith 
3069d763cef2SBarry Smith    Output Parameters:
3070e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3071e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3072e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3073e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3074d763cef2SBarry Smith 
30750298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3076d763cef2SBarry Smith 
3077d763cef2SBarry Smith    Level: intermediate
3078d763cef2SBarry Smith 
307926d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3080d763cef2SBarry Smith 
3081d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3082d763cef2SBarry Smith @*/
3083e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3084d763cef2SBarry Smith {
3085089b2837SJed Brown   PetscErrorCode ierr;
3086089b2837SJed Brown   SNES           snes;
308724989b8cSPeter Brune   DM             dm;
3088089b2837SJed Brown 
3089d763cef2SBarry Smith   PetscFunctionBegin;
3090089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3091e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
309224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
309324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3094d763cef2SBarry Smith   PetscFunctionReturn(0);
3095d763cef2SBarry Smith }
3096d763cef2SBarry Smith 
30971713a123SBarry Smith #undef __FUNCT__
30982eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
30992eca1d9cSJed Brown /*@C
31002eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
31012eca1d9cSJed Brown 
31022eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
31032eca1d9cSJed Brown 
31042eca1d9cSJed Brown    Input Parameter:
31052eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
31062eca1d9cSJed Brown 
31072eca1d9cSJed Brown    Output Parameters:
3108e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3109e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
31102eca1d9cSJed Brown .  f   - The function to compute the matrices
31112eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
31122eca1d9cSJed Brown 
31130298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
31142eca1d9cSJed Brown 
31152eca1d9cSJed Brown    Level: advanced
31162eca1d9cSJed Brown 
31172eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
31182eca1d9cSJed Brown 
31192eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
31202eca1d9cSJed Brown @*/
3121e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
31222eca1d9cSJed Brown {
3123089b2837SJed Brown   PetscErrorCode ierr;
3124089b2837SJed Brown   SNES           snes;
312524989b8cSPeter Brune   DM             dm;
31260910c330SBarry Smith 
31272eca1d9cSJed Brown   PetscFunctionBegin;
3128089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3129f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3130e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
313124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
313224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
31332eca1d9cSJed Brown   PetscFunctionReturn(0);
31342eca1d9cSJed Brown }
31352eca1d9cSJed Brown 
31366083293cSBarry Smith 
31372eca1d9cSJed Brown #undef __FUNCT__
313883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
31391713a123SBarry Smith /*@C
314083a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
31411713a123SBarry Smith    VecView() for the solution at each timestep
31421713a123SBarry Smith 
31431713a123SBarry Smith    Collective on TS
31441713a123SBarry Smith 
31451713a123SBarry Smith    Input Parameters:
31461713a123SBarry Smith +  ts - the TS context
31471713a123SBarry Smith .  step - current time-step
3148142b95e3SSatish Balay .  ptime - current time
31490298fd71SBarry Smith -  dummy - either a viewer or NULL
31501713a123SBarry Smith 
315199fdda47SBarry Smith    Options Database:
315299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
315399fdda47SBarry Smith 
315499fdda47SBarry Smith    Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
315599fdda47SBarry Smith        will look bad
315699fdda47SBarry Smith 
31571713a123SBarry Smith    Level: intermediate
31581713a123SBarry Smith 
31591713a123SBarry Smith .keywords: TS,  vector, monitor, view
31601713a123SBarry Smith 
3161a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31621713a123SBarry Smith @*/
31630910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31641713a123SBarry Smith {
3165dfbe8321SBarry Smith   PetscErrorCode   ierr;
316683a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3167473a3ab2SBarry Smith   PetscDraw        draw;
31681713a123SBarry Smith 
31691713a123SBarry Smith   PetscFunctionBegin;
31706083293cSBarry Smith   if (!step && ictx->showinitial) {
31716083293cSBarry Smith     if (!ictx->initialsolution) {
31720910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
31731713a123SBarry Smith     }
31740910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
31756083293cSBarry Smith   }
3176b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
31770dcf80beSBarry Smith 
31786083293cSBarry Smith   if (ictx->showinitial) {
31796083293cSBarry Smith     PetscReal pause;
31806083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
31816083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
31826083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
31836083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
31846083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
31856083293cSBarry Smith   }
31860910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3187473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3188473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3189473a3ab2SBarry Smith     char      time[32];
3190473a3ab2SBarry Smith     size_t    len;
3191473a3ab2SBarry Smith 
3192473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3193473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3194473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3195473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
31960298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3197473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3198473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3199473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3200473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3201473a3ab2SBarry Smith   }
3202473a3ab2SBarry Smith 
32036083293cSBarry Smith   if (ictx->showinitial) {
32046083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
32056083293cSBarry Smith   }
32061713a123SBarry Smith   PetscFunctionReturn(0);
32071713a123SBarry Smith }
32081713a123SBarry Smith 
32092d5ee99bSBarry Smith #undef __FUNCT__
32102d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
32112d5ee99bSBarry Smith /*@C
32122d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
32132d5ee99bSBarry Smith 
32142d5ee99bSBarry Smith    Collective on TS
32152d5ee99bSBarry Smith 
32162d5ee99bSBarry Smith    Input Parameters:
32172d5ee99bSBarry Smith +  ts - the TS context
32182d5ee99bSBarry Smith .  step - current time-step
32192d5ee99bSBarry Smith .  ptime - current time
32202d5ee99bSBarry Smith -  dummy - either a viewer or NULL
32212d5ee99bSBarry Smith 
32222d5ee99bSBarry Smith    Level: intermediate
32232d5ee99bSBarry Smith 
32242d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
32252d5ee99bSBarry Smith 
32262d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32272d5ee99bSBarry Smith @*/
32282d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
32292d5ee99bSBarry Smith {
32302d5ee99bSBarry Smith   PetscErrorCode    ierr;
32312d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
32322d5ee99bSBarry Smith   PetscDraw         draw;
32332d5ee99bSBarry Smith   MPI_Comm          comm;
32342d5ee99bSBarry Smith   PetscInt          n;
32352d5ee99bSBarry Smith   PetscMPIInt       size;
32362d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
32372d5ee99bSBarry Smith   char              time[32];
32382d5ee99bSBarry Smith   size_t            len;
32392d5ee99bSBarry Smith   const PetscScalar *U;
32402d5ee99bSBarry Smith 
32412d5ee99bSBarry Smith   PetscFunctionBegin;
32422d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
32432d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
32442d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
32452d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
32462d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
32472d5ee99bSBarry Smith 
32482d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
32492d5ee99bSBarry Smith 
32502d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
32514b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3252ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3253a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3254a4494fc1SBarry Smith       PetscFunctionReturn(0);
3255a4494fc1SBarry Smith   }
32562d5ee99bSBarry Smith   if (!step) ictx->color++;
32572d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
32582d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
32592d5ee99bSBarry Smith 
32602d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
32614b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
32622d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
32632d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
32642d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
32652d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
32662d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
32672d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
32682d5ee99bSBarry Smith   }
32692d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
32702d5ee99bSBarry Smith   PetscFunctionReturn(0);
32712d5ee99bSBarry Smith }
32722d5ee99bSBarry Smith 
32731713a123SBarry Smith 
32746c699258SBarry Smith #undef __FUNCT__
327583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
32766083293cSBarry Smith /*@C
327783a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
32786083293cSBarry Smith 
32796083293cSBarry Smith    Collective on TS
32806083293cSBarry Smith 
32816083293cSBarry Smith    Input Parameters:
32826083293cSBarry Smith .    ctx - the monitor context
32836083293cSBarry Smith 
32846083293cSBarry Smith    Level: intermediate
32856083293cSBarry Smith 
32866083293cSBarry Smith .keywords: TS,  vector, monitor, view
32876083293cSBarry Smith 
328883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
32896083293cSBarry Smith @*/
329083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
32916083293cSBarry Smith {
32926083293cSBarry Smith   PetscErrorCode ierr;
32936083293cSBarry Smith 
32946083293cSBarry Smith   PetscFunctionBegin;
32954b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
329683a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
329783a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
329883a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
32996083293cSBarry Smith   PetscFunctionReturn(0);
33006083293cSBarry Smith }
33016083293cSBarry Smith 
33026083293cSBarry Smith #undef __FUNCT__
330383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
33046083293cSBarry Smith /*@C
330583a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
33066083293cSBarry Smith 
33076083293cSBarry Smith    Collective on TS
33086083293cSBarry Smith 
33096083293cSBarry Smith    Input Parameter:
33106083293cSBarry Smith .    ts - time-step context
33116083293cSBarry Smith 
33126083293cSBarry Smith    Output Patameter:
33136083293cSBarry Smith .    ctx - the monitor context
33146083293cSBarry Smith 
331599fdda47SBarry Smith    Options Database:
331699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
331799fdda47SBarry Smith 
33186083293cSBarry Smith    Level: intermediate
33196083293cSBarry Smith 
33206083293cSBarry Smith .keywords: TS,  vector, monitor, view
33216083293cSBarry Smith 
332283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
33236083293cSBarry Smith @*/
332483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
33256083293cSBarry Smith {
33266083293cSBarry Smith   PetscErrorCode   ierr;
33276083293cSBarry Smith 
33286083293cSBarry Smith   PetscFunctionBegin;
332983a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
333083a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3331e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3332bbd56ea5SKarl Rupp 
333383a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3334473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
33350298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3336473a3ab2SBarry Smith 
3337473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
33380298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
33392d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
33406083293cSBarry Smith   PetscFunctionReturn(0);
33416083293cSBarry Smith }
33426083293cSBarry Smith 
33436083293cSBarry Smith #undef __FUNCT__
334483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
33453a471f94SBarry Smith /*@C
334683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
33473a471f94SBarry Smith    VecView() for the error at each timestep
33483a471f94SBarry Smith 
33493a471f94SBarry Smith    Collective on TS
33503a471f94SBarry Smith 
33513a471f94SBarry Smith    Input Parameters:
33523a471f94SBarry Smith +  ts - the TS context
33533a471f94SBarry Smith .  step - current time-step
33543a471f94SBarry Smith .  ptime - current time
33550298fd71SBarry Smith -  dummy - either a viewer or NULL
33563a471f94SBarry Smith 
33573a471f94SBarry Smith    Level: intermediate
33583a471f94SBarry Smith 
33593a471f94SBarry Smith .keywords: TS,  vector, monitor, view
33603a471f94SBarry Smith 
33613a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
33623a471f94SBarry Smith @*/
33630910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
33643a471f94SBarry Smith {
33653a471f94SBarry Smith   PetscErrorCode   ierr;
336683a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
336783a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
33683a471f94SBarry Smith   Vec              work;
33693a471f94SBarry Smith 
33703a471f94SBarry Smith   PetscFunctionBegin;
3371b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
33720910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
33733a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
33740910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
33753a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
33763a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
33773a471f94SBarry Smith   PetscFunctionReturn(0);
33783a471f94SBarry Smith }
33793a471f94SBarry Smith 
33802a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
33813a471f94SBarry Smith #undef __FUNCT__
33826c699258SBarry Smith #define __FUNCT__ "TSSetDM"
33836c699258SBarry Smith /*@
33846c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
33856c699258SBarry Smith 
33863f9fe445SBarry Smith    Logically Collective on TS and DM
33876c699258SBarry Smith 
33886c699258SBarry Smith    Input Parameters:
33896c699258SBarry Smith +  ts - the preconditioner context
33906c699258SBarry Smith -  dm - the dm
33916c699258SBarry Smith 
33926c699258SBarry Smith    Level: intermediate
33936c699258SBarry Smith 
33946c699258SBarry Smith 
33956c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
33966c699258SBarry Smith @*/
33977087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
33986c699258SBarry Smith {
33996c699258SBarry Smith   PetscErrorCode ierr;
3400089b2837SJed Brown   SNES           snes;
3401942e3340SBarry Smith   DMTS           tsdm;
34026c699258SBarry Smith 
34036c699258SBarry Smith   PetscFunctionBegin;
34040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
340570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3406942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
34072a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3408942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3409942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
341024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
341124989b8cSPeter Brune         tsdm->originaldm = dm;
341224989b8cSPeter Brune       }
341324989b8cSPeter Brune     }
34146bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
341524989b8cSPeter Brune   }
34166c699258SBarry Smith   ts->dm = dm;
3417bbd56ea5SKarl Rupp 
3418089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3419089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
34206c699258SBarry Smith   PetscFunctionReturn(0);
34216c699258SBarry Smith }
34226c699258SBarry Smith 
34236c699258SBarry Smith #undef __FUNCT__
34246c699258SBarry Smith #define __FUNCT__ "TSGetDM"
34256c699258SBarry Smith /*@
34266c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
34276c699258SBarry Smith 
34283f9fe445SBarry Smith    Not Collective
34296c699258SBarry Smith 
34306c699258SBarry Smith    Input Parameter:
34316c699258SBarry Smith . ts - the preconditioner context
34326c699258SBarry Smith 
34336c699258SBarry Smith    Output Parameter:
34346c699258SBarry Smith .  dm - the dm
34356c699258SBarry Smith 
34366c699258SBarry Smith    Level: intermediate
34376c699258SBarry Smith 
34386c699258SBarry Smith 
34396c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
34406c699258SBarry Smith @*/
34417087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
34426c699258SBarry Smith {
3443496e6a7aSJed Brown   PetscErrorCode ierr;
3444496e6a7aSJed Brown 
34456c699258SBarry Smith   PetscFunctionBegin;
34460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3447496e6a7aSJed Brown   if (!ts->dm) {
3448ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3449496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3450496e6a7aSJed Brown   }
34516c699258SBarry Smith   *dm = ts->dm;
34526c699258SBarry Smith   PetscFunctionReturn(0);
34536c699258SBarry Smith }
34541713a123SBarry Smith 
34550f5c6efeSJed Brown #undef __FUNCT__
34560f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
34570f5c6efeSJed Brown /*@
34580f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
34590f5c6efeSJed Brown 
34603f9fe445SBarry Smith    Logically Collective on SNES
34610f5c6efeSJed Brown 
34620f5c6efeSJed Brown    Input Parameter:
3463d42a1c89SJed Brown + snes - nonlinear solver
34640910c330SBarry Smith . U - the current state at which to evaluate the residual
3465d42a1c89SJed Brown - ctx - user context, must be a TS
34660f5c6efeSJed Brown 
34670f5c6efeSJed Brown    Output Parameter:
34680f5c6efeSJed Brown . F - the nonlinear residual
34690f5c6efeSJed Brown 
34700f5c6efeSJed Brown    Notes:
34710f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
34720f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
34730f5c6efeSJed Brown 
34740f5c6efeSJed Brown    Level: advanced
34750f5c6efeSJed Brown 
34760f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
34770f5c6efeSJed Brown @*/
34780910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
34790f5c6efeSJed Brown {
34800f5c6efeSJed Brown   TS             ts = (TS)ctx;
34810f5c6efeSJed Brown   PetscErrorCode ierr;
34820f5c6efeSJed Brown 
34830f5c6efeSJed Brown   PetscFunctionBegin;
34840f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34850910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34860f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
34870f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
34880910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
34890f5c6efeSJed Brown   PetscFunctionReturn(0);
34900f5c6efeSJed Brown }
34910f5c6efeSJed Brown 
34920f5c6efeSJed Brown #undef __FUNCT__
34930f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
34940f5c6efeSJed Brown /*@
34950f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
34960f5c6efeSJed Brown 
34970f5c6efeSJed Brown    Collective on SNES
34980f5c6efeSJed Brown 
34990f5c6efeSJed Brown    Input Parameter:
35000f5c6efeSJed Brown + snes - nonlinear solver
35010910c330SBarry Smith . U - the current state at which to evaluate the residual
35020f5c6efeSJed Brown - ctx - user context, must be a TS
35030f5c6efeSJed Brown 
35040f5c6efeSJed Brown    Output Parameter:
35050f5c6efeSJed Brown + A - the Jacobian
35060f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
35070f5c6efeSJed Brown - flag - indicates any structure change in the matrix
35080f5c6efeSJed Brown 
35090f5c6efeSJed Brown    Notes:
35100f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
35110f5c6efeSJed Brown 
35120f5c6efeSJed Brown    Level: developer
35130f5c6efeSJed Brown 
35140f5c6efeSJed Brown .seealso: SNESSetJacobian()
35150f5c6efeSJed Brown @*/
35160910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
35170f5c6efeSJed Brown {
35180f5c6efeSJed Brown   TS             ts = (TS)ctx;
35190f5c6efeSJed Brown   PetscErrorCode ierr;
35200f5c6efeSJed Brown 
35210f5c6efeSJed Brown   PetscFunctionBegin;
35220f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
35230910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
35240f5c6efeSJed Brown   PetscValidPointer(A,3);
35250f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
35260f5c6efeSJed Brown   PetscValidPointer(B,4);
35270f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
35280f5c6efeSJed Brown   PetscValidPointer(flag,5);
35290f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
35300910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
35310f5c6efeSJed Brown   PetscFunctionReturn(0);
35320f5c6efeSJed Brown }
3533325fc9f4SBarry Smith 
35340e4ef248SJed Brown #undef __FUNCT__
35350e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
35360e4ef248SJed Brown /*@C
35370e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
35380e4ef248SJed Brown 
35390e4ef248SJed Brown    Collective on TS
35400e4ef248SJed Brown 
35410e4ef248SJed Brown    Input Arguments:
35420e4ef248SJed Brown +  ts - time stepping context
35430e4ef248SJed Brown .  t - time at which to evaluate
35440910c330SBarry Smith .  U - state at which to evaluate
35450e4ef248SJed Brown -  ctx - context
35460e4ef248SJed Brown 
35470e4ef248SJed Brown    Output Arguments:
35480e4ef248SJed Brown .  F - right hand side
35490e4ef248SJed Brown 
35500e4ef248SJed Brown    Level: intermediate
35510e4ef248SJed Brown 
35520e4ef248SJed Brown    Notes:
35530e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
35540e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
35550e4ef248SJed Brown 
35560e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
35570e4ef248SJed Brown @*/
35580910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
35590e4ef248SJed Brown {
35600e4ef248SJed Brown   PetscErrorCode ierr;
35610e4ef248SJed Brown   Mat            Arhs,Brhs;
35620e4ef248SJed Brown   MatStructure   flg2;
35630e4ef248SJed Brown 
35640e4ef248SJed Brown   PetscFunctionBegin;
35650e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
35660910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
35670910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
35680e4ef248SJed Brown   PetscFunctionReturn(0);
35690e4ef248SJed Brown }
35700e4ef248SJed Brown 
35710e4ef248SJed Brown #undef __FUNCT__
35720e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
35730e4ef248SJed Brown /*@C
35740e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
35750e4ef248SJed Brown 
35760e4ef248SJed Brown    Collective on TS
35770e4ef248SJed Brown 
35780e4ef248SJed Brown    Input Arguments:
35790e4ef248SJed Brown +  ts - time stepping context
35800e4ef248SJed Brown .  t - time at which to evaluate
35810910c330SBarry Smith .  U - state at which to evaluate
35820e4ef248SJed Brown -  ctx - context
35830e4ef248SJed Brown 
35840e4ef248SJed Brown    Output Arguments:
35850e4ef248SJed Brown +  A - pointer to operator
35860e4ef248SJed Brown .  B - pointer to preconditioning matrix
35870e4ef248SJed Brown -  flg - matrix structure flag
35880e4ef248SJed Brown 
35890e4ef248SJed Brown    Level: intermediate
35900e4ef248SJed Brown 
35910e4ef248SJed Brown    Notes:
35920e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
35930e4ef248SJed Brown 
35940e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
35950e4ef248SJed Brown @*/
35960910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
35970e4ef248SJed Brown {
35980e4ef248SJed Brown   PetscFunctionBegin;
35990e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
36000e4ef248SJed Brown   PetscFunctionReturn(0);
36010e4ef248SJed Brown }
36020e4ef248SJed Brown 
36030026cea9SSean Farley #undef __FUNCT__
36040026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
36050026cea9SSean Farley /*@C
36060026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
36070026cea9SSean Farley 
36080026cea9SSean Farley    Collective on TS
36090026cea9SSean Farley 
36100026cea9SSean Farley    Input Arguments:
36110026cea9SSean Farley +  ts - time stepping context
36120026cea9SSean Farley .  t - time at which to evaluate
36130910c330SBarry Smith .  U - state at which to evaluate
36140910c330SBarry Smith .  Udot - time derivative of state vector
36150026cea9SSean Farley -  ctx - context
36160026cea9SSean Farley 
36170026cea9SSean Farley    Output Arguments:
36180026cea9SSean Farley .  F - left hand side
36190026cea9SSean Farley 
36200026cea9SSean Farley    Level: intermediate
36210026cea9SSean Farley 
36220026cea9SSean Farley    Notes:
36230910c330SBarry 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
36240026cea9SSean Farley    user is required to write their own TSComputeIFunction.
36250026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
36260026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
36270026cea9SSean Farley 
36280026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
36290026cea9SSean Farley @*/
36300910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
36310026cea9SSean Farley {
36320026cea9SSean Farley   PetscErrorCode ierr;
36330026cea9SSean Farley   Mat            A,B;
36340026cea9SSean Farley   MatStructure   flg2;
36350026cea9SSean Farley 
36360026cea9SSean Farley   PetscFunctionBegin;
36370298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
36380910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
36390910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
36400026cea9SSean Farley   PetscFunctionReturn(0);
36410026cea9SSean Farley }
36420026cea9SSean Farley 
36430026cea9SSean Farley #undef __FUNCT__
36440026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
36450026cea9SSean Farley /*@C
3646b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
36470026cea9SSean Farley 
36480026cea9SSean Farley    Collective on TS
36490026cea9SSean Farley 
36500026cea9SSean Farley    Input Arguments:
36510026cea9SSean Farley +  ts - time stepping context
36520026cea9SSean Farley .  t - time at which to evaluate
36530910c330SBarry Smith .  U - state at which to evaluate
36540910c330SBarry Smith .  Udot - time derivative of state vector
36550026cea9SSean Farley .  shift - shift to apply
36560026cea9SSean Farley -  ctx - context
36570026cea9SSean Farley 
36580026cea9SSean Farley    Output Arguments:
36590026cea9SSean Farley +  A - pointer to operator
36600026cea9SSean Farley .  B - pointer to preconditioning matrix
36610026cea9SSean Farley -  flg - matrix structure flag
36620026cea9SSean Farley 
3663b41af12eSJed Brown    Level: advanced
36640026cea9SSean Farley 
36650026cea9SSean Farley    Notes:
36660026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
36670026cea9SSean Farley 
3668b41af12eSJed Brown    It is only appropriate for problems of the form
3669b41af12eSJed Brown 
3670b41af12eSJed Brown $     M Udot = F(U,t)
3671b41af12eSJed Brown 
3672b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
3673b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
3674b41af12eSJed Brown   an implicit operator of the form
3675b41af12eSJed Brown 
3676b41af12eSJed Brown $    shift*M + J
3677b41af12eSJed Brown 
3678b41af12eSJed 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
3679b41af12eSJed Brown   a copy of M or reassemble it when requested.
3680b41af12eSJed Brown 
36810026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
36820026cea9SSean Farley @*/
36830910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
36840026cea9SSean Farley {
3685b41af12eSJed Brown   PetscErrorCode ierr;
3686b41af12eSJed Brown 
36870026cea9SSean Farley   PetscFunctionBegin;
3688b41af12eSJed Brown   ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
3689b41af12eSJed Brown   ts->ijacobian.shift = shift;
36900026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
36910026cea9SSean Farley   PetscFunctionReturn(0);
36920026cea9SSean Farley }
3693b41af12eSJed Brown 
3694e817cc15SEmil Constantinescu #undef __FUNCT__
3695e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3696e817cc15SEmil Constantinescu /*@
3697e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3698e817cc15SEmil Constantinescu 
3699e817cc15SEmil Constantinescu    Not Collective
3700e817cc15SEmil Constantinescu 
3701e817cc15SEmil Constantinescu    Input Parameter:
3702e817cc15SEmil Constantinescu .  ts - the TS context
3703e817cc15SEmil Constantinescu 
3704e817cc15SEmil Constantinescu    Output Parameter:
37054e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3706e817cc15SEmil Constantinescu 
3707e817cc15SEmil Constantinescu    Level: beginner
3708e817cc15SEmil Constantinescu 
3709e817cc15SEmil Constantinescu .keywords: TS, equation type
3710e817cc15SEmil Constantinescu 
3711e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3712e817cc15SEmil Constantinescu @*/
3713e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3714e817cc15SEmil Constantinescu {
3715e817cc15SEmil Constantinescu   PetscFunctionBegin;
3716e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3717e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3718e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3719e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3720e817cc15SEmil Constantinescu }
3721e817cc15SEmil Constantinescu 
3722e817cc15SEmil Constantinescu #undef __FUNCT__
3723e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3724e817cc15SEmil Constantinescu /*@
3725e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3726e817cc15SEmil Constantinescu 
3727e817cc15SEmil Constantinescu    Not Collective
3728e817cc15SEmil Constantinescu 
3729e817cc15SEmil Constantinescu    Input Parameter:
3730e817cc15SEmil Constantinescu +  ts - the TS context
37314e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3732e817cc15SEmil Constantinescu 
3733e817cc15SEmil Constantinescu    Level: advanced
3734e817cc15SEmil Constantinescu 
3735e817cc15SEmil Constantinescu .keywords: TS, equation type
3736e817cc15SEmil Constantinescu 
3737e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3738e817cc15SEmil Constantinescu @*/
3739e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3740e817cc15SEmil Constantinescu {
3741e817cc15SEmil Constantinescu   PetscFunctionBegin;
3742e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3743e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3744e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3745e817cc15SEmil Constantinescu }
37460026cea9SSean Farley 
37474af1b03aSJed Brown #undef __FUNCT__
37484af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
37494af1b03aSJed Brown /*@
37504af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
37514af1b03aSJed Brown 
37524af1b03aSJed Brown    Not Collective
37534af1b03aSJed Brown 
37544af1b03aSJed Brown    Input Parameter:
37554af1b03aSJed Brown .  ts - the TS context
37564af1b03aSJed Brown 
37574af1b03aSJed Brown    Output Parameter:
37584af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
37594af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
37604af1b03aSJed Brown 
3761487e0bb9SJed Brown    Level: beginner
37624af1b03aSJed Brown 
3763cd652676SJed Brown    Notes:
3764cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
37654af1b03aSJed Brown 
37664af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
37674af1b03aSJed Brown 
37684af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
37694af1b03aSJed Brown @*/
37704af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
37714af1b03aSJed Brown {
37724af1b03aSJed Brown   PetscFunctionBegin;
37734af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37744af1b03aSJed Brown   PetscValidPointer(reason,2);
37754af1b03aSJed Brown   *reason = ts->reason;
37764af1b03aSJed Brown   PetscFunctionReturn(0);
37774af1b03aSJed Brown }
37784af1b03aSJed Brown 
3779fb1732b5SBarry Smith #undef __FUNCT__
3780d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3781d6ad946cSShri Abhyankar /*@
3782d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3783d6ad946cSShri Abhyankar 
3784d6ad946cSShri Abhyankar    Not Collective
3785d6ad946cSShri Abhyankar 
3786d6ad946cSShri Abhyankar    Input Parameter:
3787d6ad946cSShri Abhyankar +  ts - the TS context
3788d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3789d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3790d6ad946cSShri Abhyankar 
3791f5abba47SShri Abhyankar    Level: advanced
3792d6ad946cSShri Abhyankar 
3793d6ad946cSShri Abhyankar    Notes:
3794d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3795d6ad946cSShri Abhyankar 
3796d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3797d6ad946cSShri Abhyankar 
3798d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3799d6ad946cSShri Abhyankar @*/
3800d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3801d6ad946cSShri Abhyankar {
3802d6ad946cSShri Abhyankar   PetscFunctionBegin;
3803d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3804d6ad946cSShri Abhyankar   ts->reason = reason;
3805d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3806d6ad946cSShri Abhyankar }
3807d6ad946cSShri Abhyankar 
3808d6ad946cSShri Abhyankar #undef __FUNCT__
3809cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3810cc708dedSBarry Smith /*@
3811cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3812cc708dedSBarry Smith 
3813cc708dedSBarry Smith    Not Collective
3814cc708dedSBarry Smith 
3815cc708dedSBarry Smith    Input Parameter:
3816cc708dedSBarry Smith .  ts - the TS context
3817cc708dedSBarry Smith 
3818cc708dedSBarry Smith    Output Parameter:
3819cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3820cc708dedSBarry Smith 
3821487e0bb9SJed Brown    Level: beginner
3822cc708dedSBarry Smith 
3823cc708dedSBarry Smith    Notes:
3824cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3825cc708dedSBarry Smith 
3826cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3827cc708dedSBarry Smith 
3828cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3829cc708dedSBarry Smith @*/
3830cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3831cc708dedSBarry Smith {
3832cc708dedSBarry Smith   PetscFunctionBegin;
3833cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3834cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3835cc708dedSBarry Smith   *ftime = ts->solvetime;
3836cc708dedSBarry Smith   PetscFunctionReturn(0);
3837cc708dedSBarry Smith }
3838cc708dedSBarry Smith 
3839cc708dedSBarry Smith #undef __FUNCT__
38405ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
38419f67acb7SJed Brown /*@
38425ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
38439f67acb7SJed Brown    used by the time integrator.
38449f67acb7SJed Brown 
38459f67acb7SJed Brown    Not Collective
38469f67acb7SJed Brown 
38479f67acb7SJed Brown    Input Parameter:
38489f67acb7SJed Brown .  ts - TS context
38499f67acb7SJed Brown 
38509f67acb7SJed Brown    Output Parameter:
38519f67acb7SJed Brown .  nits - number of nonlinear iterations
38529f67acb7SJed Brown 
38539f67acb7SJed Brown    Notes:
38549f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
38559f67acb7SJed Brown 
38569f67acb7SJed Brown    Level: intermediate
38579f67acb7SJed Brown 
38589f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
38599f67acb7SJed Brown 
38605ef26d82SJed Brown .seealso:  TSGetKSPIterations()
38619f67acb7SJed Brown @*/
38625ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
38639f67acb7SJed Brown {
38649f67acb7SJed Brown   PetscFunctionBegin;
38659f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38669f67acb7SJed Brown   PetscValidIntPointer(nits,2);
38675ef26d82SJed Brown   *nits = ts->snes_its;
38689f67acb7SJed Brown   PetscFunctionReturn(0);
38699f67acb7SJed Brown }
38709f67acb7SJed Brown 
38719f67acb7SJed Brown #undef __FUNCT__
38725ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
38739f67acb7SJed Brown /*@
38745ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
38759f67acb7SJed Brown    used by the time integrator.
38769f67acb7SJed Brown 
38779f67acb7SJed Brown    Not Collective
38789f67acb7SJed Brown 
38799f67acb7SJed Brown    Input Parameter:
38809f67acb7SJed Brown .  ts - TS context
38819f67acb7SJed Brown 
38829f67acb7SJed Brown    Output Parameter:
38839f67acb7SJed Brown .  lits - number of linear iterations
38849f67acb7SJed Brown 
38859f67acb7SJed Brown    Notes:
38869f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
38879f67acb7SJed Brown 
38889f67acb7SJed Brown    Level: intermediate
38899f67acb7SJed Brown 
38909f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
38919f67acb7SJed Brown 
38925ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
38939f67acb7SJed Brown @*/
38945ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
38959f67acb7SJed Brown {
38969f67acb7SJed Brown   PetscFunctionBegin;
38979f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38989f67acb7SJed Brown   PetscValidIntPointer(lits,2);
38995ef26d82SJed Brown   *lits = ts->ksp_its;
39009f67acb7SJed Brown   PetscFunctionReturn(0);
39019f67acb7SJed Brown }
39029f67acb7SJed Brown 
39039f67acb7SJed Brown #undef __FUNCT__
3904cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3905cef5090cSJed Brown /*@
3906cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3907cef5090cSJed Brown 
3908cef5090cSJed Brown    Not Collective
3909cef5090cSJed Brown 
3910cef5090cSJed Brown    Input Parameter:
3911cef5090cSJed Brown .  ts - TS context
3912cef5090cSJed Brown 
3913cef5090cSJed Brown    Output Parameter:
3914cef5090cSJed Brown .  rejects - number of steps rejected
3915cef5090cSJed Brown 
3916cef5090cSJed Brown    Notes:
3917cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3918cef5090cSJed Brown 
3919cef5090cSJed Brown    Level: intermediate
3920cef5090cSJed Brown 
3921cef5090cSJed Brown .keywords: TS, get, number
3922cef5090cSJed Brown 
39235ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3924cef5090cSJed Brown @*/
3925cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3926cef5090cSJed Brown {
3927cef5090cSJed Brown   PetscFunctionBegin;
3928cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3929cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3930cef5090cSJed Brown   *rejects = ts->reject;
3931cef5090cSJed Brown   PetscFunctionReturn(0);
3932cef5090cSJed Brown }
3933cef5090cSJed Brown 
3934cef5090cSJed Brown #undef __FUNCT__
3935cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3936cef5090cSJed Brown /*@
3937cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3938cef5090cSJed Brown 
3939cef5090cSJed Brown    Not Collective
3940cef5090cSJed Brown 
3941cef5090cSJed Brown    Input Parameter:
3942cef5090cSJed Brown .  ts - TS context
3943cef5090cSJed Brown 
3944cef5090cSJed Brown    Output Parameter:
3945cef5090cSJed Brown .  fails - number of failed nonlinear solves
3946cef5090cSJed Brown 
3947cef5090cSJed Brown    Notes:
3948cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3949cef5090cSJed Brown 
3950cef5090cSJed Brown    Level: intermediate
3951cef5090cSJed Brown 
3952cef5090cSJed Brown .keywords: TS, get, number
3953cef5090cSJed Brown 
39545ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3955cef5090cSJed Brown @*/
3956cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3957cef5090cSJed Brown {
3958cef5090cSJed Brown   PetscFunctionBegin;
3959cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3960cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3961cef5090cSJed Brown   *fails = ts->num_snes_failures;
3962cef5090cSJed Brown   PetscFunctionReturn(0);
3963cef5090cSJed Brown }
3964cef5090cSJed Brown 
3965cef5090cSJed Brown #undef __FUNCT__
3966cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3967cef5090cSJed Brown /*@
3968cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3969cef5090cSJed Brown 
3970cef5090cSJed Brown    Not Collective
3971cef5090cSJed Brown 
3972cef5090cSJed Brown    Input Parameter:
3973cef5090cSJed Brown +  ts - TS context
3974cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3975cef5090cSJed Brown 
3976cef5090cSJed Brown    Notes:
3977cef5090cSJed Brown    The counter is reset to zero for each step
3978cef5090cSJed Brown 
3979cef5090cSJed Brown    Options Database Key:
3980cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3981cef5090cSJed Brown 
3982cef5090cSJed Brown    Level: intermediate
3983cef5090cSJed Brown 
3984cef5090cSJed Brown .keywords: TS, set, maximum, number
3985cef5090cSJed Brown 
39865ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3987cef5090cSJed Brown @*/
3988cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3989cef5090cSJed Brown {
3990cef5090cSJed Brown   PetscFunctionBegin;
3991cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3992cef5090cSJed Brown   ts->max_reject = rejects;
3993cef5090cSJed Brown   PetscFunctionReturn(0);
3994cef5090cSJed Brown }
3995cef5090cSJed Brown 
3996cef5090cSJed Brown #undef __FUNCT__
3997cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3998cef5090cSJed Brown /*@
3999cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4000cef5090cSJed Brown 
4001cef5090cSJed Brown    Not Collective
4002cef5090cSJed Brown 
4003cef5090cSJed Brown    Input Parameter:
4004cef5090cSJed Brown +  ts - TS context
4005cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4006cef5090cSJed Brown 
4007cef5090cSJed Brown    Notes:
4008cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4009cef5090cSJed Brown 
4010cef5090cSJed Brown    Options Database Key:
4011cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4012cef5090cSJed Brown 
4013cef5090cSJed Brown    Level: intermediate
4014cef5090cSJed Brown 
4015cef5090cSJed Brown .keywords: TS, set, maximum, number
4016cef5090cSJed Brown 
40175ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4018cef5090cSJed Brown @*/
4019cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4020cef5090cSJed Brown {
4021cef5090cSJed Brown   PetscFunctionBegin;
4022cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4023cef5090cSJed Brown   ts->max_snes_failures = fails;
4024cef5090cSJed Brown   PetscFunctionReturn(0);
4025cef5090cSJed Brown }
4026cef5090cSJed Brown 
4027cef5090cSJed Brown #undef __FUNCT__
4028cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
4029cef5090cSJed Brown /*@
4030cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4031cef5090cSJed Brown 
4032cef5090cSJed Brown    Not Collective
4033cef5090cSJed Brown 
4034cef5090cSJed Brown    Input Parameter:
4035cef5090cSJed Brown +  ts - TS context
4036cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4037cef5090cSJed Brown 
4038cef5090cSJed Brown    Options Database Key:
4039cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4040cef5090cSJed Brown 
4041cef5090cSJed Brown    Level: intermediate
4042cef5090cSJed Brown 
4043cef5090cSJed Brown .keywords: TS, set, error
4044cef5090cSJed Brown 
40455ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4046cef5090cSJed Brown @*/
4047cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4048cef5090cSJed Brown {
4049cef5090cSJed Brown   PetscFunctionBegin;
4050cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4051cef5090cSJed Brown   ts->errorifstepfailed = err;
4052cef5090cSJed Brown   PetscFunctionReturn(0);
4053cef5090cSJed Brown }
4054cef5090cSJed Brown 
4055cef5090cSJed Brown #undef __FUNCT__
4056fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4057fb1732b5SBarry Smith /*@C
4058fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4059fb1732b5SBarry Smith 
4060fb1732b5SBarry Smith    Collective on TS
4061fb1732b5SBarry Smith 
4062fb1732b5SBarry Smith    Input Parameters:
4063fb1732b5SBarry Smith +  ts - the TS context
4064fb1732b5SBarry Smith .  step - current time-step
4065fb1732b5SBarry Smith .  ptime - current time
40660910c330SBarry Smith .  u - current state
4067fb1732b5SBarry Smith -  viewer - binary viewer
4068fb1732b5SBarry Smith 
4069fb1732b5SBarry Smith    Level: intermediate
4070fb1732b5SBarry Smith 
4071fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4072fb1732b5SBarry Smith 
4073fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4074fb1732b5SBarry Smith @*/
40750910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4076fb1732b5SBarry Smith {
4077fb1732b5SBarry Smith   PetscErrorCode ierr;
4078ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4079fb1732b5SBarry Smith 
4080fb1732b5SBarry Smith   PetscFunctionBegin;
40810910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4082ed81e22dSJed Brown   PetscFunctionReturn(0);
4083ed81e22dSJed Brown }
4084ed81e22dSJed Brown 
4085ed81e22dSJed Brown #undef __FUNCT__
4086ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4087ed81e22dSJed Brown /*@C
4088ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4089ed81e22dSJed Brown 
4090ed81e22dSJed Brown    Collective on TS
4091ed81e22dSJed Brown 
4092ed81e22dSJed Brown    Input Parameters:
4093ed81e22dSJed Brown +  ts - the TS context
4094ed81e22dSJed Brown .  step - current time-step
4095ed81e22dSJed Brown .  ptime - current time
40960910c330SBarry Smith .  u - current state
4097ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4098ed81e22dSJed Brown 
4099ed81e22dSJed Brown    Level: intermediate
4100ed81e22dSJed Brown 
4101ed81e22dSJed Brown    Notes:
4102ed81e22dSJed 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.
4103ed81e22dSJed Brown    These are named according to the file name template.
4104ed81e22dSJed Brown 
4105ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4106ed81e22dSJed Brown 
4107ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4108ed81e22dSJed Brown 
4109ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4110ed81e22dSJed Brown @*/
41110910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4112ed81e22dSJed Brown {
4113ed81e22dSJed Brown   PetscErrorCode ierr;
4114ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4115ed81e22dSJed Brown   PetscViewer    viewer;
4116ed81e22dSJed Brown 
4117ed81e22dSJed Brown   PetscFunctionBegin;
41188caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4119ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
41200910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4121ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4122ed81e22dSJed Brown   PetscFunctionReturn(0);
4123ed81e22dSJed Brown }
4124ed81e22dSJed Brown 
4125ed81e22dSJed Brown #undef __FUNCT__
4126ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4127ed81e22dSJed Brown /*@C
4128ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4129ed81e22dSJed Brown 
4130ed81e22dSJed Brown    Collective on TS
4131ed81e22dSJed Brown 
4132ed81e22dSJed Brown    Input Parameters:
4133ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4134ed81e22dSJed Brown 
4135ed81e22dSJed Brown    Level: intermediate
4136ed81e22dSJed Brown 
4137ed81e22dSJed Brown    Note:
4138ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4139ed81e22dSJed Brown 
4140ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4141ed81e22dSJed Brown 
4142ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4143ed81e22dSJed Brown @*/
4144ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4145ed81e22dSJed Brown {
4146ed81e22dSJed Brown   PetscErrorCode ierr;
4147ed81e22dSJed Brown 
4148ed81e22dSJed Brown   PetscFunctionBegin;
4149ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4150fb1732b5SBarry Smith   PetscFunctionReturn(0);
4151fb1732b5SBarry Smith }
4152fb1732b5SBarry Smith 
415384df9cb4SJed Brown #undef __FUNCT__
4154552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
415584df9cb4SJed Brown /*@
4156552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
415784df9cb4SJed Brown 
4158ed81e22dSJed Brown    Collective on TS if controller has not been created yet
415984df9cb4SJed Brown 
416084df9cb4SJed Brown    Input Arguments:
4161ed81e22dSJed Brown .  ts - time stepping context
416284df9cb4SJed Brown 
416384df9cb4SJed Brown    Output Arguments:
4164ed81e22dSJed Brown .  adapt - adaptive controller
416584df9cb4SJed Brown 
416684df9cb4SJed Brown    Level: intermediate
416784df9cb4SJed Brown 
4168ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
416984df9cb4SJed Brown @*/
4170552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
417184df9cb4SJed Brown {
417284df9cb4SJed Brown   PetscErrorCode ierr;
417384df9cb4SJed Brown 
417484df9cb4SJed Brown   PetscFunctionBegin;
417584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
417684df9cb4SJed Brown   PetscValidPointer(adapt,2);
417784df9cb4SJed Brown   if (!ts->adapt) {
4178ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
41793bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
41801c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
418184df9cb4SJed Brown   }
418284df9cb4SJed Brown   *adapt = ts->adapt;
418384df9cb4SJed Brown   PetscFunctionReturn(0);
418484df9cb4SJed Brown }
4185d6ebe24aSShri Abhyankar 
4186d6ebe24aSShri Abhyankar #undef __FUNCT__
41871c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
41881c3436cfSJed Brown /*@
41891c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
41901c3436cfSJed Brown 
41911c3436cfSJed Brown    Logically Collective
41921c3436cfSJed Brown 
41931c3436cfSJed Brown    Input Arguments:
41941c3436cfSJed Brown +  ts - time integration context
41951c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
41960298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
41971c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
41980298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
41991c3436cfSJed Brown 
42001c3436cfSJed Brown    Level: beginner
42011c3436cfSJed Brown 
4202c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
42031c3436cfSJed Brown @*/
42041c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
42051c3436cfSJed Brown {
42061c3436cfSJed Brown   PetscErrorCode ierr;
42071c3436cfSJed Brown 
42081c3436cfSJed Brown   PetscFunctionBegin;
4209c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
42101c3436cfSJed Brown   if (vatol) {
42111c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
42121c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4213bbd56ea5SKarl Rupp 
42141c3436cfSJed Brown     ts->vatol = vatol;
42151c3436cfSJed Brown   }
4216c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
42171c3436cfSJed Brown   if (vrtol) {
42181c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
42191c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4220bbd56ea5SKarl Rupp 
42211c3436cfSJed Brown     ts->vrtol = vrtol;
42221c3436cfSJed Brown   }
42231c3436cfSJed Brown   PetscFunctionReturn(0);
42241c3436cfSJed Brown }
42251c3436cfSJed Brown 
42261c3436cfSJed Brown #undef __FUNCT__
4227c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4228c5033834SJed Brown /*@
4229c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4230c5033834SJed Brown 
4231c5033834SJed Brown    Logically Collective
4232c5033834SJed Brown 
4233c5033834SJed Brown    Input Arguments:
4234c5033834SJed Brown .  ts - time integration context
4235c5033834SJed Brown 
4236c5033834SJed Brown    Output Arguments:
42370298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
42380298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
42390298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
42400298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4241c5033834SJed Brown 
4242c5033834SJed Brown    Level: beginner
4243c5033834SJed Brown 
4244c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4245c5033834SJed Brown @*/
4246c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4247c5033834SJed Brown {
4248c5033834SJed Brown   PetscFunctionBegin;
4249c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4250c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4251c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4252c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4253c5033834SJed Brown   PetscFunctionReturn(0);
4254c5033834SJed Brown }
4255c5033834SJed Brown 
4256c5033834SJed Brown #undef __FUNCT__
42571c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
42581c3436cfSJed Brown /*@
42591c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
42601c3436cfSJed Brown 
42611c3436cfSJed Brown    Collective on TS
42621c3436cfSJed Brown 
42631c3436cfSJed Brown    Input Arguments:
42641c3436cfSJed Brown +  ts - time stepping context
42651c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
42661c3436cfSJed Brown 
42671c3436cfSJed Brown    Output Arguments:
42681c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
42691c3436cfSJed Brown 
42701c3436cfSJed Brown    Level: developer
42711c3436cfSJed Brown 
42721c3436cfSJed Brown .seealso: TSSetTolerances()
42731c3436cfSJed Brown @*/
42741c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
42751c3436cfSJed Brown {
42761c3436cfSJed Brown   PetscErrorCode    ierr;
42771c3436cfSJed Brown   PetscInt          i,n,N;
42780910c330SBarry Smith   const PetscScalar *u,*y;
42790910c330SBarry Smith   Vec               U;
42801c3436cfSJed Brown   PetscReal         sum,gsum;
42811c3436cfSJed Brown 
42821c3436cfSJed Brown   PetscFunctionBegin;
42831c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42841c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
42851c3436cfSJed Brown   PetscValidPointer(norm,3);
42860910c330SBarry Smith   U = ts->vec_sol;
42870910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4288ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
42891c3436cfSJed Brown 
42900910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
42910910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
42920910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
42931c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
42941c3436cfSJed Brown   sum  = 0.;
4295ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4296ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4297ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4298ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4299ceefc113SJed Brown     for (i=0; i<n; i++) {
43000910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43010910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4302ceefc113SJed Brown     }
4303ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4304ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4305ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4306ceefc113SJed Brown     const PetscScalar *atol;
4307ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4308ceefc113SJed Brown     for (i=0; i<n; i++) {
43090910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43100910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4311ceefc113SJed Brown     }
4312ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4313ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4314ceefc113SJed Brown     const PetscScalar *rtol;
4315ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4316ceefc113SJed Brown     for (i=0; i<n; i++) {
43170910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43180910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4319ceefc113SJed Brown     }
4320ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4321ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
43221c3436cfSJed Brown     for (i=0; i<n; i++) {
43230910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
43240910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
43251c3436cfSJed Brown     }
4326ceefc113SJed Brown   }
43270910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
43281c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
43291c3436cfSJed Brown 
4330ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
43311c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4332ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
43331c3436cfSJed Brown   PetscFunctionReturn(0);
43341c3436cfSJed Brown }
43351c3436cfSJed Brown 
43361c3436cfSJed Brown #undef __FUNCT__
43378d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
43388d59e960SJed Brown /*@
43398d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
43408d59e960SJed Brown 
43418d59e960SJed Brown    Logically Collective on TS
43428d59e960SJed Brown 
43438d59e960SJed Brown    Input Arguments:
43448d59e960SJed Brown +  ts - time stepping context
43458d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
43468d59e960SJed Brown 
43478d59e960SJed Brown    Note:
43488d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
43498d59e960SJed Brown 
43508d59e960SJed Brown    Level: intermediate
43518d59e960SJed Brown 
43528d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
43538d59e960SJed Brown @*/
43548d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
43558d59e960SJed Brown {
43568d59e960SJed Brown   PetscFunctionBegin;
43578d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43588d59e960SJed Brown   ts->cfltime_local = cfltime;
43598d59e960SJed Brown   ts->cfltime       = -1.;
43608d59e960SJed Brown   PetscFunctionReturn(0);
43618d59e960SJed Brown }
43628d59e960SJed Brown 
43638d59e960SJed Brown #undef __FUNCT__
43648d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
43658d59e960SJed Brown /*@
43668d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
43678d59e960SJed Brown 
43688d59e960SJed Brown    Collective on TS
43698d59e960SJed Brown 
43708d59e960SJed Brown    Input Arguments:
43718d59e960SJed Brown .  ts - time stepping context
43728d59e960SJed Brown 
43738d59e960SJed Brown    Output Arguments:
43748d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
43758d59e960SJed Brown 
43768d59e960SJed Brown    Level: advanced
43778d59e960SJed Brown 
43788d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
43798d59e960SJed Brown @*/
43808d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
43818d59e960SJed Brown {
43828d59e960SJed Brown   PetscErrorCode ierr;
43838d59e960SJed Brown 
43848d59e960SJed Brown   PetscFunctionBegin;
43858d59e960SJed Brown   if (ts->cfltime < 0) {
4386ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
43878d59e960SJed Brown   }
43888d59e960SJed Brown   *cfltime = ts->cfltime;
43898d59e960SJed Brown   PetscFunctionReturn(0);
43908d59e960SJed Brown }
43918d59e960SJed Brown 
43928d59e960SJed Brown #undef __FUNCT__
4393d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4394d6ebe24aSShri Abhyankar /*@
4395d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4396d6ebe24aSShri Abhyankar 
4397d6ebe24aSShri Abhyankar    Input Parameters:
4398d6ebe24aSShri Abhyankar .  ts   - the TS context.
4399d6ebe24aSShri Abhyankar .  xl   - lower bound.
4400d6ebe24aSShri Abhyankar .  xu   - upper bound.
4401d6ebe24aSShri Abhyankar 
4402d6ebe24aSShri Abhyankar    Notes:
4403d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
440433c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4405d6ebe24aSShri Abhyankar 
44062bd2b0e6SSatish Balay    Level: advanced
44072bd2b0e6SSatish Balay 
4408d6ebe24aSShri Abhyankar @*/
4409d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4410d6ebe24aSShri Abhyankar {
4411d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4412d6ebe24aSShri Abhyankar   SNES           snes;
4413d6ebe24aSShri Abhyankar 
4414d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4415d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4416d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4417d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4418d6ebe24aSShri Abhyankar }
4419d6ebe24aSShri Abhyankar 
4420325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4421c6db04a5SJed Brown #include <mex.h>
4422325fc9f4SBarry Smith 
4423325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4424325fc9f4SBarry Smith 
4425325fc9f4SBarry Smith #undef __FUNCT__
4426325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4427325fc9f4SBarry Smith /*
4428325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4429325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4430325fc9f4SBarry Smith 
4431325fc9f4SBarry Smith    Collective on TS
4432325fc9f4SBarry Smith 
4433325fc9f4SBarry Smith    Input Parameters:
4434325fc9f4SBarry Smith +  snes - the TS context
44350910c330SBarry Smith -  u - input vector
4436325fc9f4SBarry Smith 
4437325fc9f4SBarry Smith    Output Parameter:
4438325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4439325fc9f4SBarry Smith 
4440325fc9f4SBarry Smith    Notes:
4441325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4442325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4443325fc9f4SBarry Smith    themselves.
4444325fc9f4SBarry Smith 
4445325fc9f4SBarry Smith    Level: developer
4446325fc9f4SBarry Smith 
4447325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4448325fc9f4SBarry Smith 
4449325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4450325fc9f4SBarry Smith */
44510910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4452325fc9f4SBarry Smith {
4453325fc9f4SBarry Smith   PetscErrorCode  ierr;
4454325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4455325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4456325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4457325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4458325fc9f4SBarry Smith 
4459325fc9f4SBarry Smith   PetscFunctionBegin;
4460325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
44610910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
44620910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4463325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
44640910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4465325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4466325fc9f4SBarry Smith 
4467325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
44680910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
44690910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
44700910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4471bbd56ea5SKarl Rupp 
4472325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4473325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4474325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4475325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4476325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4477325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4478325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4479325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4480325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4481325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4482325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4483325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4484325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4485325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4486325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4487325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4488325fc9f4SBarry Smith   PetscFunctionReturn(0);
4489325fc9f4SBarry Smith }
4490325fc9f4SBarry Smith 
4491325fc9f4SBarry Smith 
4492325fc9f4SBarry Smith #undef __FUNCT__
4493325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4494325fc9f4SBarry Smith /*
4495325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4496325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4497e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4498325fc9f4SBarry Smith 
4499325fc9f4SBarry Smith    Logically Collective on TS
4500325fc9f4SBarry Smith 
4501325fc9f4SBarry Smith    Input Parameters:
4502325fc9f4SBarry Smith +  ts - the TS context
4503325fc9f4SBarry Smith -  func - function evaluation routine
4504325fc9f4SBarry Smith 
4505325fc9f4SBarry Smith    Calling sequence of func:
45060910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4507325fc9f4SBarry Smith 
4508325fc9f4SBarry Smith    Level: beginner
4509325fc9f4SBarry Smith 
4510325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4511325fc9f4SBarry Smith 
4512325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4513325fc9f4SBarry Smith */
4514cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4515325fc9f4SBarry Smith {
4516325fc9f4SBarry Smith   PetscErrorCode  ierr;
4517325fc9f4SBarry Smith   TSMatlabContext *sctx;
4518325fc9f4SBarry Smith 
4519325fc9f4SBarry Smith   PetscFunctionBegin;
4520325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4521325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4522325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4523325fc9f4SBarry Smith   /*
4524325fc9f4SBarry Smith      This should work, but it doesn't
4525325fc9f4SBarry Smith   sctx->ctx = ctx;
4526325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4527325fc9f4SBarry Smith   */
4528325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4529bbd56ea5SKarl Rupp 
45300298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4531325fc9f4SBarry Smith   PetscFunctionReturn(0);
4532325fc9f4SBarry Smith }
4533325fc9f4SBarry Smith 
4534325fc9f4SBarry Smith #undef __FUNCT__
4535325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4536325fc9f4SBarry Smith /*
4537325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4538325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4539325fc9f4SBarry Smith 
4540325fc9f4SBarry Smith    Collective on TS
4541325fc9f4SBarry Smith 
4542325fc9f4SBarry Smith    Input Parameters:
4543cdcf91faSSean Farley +  ts - the TS context
45440910c330SBarry Smith .  u - input vector
4545325fc9f4SBarry Smith .  A, B - the matrices
4546325fc9f4SBarry Smith -  ctx - user context
4547325fc9f4SBarry Smith 
4548325fc9f4SBarry Smith    Output Parameter:
4549325fc9f4SBarry Smith .  flag - structure of the matrix
4550325fc9f4SBarry Smith 
4551325fc9f4SBarry Smith    Level: developer
4552325fc9f4SBarry Smith 
4553325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4554325fc9f4SBarry Smith 
4555325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4556325fc9f4SBarry Smith @*/
45570910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4558325fc9f4SBarry Smith {
4559325fc9f4SBarry Smith   PetscErrorCode  ierr;
4560325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4561325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4562325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4563325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4564325fc9f4SBarry Smith 
4565325fc9f4SBarry Smith   PetscFunctionBegin;
4566cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45670910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4568325fc9f4SBarry Smith 
45690910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4570325fc9f4SBarry Smith 
4571cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
45720910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
45730910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
45740910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
45750910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4576bbd56ea5SKarl Rupp 
4577325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4578325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4579325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4580325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4581325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4582325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4583325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4584325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4585325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4586325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4587325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4588325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4589325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4590325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4591325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4592325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4593325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4594325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4595325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4596325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4597325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4598325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4599325fc9f4SBarry Smith   PetscFunctionReturn(0);
4600325fc9f4SBarry Smith }
4601325fc9f4SBarry Smith 
4602325fc9f4SBarry Smith 
4603325fc9f4SBarry Smith #undef __FUNCT__
4604325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4605325fc9f4SBarry Smith /*
4606325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4607e3c5b3baSBarry 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
4608325fc9f4SBarry Smith 
4609325fc9f4SBarry Smith    Logically Collective on TS
4610325fc9f4SBarry Smith 
4611325fc9f4SBarry Smith    Input Parameters:
4612cdcf91faSSean Farley +  ts - the TS context
4613325fc9f4SBarry Smith .  A,B - Jacobian matrices
4614325fc9f4SBarry Smith .  func - function evaluation routine
4615325fc9f4SBarry Smith -  ctx - user context
4616325fc9f4SBarry Smith 
4617325fc9f4SBarry Smith    Calling sequence of func:
46180910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4619325fc9f4SBarry Smith 
4620325fc9f4SBarry Smith 
4621325fc9f4SBarry Smith    Level: developer
4622325fc9f4SBarry Smith 
4623325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4624325fc9f4SBarry Smith 
4625325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4626325fc9f4SBarry Smith */
4627cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4628325fc9f4SBarry Smith {
4629325fc9f4SBarry Smith   PetscErrorCode  ierr;
4630325fc9f4SBarry Smith   TSMatlabContext *sctx;
4631325fc9f4SBarry Smith 
4632325fc9f4SBarry Smith   PetscFunctionBegin;
4633325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4634325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4635325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4636325fc9f4SBarry Smith   /*
4637325fc9f4SBarry Smith      This should work, but it doesn't
4638325fc9f4SBarry Smith   sctx->ctx = ctx;
4639325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4640325fc9f4SBarry Smith   */
4641325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4642bbd56ea5SKarl Rupp 
4643cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4644325fc9f4SBarry Smith   PetscFunctionReturn(0);
4645325fc9f4SBarry Smith }
4646325fc9f4SBarry Smith 
4647b5b1a830SBarry Smith #undef __FUNCT__
4648b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4649b5b1a830SBarry Smith /*
4650b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4651b5b1a830SBarry Smith 
4652b5b1a830SBarry Smith    Collective on TS
4653b5b1a830SBarry Smith 
4654b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4655b5b1a830SBarry Smith @*/
46560910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4657b5b1a830SBarry Smith {
4658b5b1a830SBarry Smith   PetscErrorCode  ierr;
4659b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4660a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4661b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4662b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4663b5b1a830SBarry Smith 
4664b5b1a830SBarry Smith   PetscFunctionBegin;
4665cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46660910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4667b5b1a830SBarry Smith 
4668cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
46690910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4670bbd56ea5SKarl Rupp 
4671b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4672b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4673b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4674b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4675b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4676b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4677b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4678b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4679b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4680b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4681b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4682b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4683b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4684b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4685b5b1a830SBarry Smith   PetscFunctionReturn(0);
4686b5b1a830SBarry Smith }
4687b5b1a830SBarry Smith 
4688b5b1a830SBarry Smith 
4689b5b1a830SBarry Smith #undef __FUNCT__
4690b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4691b5b1a830SBarry Smith /*
4692b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4693b5b1a830SBarry Smith 
4694b5b1a830SBarry Smith    Level: developer
4695b5b1a830SBarry Smith 
4696b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4697b5b1a830SBarry Smith 
4698b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4699b5b1a830SBarry Smith */
4700cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4701b5b1a830SBarry Smith {
4702b5b1a830SBarry Smith   PetscErrorCode  ierr;
4703b5b1a830SBarry Smith   TSMatlabContext *sctx;
4704b5b1a830SBarry Smith 
4705b5b1a830SBarry Smith   PetscFunctionBegin;
4706b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4707b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4708b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4709b5b1a830SBarry Smith   /*
4710b5b1a830SBarry Smith      This should work, but it doesn't
4711b5b1a830SBarry Smith   sctx->ctx = ctx;
4712b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4713b5b1a830SBarry Smith   */
4714b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4715bbd56ea5SKarl Rupp 
47160298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4717b5b1a830SBarry Smith   PetscFunctionReturn(0);
4718b5b1a830SBarry Smith }
4719325fc9f4SBarry Smith #endif
4720b3603a34SBarry Smith 
47210b039ecaSBarry Smith 
47220b039ecaSBarry Smith 
4723b3603a34SBarry Smith #undef __FUNCT__
47244f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4725b3603a34SBarry Smith /*@C
47264f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4727b3603a34SBarry Smith        in a time based line graph
4728b3603a34SBarry Smith 
4729b3603a34SBarry Smith    Collective on TS
4730b3603a34SBarry Smith 
4731b3603a34SBarry Smith    Input Parameters:
4732b3603a34SBarry Smith +  ts - the TS context
4733b3603a34SBarry Smith .  step - current time-step
4734b3603a34SBarry Smith .  ptime - current time
4735b3603a34SBarry Smith -  lg - a line graph object
4736b3603a34SBarry Smith 
4737b3603a34SBarry Smith    Level: intermediate
4738b3603a34SBarry Smith 
47390b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4740b3603a34SBarry Smith 
4741b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4742b3603a34SBarry Smith 
4743b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4744b3603a34SBarry Smith @*/
47450910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4746b3603a34SBarry Smith {
4747b3603a34SBarry Smith   PetscErrorCode    ierr;
47480b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4749b3603a34SBarry Smith   const PetscScalar *yy;
475058ff32f7SBarry Smith   PetscInt          dim;
4751b3603a34SBarry Smith 
4752b3603a34SBarry Smith   PetscFunctionBegin;
475358ff32f7SBarry Smith   if (!step) {
4754a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4755a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4756a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
47570910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
47580b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
47590b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
476058ff32f7SBarry Smith   }
47610910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4762e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4763e3efe391SJed Brown   {
4764e3efe391SJed Brown     PetscReal *yreal;
4765e3efe391SJed Brown     PetscInt  i,n;
47660910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4767e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4768e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
47690b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4770e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4771e3efe391SJed Brown   }
4772e3efe391SJed Brown #else
47730b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4774e3efe391SJed Brown #endif
47750910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
4776b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
47770b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
47783923b477SBarry Smith   }
4779b3603a34SBarry Smith   PetscFunctionReturn(0);
4780b3603a34SBarry Smith }
4781b3603a34SBarry Smith 
4782b3603a34SBarry Smith #undef __FUNCT__
47834f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4784ef20d060SBarry Smith /*@C
47854f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4786ef20d060SBarry Smith        in a time based line graph
4787ef20d060SBarry Smith 
4788ef20d060SBarry Smith    Collective on TS
4789ef20d060SBarry Smith 
4790ef20d060SBarry Smith    Input Parameters:
4791ef20d060SBarry Smith +  ts - the TS context
4792ef20d060SBarry Smith .  step - current time-step
4793ef20d060SBarry Smith .  ptime - current time
4794ef20d060SBarry Smith -  lg - a line graph object
4795ef20d060SBarry Smith 
4796ef20d060SBarry Smith    Level: intermediate
4797ef20d060SBarry Smith 
4798abd5a294SJed Brown    Notes:
4799abd5a294SJed Brown    Only for sequential solves.
4800abd5a294SJed Brown 
4801abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4802abd5a294SJed Brown 
4803abd5a294SJed Brown    Options Database Keys:
48044f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4805ef20d060SBarry Smith 
4806ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4807ef20d060SBarry Smith 
4808abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4809ef20d060SBarry Smith @*/
48100910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4811ef20d060SBarry Smith {
4812ef20d060SBarry Smith   PetscErrorCode    ierr;
48130b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4814ef20d060SBarry Smith   const PetscScalar *yy;
4815ef20d060SBarry Smith   Vec               y;
4816a9f9c1f6SBarry Smith   PetscInt          dim;
4817ef20d060SBarry Smith 
4818ef20d060SBarry Smith   PetscFunctionBegin;
4819a9f9c1f6SBarry Smith   if (!step) {
4820a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4821a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
48221ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
48230910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4824a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4825a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4826a9f9c1f6SBarry Smith   }
48270910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4828ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
48290910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4830ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4831e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4832e3efe391SJed Brown   {
4833e3efe391SJed Brown     PetscReal *yreal;
4834e3efe391SJed Brown     PetscInt  i,n;
4835e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4836e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4837e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
48380b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4839e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4840e3efe391SJed Brown   }
4841e3efe391SJed Brown #else
48420b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4843e3efe391SJed Brown #endif
4844ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4845ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
4846b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
48470b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
48483923b477SBarry Smith   }
4849ef20d060SBarry Smith   PetscFunctionReturn(0);
4850ef20d060SBarry Smith }
4851ef20d060SBarry Smith 
4852201da799SBarry Smith #undef __FUNCT__
4853201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4854201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4855201da799SBarry Smith {
4856201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4857201da799SBarry Smith   PetscReal      x   = ptime,y;
4858201da799SBarry Smith   PetscErrorCode ierr;
4859201da799SBarry Smith   PetscInt       its;
4860201da799SBarry Smith 
4861201da799SBarry Smith   PetscFunctionBegin;
4862201da799SBarry Smith   if (!n) {
4863201da799SBarry Smith     PetscDrawAxis axis;
4864bbd56ea5SKarl Rupp 
4865201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4866201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4867201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4868bbd56ea5SKarl Rupp 
4869201da799SBarry Smith     ctx->snes_its = 0;
4870201da799SBarry Smith   }
4871201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4872201da799SBarry Smith   y    = its - ctx->snes_its;
4873201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
48743fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4875201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4876201da799SBarry Smith   }
4877201da799SBarry Smith   ctx->snes_its = its;
4878201da799SBarry Smith   PetscFunctionReturn(0);
4879201da799SBarry Smith }
4880201da799SBarry Smith 
4881201da799SBarry Smith #undef __FUNCT__
4882201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4883201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4884201da799SBarry Smith {
4885201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4886201da799SBarry Smith   PetscReal      x   = ptime,y;
4887201da799SBarry Smith   PetscErrorCode ierr;
4888201da799SBarry Smith   PetscInt       its;
4889201da799SBarry Smith 
4890201da799SBarry Smith   PetscFunctionBegin;
4891201da799SBarry Smith   if (!n) {
4892201da799SBarry Smith     PetscDrawAxis axis;
4893bbd56ea5SKarl Rupp 
4894201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4895201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4896201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4897bbd56ea5SKarl Rupp 
4898201da799SBarry Smith     ctx->ksp_its = 0;
4899201da799SBarry Smith   }
4900201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4901201da799SBarry Smith   y    = its - ctx->ksp_its;
4902201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
490399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4904201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4905201da799SBarry Smith   }
4906201da799SBarry Smith   ctx->ksp_its = its;
4907201da799SBarry Smith   PetscFunctionReturn(0);
4908201da799SBarry Smith }
4909f9c1d6abSBarry Smith 
4910f9c1d6abSBarry Smith #undef __FUNCT__
4911f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4912f9c1d6abSBarry Smith /*@
4913f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4914f9c1d6abSBarry Smith 
4915f9c1d6abSBarry Smith    Collective on TS and Vec
4916f9c1d6abSBarry Smith 
4917f9c1d6abSBarry Smith    Input Parameters:
4918f9c1d6abSBarry Smith +  ts - the TS context
4919f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4920f9c1d6abSBarry Smith 
4921f9c1d6abSBarry Smith    Output Parameters:
4922f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4923f9c1d6abSBarry Smith 
4924f9c1d6abSBarry Smith    Level: developer
4925f9c1d6abSBarry Smith 
4926f9c1d6abSBarry Smith .keywords: TS, compute
4927f9c1d6abSBarry Smith 
4928f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4929f9c1d6abSBarry Smith @*/
4930f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4931f9c1d6abSBarry Smith {
4932f9c1d6abSBarry Smith   PetscErrorCode ierr;
4933f9c1d6abSBarry Smith 
4934f9c1d6abSBarry Smith   PetscFunctionBegin;
4935f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4936ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4937f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4938f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4939f9c1d6abSBarry Smith }
4940