xref: /petsc/src/ts/interface/ts.c (revision b41af12e0cf5c8cfcc61fcc96f5ea4cd576f4469)
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     }
285d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
286d1212d36SBarry Smith   }
287d1212d36SBarry Smith 
288552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2891c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2901c3436cfSJed Brown 
291d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
292d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
293d52bd9f3SBarry Smith 
294bdad233fSMatthew Knepley   /* Handle specific TS options */
295abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
296bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
297bdad233fSMatthew Knepley   }
2985d973c19SBarry Smith 
2995d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3005d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
301bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
302bdad233fSMatthew Knepley   PetscFunctionReturn(0);
303bdad233fSMatthew Knepley }
304bdad233fSMatthew Knepley 
305bdad233fSMatthew Knepley #undef __FUNCT__
306cdcf91faSSean Farley #undef __FUNCT__
3074a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
308a7a1495cSBarry Smith /*@
3098c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
310a7a1495cSBarry Smith       set with TSSetRHSJacobian().
311a7a1495cSBarry Smith 
312a7a1495cSBarry Smith    Collective on TS and Vec
313a7a1495cSBarry Smith 
314a7a1495cSBarry Smith    Input Parameters:
315316643e7SJed Brown +  ts - the TS context
316a7a1495cSBarry Smith .  t - current timestep
3170910c330SBarry Smith -  U - input vector
318a7a1495cSBarry Smith 
319a7a1495cSBarry Smith    Output Parameters:
320a7a1495cSBarry Smith +  A - Jacobian matrix
321a7a1495cSBarry Smith .  B - optional preconditioning matrix
322a7a1495cSBarry Smith -  flag - flag indicating matrix structure
323a7a1495cSBarry Smith 
324a7a1495cSBarry Smith    Notes:
325a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
326a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
327a7a1495cSBarry Smith 
32894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
329a7a1495cSBarry Smith    flag parameter.
330a7a1495cSBarry Smith 
331a7a1495cSBarry Smith    Level: developer
332a7a1495cSBarry Smith 
333a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
334a7a1495cSBarry Smith 
33594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
336a7a1495cSBarry Smith @*/
3370910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
338a7a1495cSBarry Smith {
339dfbe8321SBarry Smith   PetscErrorCode ierr;
3400910c330SBarry Smith   PetscInt       Ustate;
34124989b8cSPeter Brune   DM             dm;
342942e3340SBarry Smith   DMTS           tsdm;
34324989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
34424989b8cSPeter Brune   void           *ctx;
34524989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
346a7a1495cSBarry Smith 
347a7a1495cSBarry Smith   PetscFunctionBegin;
3480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3490910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3500910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
35124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
352942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
35324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3540298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
3550910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3560910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3570e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3580e4ef248SJed Brown     PetscFunctionReturn(0);
359f8ede8e7SJed Brown   }
360d90be118SSean Farley 
361ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
362d90be118SSean Farley 
363e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
364e1244c69SJed Brown     ierr = MatShift(*A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
365e1244c69SJed Brown     ierr = MatScale(*A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
366e1244c69SJed Brown     if (*A != *B) {
367e1244c69SJed Brown       ierr = MatShift(*B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
368e1244c69SJed Brown       ierr = MatScale(*B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
369e1244c69SJed Brown     }
370e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
371e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
372e1244c69SJed Brown   }
373e1244c69SJed Brown 
37424989b8cSPeter Brune   if (rhsjacobianfunc) {
3750910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
376a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
377a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3780910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
379a7a1495cSBarry Smith     PetscStackPop;
3800910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
381a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3820700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3830700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
384ef66eb69SBarry Smith   } else {
385214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
386214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
387214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
388ef66eb69SBarry Smith   }
3890e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3900910c330SBarry Smith   ts->rhsjacobian.X          = U;
3910910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3920e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
393a7a1495cSBarry Smith   PetscFunctionReturn(0);
394a7a1495cSBarry Smith }
395a7a1495cSBarry Smith 
3964a2ae208SSatish Balay #undef __FUNCT__
3974a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
398316643e7SJed Brown /*@
399d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
400d763cef2SBarry Smith 
401316643e7SJed Brown    Collective on TS and Vec
402316643e7SJed Brown 
403316643e7SJed Brown    Input Parameters:
404316643e7SJed Brown +  ts - the TS context
405316643e7SJed Brown .  t - current time
4060910c330SBarry Smith -  U - state vector
407316643e7SJed Brown 
408316643e7SJed Brown    Output Parameter:
409316643e7SJed Brown .  y - right hand side
410316643e7SJed Brown 
411316643e7SJed Brown    Note:
412316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
413316643e7SJed Brown    is used internally within the nonlinear solvers.
414316643e7SJed Brown 
415316643e7SJed Brown    Level: developer
416316643e7SJed Brown 
417316643e7SJed Brown .keywords: TS, compute
418316643e7SJed Brown 
419316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
420316643e7SJed Brown @*/
4210910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
422d763cef2SBarry Smith {
423dfbe8321SBarry Smith   PetscErrorCode ierr;
42424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
42524989b8cSPeter Brune   TSIFunction    ifunction;
42624989b8cSPeter Brune   void           *ctx;
42724989b8cSPeter Brune   DM             dm;
42824989b8cSPeter Brune 
429d763cef2SBarry Smith   PetscFunctionBegin;
4300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4320700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
43324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
43424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4350298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
436d763cef2SBarry Smith 
437ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
438d763cef2SBarry Smith 
4390910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
44024989b8cSPeter Brune   if (rhsfunction) {
441d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4420910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
443d763cef2SBarry Smith     PetscStackPop;
444214bc6a2SJed Brown   } else {
445214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
446b2cd27e8SJed Brown   }
44744a41b28SSean Farley 
4480910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
449d763cef2SBarry Smith   PetscFunctionReturn(0);
450d763cef2SBarry Smith }
451d763cef2SBarry Smith 
4524a2ae208SSatish Balay #undef __FUNCT__
453ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
454ef20d060SBarry Smith /*@
455ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
456ef20d060SBarry Smith 
457ef20d060SBarry Smith    Collective on TS and Vec
458ef20d060SBarry Smith 
459ef20d060SBarry Smith    Input Parameters:
460ef20d060SBarry Smith +  ts - the TS context
461ef20d060SBarry Smith -  t - current time
462ef20d060SBarry Smith 
463ef20d060SBarry Smith    Output Parameter:
4640910c330SBarry Smith .  U - the solution
465ef20d060SBarry Smith 
466ef20d060SBarry Smith    Note:
467ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
468ef20d060SBarry Smith    is used internally within the nonlinear solvers.
469ef20d060SBarry Smith 
470ef20d060SBarry Smith    Level: developer
471ef20d060SBarry Smith 
472ef20d060SBarry Smith .keywords: TS, compute
473ef20d060SBarry Smith 
474abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
475ef20d060SBarry Smith @*/
4760910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
477ef20d060SBarry Smith {
478ef20d060SBarry Smith   PetscErrorCode     ierr;
479ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
480ef20d060SBarry Smith   void               *ctx;
481ef20d060SBarry Smith   DM                 dm;
482ef20d060SBarry Smith 
483ef20d060SBarry Smith   PetscFunctionBegin;
484ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4850910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
486ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
487ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
488ef20d060SBarry Smith 
489ef20d060SBarry Smith   if (solutionfunction) {
4909b7cd975SBarry Smith     PetscStackPush("TS user solution function");
4910910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
492ef20d060SBarry Smith     PetscStackPop;
493ef20d060SBarry Smith   }
494ef20d060SBarry Smith   PetscFunctionReturn(0);
495ef20d060SBarry Smith }
4969b7cd975SBarry Smith #undef __FUNCT__
4979b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
4989b7cd975SBarry Smith /*@
4999b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5009b7cd975SBarry Smith 
5019b7cd975SBarry Smith    Collective on TS and Vec
5029b7cd975SBarry Smith 
5039b7cd975SBarry Smith    Input Parameters:
5049b7cd975SBarry Smith +  ts - the TS context
5059b7cd975SBarry Smith -  t - current time
5069b7cd975SBarry Smith 
5079b7cd975SBarry Smith    Output Parameter:
5089b7cd975SBarry Smith .  U - the function value
5099b7cd975SBarry Smith 
5109b7cd975SBarry Smith    Note:
5119b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5129b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5139b7cd975SBarry Smith 
5149b7cd975SBarry Smith    Level: developer
5159b7cd975SBarry Smith 
5169b7cd975SBarry Smith .keywords: TS, compute
5179b7cd975SBarry Smith 
5189b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5199b7cd975SBarry Smith @*/
5209b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5219b7cd975SBarry Smith {
5229b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5239b7cd975SBarry Smith   void               *ctx;
5249b7cd975SBarry Smith   DM                 dm;
5259b7cd975SBarry Smith 
5269b7cd975SBarry Smith   PetscFunctionBegin;
5279b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5289b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5299b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5309b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5319b7cd975SBarry Smith 
5329b7cd975SBarry Smith   if (forcing) {
5339b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5349b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5359b7cd975SBarry Smith     PetscStackPop;
5369b7cd975SBarry Smith   }
5379b7cd975SBarry Smith   PetscFunctionReturn(0);
5389b7cd975SBarry Smith }
539ef20d060SBarry Smith 
540ef20d060SBarry Smith #undef __FUNCT__
541214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
542214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
543214bc6a2SJed Brown {
5442dd45cf8SJed Brown   Vec            F;
545214bc6a2SJed Brown   PetscErrorCode ierr;
546214bc6a2SJed Brown 
547214bc6a2SJed Brown   PetscFunctionBegin;
5480298fd71SBarry Smith   *Frhs = NULL;
5490298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
550214bc6a2SJed Brown   if (!ts->Frhs) {
5512dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
552214bc6a2SJed Brown   }
553214bc6a2SJed Brown   *Frhs = ts->Frhs;
554214bc6a2SJed Brown   PetscFunctionReturn(0);
555214bc6a2SJed Brown }
556214bc6a2SJed Brown 
557214bc6a2SJed Brown #undef __FUNCT__
558214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
559214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
560214bc6a2SJed Brown {
561214bc6a2SJed Brown   Mat            A,B;
5622dd45cf8SJed Brown   PetscErrorCode ierr;
563214bc6a2SJed Brown 
564214bc6a2SJed Brown   PetscFunctionBegin;
5650298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
566214bc6a2SJed Brown   if (Arhs) {
567214bc6a2SJed Brown     if (!ts->Arhs) {
568214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
569214bc6a2SJed Brown     }
570214bc6a2SJed Brown     *Arhs = ts->Arhs;
571214bc6a2SJed Brown   }
572214bc6a2SJed Brown   if (Brhs) {
573214bc6a2SJed Brown     if (!ts->Brhs) {
574214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
575214bc6a2SJed Brown     }
576214bc6a2SJed Brown     *Brhs = ts->Brhs;
577214bc6a2SJed Brown   }
578214bc6a2SJed Brown   PetscFunctionReturn(0);
579214bc6a2SJed Brown }
580214bc6a2SJed Brown 
581214bc6a2SJed Brown #undef __FUNCT__
582316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
583316643e7SJed Brown /*@
5840910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
585316643e7SJed Brown 
586316643e7SJed Brown    Collective on TS and Vec
587316643e7SJed Brown 
588316643e7SJed Brown    Input Parameters:
589316643e7SJed Brown +  ts - the TS context
590316643e7SJed Brown .  t - current time
5910910c330SBarry Smith .  U - state vector
5920910c330SBarry Smith .  Udot - time derivative of state vector
593214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
594316643e7SJed Brown 
595316643e7SJed Brown    Output Parameter:
596316643e7SJed Brown .  Y - right hand side
597316643e7SJed Brown 
598316643e7SJed Brown    Note:
599316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
600316643e7SJed Brown    is used internally within the nonlinear solvers.
601316643e7SJed Brown 
602316643e7SJed Brown    If the user did did not write their equations in implicit form, this
603316643e7SJed Brown    function recasts them in implicit form.
604316643e7SJed Brown 
605316643e7SJed Brown    Level: developer
606316643e7SJed Brown 
607316643e7SJed Brown .keywords: TS, compute
608316643e7SJed Brown 
609316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
610316643e7SJed Brown @*/
6110910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
612316643e7SJed Brown {
613316643e7SJed Brown   PetscErrorCode ierr;
61424989b8cSPeter Brune   TSIFunction    ifunction;
61524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
61624989b8cSPeter Brune   void           *ctx;
61724989b8cSPeter Brune   DM             dm;
618316643e7SJed Brown 
619316643e7SJed Brown   PetscFunctionBegin;
6200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6210910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6220910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6230700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
624316643e7SJed Brown 
62524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
62624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6270298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
62824989b8cSPeter Brune 
629ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
630d90be118SSean Farley 
6310910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
63224989b8cSPeter Brune   if (ifunction) {
633316643e7SJed Brown     PetscStackPush("TS user implicit function");
6340910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
635316643e7SJed Brown     PetscStackPop;
636214bc6a2SJed Brown   }
637214bc6a2SJed Brown   if (imex) {
63824989b8cSPeter Brune     if (!ifunction) {
6390910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6402dd45cf8SJed Brown     }
64124989b8cSPeter Brune   } else if (rhsfunction) {
64224989b8cSPeter Brune     if (ifunction) {
643214bc6a2SJed Brown       Vec Frhs;
644214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6450910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
646214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6472dd45cf8SJed Brown     } else {
6480910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6490910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
650316643e7SJed Brown     }
6514a6899ffSJed Brown   }
6520910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
653316643e7SJed Brown   PetscFunctionReturn(0);
654316643e7SJed Brown }
655316643e7SJed Brown 
656316643e7SJed Brown #undef __FUNCT__
657316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
658316643e7SJed Brown /*@
659316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
660316643e7SJed Brown 
661316643e7SJed Brown    Collective on TS and Vec
662316643e7SJed Brown 
663316643e7SJed Brown    Input
664316643e7SJed Brown       Input Parameters:
665316643e7SJed Brown +  ts - the TS context
666316643e7SJed Brown .  t - current timestep
6670910c330SBarry Smith .  U - state vector
6680910c330SBarry Smith .  Udot - time derivative of state vector
669214bc6a2SJed Brown .  shift - shift to apply, see note below
670214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
671316643e7SJed Brown 
672316643e7SJed Brown    Output Parameters:
673316643e7SJed Brown +  A - Jacobian matrix
674316643e7SJed Brown .  B - optional preconditioning matrix
675316643e7SJed Brown -  flag - flag indicating matrix structure
676316643e7SJed Brown 
677316643e7SJed Brown    Notes:
6780910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
679316643e7SJed Brown 
6800910c330SBarry Smith    dF/dU + shift*dF/dUdot
681316643e7SJed Brown 
682316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
683316643e7SJed Brown    is used internally within the nonlinear solvers.
684316643e7SJed Brown 
685316643e7SJed Brown    Level: developer
686316643e7SJed Brown 
687316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
688316643e7SJed Brown 
689316643e7SJed Brown .seealso:  TSSetIJacobian()
69063495f91SJed Brown @*/
6910910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
692316643e7SJed Brown {
693316643e7SJed Brown   PetscErrorCode ierr;
69424989b8cSPeter Brune   TSIJacobian    ijacobian;
69524989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
69624989b8cSPeter Brune   DM             dm;
69724989b8cSPeter Brune   void           *ctx;
698316643e7SJed Brown 
699316643e7SJed Brown   PetscFunctionBegin;
7000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7010910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7020910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
703316643e7SJed Brown   PetscValidPointer(A,6);
7040700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
705316643e7SJed Brown   PetscValidPointer(B,7);
7060700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
707316643e7SJed Brown   PetscValidPointer(flg,8);
70824989b8cSPeter Brune 
70924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
71024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7110298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
71224989b8cSPeter Brune 
713ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
714316643e7SJed Brown 
7154e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7160910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
71724989b8cSPeter Brune   if (ijacobian) {
7182dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
719316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7200910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
721316643e7SJed Brown     PetscStackPop;
722214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
723214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
724214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7254a6899ffSJed Brown   }
726214bc6a2SJed Brown   if (imex) {
727b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
728214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7292dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
730214bc6a2SJed Brown       if (*A != *B) {
731214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
732214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
733214bc6a2SJed Brown       }
734214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
735214bc6a2SJed Brown     }
736214bc6a2SJed Brown   } else {
737e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
738e1244c69SJed Brown     MatStructure flg2;
739e1244c69SJed Brown     if (rhsjacobian) {
740e1244c69SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
741e1244c69SJed Brown       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
742e1244c69SJed Brown     }
743e1244c69SJed Brown     if (Arhs == *A) {           /* No IJacobian, so we only have the RHS matrix */
744e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
745e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
746214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
747214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
748316643e7SJed Brown       if (*A != *B) {
749316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
750316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
751316643e7SJed Brown       }
752e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
753e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
754e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
755e1244c69SJed Brown         ierr = MatZeroEntries(*A);CHKERRQ(ierr);
756e1244c69SJed Brown         ierr = MatShift(*A,shift);CHKERRQ(ierr);
757e1244c69SJed Brown         if (*A != *B) {
758e1244c69SJed Brown           ierr = MatZeroEntries(*B);CHKERRQ(ierr);
759e1244c69SJed Brown           ierr = MatShift(*B,shift);CHKERRQ(ierr);
760e1244c69SJed Brown         }
761e1244c69SJed Brown       }
762214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
763214bc6a2SJed Brown       if (*A != *B) {
764214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
765214bc6a2SJed Brown       }
766214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
767214bc6a2SJed Brown     }
768316643e7SJed Brown   }
7690026cea9SSean Farley 
7700910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
771316643e7SJed Brown   PetscFunctionReturn(0);
772316643e7SJed Brown }
773316643e7SJed Brown 
774316643e7SJed Brown #undef __FUNCT__
7754a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
776d763cef2SBarry Smith /*@C
777d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
778b5abc632SBarry Smith     where U_t = G(t,u).
779d763cef2SBarry Smith 
7803f9fe445SBarry Smith     Logically Collective on TS
781d763cef2SBarry Smith 
782d763cef2SBarry Smith     Input Parameters:
783d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
7840298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
785d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
786d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
7870298fd71SBarry Smith           function evaluation routine (may be NULL)
788d763cef2SBarry Smith 
789d763cef2SBarry Smith     Calling sequence of func:
79087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
791d763cef2SBarry Smith 
792d763cef2SBarry Smith +   t - current timestep
793d763cef2SBarry Smith .   u - input vector
794d763cef2SBarry Smith .   F - function vector
795d763cef2SBarry Smith -   ctx - [optional] user-defined function context
796d763cef2SBarry Smith 
797d763cef2SBarry Smith     Level: beginner
798d763cef2SBarry Smith 
799d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
800d763cef2SBarry Smith 
801d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
802d763cef2SBarry Smith @*/
803089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
804d763cef2SBarry Smith {
805089b2837SJed Brown   PetscErrorCode ierr;
806089b2837SJed Brown   SNES           snes;
8070298fd71SBarry Smith   Vec            ralloc = NULL;
80824989b8cSPeter Brune   DM             dm;
809d763cef2SBarry Smith 
810089b2837SJed Brown   PetscFunctionBegin;
8110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
812ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
81324989b8cSPeter Brune 
81424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
816089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
817e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
818e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
819e856ceecSJed Brown     r    = ralloc;
820e856ceecSJed Brown   }
821089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
822e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
823d763cef2SBarry Smith   PetscFunctionReturn(0);
824d763cef2SBarry Smith }
825d763cef2SBarry Smith 
8264a2ae208SSatish Balay #undef __FUNCT__
827ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
828ef20d060SBarry Smith /*@C
829abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
830ef20d060SBarry Smith 
831ef20d060SBarry Smith     Logically Collective on TS
832ef20d060SBarry Smith 
833ef20d060SBarry Smith     Input Parameters:
834ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
835ef20d060SBarry Smith .   f - routine for evaluating the solution
836ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8370298fd71SBarry Smith           function evaluation routine (may be NULL)
838ef20d060SBarry Smith 
839ef20d060SBarry Smith     Calling sequence of func:
840ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
841ef20d060SBarry Smith 
842ef20d060SBarry Smith +   t - current timestep
843ef20d060SBarry Smith .   u - output vector
844ef20d060SBarry Smith -   ctx - [optional] user-defined function context
845ef20d060SBarry Smith 
846abd5a294SJed Brown     Notes:
847abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
848abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
849abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
850abd5a294SJed Brown 
8514f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
852abd5a294SJed Brown 
853ef20d060SBarry Smith     Level: beginner
854ef20d060SBarry Smith 
855ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
856ef20d060SBarry Smith 
8579b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
858ef20d060SBarry Smith @*/
859ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
860ef20d060SBarry Smith {
861ef20d060SBarry Smith   PetscErrorCode ierr;
862ef20d060SBarry Smith   DM             dm;
863ef20d060SBarry Smith 
864ef20d060SBarry Smith   PetscFunctionBegin;
865ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
866ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
867ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
868ef20d060SBarry Smith   PetscFunctionReturn(0);
869ef20d060SBarry Smith }
870ef20d060SBarry Smith 
871ef20d060SBarry Smith #undef __FUNCT__
8729b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8739b7cd975SBarry Smith /*@C
8749b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8759b7cd975SBarry Smith 
8769b7cd975SBarry Smith     Logically Collective on TS
8779b7cd975SBarry Smith 
8789b7cd975SBarry Smith     Input Parameters:
8799b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
8809b7cd975SBarry Smith .   f - routine for evaluating the forcing function
8819b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
8820298fd71SBarry Smith           function evaluation routine (may be NULL)
8839b7cd975SBarry Smith 
8849b7cd975SBarry Smith     Calling sequence of func:
8859b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
8869b7cd975SBarry Smith 
8879b7cd975SBarry Smith +   t - current timestep
8889b7cd975SBarry Smith .   u - output vector
8899b7cd975SBarry Smith -   ctx - [optional] user-defined function context
8909b7cd975SBarry Smith 
8919b7cd975SBarry Smith     Notes:
8929b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
8939b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
8949b7cd975SBarry Smith 
8959b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
8969b7cd975SBarry Smith 
8979b7cd975SBarry Smith     Level: beginner
8989b7cd975SBarry Smith 
8999b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9009b7cd975SBarry Smith 
9019b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9029b7cd975SBarry Smith @*/
9039b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9049b7cd975SBarry Smith {
9059b7cd975SBarry Smith   PetscErrorCode ierr;
9069b7cd975SBarry Smith   DM             dm;
9079b7cd975SBarry Smith 
9089b7cd975SBarry Smith   PetscFunctionBegin;
9099b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9109b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9119b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9129b7cd975SBarry Smith   PetscFunctionReturn(0);
9139b7cd975SBarry Smith }
9149b7cd975SBarry Smith 
9159b7cd975SBarry Smith #undef __FUNCT__
9164a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
917d763cef2SBarry Smith /*@C
918d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
919b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
920d763cef2SBarry Smith 
9213f9fe445SBarry Smith    Logically Collective on TS
922d763cef2SBarry Smith 
923d763cef2SBarry Smith    Input Parameters:
924d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
925e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
926e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
927d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
928d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9290298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
930d763cef2SBarry Smith 
931d763cef2SBarry Smith    Calling sequence of func:
93287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
933d763cef2SBarry Smith 
934d763cef2SBarry Smith +  t - current timestep
935d763cef2SBarry Smith .  u - input vector
936e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
937e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
938d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
93994b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
940d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
941d763cef2SBarry Smith 
942d763cef2SBarry Smith    Notes:
94394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
944d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
945d763cef2SBarry Smith 
946d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
947d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
94856335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
949d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
950d763cef2SBarry Smith    throughout the global iterations.
951d763cef2SBarry Smith 
952d763cef2SBarry Smith    Level: beginner
953d763cef2SBarry Smith 
954d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
955d763cef2SBarry Smith 
956e1244c69SJed Brown .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse()
957d763cef2SBarry Smith 
958d763cef2SBarry Smith @*/
959e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
960d763cef2SBarry Smith {
961277b19d0SLisandro Dalcin   PetscErrorCode ierr;
962089b2837SJed Brown   SNES           snes;
96324989b8cSPeter Brune   DM             dm;
96424989b8cSPeter Brune   TSIJacobian    ijacobian;
965277b19d0SLisandro Dalcin 
966d763cef2SBarry Smith   PetscFunctionBegin;
9670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
968e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
969e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
970e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
971e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
972d763cef2SBarry Smith 
97324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
97424989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
975e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
976e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
977e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
978e1244c69SJed Brown   }
9790298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
980089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
9815f659677SPeter Brune   if (!ijacobian) {
982e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
9830e4ef248SJed Brown   }
984e5d3d808SBarry Smith   if (Amat) {
985e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
9860e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
987bbd56ea5SKarl Rupp 
988e5d3d808SBarry Smith     ts->Arhs = Amat;
9890e4ef248SJed Brown   }
990e5d3d808SBarry Smith   if (Pmat) {
991e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
9920e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
993bbd56ea5SKarl Rupp 
994e5d3d808SBarry Smith     ts->Brhs = Pmat;
9950e4ef248SJed Brown   }
996d763cef2SBarry Smith   PetscFunctionReturn(0);
997d763cef2SBarry Smith }
998d763cef2SBarry Smith 
999316643e7SJed Brown 
1000316643e7SJed Brown #undef __FUNCT__
1001316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1002316643e7SJed Brown /*@C
1003b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1004316643e7SJed Brown 
10053f9fe445SBarry Smith    Logically Collective on TS
1006316643e7SJed Brown 
1007316643e7SJed Brown    Input Parameters:
1008316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10090298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1010316643e7SJed Brown .  f   - the function evaluation routine
10110298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1012316643e7SJed Brown 
1013316643e7SJed Brown    Calling sequence of f:
1014316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1015316643e7SJed Brown 
1016316643e7SJed Brown +  t   - time at step/stage being solved
1017316643e7SJed Brown .  u   - state vector
1018316643e7SJed Brown .  u_t - time derivative of state vector
1019316643e7SJed Brown .  F   - function vector
1020316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1021316643e7SJed Brown 
1022316643e7SJed Brown    Important:
1023d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1024316643e7SJed Brown 
1025316643e7SJed Brown    Level: beginner
1026316643e7SJed Brown 
1027316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1028316643e7SJed Brown 
1029d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1030316643e7SJed Brown @*/
1031089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1032316643e7SJed Brown {
1033089b2837SJed Brown   PetscErrorCode ierr;
1034089b2837SJed Brown   SNES           snes;
10350298fd71SBarry Smith   Vec            resalloc = NULL;
103624989b8cSPeter Brune   DM             dm;
1037316643e7SJed Brown 
1038316643e7SJed Brown   PetscFunctionBegin;
10390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1040ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
104124989b8cSPeter Brune 
104224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
104324989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
104424989b8cSPeter Brune 
1045089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1046e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1047e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1048e856ceecSJed Brown     res  = resalloc;
1049e856ceecSJed Brown   }
1050089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1051e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1052089b2837SJed Brown   PetscFunctionReturn(0);
1053089b2837SJed Brown }
1054089b2837SJed Brown 
1055089b2837SJed Brown #undef __FUNCT__
1056089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1057089b2837SJed Brown /*@C
1058089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1059089b2837SJed Brown 
1060089b2837SJed Brown    Not Collective
1061089b2837SJed Brown 
1062089b2837SJed Brown    Input Parameter:
1063089b2837SJed Brown .  ts - the TS context
1064089b2837SJed Brown 
1065089b2837SJed Brown    Output Parameter:
10660298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10670298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10680298fd71SBarry Smith -  ctx - the function context (or NULL)
1069089b2837SJed Brown 
1070089b2837SJed Brown    Level: advanced
1071089b2837SJed Brown 
1072089b2837SJed Brown .keywords: TS, nonlinear, get, function
1073089b2837SJed Brown 
1074089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1075089b2837SJed Brown @*/
1076089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1077089b2837SJed Brown {
1078089b2837SJed Brown   PetscErrorCode ierr;
1079089b2837SJed Brown   SNES           snes;
108024989b8cSPeter Brune   DM             dm;
1081089b2837SJed Brown 
1082089b2837SJed Brown   PetscFunctionBegin;
1083089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1084089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10850298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
108624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1088089b2837SJed Brown   PetscFunctionReturn(0);
1089089b2837SJed Brown }
1090089b2837SJed Brown 
1091089b2837SJed Brown #undef __FUNCT__
1092089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1093089b2837SJed Brown /*@C
1094089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1095089b2837SJed Brown 
1096089b2837SJed Brown    Not Collective
1097089b2837SJed Brown 
1098089b2837SJed Brown    Input Parameter:
1099089b2837SJed Brown .  ts - the TS context
1100089b2837SJed Brown 
1101089b2837SJed Brown    Output Parameter:
11020298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11030298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11040298fd71SBarry Smith -  ctx - the function context (or NULL)
1105089b2837SJed Brown 
1106089b2837SJed Brown    Level: advanced
1107089b2837SJed Brown 
1108089b2837SJed Brown .keywords: TS, nonlinear, get, function
1109089b2837SJed Brown 
1110089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1111089b2837SJed Brown @*/
1112089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1113089b2837SJed Brown {
1114089b2837SJed Brown   PetscErrorCode ierr;
1115089b2837SJed Brown   SNES           snes;
111624989b8cSPeter Brune   DM             dm;
1117089b2837SJed Brown 
1118089b2837SJed Brown   PetscFunctionBegin;
1119089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1120089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11210298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
112224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
112324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1124316643e7SJed Brown   PetscFunctionReturn(0);
1125316643e7SJed Brown }
1126316643e7SJed Brown 
1127316643e7SJed Brown #undef __FUNCT__
1128316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1129316643e7SJed Brown /*@C
1130a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1131a4f0a591SBarry Smith         you provided with TSSetIFunction().
1132316643e7SJed Brown 
11333f9fe445SBarry Smith    Logically Collective on TS
1134316643e7SJed Brown 
1135316643e7SJed Brown    Input Parameters:
1136316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1137e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1138e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1139316643e7SJed Brown .  f   - the Jacobian evaluation routine
11400298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1141316643e7SJed Brown 
1142316643e7SJed Brown    Calling sequence of f:
1143e5d3d808SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx);
1144316643e7SJed Brown 
1145316643e7SJed Brown +  t    - time at step/stage being solved
11461b4a444bSJed Brown .  U    - state vector
11471b4a444bSJed Brown .  U_t  - time derivative of state vector
1148316643e7SJed Brown .  a    - shift
1149e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1150e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1151316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1152316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1153316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1154316643e7SJed Brown 
1155316643e7SJed Brown    Notes:
1156e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1157316643e7SJed Brown 
1158a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1159b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1160a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1161a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1162a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1163a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1164a4f0a591SBarry Smith 
1165316643e7SJed Brown    Level: beginner
1166316643e7SJed Brown 
1167316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1168316643e7SJed Brown 
11698d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault()
1170316643e7SJed Brown 
1171316643e7SJed Brown @*/
1172e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1173316643e7SJed Brown {
1174316643e7SJed Brown   PetscErrorCode ierr;
1175089b2837SJed Brown   SNES           snes;
117624989b8cSPeter Brune   DM             dm;
1177316643e7SJed Brown 
1178316643e7SJed Brown   PetscFunctionBegin;
11790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1180e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1181e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1182e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1183e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
118424989b8cSPeter Brune 
118524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
118724989b8cSPeter Brune 
1188089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1189e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1190316643e7SJed Brown   PetscFunctionReturn(0);
1191316643e7SJed Brown }
1192316643e7SJed Brown 
11934a2ae208SSatish Balay #undef __FUNCT__
1194e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1195e1244c69SJed Brown /*@
1196e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1197e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1198e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1199e1244c69SJed Brown    not been changed by the TS.
1200e1244c69SJed Brown 
1201e1244c69SJed Brown    Logically Collective
1202e1244c69SJed Brown 
1203e1244c69SJed Brown    Input Arguments:
1204e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1205e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1206e1244c69SJed Brown 
1207e1244c69SJed Brown    Level: intermediate
1208e1244c69SJed Brown 
1209e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1210e1244c69SJed Brown @*/
1211e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1212e1244c69SJed Brown {
1213e1244c69SJed Brown   PetscFunctionBegin;
1214e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1215e1244c69SJed Brown   PetscFunctionReturn(0);
1216e1244c69SJed Brown }
1217e1244c69SJed Brown 
1218e1244c69SJed Brown #undef __FUNCT__
121955849f57SBarry Smith #define __FUNCT__ "TSLoad"
122055849f57SBarry Smith /*@C
122155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
122255849f57SBarry Smith 
122355849f57SBarry Smith   Collective on PetscViewer
122455849f57SBarry Smith 
122555849f57SBarry Smith   Input Parameters:
122655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
122755849f57SBarry Smith            some related function before a call to TSLoad().
122855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
122955849f57SBarry Smith 
123055849f57SBarry Smith    Level: intermediate
123155849f57SBarry Smith 
123255849f57SBarry Smith   Notes:
123355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
123455849f57SBarry Smith 
123555849f57SBarry Smith   Notes for advanced users:
123655849f57SBarry Smith   Most users should not need to know the details of the binary storage
123755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
123855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
123955849f57SBarry Smith   format is
124055849f57SBarry Smith .vb
124155849f57SBarry Smith      has not yet been determined
124255849f57SBarry Smith .ve
124355849f57SBarry Smith 
124455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
124555849f57SBarry Smith @*/
1246f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
124755849f57SBarry Smith {
124855849f57SBarry Smith   PetscErrorCode ierr;
124955849f57SBarry Smith   PetscBool      isbinary;
125055849f57SBarry Smith   PetscInt       classid;
125155849f57SBarry Smith   char           type[256];
12522d53ad75SBarry Smith   DMTS           sdm;
1253ad6bc421SBarry Smith   DM             dm;
125455849f57SBarry Smith 
125555849f57SBarry Smith   PetscFunctionBegin;
1256f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
125755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
125855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
125955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
126055849f57SBarry Smith 
126155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1262ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
126355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1264f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1265f2c2a1b9SBarry Smith   if (ts->ops->load) {
1266f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1267f2c2a1b9SBarry Smith   }
1268ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1269ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1270ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1271f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1272f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12732d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12742d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
127555849f57SBarry Smith   PetscFunctionReturn(0);
127655849f57SBarry Smith }
127755849f57SBarry Smith 
12789804daf3SBarry Smith #include <petscdraw.h>
1279f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1280f05ece33SBarry Smith #include <petscviewerams.h>
1281f05ece33SBarry Smith #endif
128255849f57SBarry Smith #undef __FUNCT__
12834a2ae208SSatish Balay #define __FUNCT__ "TSView"
12847e2c5f70SBarry Smith /*@C
1285d763cef2SBarry Smith     TSView - Prints the TS data structure.
1286d763cef2SBarry Smith 
12874c49b128SBarry Smith     Collective on TS
1288d763cef2SBarry Smith 
1289d763cef2SBarry Smith     Input Parameters:
1290d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1291d763cef2SBarry Smith -   viewer - visualization context
1292d763cef2SBarry Smith 
1293d763cef2SBarry Smith     Options Database Key:
1294d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1295d763cef2SBarry Smith 
1296d763cef2SBarry Smith     Notes:
1297d763cef2SBarry Smith     The available visualization contexts include
1298b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1299b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1300d763cef2SBarry Smith          output where only the first processor opens
1301d763cef2SBarry Smith          the file.  All other processors send their
1302d763cef2SBarry Smith          data to the first processor to print.
1303d763cef2SBarry Smith 
1304d763cef2SBarry Smith     The user can open an alternative visualization context with
1305b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1306d763cef2SBarry Smith 
1307d763cef2SBarry Smith     Level: beginner
1308d763cef2SBarry Smith 
1309d763cef2SBarry Smith .keywords: TS, timestep, view
1310d763cef2SBarry Smith 
1311b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1312d763cef2SBarry Smith @*/
13137087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1314d763cef2SBarry Smith {
1315dfbe8321SBarry Smith   PetscErrorCode ierr;
131619fd82e9SBarry Smith   TSType         type;
13172b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13182d53ad75SBarry Smith   DMTS           sdm;
1319f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1320f05ece33SBarry Smith   PetscBool      isams;
1321f05ece33SBarry Smith #endif
1322d763cef2SBarry Smith 
1323d763cef2SBarry Smith   PetscFunctionBegin;
13240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13253050cee2SBarry Smith   if (!viewer) {
1326ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13273050cee2SBarry Smith   }
13280700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1329c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1330fd16b177SBarry Smith 
1331251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1332251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
133355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13342b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1335f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1336f05ece33SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERAMS,&isams);CHKERRQ(ierr);
1337f05ece33SBarry Smith #endif
133832077d6dSBarry Smith   if (iascii) {
1339317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
134077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1341a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1342d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13435ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1344c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1345d763cef2SBarry Smith     }
13465ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1347193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13482d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13492d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1350d52bd9f3SBarry Smith     if (ts->ops->view) {
1351d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1352d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1353d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1354d52bd9f3SBarry Smith     }
13550f5bd95cSBarry Smith   } else if (isstring) {
1356a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1357b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
135855849f57SBarry Smith   } else if (isbinary) {
135955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
136055849f57SBarry Smith     MPI_Comm    comm;
136155849f57SBarry Smith     PetscMPIInt rank;
136255849f57SBarry Smith     char        type[256];
136355849f57SBarry Smith 
136455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
136555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
136655849f57SBarry Smith     if (!rank) {
136755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
136855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
136955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
137055849f57SBarry Smith     }
137155849f57SBarry Smith     if (ts->ops->view) {
137255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
137355849f57SBarry Smith     }
1374f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1375f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13762d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13772d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13782b0a91c0SBarry Smith   } else if (isdraw) {
13792b0a91c0SBarry Smith     PetscDraw draw;
13802b0a91c0SBarry Smith     char      str[36];
138189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13822b0a91c0SBarry Smith 
13832b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13842b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
13852b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
13862b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
13870298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
138889fd9fafSBarry Smith     bottom = y - h;
13892b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
13902b0a91c0SBarry Smith     if (ts->ops->view) {
13912b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13922b0a91c0SBarry Smith     }
13932b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1394f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1395f05ece33SBarry Smith   } else if (isams) {
1396f05ece33SBarry Smith     if (((PetscObject)ts)->amsmem == -1) {
1397f05ece33SBarry Smith       ierr = PetscObjectViewAMS((PetscObject)ts,viewer);CHKERRQ(ierr);
1398f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_take_access,(((PetscObject)ts)->amsmem));
1399f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time step",&ts->steps,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1400f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time",&ts->ptime,1,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1401f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_grant_access,(((PetscObject)ts)->amsmem));
1402d763cef2SBarry Smith     }
14030acecf5bSBarry Smith     if (ts->ops->view) {
14040acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14050acecf5bSBarry Smith     }
1406f05ece33SBarry Smith #endif
1407f05ece33SBarry Smith   }
1408f05ece33SBarry Smith 
1409b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1410251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1411b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1412d763cef2SBarry Smith   PetscFunctionReturn(0);
1413d763cef2SBarry Smith }
1414d763cef2SBarry Smith 
1415d763cef2SBarry Smith 
14164a2ae208SSatish Balay #undef __FUNCT__
14174a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1418b07ff414SBarry Smith /*@
1419d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1420d763cef2SBarry Smith    the timesteppers.
1421d763cef2SBarry Smith 
14223f9fe445SBarry Smith    Logically Collective on TS
1423d763cef2SBarry Smith 
1424d763cef2SBarry Smith    Input Parameters:
1425d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1426d763cef2SBarry Smith -  usrP - optional user context
1427d763cef2SBarry Smith 
1428d763cef2SBarry Smith    Level: intermediate
1429d763cef2SBarry Smith 
1430d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1431d763cef2SBarry Smith 
1432d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1433d763cef2SBarry Smith @*/
14347087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1435d763cef2SBarry Smith {
1436d763cef2SBarry Smith   PetscFunctionBegin;
14370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1438d763cef2SBarry Smith   ts->user = usrP;
1439d763cef2SBarry Smith   PetscFunctionReturn(0);
1440d763cef2SBarry Smith }
1441d763cef2SBarry Smith 
14424a2ae208SSatish Balay #undef __FUNCT__
14434a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1444b07ff414SBarry Smith /*@
1445d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1446d763cef2SBarry Smith     timestepper.
1447d763cef2SBarry Smith 
1448d763cef2SBarry Smith     Not Collective
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith     Input Parameter:
1451d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1452d763cef2SBarry Smith 
1453d763cef2SBarry Smith     Output Parameter:
1454d763cef2SBarry Smith .   usrP - user context
1455d763cef2SBarry Smith 
1456d763cef2SBarry Smith     Level: intermediate
1457d763cef2SBarry Smith 
1458d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1459d763cef2SBarry Smith 
1460d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1461d763cef2SBarry Smith @*/
1462e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1463d763cef2SBarry Smith {
1464d763cef2SBarry Smith   PetscFunctionBegin;
14650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1466e71120c6SJed Brown   *(void**)usrP = ts->user;
1467d763cef2SBarry Smith   PetscFunctionReturn(0);
1468d763cef2SBarry Smith }
1469d763cef2SBarry Smith 
14704a2ae208SSatish Balay #undef __FUNCT__
14714a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1472d763cef2SBarry Smith /*@
1473b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1474d763cef2SBarry Smith 
1475d763cef2SBarry Smith    Not Collective
1476d763cef2SBarry Smith 
1477d763cef2SBarry Smith    Input Parameter:
1478d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith    Output Parameter:
1481b8123daeSJed Brown .  iter - number of steps completed so far
1482d763cef2SBarry Smith 
1483d763cef2SBarry Smith    Level: intermediate
1484d763cef2SBarry Smith 
1485d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1486b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1487d763cef2SBarry Smith @*/
14887087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1489d763cef2SBarry Smith {
1490d763cef2SBarry Smith   PetscFunctionBegin;
14910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14924482741eSBarry Smith   PetscValidIntPointer(iter,2);
1493d763cef2SBarry Smith   *iter = ts->steps;
1494d763cef2SBarry Smith   PetscFunctionReturn(0);
1495d763cef2SBarry Smith }
1496d763cef2SBarry Smith 
14974a2ae208SSatish Balay #undef __FUNCT__
14984a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1499d763cef2SBarry Smith /*@
1500d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1501d763cef2SBarry Smith    as well as the initial time.
1502d763cef2SBarry Smith 
15033f9fe445SBarry Smith    Logically Collective on TS
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith    Input Parameters:
1506d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1507d763cef2SBarry Smith .  initial_time - the initial time
1508d763cef2SBarry Smith -  time_step - the size of the timestep
1509d763cef2SBarry Smith 
1510d763cef2SBarry Smith    Level: intermediate
1511d763cef2SBarry Smith 
1512d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1515d763cef2SBarry Smith @*/
15167087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1517d763cef2SBarry Smith {
1518e144a568SJed Brown   PetscErrorCode ierr;
1519e144a568SJed Brown 
1520d763cef2SBarry Smith   PetscFunctionBegin;
15210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1522e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1523d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1524d763cef2SBarry Smith   PetscFunctionReturn(0);
1525d763cef2SBarry Smith }
1526d763cef2SBarry Smith 
15274a2ae208SSatish Balay #undef __FUNCT__
15284a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1529d763cef2SBarry Smith /*@
1530d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1531d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1532d763cef2SBarry Smith 
15333f9fe445SBarry Smith    Logically Collective on TS
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith    Input Parameters:
1536d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1537d763cef2SBarry Smith -  time_step - the size of the timestep
1538d763cef2SBarry Smith 
1539d763cef2SBarry Smith    Level: intermediate
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith .keywords: TS, set, timestep
1544d763cef2SBarry Smith @*/
15457087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1546d763cef2SBarry Smith {
1547d763cef2SBarry Smith   PetscFunctionBegin;
15480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1549c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1550d763cef2SBarry Smith   ts->time_step      = time_step;
155131748224SBarry Smith   ts->time_step_orig = time_step;
1552d763cef2SBarry Smith   PetscFunctionReturn(0);
1553d763cef2SBarry Smith }
1554d763cef2SBarry Smith 
15554a2ae208SSatish Balay #undef __FUNCT__
1556a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1557a43b19c4SJed Brown /*@
155849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
155949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
156049354f04SShri Abhyankar      or just return at the final time TS computed.
1561a43b19c4SJed Brown 
1562a43b19c4SJed Brown   Logically Collective on TS
1563a43b19c4SJed Brown 
1564a43b19c4SJed Brown    Input Parameter:
1565a43b19c4SJed Brown +   ts - the time-step context
156649354f04SShri Abhyankar -   eftopt - exact final time option
1567a43b19c4SJed Brown 
1568a43b19c4SJed Brown    Level: beginner
1569a43b19c4SJed Brown 
1570a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1571a43b19c4SJed Brown @*/
157249354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1573a43b19c4SJed Brown {
1574a43b19c4SJed Brown   PetscFunctionBegin;
1575a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
157649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
157749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1578a43b19c4SJed Brown   PetscFunctionReturn(0);
1579a43b19c4SJed Brown }
1580a43b19c4SJed Brown 
1581a43b19c4SJed Brown #undef __FUNCT__
15824a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1583d763cef2SBarry Smith /*@
1584d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1585d763cef2SBarry Smith 
1586d763cef2SBarry Smith    Not Collective
1587d763cef2SBarry Smith 
1588d763cef2SBarry Smith    Input Parameter:
1589d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1590d763cef2SBarry Smith 
1591d763cef2SBarry Smith    Output Parameter:
1592d763cef2SBarry Smith .  dt - the current timestep size
1593d763cef2SBarry Smith 
1594d763cef2SBarry Smith    Level: intermediate
1595d763cef2SBarry Smith 
1596d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1597d763cef2SBarry Smith 
1598d763cef2SBarry Smith .keywords: TS, get, timestep
1599d763cef2SBarry Smith @*/
16007087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1601d763cef2SBarry Smith {
1602d763cef2SBarry Smith   PetscFunctionBegin;
16030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1604f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1605d763cef2SBarry Smith   *dt = ts->time_step;
1606d763cef2SBarry Smith   PetscFunctionReturn(0);
1607d763cef2SBarry Smith }
1608d763cef2SBarry Smith 
16094a2ae208SSatish Balay #undef __FUNCT__
16104a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1611d8e5e3e6SSatish Balay /*@
1612d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1613d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1614d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1615d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1616d763cef2SBarry Smith 
1617d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1618d763cef2SBarry Smith 
1619d763cef2SBarry Smith    Input Parameter:
1620d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1621d763cef2SBarry Smith 
1622d763cef2SBarry Smith    Output Parameter:
1623d763cef2SBarry Smith .  v - the vector containing the solution
1624d763cef2SBarry Smith 
1625d763cef2SBarry Smith    Level: intermediate
1626d763cef2SBarry Smith 
1627d763cef2SBarry Smith .seealso: TSGetTimeStep()
1628d763cef2SBarry Smith 
1629d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1630d763cef2SBarry Smith @*/
16317087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1632d763cef2SBarry Smith {
1633d763cef2SBarry Smith   PetscFunctionBegin;
16340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16354482741eSBarry Smith   PetscValidPointer(v,2);
16368737fe31SLisandro Dalcin   *v = ts->vec_sol;
1637d763cef2SBarry Smith   PetscFunctionReturn(0);
1638d763cef2SBarry Smith }
1639d763cef2SBarry Smith 
1640bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
16414a2ae208SSatish Balay #undef __FUNCT__
1642bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1643d8e5e3e6SSatish Balay /*@
1644bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1645d763cef2SBarry Smith 
1646bdad233fSMatthew Knepley   Not collective
1647d763cef2SBarry Smith 
1648bdad233fSMatthew Knepley   Input Parameters:
1649bdad233fSMatthew Knepley + ts   - The TS
1650bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1651d763cef2SBarry Smith .vb
16520910c330SBarry Smith          U_t - A U = 0      (linear)
16530910c330SBarry Smith          U_t - A(t) U = 0   (linear)
16540910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1655d763cef2SBarry Smith .ve
1656d763cef2SBarry Smith 
1657d763cef2SBarry Smith    Level: beginner
1658d763cef2SBarry Smith 
1659bdad233fSMatthew Knepley .keywords: TS, problem type
1660bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1661d763cef2SBarry Smith @*/
16627087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1663a7cc72afSBarry Smith {
16649e2a6581SJed Brown   PetscErrorCode ierr;
16659e2a6581SJed Brown 
1666d763cef2SBarry Smith   PetscFunctionBegin;
16670700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1668bdad233fSMatthew Knepley   ts->problem_type = type;
16699e2a6581SJed Brown   if (type == TS_LINEAR) {
16709e2a6581SJed Brown     SNES snes;
16719e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
16729e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
16739e2a6581SJed Brown   }
1674d763cef2SBarry Smith   PetscFunctionReturn(0);
1675d763cef2SBarry Smith }
1676d763cef2SBarry Smith 
1677bdad233fSMatthew Knepley #undef __FUNCT__
1678bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1679bdad233fSMatthew Knepley /*@C
1680bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1681bdad233fSMatthew Knepley 
1682bdad233fSMatthew Knepley   Not collective
1683bdad233fSMatthew Knepley 
1684bdad233fSMatthew Knepley   Input Parameter:
1685bdad233fSMatthew Knepley . ts   - The TS
1686bdad233fSMatthew Knepley 
1687bdad233fSMatthew Knepley   Output Parameter:
1688bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1689bdad233fSMatthew Knepley .vb
1690089b2837SJed Brown          M U_t = A U
1691089b2837SJed Brown          M(t) U_t = A(t) U
1692b5abc632SBarry Smith          F(t,U,U_t)
1693bdad233fSMatthew Knepley .ve
1694bdad233fSMatthew Knepley 
1695bdad233fSMatthew Knepley    Level: beginner
1696bdad233fSMatthew Knepley 
1697bdad233fSMatthew Knepley .keywords: TS, problem type
1698bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1699bdad233fSMatthew Knepley @*/
17007087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1701a7cc72afSBarry Smith {
1702bdad233fSMatthew Knepley   PetscFunctionBegin;
17030700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17044482741eSBarry Smith   PetscValidIntPointer(type,2);
1705bdad233fSMatthew Knepley   *type = ts->problem_type;
1706bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1707bdad233fSMatthew Knepley }
1708d763cef2SBarry Smith 
17094a2ae208SSatish Balay #undef __FUNCT__
17104a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1711d763cef2SBarry Smith /*@
1712d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1713d763cef2SBarry Smith    of a timestepper.
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith    Collective on TS
1716d763cef2SBarry Smith 
1717d763cef2SBarry Smith    Input Parameter:
1718d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1719d763cef2SBarry Smith 
1720d763cef2SBarry Smith    Notes:
1721d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1722d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1723d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1724d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1725d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1726d763cef2SBarry Smith 
1727d763cef2SBarry Smith    Level: advanced
1728d763cef2SBarry Smith 
1729d763cef2SBarry Smith .keywords: TS, timestep, setup
1730d763cef2SBarry Smith 
1731d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1732d763cef2SBarry Smith @*/
17337087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1734d763cef2SBarry Smith {
1735dfbe8321SBarry Smith   PetscErrorCode ierr;
17366c6b9e74SPeter Brune   DM             dm;
17376c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17386c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
17396c6b9e74SPeter Brune   TSIJacobian    ijac;
17406c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1741d763cef2SBarry Smith 
1742d763cef2SBarry Smith   PetscFunctionBegin;
17430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1744277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1745277b19d0SLisandro Dalcin 
17467adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
17479596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1748d763cef2SBarry Smith   }
1749277b19d0SLisandro Dalcin 
1750277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1751277b19d0SLisandro Dalcin 
1752552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
17531c3436cfSJed Brown 
1754e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1755e1244c69SJed Brown     Mat Amat,Pmat;
1756e1244c69SJed Brown     SNES snes;
1757e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1758e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1759e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1760e1244c69SJed Brown      * have displaced the RHS matrix */
1761e1244c69SJed Brown     if (Amat == ts->Arhs) {
1762e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1763e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1764e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1765e1244c69SJed Brown     }
1766e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1767e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1768e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1769e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1770e1244c69SJed Brown     }
1771e1244c69SJed Brown   }
1772e1244c69SJed Brown 
1773277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1774000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1775277b19d0SLisandro Dalcin   }
1776277b19d0SLisandro Dalcin 
17776c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
17786c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
17796c6b9e74SPeter Brune    */
17806c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
17810298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
17826c6b9e74SPeter Brune   if (!func) {
17836c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
17846c6b9e74SPeter Brune   }
17856c6b9e74SPeter 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.
17866c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
17876c6b9e74SPeter Brune    */
17880298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
17890298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
17900298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
17916c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
17926c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
17936c6b9e74SPeter Brune   }
1794277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1795277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1796277b19d0SLisandro Dalcin }
1797277b19d0SLisandro Dalcin 
1798277b19d0SLisandro Dalcin #undef __FUNCT__
1799277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1800277b19d0SLisandro Dalcin /*@
1801277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1802277b19d0SLisandro Dalcin 
1803277b19d0SLisandro Dalcin    Collective on TS
1804277b19d0SLisandro Dalcin 
1805277b19d0SLisandro Dalcin    Input Parameter:
1806277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1807277b19d0SLisandro Dalcin 
1808277b19d0SLisandro Dalcin    Level: beginner
1809277b19d0SLisandro Dalcin 
1810277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1811277b19d0SLisandro Dalcin 
1812277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1813277b19d0SLisandro Dalcin @*/
1814277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1815277b19d0SLisandro Dalcin {
1816277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1817277b19d0SLisandro Dalcin 
1818277b19d0SLisandro Dalcin   PetscFunctionBegin;
1819277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1820277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1821277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1822277b19d0SLisandro Dalcin   }
1823277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1824bbd56ea5SKarl Rupp 
18254e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
18264e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1827214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
18286bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1829e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1830e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
183138637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1832bbd56ea5SKarl Rupp 
1833277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1834d763cef2SBarry Smith   PetscFunctionReturn(0);
1835d763cef2SBarry Smith }
1836d763cef2SBarry Smith 
18374a2ae208SSatish Balay #undef __FUNCT__
18384a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1839d8e5e3e6SSatish Balay /*@
1840d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1841d763cef2SBarry Smith    with TSCreate().
1842d763cef2SBarry Smith 
1843d763cef2SBarry Smith    Collective on TS
1844d763cef2SBarry Smith 
1845d763cef2SBarry Smith    Input Parameter:
1846d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith    Level: beginner
1849d763cef2SBarry Smith 
1850d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1851d763cef2SBarry Smith 
1852d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1853d763cef2SBarry Smith @*/
18546bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1855d763cef2SBarry Smith {
18566849ba73SBarry Smith   PetscErrorCode ierr;
1857d763cef2SBarry Smith 
1858d763cef2SBarry Smith   PetscFunctionBegin;
18596bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
18606bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
18616bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1862d763cef2SBarry Smith 
18636bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1864277b19d0SLisandro Dalcin 
1865be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
1866f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*ts);CHKERRQ(ierr);
18676bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
18686d4c513bSLisandro Dalcin 
186984df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
18706bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
18716bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
18726bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
18736d4c513bSLisandro Dalcin 
1874a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1875d763cef2SBarry Smith   PetscFunctionReturn(0);
1876d763cef2SBarry Smith }
1877d763cef2SBarry Smith 
18784a2ae208SSatish Balay #undef __FUNCT__
18794a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1880d8e5e3e6SSatish Balay /*@
1881d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1882d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1883d763cef2SBarry Smith 
1884d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1885d763cef2SBarry Smith 
1886d763cef2SBarry Smith    Input Parameter:
1887d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1888d763cef2SBarry Smith 
1889d763cef2SBarry Smith    Output Parameter:
1890d763cef2SBarry Smith .  snes - the nonlinear solver context
1891d763cef2SBarry Smith 
1892d763cef2SBarry Smith    Notes:
1893d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1894d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
189594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1896d763cef2SBarry Smith 
1897d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
18980298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1899d763cef2SBarry Smith 
1900d763cef2SBarry Smith    Level: beginner
1901d763cef2SBarry Smith 
1902d763cef2SBarry Smith .keywords: timestep, get, SNES
1903d763cef2SBarry Smith @*/
19047087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1905d763cef2SBarry Smith {
1906d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1907d372ba47SLisandro Dalcin 
1908d763cef2SBarry Smith   PetscFunctionBegin;
19090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19104482741eSBarry Smith   PetscValidPointer(snes,2);
1911d372ba47SLisandro Dalcin   if (!ts->snes) {
1912ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
19130298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1914d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1915d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1916496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
19179e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
19189e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
19199e2a6581SJed Brown     }
1920d372ba47SLisandro Dalcin   }
1921d763cef2SBarry Smith   *snes = ts->snes;
1922d763cef2SBarry Smith   PetscFunctionReturn(0);
1923d763cef2SBarry Smith }
1924d763cef2SBarry Smith 
19254a2ae208SSatish Balay #undef __FUNCT__
1926deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1927deb2cd25SJed Brown /*@
1928deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1929deb2cd25SJed Brown 
1930deb2cd25SJed Brown    Collective
1931deb2cd25SJed Brown 
1932deb2cd25SJed Brown    Input Parameter:
1933deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1934deb2cd25SJed Brown -  snes - the nonlinear solver context
1935deb2cd25SJed Brown 
1936deb2cd25SJed Brown    Notes:
1937deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1938deb2cd25SJed Brown 
1939deb2cd25SJed Brown    Level: developer
1940deb2cd25SJed Brown 
1941deb2cd25SJed Brown .keywords: timestep, set, SNES
1942deb2cd25SJed Brown @*/
1943deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1944deb2cd25SJed Brown {
1945deb2cd25SJed Brown   PetscErrorCode ierr;
1946740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1947deb2cd25SJed Brown 
1948deb2cd25SJed Brown   PetscFunctionBegin;
1949deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1950deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1951deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1952deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1953bbd56ea5SKarl Rupp 
1954deb2cd25SJed Brown   ts->snes = snes;
1955bbd56ea5SKarl Rupp 
19560298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19570298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1958740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
19590298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1960740132f1SEmil Constantinescu   }
1961deb2cd25SJed Brown   PetscFunctionReturn(0);
1962deb2cd25SJed Brown }
1963deb2cd25SJed Brown 
1964deb2cd25SJed Brown #undef __FUNCT__
196594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1966d8e5e3e6SSatish Balay /*@
196794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1968d763cef2SBarry Smith    a TS (timestepper) context.
1969d763cef2SBarry Smith 
197094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1971d763cef2SBarry Smith 
1972d763cef2SBarry Smith    Input Parameter:
1973d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1974d763cef2SBarry Smith 
1975d763cef2SBarry Smith    Output Parameter:
197694b7f48cSBarry Smith .  ksp - the nonlinear solver context
1977d763cef2SBarry Smith 
1978d763cef2SBarry Smith    Notes:
197994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1980d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1981d763cef2SBarry Smith    KSP and PC contexts as well.
1982d763cef2SBarry Smith 
198394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
19840298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith    Level: beginner
1987d763cef2SBarry Smith 
198894b7f48cSBarry Smith .keywords: timestep, get, KSP
1989d763cef2SBarry Smith @*/
19907087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1991d763cef2SBarry Smith {
1992d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1993089b2837SJed Brown   SNES           snes;
1994d372ba47SLisandro Dalcin 
1995d763cef2SBarry Smith   PetscFunctionBegin;
19960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19974482741eSBarry Smith   PetscValidPointer(ksp,2);
199817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1999e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2000089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2001089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2002d763cef2SBarry Smith   PetscFunctionReturn(0);
2003d763cef2SBarry Smith }
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2006d763cef2SBarry Smith 
20074a2ae208SSatish Balay #undef __FUNCT__
2008adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2009adb62b0dSMatthew Knepley /*@
2010adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2011adb62b0dSMatthew Knepley    maximum time for iteration.
2012adb62b0dSMatthew Knepley 
20133f9fe445SBarry Smith    Not Collective
2014adb62b0dSMatthew Knepley 
2015adb62b0dSMatthew Knepley    Input Parameters:
2016adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
20170298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
20180298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2019adb62b0dSMatthew Knepley 
2020adb62b0dSMatthew Knepley    Level: intermediate
2021adb62b0dSMatthew Knepley 
2022adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2023adb62b0dSMatthew Knepley @*/
20247087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2025adb62b0dSMatthew Knepley {
2026adb62b0dSMatthew Knepley   PetscFunctionBegin;
20270700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2028abc0a331SBarry Smith   if (maxsteps) {
20294482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2030adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2031adb62b0dSMatthew Knepley   }
2032abc0a331SBarry Smith   if (maxtime) {
20334482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2034adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2035adb62b0dSMatthew Knepley   }
2036adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2037adb62b0dSMatthew Knepley }
2038adb62b0dSMatthew Knepley 
2039adb62b0dSMatthew Knepley #undef __FUNCT__
20404a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2041d763cef2SBarry Smith /*@
2042d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2043d763cef2SBarry Smith    maximum time for iteration.
2044d763cef2SBarry Smith 
20453f9fe445SBarry Smith    Logically Collective on TS
2046d763cef2SBarry Smith 
2047d763cef2SBarry Smith    Input Parameters:
2048d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2049d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2050d763cef2SBarry Smith -  maxtime - final time to iterate to
2051d763cef2SBarry Smith 
2052d763cef2SBarry Smith    Options Database Keys:
2053d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
20543bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2055d763cef2SBarry Smith 
2056d763cef2SBarry Smith    Notes:
2057d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2058d763cef2SBarry Smith 
2059d763cef2SBarry Smith    Level: intermediate
2060d763cef2SBarry Smith 
2061d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2062a43b19c4SJed Brown 
2063a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2064d763cef2SBarry Smith @*/
20657087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2066d763cef2SBarry Smith {
2067d763cef2SBarry Smith   PetscFunctionBegin;
20680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2069c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2070c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
207139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
207239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2073d763cef2SBarry Smith   PetscFunctionReturn(0);
2074d763cef2SBarry Smith }
2075d763cef2SBarry Smith 
20764a2ae208SSatish Balay #undef __FUNCT__
20774a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2078d763cef2SBarry Smith /*@
2079d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2080d763cef2SBarry Smith    for use by the TS routines.
2081d763cef2SBarry Smith 
20823f9fe445SBarry Smith    Logically Collective on TS and Vec
2083d763cef2SBarry Smith 
2084d763cef2SBarry Smith    Input Parameters:
2085d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
20860910c330SBarry Smith -  u - the solution vector
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith    Level: beginner
2089d763cef2SBarry Smith 
2090d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2091d763cef2SBarry Smith @*/
20920910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2093d763cef2SBarry Smith {
20948737fe31SLisandro Dalcin   PetscErrorCode ierr;
2095496e6a7aSJed Brown   DM             dm;
20968737fe31SLisandro Dalcin 
2097d763cef2SBarry Smith   PetscFunctionBegin;
20980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20990910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
21000910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
21016bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2102bbd56ea5SKarl Rupp 
21030910c330SBarry Smith   ts->vec_sol = u;
2104bbd56ea5SKarl Rupp 
2105496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
21060910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2107d763cef2SBarry Smith   PetscFunctionReturn(0);
2108d763cef2SBarry Smith }
2109d763cef2SBarry Smith 
2110e74ef692SMatthew Knepley #undef __FUNCT__
2111e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2112ac226902SBarry Smith /*@C
2113000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
21143f2090d5SJed Brown   called once at the beginning of each time step.
2115000e7ae3SMatthew Knepley 
21163f9fe445SBarry Smith   Logically Collective on TS
2117000e7ae3SMatthew Knepley 
2118000e7ae3SMatthew Knepley   Input Parameters:
2119000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2120000e7ae3SMatthew Knepley - func - The function
2121000e7ae3SMatthew Knepley 
2122000e7ae3SMatthew Knepley   Calling sequence of func:
2123000e7ae3SMatthew Knepley . func (TS ts);
2124000e7ae3SMatthew Knepley 
2125000e7ae3SMatthew Knepley   Level: intermediate
2126000e7ae3SMatthew Knepley 
2127b8123daeSJed Brown   Note:
2128b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2129b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2130b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2131b8123daeSJed Brown 
2132000e7ae3SMatthew Knepley .keywords: TS, timestep
2133b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2134000e7ae3SMatthew Knepley @*/
21357087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2136000e7ae3SMatthew Knepley {
2137000e7ae3SMatthew Knepley   PetscFunctionBegin;
21380700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2139ae60f76fSBarry Smith   ts->prestep = func;
2140000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2141000e7ae3SMatthew Knepley }
2142000e7ae3SMatthew Knepley 
2143e74ef692SMatthew Knepley #undef __FUNCT__
21443f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
214509ee8438SJed Brown /*@
21463f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
21473f2090d5SJed Brown 
21483f2090d5SJed Brown   Collective on TS
21493f2090d5SJed Brown 
21503f2090d5SJed Brown   Input Parameters:
21513f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21523f2090d5SJed Brown 
21533f2090d5SJed Brown   Notes:
21543f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
21553f2090d5SJed Brown   so most users would not generally call this routine themselves.
21563f2090d5SJed Brown 
21573f2090d5SJed Brown   Level: developer
21583f2090d5SJed Brown 
21593f2090d5SJed Brown .keywords: TS, timestep
2160b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
21613f2090d5SJed Brown @*/
21627087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
21633f2090d5SJed Brown {
21643f2090d5SJed Brown   PetscErrorCode ierr;
21653f2090d5SJed Brown 
21663f2090d5SJed Brown   PetscFunctionBegin;
21670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2168ae60f76fSBarry Smith   if (ts->prestep) {
2169ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2170312ce896SJed Brown   }
21713f2090d5SJed Brown   PetscFunctionReturn(0);
21723f2090d5SJed Brown }
21733f2090d5SJed Brown 
21743f2090d5SJed Brown #undef __FUNCT__
2175b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2176b8123daeSJed Brown /*@C
2177b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2178b8123daeSJed Brown   called once at the beginning of each stage.
2179b8123daeSJed Brown 
2180b8123daeSJed Brown   Logically Collective on TS
2181b8123daeSJed Brown 
2182b8123daeSJed Brown   Input Parameters:
2183b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2184b8123daeSJed Brown - func - The function
2185b8123daeSJed Brown 
2186b8123daeSJed Brown   Calling sequence of func:
2187b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2188b8123daeSJed Brown 
2189b8123daeSJed Brown   Level: intermediate
2190b8123daeSJed Brown 
2191b8123daeSJed Brown   Note:
2192b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2193b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2194b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2195b8123daeSJed Brown 
2196b8123daeSJed Brown .keywords: TS, timestep
2197b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2198b8123daeSJed Brown @*/
2199b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2200b8123daeSJed Brown {
2201b8123daeSJed Brown   PetscFunctionBegin;
2202b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2203ae60f76fSBarry Smith   ts->prestage = func;
2204b8123daeSJed Brown   PetscFunctionReturn(0);
2205b8123daeSJed Brown }
2206b8123daeSJed Brown 
2207b8123daeSJed Brown #undef __FUNCT__
2208b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2209b8123daeSJed Brown /*@
2210b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2211b8123daeSJed Brown 
2212b8123daeSJed Brown   Collective on TS
2213b8123daeSJed Brown 
2214b8123daeSJed Brown   Input Parameters:
2215b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2216b8123daeSJed Brown 
2217b8123daeSJed Brown   Notes:
2218b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2219b8123daeSJed Brown   most users would not generally call this routine themselves.
2220b8123daeSJed Brown 
2221b8123daeSJed Brown   Level: developer
2222b8123daeSJed Brown 
2223b8123daeSJed Brown .keywords: TS, timestep
2224b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2225b8123daeSJed Brown @*/
2226b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2227b8123daeSJed Brown {
2228b8123daeSJed Brown   PetscErrorCode ierr;
2229b8123daeSJed Brown 
2230b8123daeSJed Brown   PetscFunctionBegin;
2231b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2232ae60f76fSBarry Smith   if (ts->prestage) {
2233ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2234b8123daeSJed Brown   }
2235b8123daeSJed Brown   PetscFunctionReturn(0);
2236b8123daeSJed Brown }
2237b8123daeSJed Brown 
2238b8123daeSJed Brown #undef __FUNCT__
2239e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2240ac226902SBarry Smith /*@C
2241000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
22423f2090d5SJed Brown   called once at the end of each time step.
2243000e7ae3SMatthew Knepley 
22443f9fe445SBarry Smith   Logically Collective on TS
2245000e7ae3SMatthew Knepley 
2246000e7ae3SMatthew Knepley   Input Parameters:
2247000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2248000e7ae3SMatthew Knepley - func - The function
2249000e7ae3SMatthew Knepley 
2250000e7ae3SMatthew Knepley   Calling sequence of func:
2251b8123daeSJed Brown $ func (TS ts);
2252000e7ae3SMatthew Knepley 
2253000e7ae3SMatthew Knepley   Level: intermediate
2254000e7ae3SMatthew Knepley 
2255000e7ae3SMatthew Knepley .keywords: TS, timestep
2256b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2257000e7ae3SMatthew Knepley @*/
22587087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2259000e7ae3SMatthew Knepley {
2260000e7ae3SMatthew Knepley   PetscFunctionBegin;
22610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2262ae60f76fSBarry Smith   ts->poststep = func;
2263000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2264000e7ae3SMatthew Knepley }
2265000e7ae3SMatthew Knepley 
2266e74ef692SMatthew Knepley #undef __FUNCT__
22673f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
226809ee8438SJed Brown /*@
22693f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
22703f2090d5SJed Brown 
22713f2090d5SJed Brown   Collective on TS
22723f2090d5SJed Brown 
22733f2090d5SJed Brown   Input Parameters:
22743f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
22753f2090d5SJed Brown 
22763f2090d5SJed Brown   Notes:
22773f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
22783f2090d5SJed Brown   so most users would not generally call this routine themselves.
22793f2090d5SJed Brown 
22803f2090d5SJed Brown   Level: developer
22813f2090d5SJed Brown 
22823f2090d5SJed Brown .keywords: TS, timestep
22833f2090d5SJed Brown @*/
22847087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
22853f2090d5SJed Brown {
22863f2090d5SJed Brown   PetscErrorCode ierr;
22873f2090d5SJed Brown 
22883f2090d5SJed Brown   PetscFunctionBegin;
22890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2290ae60f76fSBarry Smith   if (ts->poststep) {
2291ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
229272ac3e02SJed Brown   }
22933f2090d5SJed Brown   PetscFunctionReturn(0);
22943f2090d5SJed Brown }
22953f2090d5SJed Brown 
2296d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2297d763cef2SBarry Smith 
22984a2ae208SSatish Balay #undef __FUNCT__
2299a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2300d763cef2SBarry Smith /*@C
2301a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2302d763cef2SBarry Smith    timestep to display the iteration's  progress.
2303d763cef2SBarry Smith 
23043f9fe445SBarry Smith    Logically Collective on TS
2305d763cef2SBarry Smith 
2306d763cef2SBarry Smith    Input Parameters:
2307d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2308e213d8f1SJed Brown .  monitor - monitoring routine
2309329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
23100298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2311b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
23120298fd71SBarry Smith           (may be NULL)
2313d763cef2SBarry Smith 
2314e213d8f1SJed Brown    Calling sequence of monitor:
23150910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2316d763cef2SBarry Smith 
2317d763cef2SBarry Smith +    ts - the TS context
231888c05cc5SBarry 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
231988c05cc5SBarry Smith                                been interpolated to)
23201f06c33eSBarry Smith .    time - current time
23210910c330SBarry Smith .    u - current iterate
2322d763cef2SBarry Smith -    mctx - [optional] monitoring context
2323d763cef2SBarry Smith 
2324d763cef2SBarry Smith    Notes:
2325d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2326d763cef2SBarry Smith    already has been loaded.
2327d763cef2SBarry Smith 
2328025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2329025f1a04SBarry Smith 
2330d763cef2SBarry Smith    Level: intermediate
2331d763cef2SBarry Smith 
2332d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2333d763cef2SBarry Smith 
2334a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2335d763cef2SBarry Smith @*/
2336c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2337d763cef2SBarry Smith {
2338d763cef2SBarry Smith   PetscFunctionBegin;
23390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
234017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2341d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
23428704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2343d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2344d763cef2SBarry Smith   PetscFunctionReturn(0);
2345d763cef2SBarry Smith }
2346d763cef2SBarry Smith 
23474a2ae208SSatish Balay #undef __FUNCT__
2348a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2349d763cef2SBarry Smith /*@C
2350a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2351d763cef2SBarry Smith 
23523f9fe445SBarry Smith    Logically Collective on TS
2353d763cef2SBarry Smith 
2354d763cef2SBarry Smith    Input Parameters:
2355d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2356d763cef2SBarry Smith 
2357d763cef2SBarry Smith    Notes:
2358d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2359d763cef2SBarry Smith 
2360d763cef2SBarry Smith    Level: intermediate
2361d763cef2SBarry Smith 
2362d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2363d763cef2SBarry Smith 
2364a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2365d763cef2SBarry Smith @*/
23667087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2367d763cef2SBarry Smith {
2368d952e501SBarry Smith   PetscErrorCode ierr;
2369d952e501SBarry Smith   PetscInt       i;
2370d952e501SBarry Smith 
2371d763cef2SBarry Smith   PetscFunctionBegin;
23720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2373d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
23748704b422SBarry Smith     if (ts->monitordestroy[i]) {
23758704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2376d952e501SBarry Smith     }
2377d952e501SBarry Smith   }
2378d763cef2SBarry Smith   ts->numbermonitors = 0;
2379d763cef2SBarry Smith   PetscFunctionReturn(0);
2380d763cef2SBarry Smith }
2381d763cef2SBarry Smith 
23824a2ae208SSatish Balay #undef __FUNCT__
2383a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2384d8e5e3e6SSatish Balay /*@
2385a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
23865516499fSSatish Balay 
23875516499fSSatish Balay    Level: intermediate
238841251cbbSSatish Balay 
23895516499fSSatish Balay .keywords: TS, set, monitor
23905516499fSSatish Balay 
239141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
239241251cbbSSatish Balay @*/
2393649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2394d763cef2SBarry Smith {
2395dfbe8321SBarry Smith   PetscErrorCode ierr;
2396ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2397d132466eSBarry Smith 
2398d763cef2SBarry Smith   PetscFunctionBegin;
2399649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2400971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2401649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2402d763cef2SBarry Smith   PetscFunctionReturn(0);
2403d763cef2SBarry Smith }
2404d763cef2SBarry Smith 
24054a2ae208SSatish Balay #undef __FUNCT__
2406cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2407cd652676SJed Brown /*@
2408cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2409cd652676SJed Brown 
2410cd652676SJed Brown    Logically Collective on TS
2411cd652676SJed Brown 
2412cd652676SJed Brown    Input Argument:
2413cd652676SJed Brown .  ts - time stepping context
2414cd652676SJed Brown 
2415cd652676SJed Brown    Output Argument:
2416cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2417cd652676SJed Brown 
2418cd652676SJed Brown    Level: intermediate
2419cd652676SJed Brown 
2420cd652676SJed Brown .keywords: TS, set
2421cd652676SJed Brown 
2422cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2423cd652676SJed Brown @*/
2424cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2425cd652676SJed Brown {
2426cd652676SJed Brown   PetscFunctionBegin;
2427cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2428cd652676SJed Brown   ts->retain_stages = flg;
2429cd652676SJed Brown   PetscFunctionReturn(0);
2430cd652676SJed Brown }
2431cd652676SJed Brown 
2432cd652676SJed Brown #undef __FUNCT__
2433cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2434cd652676SJed Brown /*@
2435cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2436cd652676SJed Brown 
2437cd652676SJed Brown    Collective on TS
2438cd652676SJed Brown 
2439cd652676SJed Brown    Input Argument:
2440cd652676SJed Brown +  ts - time stepping context
2441cd652676SJed Brown -  t - time to interpolate to
2442cd652676SJed Brown 
2443cd652676SJed Brown    Output Argument:
24440910c330SBarry Smith .  U - state at given time
2445cd652676SJed Brown 
2446cd652676SJed Brown    Notes:
2447cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2448cd652676SJed Brown 
2449cd652676SJed Brown    Level: intermediate
2450cd652676SJed Brown 
2451cd652676SJed Brown    Developer Notes:
2452cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2453cd652676SJed Brown 
2454cd652676SJed Brown .keywords: TS, set
2455cd652676SJed Brown 
2456cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2457cd652676SJed Brown @*/
24580910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2459cd652676SJed Brown {
2460cd652676SJed Brown   PetscErrorCode ierr;
2461cd652676SJed Brown 
2462cd652676SJed Brown   PetscFunctionBegin;
2463cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2464b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2465ce94432eSBarry 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);
2466ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
24670910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2468cd652676SJed Brown   PetscFunctionReturn(0);
2469cd652676SJed Brown }
2470cd652676SJed Brown 
2471cd652676SJed Brown #undef __FUNCT__
24724a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2473d763cef2SBarry Smith /*@
24746d9e5789SSean Farley    TSStep - Steps one time step
2475d763cef2SBarry Smith 
2476d763cef2SBarry Smith    Collective on TS
2477d763cef2SBarry Smith 
2478d763cef2SBarry Smith    Input Parameter:
2479d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2480d763cef2SBarry Smith 
24816d9e5789SSean Farley    Level: intermediate
2482d763cef2SBarry Smith 
2483b8123daeSJed Brown    Notes:
2484b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2485b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2486b8123daeSJed Brown 
248725cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
248825cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
248925cb2221SBarry Smith 
2490d763cef2SBarry Smith .keywords: TS, timestep, solve
2491d763cef2SBarry Smith 
249225cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2493d763cef2SBarry Smith @*/
2494193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2495d763cef2SBarry Smith {
2496362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2497dfbe8321SBarry Smith   PetscErrorCode ierr;
2498d763cef2SBarry Smith 
2499d763cef2SBarry Smith   PetscFunctionBegin;
25000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2501d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2502d405a339SMatthew Knepley 
2503362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2504362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2505bbd56ea5SKarl Rupp 
2506d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2507193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2508d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2509bbd56ea5SKarl Rupp 
2510362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2511362cd11cSLisandro Dalcin 
2512362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2513cef5090cSJed Brown     if (ts->errorifstepfailed) {
2514cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2515ce94432eSBarry 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]);
2516ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2517cef5090cSJed Brown     }
2518362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2519db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2520db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2521362cd11cSLisandro Dalcin   }
2522d763cef2SBarry Smith   PetscFunctionReturn(0);
2523d763cef2SBarry Smith }
2524d763cef2SBarry Smith 
25254a2ae208SSatish Balay #undef __FUNCT__
252605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
252705175c85SJed Brown /*@
252805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
252905175c85SJed Brown 
25301c3436cfSJed Brown    Collective on TS
253105175c85SJed Brown 
253205175c85SJed Brown    Input Arguments:
25331c3436cfSJed Brown +  ts - time stepping context
25341c3436cfSJed Brown .  order - desired order of accuracy
25350298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
253605175c85SJed Brown 
253705175c85SJed Brown    Output Arguments:
25380910c330SBarry Smith .  U - state at the end of the current step
253905175c85SJed Brown 
254005175c85SJed Brown    Level: advanced
254105175c85SJed Brown 
2542108c343cSJed Brown    Notes:
2543108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2544108c343cSJed 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.
2545108c343cSJed Brown 
25461c3436cfSJed Brown .seealso: TSStep(), TSAdapt
254705175c85SJed Brown @*/
25480910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
254905175c85SJed Brown {
255005175c85SJed Brown   PetscErrorCode ierr;
255105175c85SJed Brown 
255205175c85SJed Brown   PetscFunctionBegin;
255305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
255405175c85SJed Brown   PetscValidType(ts,1);
25550910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2556ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
25570910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
255805175c85SJed Brown   PetscFunctionReturn(0);
255905175c85SJed Brown }
256005175c85SJed Brown 
256105175c85SJed Brown #undef __FUNCT__
25626a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
25636a4d4014SLisandro Dalcin /*@
25646a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
25656a4d4014SLisandro Dalcin 
25666a4d4014SLisandro Dalcin    Collective on TS
25676a4d4014SLisandro Dalcin 
25686a4d4014SLisandro Dalcin    Input Parameter:
25696a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2570cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
25715a3a76d0SJed Brown 
25726a4d4014SLisandro Dalcin    Level: beginner
25736a4d4014SLisandro Dalcin 
25745a3a76d0SJed Brown    Notes:
25755a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
25765a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
25775a3a76d0SJed Brown    stepped over the final time.
25785a3a76d0SJed Brown 
25796a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
25806a4d4014SLisandro Dalcin 
25816a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
25826a4d4014SLisandro Dalcin @*/
2583cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
25846a4d4014SLisandro Dalcin {
25854d7d938eSLisandro Dalcin   PetscBool         flg;
25864d7d938eSLisandro Dalcin   PetscViewer       viewer;
2587b06615a5SLisandro Dalcin   Vec               solution;
25886a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2589cffb1e40SBarry Smith   PetscViewerFormat format;
2590f22f69f0SBarry Smith 
25916a4d4014SLisandro Dalcin   PetscFunctionBegin;
25920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2593f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
259449354f04SShri 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 */
2595b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
25960910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
2597b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
2598b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
2599b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
26005a3a76d0SJed Brown     }
26010910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2602bbd56ea5SKarl Rupp   } else if (u) {
26030910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
26045a3a76d0SJed Brown   }
2605b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
26066a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2607193ac0bcSJed Brown   ts->steps             = 0;
26085ef26d82SJed Brown   ts->ksp_its           = 0;
26095ef26d82SJed Brown   ts->snes_its          = 0;
2610c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2611c610991cSLisandro Dalcin   ts->reject            = 0;
2612193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2613193ac0bcSJed Brown 
2614f05ece33SBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view_pre",&viewer,&format,&flg);CHKERRQ(ierr);
2615f05ece33SBarry Smith   if (flg && !PetscPreLoadingOn) {
2616f05ece33SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
2617f05ece33SBarry Smith     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2618f05ece33SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2619f05ece33SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2620f05ece33SBarry Smith   }
2621f05ece33SBarry Smith 
2622193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2623193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
26240910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2625cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2626193ac0bcSJed Brown   } else {
26276a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2628db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2629db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2630e1a7a14fSJed Brown     while (!ts->reason) {
2631b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2632193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2633193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2634193ac0bcSJed Brown     }
263549354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
26360910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2637cc708dedSBarry Smith       ts->solvetime = ts->max_time;
2638b06615a5SLisandro Dalcin       solution = u;
26390574a7fbSJed Brown     } else {
2640ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2641cc708dedSBarry Smith       ts->solvetime = ts->ptime;
2642b06615a5SLisandro Dalcin       solution = ts->vec_sol;
26430574a7fbSJed Brown     }
2644b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
2645193ac0bcSJed Brown   }
2646ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
26474d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2648cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
26494d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2650cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2651cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
26522b0a91c0SBarry Smith   }
26536a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26546a4d4014SLisandro Dalcin }
26556a4d4014SLisandro Dalcin 
26566a4d4014SLisandro Dalcin #undef __FUNCT__
26574a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2658228d79bcSJed Brown /*@
2659228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2660228d79bcSJed Brown 
2661228d79bcSJed Brown    Collective on TS
2662228d79bcSJed Brown 
2663228d79bcSJed Brown    Input Parameters:
2664228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2665228d79bcSJed Brown .  step - step number that has just completed
2666228d79bcSJed Brown .  ptime - model time of the state
26670910c330SBarry Smith -  u - state at the current model time
2668228d79bcSJed Brown 
2669228d79bcSJed Brown    Notes:
2670228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2671228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2672228d79bcSJed Brown 
2673228d79bcSJed Brown    Level: advanced
2674228d79bcSJed Brown 
2675228d79bcSJed Brown .keywords: TS, timestep
2676228d79bcSJed Brown @*/
26770910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2678d763cef2SBarry Smith {
26796849ba73SBarry Smith   PetscErrorCode ierr;
2680a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith   PetscFunctionBegin;
2683b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2684b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
2685d763cef2SBarry Smith   for (i=0; i<n; i++) {
26860910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2687d763cef2SBarry Smith   }
2688d763cef2SBarry Smith   PetscFunctionReturn(0);
2689d763cef2SBarry Smith }
2690d763cef2SBarry Smith 
2691d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
26920b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
26930b039ecaSBarry Smith   PetscDrawLG lg;
26940b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2695201da799SBarry Smith   PetscInt    ksp_its,snes_its;
26960b039ecaSBarry Smith };
26970b039ecaSBarry Smith 
2698d763cef2SBarry Smith 
26994a2ae208SSatish Balay #undef __FUNCT__
2700a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2701d763cef2SBarry Smith /*@C
2702a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2703a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2704d763cef2SBarry Smith 
2705d763cef2SBarry Smith    Collective on TS
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith    Input Parameters:
2708d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2709d763cef2SBarry Smith .  label - the title to put in the title bar
27107c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2711a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2712a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith    Output Parameter:
27150b039ecaSBarry Smith .  ctx - the context
2716d763cef2SBarry Smith 
2717d763cef2SBarry Smith    Options Database Key:
27184f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
27194f09c107SBarry Smith .  -ts_monitor_lg_solution -
272099fdda47SBarry Smith .  -ts_monitor_lg_error -
272199fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
272299fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
272399fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2724d763cef2SBarry Smith 
2725d763cef2SBarry Smith    Notes:
2726a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith    Level: intermediate
2729d763cef2SBarry Smith 
27307c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2731d763cef2SBarry Smith 
27324f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
27337c922b88SBarry Smith 
2734d763cef2SBarry Smith @*/
2735a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2736d763cef2SBarry Smith {
2737b0a32e0cSBarry Smith   PetscDraw      win;
2738dfbe8321SBarry Smith   PetscErrorCode ierr;
273999fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2740d763cef2SBarry Smith 
2741d763cef2SBarry Smith   PetscFunctionBegin;
2742201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2743a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
27443fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
27450b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
27460298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
274799fdda47SBarry Smith   if (flg) {
27480b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
274999fdda47SBarry Smith   }
27500b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2751a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2752d763cef2SBarry Smith   PetscFunctionReturn(0);
2753d763cef2SBarry Smith }
2754d763cef2SBarry Smith 
27554a2ae208SSatish Balay #undef __FUNCT__
27564f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
2757b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
2758d763cef2SBarry Smith {
27590b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2760c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2761dfbe8321SBarry Smith   PetscErrorCode ierr;
2762d763cef2SBarry Smith 
2763d763cef2SBarry Smith   PetscFunctionBegin;
2764b06615a5SLisandro Dalcin   if (!step) {
2765a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2766a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2767a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2768a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2769a9f9c1f6SBarry Smith   }
2770c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
27710b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
2772b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
27730b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
27743923b477SBarry Smith   }
2775d763cef2SBarry Smith   PetscFunctionReturn(0);
2776d763cef2SBarry Smith }
2777d763cef2SBarry Smith 
27784a2ae208SSatish Balay #undef __FUNCT__
2779a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2780d763cef2SBarry Smith /*@C
2781a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2782a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2783d763cef2SBarry Smith 
27840b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2785d763cef2SBarry Smith 
2786d763cef2SBarry Smith    Input Parameter:
27870b039ecaSBarry Smith .  ctx - the monitor context
2788d763cef2SBarry Smith 
2789d763cef2SBarry Smith    Level: intermediate
2790d763cef2SBarry Smith 
2791d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2792d763cef2SBarry Smith 
27934f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2794d763cef2SBarry Smith @*/
2795a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2796d763cef2SBarry Smith {
2797b0a32e0cSBarry Smith   PetscDraw      draw;
2798dfbe8321SBarry Smith   PetscErrorCode ierr;
2799d763cef2SBarry Smith 
2800d763cef2SBarry Smith   PetscFunctionBegin;
28010b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
28026bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
28030b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
28040b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2805d763cef2SBarry Smith   PetscFunctionReturn(0);
2806d763cef2SBarry Smith }
2807d763cef2SBarry Smith 
28084a2ae208SSatish Balay #undef __FUNCT__
28094a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2810d763cef2SBarry Smith /*@
2811b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith    Not Collective
2814d763cef2SBarry Smith 
2815d763cef2SBarry Smith    Input Parameter:
2816d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2817d763cef2SBarry Smith 
2818d763cef2SBarry Smith    Output Parameter:
2819d763cef2SBarry Smith .  t  - the current time
2820d763cef2SBarry Smith 
2821d763cef2SBarry Smith    Level: beginner
2822d763cef2SBarry Smith 
2823b8123daeSJed Brown    Note:
2824b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2825b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2826b8123daeSJed Brown 
2827d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2828d763cef2SBarry Smith 
2829d763cef2SBarry Smith .keywords: TS, get, time
2830d763cef2SBarry Smith @*/
28317087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2832d763cef2SBarry Smith {
2833d763cef2SBarry Smith   PetscFunctionBegin;
28340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2835f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2836d763cef2SBarry Smith   *t = ts->ptime;
2837d763cef2SBarry Smith   PetscFunctionReturn(0);
2838d763cef2SBarry Smith }
2839d763cef2SBarry Smith 
28404a2ae208SSatish Balay #undef __FUNCT__
28416a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
28426a4d4014SLisandro Dalcin /*@
28436a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
28446a4d4014SLisandro Dalcin 
28453f9fe445SBarry Smith    Logically Collective on TS
28466a4d4014SLisandro Dalcin 
28476a4d4014SLisandro Dalcin    Input Parameters:
28486a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
28496a4d4014SLisandro Dalcin -  time - the time
28506a4d4014SLisandro Dalcin 
28516a4d4014SLisandro Dalcin    Level: intermediate
28526a4d4014SLisandro Dalcin 
28536a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
28546a4d4014SLisandro Dalcin 
28556a4d4014SLisandro Dalcin .keywords: TS, set, time
28566a4d4014SLisandro Dalcin @*/
28577087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
28586a4d4014SLisandro Dalcin {
28596a4d4014SLisandro Dalcin   PetscFunctionBegin;
28600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2861c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
28626a4d4014SLisandro Dalcin   ts->ptime = t;
28636a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
28646a4d4014SLisandro Dalcin }
28656a4d4014SLisandro Dalcin 
28666a4d4014SLisandro Dalcin #undef __FUNCT__
28674a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2868d763cef2SBarry Smith /*@C
2869d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2870d763cef2SBarry Smith    TS options in the database.
2871d763cef2SBarry Smith 
28723f9fe445SBarry Smith    Logically Collective on TS
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith    Input Parameter:
2875d763cef2SBarry Smith +  ts     - The TS context
2876d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2877d763cef2SBarry Smith 
2878d763cef2SBarry Smith    Notes:
2879d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2880d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2881d763cef2SBarry Smith    hyphen.
2882d763cef2SBarry Smith 
2883d763cef2SBarry Smith    Level: advanced
2884d763cef2SBarry Smith 
2885d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2886d763cef2SBarry Smith 
2887d763cef2SBarry Smith .seealso: TSSetFromOptions()
2888d763cef2SBarry Smith 
2889d763cef2SBarry Smith @*/
28907087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2891d763cef2SBarry Smith {
2892dfbe8321SBarry Smith   PetscErrorCode ierr;
2893089b2837SJed Brown   SNES           snes;
2894d763cef2SBarry Smith 
2895d763cef2SBarry Smith   PetscFunctionBegin;
28960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2897d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2898089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2899089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2900d763cef2SBarry Smith   PetscFunctionReturn(0);
2901d763cef2SBarry Smith }
2902d763cef2SBarry Smith 
2903d763cef2SBarry Smith 
29044a2ae208SSatish Balay #undef __FUNCT__
29054a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2906d763cef2SBarry Smith /*@C
2907d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2908d763cef2SBarry Smith    TS options in the database.
2909d763cef2SBarry Smith 
29103f9fe445SBarry Smith    Logically Collective on TS
2911d763cef2SBarry Smith 
2912d763cef2SBarry Smith    Input Parameter:
2913d763cef2SBarry Smith +  ts     - The TS context
2914d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2915d763cef2SBarry Smith 
2916d763cef2SBarry Smith    Notes:
2917d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2918d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2919d763cef2SBarry Smith    hyphen.
2920d763cef2SBarry Smith 
2921d763cef2SBarry Smith    Level: advanced
2922d763cef2SBarry Smith 
2923d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2924d763cef2SBarry Smith 
2925d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2926d763cef2SBarry Smith 
2927d763cef2SBarry Smith @*/
29287087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2929d763cef2SBarry Smith {
2930dfbe8321SBarry Smith   PetscErrorCode ierr;
2931089b2837SJed Brown   SNES           snes;
2932d763cef2SBarry Smith 
2933d763cef2SBarry Smith   PetscFunctionBegin;
29340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2935d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2936089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2937089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2938d763cef2SBarry Smith   PetscFunctionReturn(0);
2939d763cef2SBarry Smith }
2940d763cef2SBarry Smith 
29414a2ae208SSatish Balay #undef __FUNCT__
29424a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2943d763cef2SBarry Smith /*@C
2944d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2945d763cef2SBarry Smith    TS options in the database.
2946d763cef2SBarry Smith 
2947d763cef2SBarry Smith    Not Collective
2948d763cef2SBarry Smith 
2949d763cef2SBarry Smith    Input Parameter:
2950d763cef2SBarry Smith .  ts - The TS context
2951d763cef2SBarry Smith 
2952d763cef2SBarry Smith    Output Parameter:
2953d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2954d763cef2SBarry Smith 
2955d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2956d763cef2SBarry Smith    sufficient length to hold the prefix.
2957d763cef2SBarry Smith 
2958d763cef2SBarry Smith    Level: intermediate
2959d763cef2SBarry Smith 
2960d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2961d763cef2SBarry Smith 
2962d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2963d763cef2SBarry Smith @*/
29647087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2965d763cef2SBarry Smith {
2966dfbe8321SBarry Smith   PetscErrorCode ierr;
2967d763cef2SBarry Smith 
2968d763cef2SBarry Smith   PetscFunctionBegin;
29690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29704482741eSBarry Smith   PetscValidPointer(prefix,2);
2971d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2972d763cef2SBarry Smith   PetscFunctionReturn(0);
2973d763cef2SBarry Smith }
2974d763cef2SBarry Smith 
29754a2ae208SSatish Balay #undef __FUNCT__
29764a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2977d763cef2SBarry Smith /*@C
2978d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2979d763cef2SBarry Smith 
2980d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2981d763cef2SBarry Smith 
2982d763cef2SBarry Smith    Input Parameter:
2983d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2984d763cef2SBarry Smith 
2985d763cef2SBarry Smith    Output Parameters:
2986e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
2987e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
2988e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
2989e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
2990d763cef2SBarry Smith 
29910298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
2992d763cef2SBarry Smith 
2993d763cef2SBarry Smith    Level: intermediate
2994d763cef2SBarry Smith 
299526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2996d763cef2SBarry Smith 
2997d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2998d763cef2SBarry Smith @*/
2999e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3000d763cef2SBarry Smith {
3001089b2837SJed Brown   PetscErrorCode ierr;
3002089b2837SJed Brown   SNES           snes;
300324989b8cSPeter Brune   DM             dm;
3004089b2837SJed Brown 
3005d763cef2SBarry Smith   PetscFunctionBegin;
3006089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3007e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
300824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
300924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3010d763cef2SBarry Smith   PetscFunctionReturn(0);
3011d763cef2SBarry Smith }
3012d763cef2SBarry Smith 
30131713a123SBarry Smith #undef __FUNCT__
30142eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
30152eca1d9cSJed Brown /*@C
30162eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
30172eca1d9cSJed Brown 
30182eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
30192eca1d9cSJed Brown 
30202eca1d9cSJed Brown    Input Parameter:
30212eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
30222eca1d9cSJed Brown 
30232eca1d9cSJed Brown    Output Parameters:
3024e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3025e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
30262eca1d9cSJed Brown .  f   - The function to compute the matrices
30272eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
30282eca1d9cSJed Brown 
30290298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
30302eca1d9cSJed Brown 
30312eca1d9cSJed Brown    Level: advanced
30322eca1d9cSJed Brown 
30332eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
30342eca1d9cSJed Brown 
30352eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
30362eca1d9cSJed Brown @*/
3037e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
30382eca1d9cSJed Brown {
3039089b2837SJed Brown   PetscErrorCode ierr;
3040089b2837SJed Brown   SNES           snes;
304124989b8cSPeter Brune   DM             dm;
30420910c330SBarry Smith 
30432eca1d9cSJed Brown   PetscFunctionBegin;
3044089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3045f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3046e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
304724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
304824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
30492eca1d9cSJed Brown   PetscFunctionReturn(0);
30502eca1d9cSJed Brown }
30512eca1d9cSJed Brown 
30526083293cSBarry Smith 
30532eca1d9cSJed Brown #undef __FUNCT__
305483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
30551713a123SBarry Smith /*@C
305683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
30571713a123SBarry Smith    VecView() for the solution at each timestep
30581713a123SBarry Smith 
30591713a123SBarry Smith    Collective on TS
30601713a123SBarry Smith 
30611713a123SBarry Smith    Input Parameters:
30621713a123SBarry Smith +  ts - the TS context
30631713a123SBarry Smith .  step - current time-step
3064142b95e3SSatish Balay .  ptime - current time
30650298fd71SBarry Smith -  dummy - either a viewer or NULL
30661713a123SBarry Smith 
306799fdda47SBarry Smith    Options Database:
306899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
306999fdda47SBarry Smith 
307099fdda47SBarry 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
307199fdda47SBarry Smith        will look bad
307299fdda47SBarry Smith 
30731713a123SBarry Smith    Level: intermediate
30741713a123SBarry Smith 
30751713a123SBarry Smith .keywords: TS,  vector, monitor, view
30761713a123SBarry Smith 
3077a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30781713a123SBarry Smith @*/
30790910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30801713a123SBarry Smith {
3081dfbe8321SBarry Smith   PetscErrorCode   ierr;
308283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3083473a3ab2SBarry Smith   PetscDraw        draw;
30841713a123SBarry Smith 
30851713a123SBarry Smith   PetscFunctionBegin;
30866083293cSBarry Smith   if (!step && ictx->showinitial) {
30876083293cSBarry Smith     if (!ictx->initialsolution) {
30880910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
30891713a123SBarry Smith     }
30900910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
30916083293cSBarry Smith   }
3092b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
30930dcf80beSBarry Smith 
30946083293cSBarry Smith   if (ictx->showinitial) {
30956083293cSBarry Smith     PetscReal pause;
30966083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
30976083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
30986083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
30996083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
31006083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
31016083293cSBarry Smith   }
31020910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3103473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3104473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3105473a3ab2SBarry Smith     char      time[32];
3106473a3ab2SBarry Smith     size_t    len;
3107473a3ab2SBarry Smith 
3108473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3109473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3110473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3111473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
31120298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3113473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3114473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3115473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3116473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3117473a3ab2SBarry Smith   }
3118473a3ab2SBarry Smith 
31196083293cSBarry Smith   if (ictx->showinitial) {
31206083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
31216083293cSBarry Smith   }
31221713a123SBarry Smith   PetscFunctionReturn(0);
31231713a123SBarry Smith }
31241713a123SBarry Smith 
31252d5ee99bSBarry Smith #undef __FUNCT__
31262d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
31272d5ee99bSBarry Smith /*@C
31282d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
31292d5ee99bSBarry Smith 
31302d5ee99bSBarry Smith    Collective on TS
31312d5ee99bSBarry Smith 
31322d5ee99bSBarry Smith    Input Parameters:
31332d5ee99bSBarry Smith +  ts - the TS context
31342d5ee99bSBarry Smith .  step - current time-step
31352d5ee99bSBarry Smith .  ptime - current time
31362d5ee99bSBarry Smith -  dummy - either a viewer or NULL
31372d5ee99bSBarry Smith 
31382d5ee99bSBarry Smith    Level: intermediate
31392d5ee99bSBarry Smith 
31402d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
31412d5ee99bSBarry Smith 
31422d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31432d5ee99bSBarry Smith @*/
31442d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31452d5ee99bSBarry Smith {
31462d5ee99bSBarry Smith   PetscErrorCode    ierr;
31472d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
31482d5ee99bSBarry Smith   PetscDraw         draw;
31492d5ee99bSBarry Smith   MPI_Comm          comm;
31502d5ee99bSBarry Smith   PetscInt          n;
31512d5ee99bSBarry Smith   PetscMPIInt       size;
31522d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
31532d5ee99bSBarry Smith   char              time[32];
31542d5ee99bSBarry Smith   size_t            len;
31552d5ee99bSBarry Smith   const PetscScalar *U;
31562d5ee99bSBarry Smith 
31572d5ee99bSBarry Smith   PetscFunctionBegin;
31582d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
31592d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
31602d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
31612d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
31622d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
31632d5ee99bSBarry Smith 
31642d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
31652d5ee99bSBarry Smith 
31662d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
31674b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3168ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3169a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3170a4494fc1SBarry Smith       PetscFunctionReturn(0);
3171a4494fc1SBarry Smith   }
31722d5ee99bSBarry Smith   if (!step) ictx->color++;
31732d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
31742d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
31752d5ee99bSBarry Smith 
31762d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
31774b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
31782d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
31792d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
31802d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
31812d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
31822d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
31832d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
31842d5ee99bSBarry Smith   }
31852d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
31862d5ee99bSBarry Smith   PetscFunctionReturn(0);
31872d5ee99bSBarry Smith }
31882d5ee99bSBarry Smith 
31891713a123SBarry Smith 
31906c699258SBarry Smith #undef __FUNCT__
319183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
31926083293cSBarry Smith /*@C
319383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
31946083293cSBarry Smith 
31956083293cSBarry Smith    Collective on TS
31966083293cSBarry Smith 
31976083293cSBarry Smith    Input Parameters:
31986083293cSBarry Smith .    ctx - the monitor context
31996083293cSBarry Smith 
32006083293cSBarry Smith    Level: intermediate
32016083293cSBarry Smith 
32026083293cSBarry Smith .keywords: TS,  vector, monitor, view
32036083293cSBarry Smith 
320483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
32056083293cSBarry Smith @*/
320683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
32076083293cSBarry Smith {
32086083293cSBarry Smith   PetscErrorCode ierr;
32096083293cSBarry Smith 
32106083293cSBarry Smith   PetscFunctionBegin;
32114b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
321283a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
321383a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
321483a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
32156083293cSBarry Smith   PetscFunctionReturn(0);
32166083293cSBarry Smith }
32176083293cSBarry Smith 
32186083293cSBarry Smith #undef __FUNCT__
321983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
32206083293cSBarry Smith /*@C
322183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
32226083293cSBarry Smith 
32236083293cSBarry Smith    Collective on TS
32246083293cSBarry Smith 
32256083293cSBarry Smith    Input Parameter:
32266083293cSBarry Smith .    ts - time-step context
32276083293cSBarry Smith 
32286083293cSBarry Smith    Output Patameter:
32296083293cSBarry Smith .    ctx - the monitor context
32306083293cSBarry Smith 
323199fdda47SBarry Smith    Options Database:
323299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
323399fdda47SBarry Smith 
32346083293cSBarry Smith    Level: intermediate
32356083293cSBarry Smith 
32366083293cSBarry Smith .keywords: TS,  vector, monitor, view
32376083293cSBarry Smith 
323883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
32396083293cSBarry Smith @*/
324083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
32416083293cSBarry Smith {
32426083293cSBarry Smith   PetscErrorCode   ierr;
32436083293cSBarry Smith 
32446083293cSBarry Smith   PetscFunctionBegin;
324583a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
324683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3247e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3248bbd56ea5SKarl Rupp 
324983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3250473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
32510298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3252473a3ab2SBarry Smith 
3253473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
32540298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
32552d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
32566083293cSBarry Smith   PetscFunctionReturn(0);
32576083293cSBarry Smith }
32586083293cSBarry Smith 
32596083293cSBarry Smith #undef __FUNCT__
326083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
32613a471f94SBarry Smith /*@C
326283a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
32633a471f94SBarry Smith    VecView() for the error at each timestep
32643a471f94SBarry Smith 
32653a471f94SBarry Smith    Collective on TS
32663a471f94SBarry Smith 
32673a471f94SBarry Smith    Input Parameters:
32683a471f94SBarry Smith +  ts - the TS context
32693a471f94SBarry Smith .  step - current time-step
32703a471f94SBarry Smith .  ptime - current time
32710298fd71SBarry Smith -  dummy - either a viewer or NULL
32723a471f94SBarry Smith 
32733a471f94SBarry Smith    Level: intermediate
32743a471f94SBarry Smith 
32753a471f94SBarry Smith .keywords: TS,  vector, monitor, view
32763a471f94SBarry Smith 
32773a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32783a471f94SBarry Smith @*/
32790910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
32803a471f94SBarry Smith {
32813a471f94SBarry Smith   PetscErrorCode   ierr;
328283a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
328383a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
32843a471f94SBarry Smith   Vec              work;
32853a471f94SBarry Smith 
32863a471f94SBarry Smith   PetscFunctionBegin;
3287b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
32880910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
32893a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
32900910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
32913a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
32923a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
32933a471f94SBarry Smith   PetscFunctionReturn(0);
32943a471f94SBarry Smith }
32953a471f94SBarry Smith 
32962a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
32973a471f94SBarry Smith #undef __FUNCT__
32986c699258SBarry Smith #define __FUNCT__ "TSSetDM"
32996c699258SBarry Smith /*@
33006c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
33016c699258SBarry Smith 
33023f9fe445SBarry Smith    Logically Collective on TS and DM
33036c699258SBarry Smith 
33046c699258SBarry Smith    Input Parameters:
33056c699258SBarry Smith +  ts - the preconditioner context
33066c699258SBarry Smith -  dm - the dm
33076c699258SBarry Smith 
33086c699258SBarry Smith    Level: intermediate
33096c699258SBarry Smith 
33106c699258SBarry Smith 
33116c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
33126c699258SBarry Smith @*/
33137087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
33146c699258SBarry Smith {
33156c699258SBarry Smith   PetscErrorCode ierr;
3316089b2837SJed Brown   SNES           snes;
3317942e3340SBarry Smith   DMTS           tsdm;
33186c699258SBarry Smith 
33196c699258SBarry Smith   PetscFunctionBegin;
33200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
332170663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3322942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
33232a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3324942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3325942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
332624989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
332724989b8cSPeter Brune         tsdm->originaldm = dm;
332824989b8cSPeter Brune       }
332924989b8cSPeter Brune     }
33306bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
333124989b8cSPeter Brune   }
33326c699258SBarry Smith   ts->dm = dm;
3333bbd56ea5SKarl Rupp 
3334089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3335089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
33366c699258SBarry Smith   PetscFunctionReturn(0);
33376c699258SBarry Smith }
33386c699258SBarry Smith 
33396c699258SBarry Smith #undef __FUNCT__
33406c699258SBarry Smith #define __FUNCT__ "TSGetDM"
33416c699258SBarry Smith /*@
33426c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
33436c699258SBarry Smith 
33443f9fe445SBarry Smith    Not Collective
33456c699258SBarry Smith 
33466c699258SBarry Smith    Input Parameter:
33476c699258SBarry Smith . ts - the preconditioner context
33486c699258SBarry Smith 
33496c699258SBarry Smith    Output Parameter:
33506c699258SBarry Smith .  dm - the dm
33516c699258SBarry Smith 
33526c699258SBarry Smith    Level: intermediate
33536c699258SBarry Smith 
33546c699258SBarry Smith 
33556c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
33566c699258SBarry Smith @*/
33577087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
33586c699258SBarry Smith {
3359496e6a7aSJed Brown   PetscErrorCode ierr;
3360496e6a7aSJed Brown 
33616c699258SBarry Smith   PetscFunctionBegin;
33620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3363496e6a7aSJed Brown   if (!ts->dm) {
3364ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3365496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3366496e6a7aSJed Brown   }
33676c699258SBarry Smith   *dm = ts->dm;
33686c699258SBarry Smith   PetscFunctionReturn(0);
33696c699258SBarry Smith }
33701713a123SBarry Smith 
33710f5c6efeSJed Brown #undef __FUNCT__
33720f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
33730f5c6efeSJed Brown /*@
33740f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
33750f5c6efeSJed Brown 
33763f9fe445SBarry Smith    Logically Collective on SNES
33770f5c6efeSJed Brown 
33780f5c6efeSJed Brown    Input Parameter:
3379d42a1c89SJed Brown + snes - nonlinear solver
33800910c330SBarry Smith . U - the current state at which to evaluate the residual
3381d42a1c89SJed Brown - ctx - user context, must be a TS
33820f5c6efeSJed Brown 
33830f5c6efeSJed Brown    Output Parameter:
33840f5c6efeSJed Brown . F - the nonlinear residual
33850f5c6efeSJed Brown 
33860f5c6efeSJed Brown    Notes:
33870f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
33880f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
33890f5c6efeSJed Brown 
33900f5c6efeSJed Brown    Level: advanced
33910f5c6efeSJed Brown 
33920f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
33930f5c6efeSJed Brown @*/
33940910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
33950f5c6efeSJed Brown {
33960f5c6efeSJed Brown   TS             ts = (TS)ctx;
33970f5c6efeSJed Brown   PetscErrorCode ierr;
33980f5c6efeSJed Brown 
33990f5c6efeSJed Brown   PetscFunctionBegin;
34000f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34010910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34020f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
34030f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
34040910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
34050f5c6efeSJed Brown   PetscFunctionReturn(0);
34060f5c6efeSJed Brown }
34070f5c6efeSJed Brown 
34080f5c6efeSJed Brown #undef __FUNCT__
34090f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
34100f5c6efeSJed Brown /*@
34110f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
34120f5c6efeSJed Brown 
34130f5c6efeSJed Brown    Collective on SNES
34140f5c6efeSJed Brown 
34150f5c6efeSJed Brown    Input Parameter:
34160f5c6efeSJed Brown + snes - nonlinear solver
34170910c330SBarry Smith . U - the current state at which to evaluate the residual
34180f5c6efeSJed Brown - ctx - user context, must be a TS
34190f5c6efeSJed Brown 
34200f5c6efeSJed Brown    Output Parameter:
34210f5c6efeSJed Brown + A - the Jacobian
34220f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
34230f5c6efeSJed Brown - flag - indicates any structure change in the matrix
34240f5c6efeSJed Brown 
34250f5c6efeSJed Brown    Notes:
34260f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
34270f5c6efeSJed Brown 
34280f5c6efeSJed Brown    Level: developer
34290f5c6efeSJed Brown 
34300f5c6efeSJed Brown .seealso: SNESSetJacobian()
34310f5c6efeSJed Brown @*/
34320910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
34330f5c6efeSJed Brown {
34340f5c6efeSJed Brown   TS             ts = (TS)ctx;
34350f5c6efeSJed Brown   PetscErrorCode ierr;
34360f5c6efeSJed Brown 
34370f5c6efeSJed Brown   PetscFunctionBegin;
34380f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34390910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34400f5c6efeSJed Brown   PetscValidPointer(A,3);
34410f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
34420f5c6efeSJed Brown   PetscValidPointer(B,4);
34430f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
34440f5c6efeSJed Brown   PetscValidPointer(flag,5);
34450f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
34460910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
34470f5c6efeSJed Brown   PetscFunctionReturn(0);
34480f5c6efeSJed Brown }
3449325fc9f4SBarry Smith 
34500e4ef248SJed Brown #undef __FUNCT__
34510e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
34520e4ef248SJed Brown /*@C
34530e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
34540e4ef248SJed Brown 
34550e4ef248SJed Brown    Collective on TS
34560e4ef248SJed Brown 
34570e4ef248SJed Brown    Input Arguments:
34580e4ef248SJed Brown +  ts - time stepping context
34590e4ef248SJed Brown .  t - time at which to evaluate
34600910c330SBarry Smith .  U - state at which to evaluate
34610e4ef248SJed Brown -  ctx - context
34620e4ef248SJed Brown 
34630e4ef248SJed Brown    Output Arguments:
34640e4ef248SJed Brown .  F - right hand side
34650e4ef248SJed Brown 
34660e4ef248SJed Brown    Level: intermediate
34670e4ef248SJed Brown 
34680e4ef248SJed Brown    Notes:
34690e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
34700e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
34710e4ef248SJed Brown 
34720e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
34730e4ef248SJed Brown @*/
34740910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
34750e4ef248SJed Brown {
34760e4ef248SJed Brown   PetscErrorCode ierr;
34770e4ef248SJed Brown   Mat            Arhs,Brhs;
34780e4ef248SJed Brown   MatStructure   flg2;
34790e4ef248SJed Brown 
34800e4ef248SJed Brown   PetscFunctionBegin;
34810e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
34820910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
34830910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
34840e4ef248SJed Brown   PetscFunctionReturn(0);
34850e4ef248SJed Brown }
34860e4ef248SJed Brown 
34870e4ef248SJed Brown #undef __FUNCT__
34880e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
34890e4ef248SJed Brown /*@C
34900e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
34910e4ef248SJed Brown 
34920e4ef248SJed Brown    Collective on TS
34930e4ef248SJed Brown 
34940e4ef248SJed Brown    Input Arguments:
34950e4ef248SJed Brown +  ts - time stepping context
34960e4ef248SJed Brown .  t - time at which to evaluate
34970910c330SBarry Smith .  U - state at which to evaluate
34980e4ef248SJed Brown -  ctx - context
34990e4ef248SJed Brown 
35000e4ef248SJed Brown    Output Arguments:
35010e4ef248SJed Brown +  A - pointer to operator
35020e4ef248SJed Brown .  B - pointer to preconditioning matrix
35030e4ef248SJed Brown -  flg - matrix structure flag
35040e4ef248SJed Brown 
35050e4ef248SJed Brown    Level: intermediate
35060e4ef248SJed Brown 
35070e4ef248SJed Brown    Notes:
35080e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
35090e4ef248SJed Brown 
35100e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
35110e4ef248SJed Brown @*/
35120910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
35130e4ef248SJed Brown {
35140e4ef248SJed Brown   PetscFunctionBegin;
35150e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
35160e4ef248SJed Brown   PetscFunctionReturn(0);
35170e4ef248SJed Brown }
35180e4ef248SJed Brown 
35190026cea9SSean Farley #undef __FUNCT__
35200026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
35210026cea9SSean Farley /*@C
35220026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
35230026cea9SSean Farley 
35240026cea9SSean Farley    Collective on TS
35250026cea9SSean Farley 
35260026cea9SSean Farley    Input Arguments:
35270026cea9SSean Farley +  ts - time stepping context
35280026cea9SSean Farley .  t - time at which to evaluate
35290910c330SBarry Smith .  U - state at which to evaluate
35300910c330SBarry Smith .  Udot - time derivative of state vector
35310026cea9SSean Farley -  ctx - context
35320026cea9SSean Farley 
35330026cea9SSean Farley    Output Arguments:
35340026cea9SSean Farley .  F - left hand side
35350026cea9SSean Farley 
35360026cea9SSean Farley    Level: intermediate
35370026cea9SSean Farley 
35380026cea9SSean Farley    Notes:
35390910c330SBarry 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
35400026cea9SSean Farley    user is required to write their own TSComputeIFunction.
35410026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
35420026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
35430026cea9SSean Farley 
35440026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
35450026cea9SSean Farley @*/
35460910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
35470026cea9SSean Farley {
35480026cea9SSean Farley   PetscErrorCode ierr;
35490026cea9SSean Farley   Mat            A,B;
35500026cea9SSean Farley   MatStructure   flg2;
35510026cea9SSean Farley 
35520026cea9SSean Farley   PetscFunctionBegin;
35530298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
35540910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
35550910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
35560026cea9SSean Farley   PetscFunctionReturn(0);
35570026cea9SSean Farley }
35580026cea9SSean Farley 
35590026cea9SSean Farley #undef __FUNCT__
35600026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
35610026cea9SSean Farley /*@C
3562*b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
35630026cea9SSean Farley 
35640026cea9SSean Farley    Collective on TS
35650026cea9SSean Farley 
35660026cea9SSean Farley    Input Arguments:
35670026cea9SSean Farley +  ts - time stepping context
35680026cea9SSean Farley .  t - time at which to evaluate
35690910c330SBarry Smith .  U - state at which to evaluate
35700910c330SBarry Smith .  Udot - time derivative of state vector
35710026cea9SSean Farley .  shift - shift to apply
35720026cea9SSean Farley -  ctx - context
35730026cea9SSean Farley 
35740026cea9SSean Farley    Output Arguments:
35750026cea9SSean Farley +  A - pointer to operator
35760026cea9SSean Farley .  B - pointer to preconditioning matrix
35770026cea9SSean Farley -  flg - matrix structure flag
35780026cea9SSean Farley 
3579*b41af12eSJed Brown    Level: advanced
35800026cea9SSean Farley 
35810026cea9SSean Farley    Notes:
35820026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
35830026cea9SSean Farley 
3584*b41af12eSJed Brown    It is only appropriate for problems of the form
3585*b41af12eSJed Brown 
3586*b41af12eSJed Brown $     M Udot = F(U,t)
3587*b41af12eSJed Brown 
3588*b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
3589*b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
3590*b41af12eSJed Brown   an implicit operator of the form
3591*b41af12eSJed Brown 
3592*b41af12eSJed Brown $    shift*M + J
3593*b41af12eSJed Brown 
3594*b41af12eSJed 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
3595*b41af12eSJed Brown   a copy of M or reassemble it when requested.
3596*b41af12eSJed Brown 
35970026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
35980026cea9SSean Farley @*/
35990910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
36000026cea9SSean Farley {
3601*b41af12eSJed Brown   PetscErrorCode ierr;
3602*b41af12eSJed Brown 
36030026cea9SSean Farley   PetscFunctionBegin;
3604*b41af12eSJed Brown   ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
3605*b41af12eSJed Brown   ts->ijacobian.shift = shift;
36060026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
36070026cea9SSean Farley   PetscFunctionReturn(0);
36080026cea9SSean Farley }
3609*b41af12eSJed Brown 
3610e817cc15SEmil Constantinescu #undef __FUNCT__
3611e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3612e817cc15SEmil Constantinescu /*@
3613e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3614e817cc15SEmil Constantinescu 
3615e817cc15SEmil Constantinescu    Not Collective
3616e817cc15SEmil Constantinescu 
3617e817cc15SEmil Constantinescu    Input Parameter:
3618e817cc15SEmil Constantinescu .  ts - the TS context
3619e817cc15SEmil Constantinescu 
3620e817cc15SEmil Constantinescu    Output Parameter:
36214e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3622e817cc15SEmil Constantinescu 
3623e817cc15SEmil Constantinescu    Level: beginner
3624e817cc15SEmil Constantinescu 
3625e817cc15SEmil Constantinescu .keywords: TS, equation type
3626e817cc15SEmil Constantinescu 
3627e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3628e817cc15SEmil Constantinescu @*/
3629e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3630e817cc15SEmil Constantinescu {
3631e817cc15SEmil Constantinescu   PetscFunctionBegin;
3632e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3633e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3634e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3635e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3636e817cc15SEmil Constantinescu }
3637e817cc15SEmil Constantinescu 
3638e817cc15SEmil Constantinescu #undef __FUNCT__
3639e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3640e817cc15SEmil Constantinescu /*@
3641e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3642e817cc15SEmil Constantinescu 
3643e817cc15SEmil Constantinescu    Not Collective
3644e817cc15SEmil Constantinescu 
3645e817cc15SEmil Constantinescu    Input Parameter:
3646e817cc15SEmil Constantinescu +  ts - the TS context
36474e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3648e817cc15SEmil Constantinescu 
3649e817cc15SEmil Constantinescu    Level: advanced
3650e817cc15SEmil Constantinescu 
3651e817cc15SEmil Constantinescu .keywords: TS, equation type
3652e817cc15SEmil Constantinescu 
3653e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3654e817cc15SEmil Constantinescu @*/
3655e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3656e817cc15SEmil Constantinescu {
3657e817cc15SEmil Constantinescu   PetscFunctionBegin;
3658e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3659e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3660e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3661e817cc15SEmil Constantinescu }
36620026cea9SSean Farley 
36634af1b03aSJed Brown #undef __FUNCT__
36644af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
36654af1b03aSJed Brown /*@
36664af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
36674af1b03aSJed Brown 
36684af1b03aSJed Brown    Not Collective
36694af1b03aSJed Brown 
36704af1b03aSJed Brown    Input Parameter:
36714af1b03aSJed Brown .  ts - the TS context
36724af1b03aSJed Brown 
36734af1b03aSJed Brown    Output Parameter:
36744af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
36754af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
36764af1b03aSJed Brown 
3677487e0bb9SJed Brown    Level: beginner
36784af1b03aSJed Brown 
3679cd652676SJed Brown    Notes:
3680cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
36814af1b03aSJed Brown 
36824af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
36834af1b03aSJed Brown 
36844af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
36854af1b03aSJed Brown @*/
36864af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
36874af1b03aSJed Brown {
36884af1b03aSJed Brown   PetscFunctionBegin;
36894af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36904af1b03aSJed Brown   PetscValidPointer(reason,2);
36914af1b03aSJed Brown   *reason = ts->reason;
36924af1b03aSJed Brown   PetscFunctionReturn(0);
36934af1b03aSJed Brown }
36944af1b03aSJed Brown 
3695fb1732b5SBarry Smith #undef __FUNCT__
3696d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3697d6ad946cSShri Abhyankar /*@
3698d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3699d6ad946cSShri Abhyankar 
3700d6ad946cSShri Abhyankar    Not Collective
3701d6ad946cSShri Abhyankar 
3702d6ad946cSShri Abhyankar    Input Parameter:
3703d6ad946cSShri Abhyankar +  ts - the TS context
3704d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3705d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3706d6ad946cSShri Abhyankar 
3707f5abba47SShri Abhyankar    Level: advanced
3708d6ad946cSShri Abhyankar 
3709d6ad946cSShri Abhyankar    Notes:
3710d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3711d6ad946cSShri Abhyankar 
3712d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3713d6ad946cSShri Abhyankar 
3714d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3715d6ad946cSShri Abhyankar @*/
3716d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3717d6ad946cSShri Abhyankar {
3718d6ad946cSShri Abhyankar   PetscFunctionBegin;
3719d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3720d6ad946cSShri Abhyankar   ts->reason = reason;
3721d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3722d6ad946cSShri Abhyankar }
3723d6ad946cSShri Abhyankar 
3724d6ad946cSShri Abhyankar #undef __FUNCT__
3725cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3726cc708dedSBarry Smith /*@
3727cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3728cc708dedSBarry Smith 
3729cc708dedSBarry Smith    Not Collective
3730cc708dedSBarry Smith 
3731cc708dedSBarry Smith    Input Parameter:
3732cc708dedSBarry Smith .  ts - the TS context
3733cc708dedSBarry Smith 
3734cc708dedSBarry Smith    Output Parameter:
3735cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3736cc708dedSBarry Smith 
3737487e0bb9SJed Brown    Level: beginner
3738cc708dedSBarry Smith 
3739cc708dedSBarry Smith    Notes:
3740cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3741cc708dedSBarry Smith 
3742cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3743cc708dedSBarry Smith 
3744cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3745cc708dedSBarry Smith @*/
3746cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3747cc708dedSBarry Smith {
3748cc708dedSBarry Smith   PetscFunctionBegin;
3749cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3750cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3751cc708dedSBarry Smith   *ftime = ts->solvetime;
3752cc708dedSBarry Smith   PetscFunctionReturn(0);
3753cc708dedSBarry Smith }
3754cc708dedSBarry Smith 
3755cc708dedSBarry Smith #undef __FUNCT__
37565ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
37579f67acb7SJed Brown /*@
37585ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
37599f67acb7SJed Brown    used by the time integrator.
37609f67acb7SJed Brown 
37619f67acb7SJed Brown    Not Collective
37629f67acb7SJed Brown 
37639f67acb7SJed Brown    Input Parameter:
37649f67acb7SJed Brown .  ts - TS context
37659f67acb7SJed Brown 
37669f67acb7SJed Brown    Output Parameter:
37679f67acb7SJed Brown .  nits - number of nonlinear iterations
37689f67acb7SJed Brown 
37699f67acb7SJed Brown    Notes:
37709f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
37719f67acb7SJed Brown 
37729f67acb7SJed Brown    Level: intermediate
37739f67acb7SJed Brown 
37749f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
37759f67acb7SJed Brown 
37765ef26d82SJed Brown .seealso:  TSGetKSPIterations()
37779f67acb7SJed Brown @*/
37785ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
37799f67acb7SJed Brown {
37809f67acb7SJed Brown   PetscFunctionBegin;
37819f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37829f67acb7SJed Brown   PetscValidIntPointer(nits,2);
37835ef26d82SJed Brown   *nits = ts->snes_its;
37849f67acb7SJed Brown   PetscFunctionReturn(0);
37859f67acb7SJed Brown }
37869f67acb7SJed Brown 
37879f67acb7SJed Brown #undef __FUNCT__
37885ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
37899f67acb7SJed Brown /*@
37905ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
37919f67acb7SJed Brown    used by the time integrator.
37929f67acb7SJed Brown 
37939f67acb7SJed Brown    Not Collective
37949f67acb7SJed Brown 
37959f67acb7SJed Brown    Input Parameter:
37969f67acb7SJed Brown .  ts - TS context
37979f67acb7SJed Brown 
37989f67acb7SJed Brown    Output Parameter:
37999f67acb7SJed Brown .  lits - number of linear iterations
38009f67acb7SJed Brown 
38019f67acb7SJed Brown    Notes:
38029f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
38039f67acb7SJed Brown 
38049f67acb7SJed Brown    Level: intermediate
38059f67acb7SJed Brown 
38069f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
38079f67acb7SJed Brown 
38085ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
38099f67acb7SJed Brown @*/
38105ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
38119f67acb7SJed Brown {
38129f67acb7SJed Brown   PetscFunctionBegin;
38139f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38149f67acb7SJed Brown   PetscValidIntPointer(lits,2);
38155ef26d82SJed Brown   *lits = ts->ksp_its;
38169f67acb7SJed Brown   PetscFunctionReturn(0);
38179f67acb7SJed Brown }
38189f67acb7SJed Brown 
38199f67acb7SJed Brown #undef __FUNCT__
3820cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3821cef5090cSJed Brown /*@
3822cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3823cef5090cSJed Brown 
3824cef5090cSJed Brown    Not Collective
3825cef5090cSJed Brown 
3826cef5090cSJed Brown    Input Parameter:
3827cef5090cSJed Brown .  ts - TS context
3828cef5090cSJed Brown 
3829cef5090cSJed Brown    Output Parameter:
3830cef5090cSJed Brown .  rejects - number of steps rejected
3831cef5090cSJed Brown 
3832cef5090cSJed Brown    Notes:
3833cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3834cef5090cSJed Brown 
3835cef5090cSJed Brown    Level: intermediate
3836cef5090cSJed Brown 
3837cef5090cSJed Brown .keywords: TS, get, number
3838cef5090cSJed Brown 
38395ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3840cef5090cSJed Brown @*/
3841cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3842cef5090cSJed Brown {
3843cef5090cSJed Brown   PetscFunctionBegin;
3844cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3845cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3846cef5090cSJed Brown   *rejects = ts->reject;
3847cef5090cSJed Brown   PetscFunctionReturn(0);
3848cef5090cSJed Brown }
3849cef5090cSJed Brown 
3850cef5090cSJed Brown #undef __FUNCT__
3851cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3852cef5090cSJed Brown /*@
3853cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3854cef5090cSJed Brown 
3855cef5090cSJed Brown    Not Collective
3856cef5090cSJed Brown 
3857cef5090cSJed Brown    Input Parameter:
3858cef5090cSJed Brown .  ts - TS context
3859cef5090cSJed Brown 
3860cef5090cSJed Brown    Output Parameter:
3861cef5090cSJed Brown .  fails - number of failed nonlinear solves
3862cef5090cSJed Brown 
3863cef5090cSJed Brown    Notes:
3864cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3865cef5090cSJed Brown 
3866cef5090cSJed Brown    Level: intermediate
3867cef5090cSJed Brown 
3868cef5090cSJed Brown .keywords: TS, get, number
3869cef5090cSJed Brown 
38705ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3871cef5090cSJed Brown @*/
3872cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3873cef5090cSJed Brown {
3874cef5090cSJed Brown   PetscFunctionBegin;
3875cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3876cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3877cef5090cSJed Brown   *fails = ts->num_snes_failures;
3878cef5090cSJed Brown   PetscFunctionReturn(0);
3879cef5090cSJed Brown }
3880cef5090cSJed Brown 
3881cef5090cSJed Brown #undef __FUNCT__
3882cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3883cef5090cSJed Brown /*@
3884cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3885cef5090cSJed Brown 
3886cef5090cSJed Brown    Not Collective
3887cef5090cSJed Brown 
3888cef5090cSJed Brown    Input Parameter:
3889cef5090cSJed Brown +  ts - TS context
3890cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3891cef5090cSJed Brown 
3892cef5090cSJed Brown    Notes:
3893cef5090cSJed Brown    The counter is reset to zero for each step
3894cef5090cSJed Brown 
3895cef5090cSJed Brown    Options Database Key:
3896cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3897cef5090cSJed Brown 
3898cef5090cSJed Brown    Level: intermediate
3899cef5090cSJed Brown 
3900cef5090cSJed Brown .keywords: TS, set, maximum, number
3901cef5090cSJed Brown 
39025ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3903cef5090cSJed Brown @*/
3904cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3905cef5090cSJed Brown {
3906cef5090cSJed Brown   PetscFunctionBegin;
3907cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3908cef5090cSJed Brown   ts->max_reject = rejects;
3909cef5090cSJed Brown   PetscFunctionReturn(0);
3910cef5090cSJed Brown }
3911cef5090cSJed Brown 
3912cef5090cSJed Brown #undef __FUNCT__
3913cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3914cef5090cSJed Brown /*@
3915cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3916cef5090cSJed Brown 
3917cef5090cSJed Brown    Not Collective
3918cef5090cSJed Brown 
3919cef5090cSJed Brown    Input Parameter:
3920cef5090cSJed Brown +  ts - TS context
3921cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3922cef5090cSJed Brown 
3923cef5090cSJed Brown    Notes:
3924cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3925cef5090cSJed Brown 
3926cef5090cSJed Brown    Options Database Key:
3927cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3928cef5090cSJed Brown 
3929cef5090cSJed Brown    Level: intermediate
3930cef5090cSJed Brown 
3931cef5090cSJed Brown .keywords: TS, set, maximum, number
3932cef5090cSJed Brown 
39335ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3934cef5090cSJed Brown @*/
3935cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3936cef5090cSJed Brown {
3937cef5090cSJed Brown   PetscFunctionBegin;
3938cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3939cef5090cSJed Brown   ts->max_snes_failures = fails;
3940cef5090cSJed Brown   PetscFunctionReturn(0);
3941cef5090cSJed Brown }
3942cef5090cSJed Brown 
3943cef5090cSJed Brown #undef __FUNCT__
3944cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3945cef5090cSJed Brown /*@
3946cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3947cef5090cSJed Brown 
3948cef5090cSJed Brown    Not Collective
3949cef5090cSJed Brown 
3950cef5090cSJed Brown    Input Parameter:
3951cef5090cSJed Brown +  ts - TS context
3952cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3953cef5090cSJed Brown 
3954cef5090cSJed Brown    Options Database Key:
3955cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3956cef5090cSJed Brown 
3957cef5090cSJed Brown    Level: intermediate
3958cef5090cSJed Brown 
3959cef5090cSJed Brown .keywords: TS, set, error
3960cef5090cSJed Brown 
39615ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3962cef5090cSJed Brown @*/
3963cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3964cef5090cSJed Brown {
3965cef5090cSJed Brown   PetscFunctionBegin;
3966cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3967cef5090cSJed Brown   ts->errorifstepfailed = err;
3968cef5090cSJed Brown   PetscFunctionReturn(0);
3969cef5090cSJed Brown }
3970cef5090cSJed Brown 
3971cef5090cSJed Brown #undef __FUNCT__
3972fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3973fb1732b5SBarry Smith /*@C
3974fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3975fb1732b5SBarry Smith 
3976fb1732b5SBarry Smith    Collective on TS
3977fb1732b5SBarry Smith 
3978fb1732b5SBarry Smith    Input Parameters:
3979fb1732b5SBarry Smith +  ts - the TS context
3980fb1732b5SBarry Smith .  step - current time-step
3981fb1732b5SBarry Smith .  ptime - current time
39820910c330SBarry Smith .  u - current state
3983fb1732b5SBarry Smith -  viewer - binary viewer
3984fb1732b5SBarry Smith 
3985fb1732b5SBarry Smith    Level: intermediate
3986fb1732b5SBarry Smith 
3987fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3988fb1732b5SBarry Smith 
3989fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3990fb1732b5SBarry Smith @*/
39910910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3992fb1732b5SBarry Smith {
3993fb1732b5SBarry Smith   PetscErrorCode ierr;
3994ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3995fb1732b5SBarry Smith 
3996fb1732b5SBarry Smith   PetscFunctionBegin;
39970910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3998ed81e22dSJed Brown   PetscFunctionReturn(0);
3999ed81e22dSJed Brown }
4000ed81e22dSJed Brown 
4001ed81e22dSJed Brown #undef __FUNCT__
4002ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4003ed81e22dSJed Brown /*@C
4004ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4005ed81e22dSJed Brown 
4006ed81e22dSJed Brown    Collective on TS
4007ed81e22dSJed Brown 
4008ed81e22dSJed Brown    Input Parameters:
4009ed81e22dSJed Brown +  ts - the TS context
4010ed81e22dSJed Brown .  step - current time-step
4011ed81e22dSJed Brown .  ptime - current time
40120910c330SBarry Smith .  u - current state
4013ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4014ed81e22dSJed Brown 
4015ed81e22dSJed Brown    Level: intermediate
4016ed81e22dSJed Brown 
4017ed81e22dSJed Brown    Notes:
4018ed81e22dSJed 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.
4019ed81e22dSJed Brown    These are named according to the file name template.
4020ed81e22dSJed Brown 
4021ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4022ed81e22dSJed Brown 
4023ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4024ed81e22dSJed Brown 
4025ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4026ed81e22dSJed Brown @*/
40270910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4028ed81e22dSJed Brown {
4029ed81e22dSJed Brown   PetscErrorCode ierr;
4030ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4031ed81e22dSJed Brown   PetscViewer    viewer;
4032ed81e22dSJed Brown 
4033ed81e22dSJed Brown   PetscFunctionBegin;
40348caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4035ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
40360910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4037ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4038ed81e22dSJed Brown   PetscFunctionReturn(0);
4039ed81e22dSJed Brown }
4040ed81e22dSJed Brown 
4041ed81e22dSJed Brown #undef __FUNCT__
4042ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4043ed81e22dSJed Brown /*@C
4044ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4045ed81e22dSJed Brown 
4046ed81e22dSJed Brown    Collective on TS
4047ed81e22dSJed Brown 
4048ed81e22dSJed Brown    Input Parameters:
4049ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4050ed81e22dSJed Brown 
4051ed81e22dSJed Brown    Level: intermediate
4052ed81e22dSJed Brown 
4053ed81e22dSJed Brown    Note:
4054ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4055ed81e22dSJed Brown 
4056ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4057ed81e22dSJed Brown 
4058ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4059ed81e22dSJed Brown @*/
4060ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4061ed81e22dSJed Brown {
4062ed81e22dSJed Brown   PetscErrorCode ierr;
4063ed81e22dSJed Brown 
4064ed81e22dSJed Brown   PetscFunctionBegin;
4065ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4066fb1732b5SBarry Smith   PetscFunctionReturn(0);
4067fb1732b5SBarry Smith }
4068fb1732b5SBarry Smith 
406984df9cb4SJed Brown #undef __FUNCT__
4070552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
407184df9cb4SJed Brown /*@
4072552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
407384df9cb4SJed Brown 
4074ed81e22dSJed Brown    Collective on TS if controller has not been created yet
407584df9cb4SJed Brown 
407684df9cb4SJed Brown    Input Arguments:
4077ed81e22dSJed Brown .  ts - time stepping context
407884df9cb4SJed Brown 
407984df9cb4SJed Brown    Output Arguments:
4080ed81e22dSJed Brown .  adapt - adaptive controller
408184df9cb4SJed Brown 
408284df9cb4SJed Brown    Level: intermediate
408384df9cb4SJed Brown 
4084ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
408584df9cb4SJed Brown @*/
4086552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
408784df9cb4SJed Brown {
408884df9cb4SJed Brown   PetscErrorCode ierr;
408984df9cb4SJed Brown 
409084df9cb4SJed Brown   PetscFunctionBegin;
409184df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
409284df9cb4SJed Brown   PetscValidPointer(adapt,2);
409384df9cb4SJed Brown   if (!ts->adapt) {
4094ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
40951c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
40961c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
409784df9cb4SJed Brown   }
409884df9cb4SJed Brown   *adapt = ts->adapt;
409984df9cb4SJed Brown   PetscFunctionReturn(0);
410084df9cb4SJed Brown }
4101d6ebe24aSShri Abhyankar 
4102d6ebe24aSShri Abhyankar #undef __FUNCT__
41031c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
41041c3436cfSJed Brown /*@
41051c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
41061c3436cfSJed Brown 
41071c3436cfSJed Brown    Logically Collective
41081c3436cfSJed Brown 
41091c3436cfSJed Brown    Input Arguments:
41101c3436cfSJed Brown +  ts - time integration context
41111c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
41120298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
41131c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
41140298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
41151c3436cfSJed Brown 
41161c3436cfSJed Brown    Level: beginner
41171c3436cfSJed Brown 
4118c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
41191c3436cfSJed Brown @*/
41201c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
41211c3436cfSJed Brown {
41221c3436cfSJed Brown   PetscErrorCode ierr;
41231c3436cfSJed Brown 
41241c3436cfSJed Brown   PetscFunctionBegin;
4125c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
41261c3436cfSJed Brown   if (vatol) {
41271c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
41281c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4129bbd56ea5SKarl Rupp 
41301c3436cfSJed Brown     ts->vatol = vatol;
41311c3436cfSJed Brown   }
4132c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
41331c3436cfSJed Brown   if (vrtol) {
41341c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
41351c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4136bbd56ea5SKarl Rupp 
41371c3436cfSJed Brown     ts->vrtol = vrtol;
41381c3436cfSJed Brown   }
41391c3436cfSJed Brown   PetscFunctionReturn(0);
41401c3436cfSJed Brown }
41411c3436cfSJed Brown 
41421c3436cfSJed Brown #undef __FUNCT__
4143c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4144c5033834SJed Brown /*@
4145c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4146c5033834SJed Brown 
4147c5033834SJed Brown    Logically Collective
4148c5033834SJed Brown 
4149c5033834SJed Brown    Input Arguments:
4150c5033834SJed Brown .  ts - time integration context
4151c5033834SJed Brown 
4152c5033834SJed Brown    Output Arguments:
41530298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
41540298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
41550298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
41560298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4157c5033834SJed Brown 
4158c5033834SJed Brown    Level: beginner
4159c5033834SJed Brown 
4160c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4161c5033834SJed Brown @*/
4162c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4163c5033834SJed Brown {
4164c5033834SJed Brown   PetscFunctionBegin;
4165c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4166c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4167c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4168c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4169c5033834SJed Brown   PetscFunctionReturn(0);
4170c5033834SJed Brown }
4171c5033834SJed Brown 
4172c5033834SJed Brown #undef __FUNCT__
41731c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
41741c3436cfSJed Brown /*@
41751c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
41761c3436cfSJed Brown 
41771c3436cfSJed Brown    Collective on TS
41781c3436cfSJed Brown 
41791c3436cfSJed Brown    Input Arguments:
41801c3436cfSJed Brown +  ts - time stepping context
41811c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
41821c3436cfSJed Brown 
41831c3436cfSJed Brown    Output Arguments:
41841c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
41851c3436cfSJed Brown 
41861c3436cfSJed Brown    Level: developer
41871c3436cfSJed Brown 
41881c3436cfSJed Brown .seealso: TSSetTolerances()
41891c3436cfSJed Brown @*/
41901c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
41911c3436cfSJed Brown {
41921c3436cfSJed Brown   PetscErrorCode    ierr;
41931c3436cfSJed Brown   PetscInt          i,n,N;
41940910c330SBarry Smith   const PetscScalar *u,*y;
41950910c330SBarry Smith   Vec               U;
41961c3436cfSJed Brown   PetscReal         sum,gsum;
41971c3436cfSJed Brown 
41981c3436cfSJed Brown   PetscFunctionBegin;
41991c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42001c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
42011c3436cfSJed Brown   PetscValidPointer(norm,3);
42020910c330SBarry Smith   U = ts->vec_sol;
42030910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4204ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
42051c3436cfSJed Brown 
42060910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
42070910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
42080910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
42091c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
42101c3436cfSJed Brown   sum  = 0.;
4211ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4212ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4213ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4214ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4215ceefc113SJed Brown     for (i=0; i<n; i++) {
42160910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42170910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4218ceefc113SJed Brown     }
4219ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4220ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4221ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4222ceefc113SJed Brown     const PetscScalar *atol;
4223ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4224ceefc113SJed Brown     for (i=0; i<n; i++) {
42250910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42260910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4227ceefc113SJed Brown     }
4228ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4229ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4230ceefc113SJed Brown     const PetscScalar *rtol;
4231ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4232ceefc113SJed Brown     for (i=0; i<n; i++) {
42330910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42340910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4235ceefc113SJed Brown     }
4236ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4237ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
42381c3436cfSJed Brown     for (i=0; i<n; i++) {
42390910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42400910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
42411c3436cfSJed Brown     }
4242ceefc113SJed Brown   }
42430910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
42441c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
42451c3436cfSJed Brown 
4246ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
42471c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4248ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
42491c3436cfSJed Brown   PetscFunctionReturn(0);
42501c3436cfSJed Brown }
42511c3436cfSJed Brown 
42521c3436cfSJed Brown #undef __FUNCT__
42538d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
42548d59e960SJed Brown /*@
42558d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
42568d59e960SJed Brown 
42578d59e960SJed Brown    Logically Collective on TS
42588d59e960SJed Brown 
42598d59e960SJed Brown    Input Arguments:
42608d59e960SJed Brown +  ts - time stepping context
42618d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
42628d59e960SJed Brown 
42638d59e960SJed Brown    Note:
42648d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
42658d59e960SJed Brown 
42668d59e960SJed Brown    Level: intermediate
42678d59e960SJed Brown 
42688d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
42698d59e960SJed Brown @*/
42708d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
42718d59e960SJed Brown {
42728d59e960SJed Brown   PetscFunctionBegin;
42738d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42748d59e960SJed Brown   ts->cfltime_local = cfltime;
42758d59e960SJed Brown   ts->cfltime       = -1.;
42768d59e960SJed Brown   PetscFunctionReturn(0);
42778d59e960SJed Brown }
42788d59e960SJed Brown 
42798d59e960SJed Brown #undef __FUNCT__
42808d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
42818d59e960SJed Brown /*@
42828d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
42838d59e960SJed Brown 
42848d59e960SJed Brown    Collective on TS
42858d59e960SJed Brown 
42868d59e960SJed Brown    Input Arguments:
42878d59e960SJed Brown .  ts - time stepping context
42888d59e960SJed Brown 
42898d59e960SJed Brown    Output Arguments:
42908d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
42918d59e960SJed Brown 
42928d59e960SJed Brown    Level: advanced
42938d59e960SJed Brown 
42948d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
42958d59e960SJed Brown @*/
42968d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
42978d59e960SJed Brown {
42988d59e960SJed Brown   PetscErrorCode ierr;
42998d59e960SJed Brown 
43008d59e960SJed Brown   PetscFunctionBegin;
43018d59e960SJed Brown   if (ts->cfltime < 0) {
4302ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
43038d59e960SJed Brown   }
43048d59e960SJed Brown   *cfltime = ts->cfltime;
43058d59e960SJed Brown   PetscFunctionReturn(0);
43068d59e960SJed Brown }
43078d59e960SJed Brown 
43088d59e960SJed Brown #undef __FUNCT__
4309d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4310d6ebe24aSShri Abhyankar /*@
4311d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4312d6ebe24aSShri Abhyankar 
4313d6ebe24aSShri Abhyankar    Input Parameters:
4314d6ebe24aSShri Abhyankar .  ts   - the TS context.
4315d6ebe24aSShri Abhyankar .  xl   - lower bound.
4316d6ebe24aSShri Abhyankar .  xu   - upper bound.
4317d6ebe24aSShri Abhyankar 
4318d6ebe24aSShri Abhyankar    Notes:
4319d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
432033c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4321d6ebe24aSShri Abhyankar 
43222bd2b0e6SSatish Balay    Level: advanced
43232bd2b0e6SSatish Balay 
4324d6ebe24aSShri Abhyankar @*/
4325d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4326d6ebe24aSShri Abhyankar {
4327d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4328d6ebe24aSShri Abhyankar   SNES           snes;
4329d6ebe24aSShri Abhyankar 
4330d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4331d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4332d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4333d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4334d6ebe24aSShri Abhyankar }
4335d6ebe24aSShri Abhyankar 
4336325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4337c6db04a5SJed Brown #include <mex.h>
4338325fc9f4SBarry Smith 
4339325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4340325fc9f4SBarry Smith 
4341325fc9f4SBarry Smith #undef __FUNCT__
4342325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4343325fc9f4SBarry Smith /*
4344325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4345325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4346325fc9f4SBarry Smith 
4347325fc9f4SBarry Smith    Collective on TS
4348325fc9f4SBarry Smith 
4349325fc9f4SBarry Smith    Input Parameters:
4350325fc9f4SBarry Smith +  snes - the TS context
43510910c330SBarry Smith -  u - input vector
4352325fc9f4SBarry Smith 
4353325fc9f4SBarry Smith    Output Parameter:
4354325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4355325fc9f4SBarry Smith 
4356325fc9f4SBarry Smith    Notes:
4357325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4358325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4359325fc9f4SBarry Smith    themselves.
4360325fc9f4SBarry Smith 
4361325fc9f4SBarry Smith    Level: developer
4362325fc9f4SBarry Smith 
4363325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4364325fc9f4SBarry Smith 
4365325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4366325fc9f4SBarry Smith */
43670910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4368325fc9f4SBarry Smith {
4369325fc9f4SBarry Smith   PetscErrorCode  ierr;
4370325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4371325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4372325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4373325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4374325fc9f4SBarry Smith 
4375325fc9f4SBarry Smith   PetscFunctionBegin;
4376325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
43770910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
43780910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4379325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
43800910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4381325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4382325fc9f4SBarry Smith 
4383325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
43840910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43850910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
43860910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4387bbd56ea5SKarl Rupp 
4388325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4389325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4390325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4391325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4392325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4393325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4394325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4395325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4396325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4397325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4398325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4399325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4400325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4401325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4402325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4403325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4404325fc9f4SBarry Smith   PetscFunctionReturn(0);
4405325fc9f4SBarry Smith }
4406325fc9f4SBarry Smith 
4407325fc9f4SBarry Smith 
4408325fc9f4SBarry Smith #undef __FUNCT__
4409325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4410325fc9f4SBarry Smith /*
4411325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4412325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4413e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4414325fc9f4SBarry Smith 
4415325fc9f4SBarry Smith    Logically Collective on TS
4416325fc9f4SBarry Smith 
4417325fc9f4SBarry Smith    Input Parameters:
4418325fc9f4SBarry Smith +  ts - the TS context
4419325fc9f4SBarry Smith -  func - function evaluation routine
4420325fc9f4SBarry Smith 
4421325fc9f4SBarry Smith    Calling sequence of func:
44220910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4423325fc9f4SBarry Smith 
4424325fc9f4SBarry Smith    Level: beginner
4425325fc9f4SBarry Smith 
4426325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4427325fc9f4SBarry Smith 
4428325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4429325fc9f4SBarry Smith */
4430cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4431325fc9f4SBarry Smith {
4432325fc9f4SBarry Smith   PetscErrorCode  ierr;
4433325fc9f4SBarry Smith   TSMatlabContext *sctx;
4434325fc9f4SBarry Smith 
4435325fc9f4SBarry Smith   PetscFunctionBegin;
4436325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4437325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4438325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4439325fc9f4SBarry Smith   /*
4440325fc9f4SBarry Smith      This should work, but it doesn't
4441325fc9f4SBarry Smith   sctx->ctx = ctx;
4442325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4443325fc9f4SBarry Smith   */
4444325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4445bbd56ea5SKarl Rupp 
44460298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4447325fc9f4SBarry Smith   PetscFunctionReturn(0);
4448325fc9f4SBarry Smith }
4449325fc9f4SBarry Smith 
4450325fc9f4SBarry Smith #undef __FUNCT__
4451325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4452325fc9f4SBarry Smith /*
4453325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4454325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4455325fc9f4SBarry Smith 
4456325fc9f4SBarry Smith    Collective on TS
4457325fc9f4SBarry Smith 
4458325fc9f4SBarry Smith    Input Parameters:
4459cdcf91faSSean Farley +  ts - the TS context
44600910c330SBarry Smith .  u - input vector
4461325fc9f4SBarry Smith .  A, B - the matrices
4462325fc9f4SBarry Smith -  ctx - user context
4463325fc9f4SBarry Smith 
4464325fc9f4SBarry Smith    Output Parameter:
4465325fc9f4SBarry Smith .  flag - structure of the matrix
4466325fc9f4SBarry Smith 
4467325fc9f4SBarry Smith    Level: developer
4468325fc9f4SBarry Smith 
4469325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4470325fc9f4SBarry Smith 
4471325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4472325fc9f4SBarry Smith @*/
44730910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4474325fc9f4SBarry Smith {
4475325fc9f4SBarry Smith   PetscErrorCode  ierr;
4476325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4477325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4478325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4479325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4480325fc9f4SBarry Smith 
4481325fc9f4SBarry Smith   PetscFunctionBegin;
4482cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44830910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4484325fc9f4SBarry Smith 
44850910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4486325fc9f4SBarry Smith 
4487cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
44880910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
44890910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
44900910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
44910910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4492bbd56ea5SKarl Rupp 
4493325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4494325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4495325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4496325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4497325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4498325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4499325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4500325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4501325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4502325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4503325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4504325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4505325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4506325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4507325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4508325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4509325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4510325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4511325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4512325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4513325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4514325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4515325fc9f4SBarry Smith   PetscFunctionReturn(0);
4516325fc9f4SBarry Smith }
4517325fc9f4SBarry Smith 
4518325fc9f4SBarry Smith 
4519325fc9f4SBarry Smith #undef __FUNCT__
4520325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4521325fc9f4SBarry Smith /*
4522325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4523e3c5b3baSBarry 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
4524325fc9f4SBarry Smith 
4525325fc9f4SBarry Smith    Logically Collective on TS
4526325fc9f4SBarry Smith 
4527325fc9f4SBarry Smith    Input Parameters:
4528cdcf91faSSean Farley +  ts - the TS context
4529325fc9f4SBarry Smith .  A,B - Jacobian matrices
4530325fc9f4SBarry Smith .  func - function evaluation routine
4531325fc9f4SBarry Smith -  ctx - user context
4532325fc9f4SBarry Smith 
4533325fc9f4SBarry Smith    Calling sequence of func:
45340910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4535325fc9f4SBarry Smith 
4536325fc9f4SBarry Smith 
4537325fc9f4SBarry Smith    Level: developer
4538325fc9f4SBarry Smith 
4539325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4540325fc9f4SBarry Smith 
4541325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4542325fc9f4SBarry Smith */
4543cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4544325fc9f4SBarry Smith {
4545325fc9f4SBarry Smith   PetscErrorCode  ierr;
4546325fc9f4SBarry Smith   TSMatlabContext *sctx;
4547325fc9f4SBarry Smith 
4548325fc9f4SBarry Smith   PetscFunctionBegin;
4549325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4550325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4551325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4552325fc9f4SBarry Smith   /*
4553325fc9f4SBarry Smith      This should work, but it doesn't
4554325fc9f4SBarry Smith   sctx->ctx = ctx;
4555325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4556325fc9f4SBarry Smith   */
4557325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4558bbd56ea5SKarl Rupp 
4559cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4560325fc9f4SBarry Smith   PetscFunctionReturn(0);
4561325fc9f4SBarry Smith }
4562325fc9f4SBarry Smith 
4563b5b1a830SBarry Smith #undef __FUNCT__
4564b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4565b5b1a830SBarry Smith /*
4566b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4567b5b1a830SBarry Smith 
4568b5b1a830SBarry Smith    Collective on TS
4569b5b1a830SBarry Smith 
4570b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4571b5b1a830SBarry Smith @*/
45720910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4573b5b1a830SBarry Smith {
4574b5b1a830SBarry Smith   PetscErrorCode  ierr;
4575b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4576a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4577b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4578b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4579b5b1a830SBarry Smith 
4580b5b1a830SBarry Smith   PetscFunctionBegin;
4581cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45820910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4583b5b1a830SBarry Smith 
4584cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
45850910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4586bbd56ea5SKarl Rupp 
4587b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4588b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4589b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4590b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4591b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4592b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4593b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4594b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4595b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4596b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4597b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4598b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4599b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4600b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4601b5b1a830SBarry Smith   PetscFunctionReturn(0);
4602b5b1a830SBarry Smith }
4603b5b1a830SBarry Smith 
4604b5b1a830SBarry Smith 
4605b5b1a830SBarry Smith #undef __FUNCT__
4606b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4607b5b1a830SBarry Smith /*
4608b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4609b5b1a830SBarry Smith 
4610b5b1a830SBarry Smith    Level: developer
4611b5b1a830SBarry Smith 
4612b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4613b5b1a830SBarry Smith 
4614b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4615b5b1a830SBarry Smith */
4616cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4617b5b1a830SBarry Smith {
4618b5b1a830SBarry Smith   PetscErrorCode  ierr;
4619b5b1a830SBarry Smith   TSMatlabContext *sctx;
4620b5b1a830SBarry Smith 
4621b5b1a830SBarry Smith   PetscFunctionBegin;
4622b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4623b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4624b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4625b5b1a830SBarry Smith   /*
4626b5b1a830SBarry Smith      This should work, but it doesn't
4627b5b1a830SBarry Smith   sctx->ctx = ctx;
4628b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4629b5b1a830SBarry Smith   */
4630b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4631bbd56ea5SKarl Rupp 
46320298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4633b5b1a830SBarry Smith   PetscFunctionReturn(0);
4634b5b1a830SBarry Smith }
4635325fc9f4SBarry Smith #endif
4636b3603a34SBarry Smith 
46370b039ecaSBarry Smith 
46380b039ecaSBarry Smith 
4639b3603a34SBarry Smith #undef __FUNCT__
46404f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4641b3603a34SBarry Smith /*@C
46424f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4643b3603a34SBarry Smith        in a time based line graph
4644b3603a34SBarry Smith 
4645b3603a34SBarry Smith    Collective on TS
4646b3603a34SBarry Smith 
4647b3603a34SBarry Smith    Input Parameters:
4648b3603a34SBarry Smith +  ts - the TS context
4649b3603a34SBarry Smith .  step - current time-step
4650b3603a34SBarry Smith .  ptime - current time
4651b3603a34SBarry Smith -  lg - a line graph object
4652b3603a34SBarry Smith 
4653b3603a34SBarry Smith    Level: intermediate
4654b3603a34SBarry Smith 
46550b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4656b3603a34SBarry Smith 
4657b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4658b3603a34SBarry Smith 
4659b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4660b3603a34SBarry Smith @*/
46610910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4662b3603a34SBarry Smith {
4663b3603a34SBarry Smith   PetscErrorCode    ierr;
46640b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4665b3603a34SBarry Smith   const PetscScalar *yy;
466658ff32f7SBarry Smith   PetscInt          dim;
4667b3603a34SBarry Smith 
4668b3603a34SBarry Smith   PetscFunctionBegin;
466958ff32f7SBarry Smith   if (!step) {
4670a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4671a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4672a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
46730910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
46740b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
46750b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
467658ff32f7SBarry Smith   }
46770910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4678e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4679e3efe391SJed Brown   {
4680e3efe391SJed Brown     PetscReal *yreal;
4681e3efe391SJed Brown     PetscInt  i,n;
46820910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4683e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4684e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
46850b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4686e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4687e3efe391SJed Brown   }
4688e3efe391SJed Brown #else
46890b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4690e3efe391SJed Brown #endif
46910910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
4692b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
46930b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46943923b477SBarry Smith   }
4695b3603a34SBarry Smith   PetscFunctionReturn(0);
4696b3603a34SBarry Smith }
4697b3603a34SBarry Smith 
4698b3603a34SBarry Smith #undef __FUNCT__
46994f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4700ef20d060SBarry Smith /*@C
47014f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4702ef20d060SBarry Smith        in a time based line graph
4703ef20d060SBarry Smith 
4704ef20d060SBarry Smith    Collective on TS
4705ef20d060SBarry Smith 
4706ef20d060SBarry Smith    Input Parameters:
4707ef20d060SBarry Smith +  ts - the TS context
4708ef20d060SBarry Smith .  step - current time-step
4709ef20d060SBarry Smith .  ptime - current time
4710ef20d060SBarry Smith -  lg - a line graph object
4711ef20d060SBarry Smith 
4712ef20d060SBarry Smith    Level: intermediate
4713ef20d060SBarry Smith 
4714abd5a294SJed Brown    Notes:
4715abd5a294SJed Brown    Only for sequential solves.
4716abd5a294SJed Brown 
4717abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4718abd5a294SJed Brown 
4719abd5a294SJed Brown    Options Database Keys:
47204f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4721ef20d060SBarry Smith 
4722ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4723ef20d060SBarry Smith 
4724abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4725ef20d060SBarry Smith @*/
47260910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4727ef20d060SBarry Smith {
4728ef20d060SBarry Smith   PetscErrorCode    ierr;
47290b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4730ef20d060SBarry Smith   const PetscScalar *yy;
4731ef20d060SBarry Smith   Vec               y;
4732a9f9c1f6SBarry Smith   PetscInt          dim;
4733ef20d060SBarry Smith 
4734ef20d060SBarry Smith   PetscFunctionBegin;
4735a9f9c1f6SBarry Smith   if (!step) {
4736a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4737a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
47381ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
47390910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4740a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4741a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4742a9f9c1f6SBarry Smith   }
47430910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4744ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
47450910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4746ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4747e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4748e3efe391SJed Brown   {
4749e3efe391SJed Brown     PetscReal *yreal;
4750e3efe391SJed Brown     PetscInt  i,n;
4751e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4752e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4753e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
47540b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4755e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4756e3efe391SJed Brown   }
4757e3efe391SJed Brown #else
47580b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4759e3efe391SJed Brown #endif
4760ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4761ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
4762b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
47630b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
47643923b477SBarry Smith   }
4765ef20d060SBarry Smith   PetscFunctionReturn(0);
4766ef20d060SBarry Smith }
4767ef20d060SBarry Smith 
4768201da799SBarry Smith #undef __FUNCT__
4769201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4770201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4771201da799SBarry Smith {
4772201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4773201da799SBarry Smith   PetscReal      x   = ptime,y;
4774201da799SBarry Smith   PetscErrorCode ierr;
4775201da799SBarry Smith   PetscInt       its;
4776201da799SBarry Smith 
4777201da799SBarry Smith   PetscFunctionBegin;
4778201da799SBarry Smith   if (!n) {
4779201da799SBarry Smith     PetscDrawAxis axis;
4780bbd56ea5SKarl Rupp 
4781201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4782201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4783201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4784bbd56ea5SKarl Rupp 
4785201da799SBarry Smith     ctx->snes_its = 0;
4786201da799SBarry Smith   }
4787201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4788201da799SBarry Smith   y    = its - ctx->snes_its;
4789201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
47903fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4791201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4792201da799SBarry Smith   }
4793201da799SBarry Smith   ctx->snes_its = its;
4794201da799SBarry Smith   PetscFunctionReturn(0);
4795201da799SBarry Smith }
4796201da799SBarry Smith 
4797201da799SBarry Smith #undef __FUNCT__
4798201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4799201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4800201da799SBarry Smith {
4801201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4802201da799SBarry Smith   PetscReal      x   = ptime,y;
4803201da799SBarry Smith   PetscErrorCode ierr;
4804201da799SBarry Smith   PetscInt       its;
4805201da799SBarry Smith 
4806201da799SBarry Smith   PetscFunctionBegin;
4807201da799SBarry Smith   if (!n) {
4808201da799SBarry Smith     PetscDrawAxis axis;
4809bbd56ea5SKarl Rupp 
4810201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4811201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4812201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4813bbd56ea5SKarl Rupp 
4814201da799SBarry Smith     ctx->ksp_its = 0;
4815201da799SBarry Smith   }
4816201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4817201da799SBarry Smith   y    = its - ctx->ksp_its;
4818201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
481999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4820201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4821201da799SBarry Smith   }
4822201da799SBarry Smith   ctx->ksp_its = its;
4823201da799SBarry Smith   PetscFunctionReturn(0);
4824201da799SBarry Smith }
4825f9c1d6abSBarry Smith 
4826f9c1d6abSBarry Smith #undef __FUNCT__
4827f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4828f9c1d6abSBarry Smith /*@
4829f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4830f9c1d6abSBarry Smith 
4831f9c1d6abSBarry Smith    Collective on TS and Vec
4832f9c1d6abSBarry Smith 
4833f9c1d6abSBarry Smith    Input Parameters:
4834f9c1d6abSBarry Smith +  ts - the TS context
4835f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4836f9c1d6abSBarry Smith 
4837f9c1d6abSBarry Smith    Output Parameters:
4838f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4839f9c1d6abSBarry Smith 
4840f9c1d6abSBarry Smith    Level: developer
4841f9c1d6abSBarry Smith 
4842f9c1d6abSBarry Smith .keywords: TS, compute
4843f9c1d6abSBarry Smith 
4844f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4845f9c1d6abSBarry Smith @*/
4846f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4847f9c1d6abSBarry Smith {
4848f9c1d6abSBarry Smith   PetscErrorCode ierr;
4849f9c1d6abSBarry Smith 
4850f9c1d6abSBarry Smith   PetscFunctionBegin;
4851f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4852ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4853f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4854f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4855f9c1d6abSBarry Smith }
4856