xref: /petsc/src/ts/interface/ts.c (revision b29392e18ae60063e82936962b39d2cdd1e3e46c)
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 
288ad6bc421SBarry Smith   ierr = TSGetTSAdapt(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 
36324989b8cSPeter Brune   if (rhsjacobianfunc) {
3640910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
365a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
366a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3670910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
368a7a1495cSBarry Smith     PetscStackPop;
3690910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
370a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3710700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3720700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
373ef66eb69SBarry Smith   } else {
374214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
375214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
376214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
377ef66eb69SBarry Smith   }
3780e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3790910c330SBarry Smith   ts->rhsjacobian.X          = U;
3800910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3810e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
382a7a1495cSBarry Smith   PetscFunctionReturn(0);
383a7a1495cSBarry Smith }
384a7a1495cSBarry Smith 
3854a2ae208SSatish Balay #undef __FUNCT__
3864a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
387316643e7SJed Brown /*@
388d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
389d763cef2SBarry Smith 
390316643e7SJed Brown    Collective on TS and Vec
391316643e7SJed Brown 
392316643e7SJed Brown    Input Parameters:
393316643e7SJed Brown +  ts - the TS context
394316643e7SJed Brown .  t - current time
3950910c330SBarry Smith -  U - state vector
396316643e7SJed Brown 
397316643e7SJed Brown    Output Parameter:
398316643e7SJed Brown .  y - right hand side
399316643e7SJed Brown 
400316643e7SJed Brown    Note:
401316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
402316643e7SJed Brown    is used internally within the nonlinear solvers.
403316643e7SJed Brown 
404316643e7SJed Brown    Level: developer
405316643e7SJed Brown 
406316643e7SJed Brown .keywords: TS, compute
407316643e7SJed Brown 
408316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
409316643e7SJed Brown @*/
4100910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
411d763cef2SBarry Smith {
412dfbe8321SBarry Smith   PetscErrorCode ierr;
41324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
41424989b8cSPeter Brune   TSIFunction    ifunction;
41524989b8cSPeter Brune   void           *ctx;
41624989b8cSPeter Brune   DM             dm;
41724989b8cSPeter Brune 
418d763cef2SBarry Smith   PetscFunctionBegin;
4190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4200910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4210700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
42224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
42324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4240298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
425d763cef2SBarry Smith 
426ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
427d763cef2SBarry Smith 
4280910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
42924989b8cSPeter Brune   if (rhsfunction) {
430d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4310910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
432d763cef2SBarry Smith     PetscStackPop;
433214bc6a2SJed Brown   } else {
434214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
435b2cd27e8SJed Brown   }
43644a41b28SSean Farley 
4370910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
438d763cef2SBarry Smith   PetscFunctionReturn(0);
439d763cef2SBarry Smith }
440d763cef2SBarry Smith 
4414a2ae208SSatish Balay #undef __FUNCT__
442ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
443ef20d060SBarry Smith /*@
444ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
445ef20d060SBarry Smith 
446ef20d060SBarry Smith    Collective on TS and Vec
447ef20d060SBarry Smith 
448ef20d060SBarry Smith    Input Parameters:
449ef20d060SBarry Smith +  ts - the TS context
450ef20d060SBarry Smith -  t - current time
451ef20d060SBarry Smith 
452ef20d060SBarry Smith    Output Parameter:
4530910c330SBarry Smith .  U - the solution
454ef20d060SBarry Smith 
455ef20d060SBarry Smith    Note:
456ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
457ef20d060SBarry Smith    is used internally within the nonlinear solvers.
458ef20d060SBarry Smith 
459ef20d060SBarry Smith    Level: developer
460ef20d060SBarry Smith 
461ef20d060SBarry Smith .keywords: TS, compute
462ef20d060SBarry Smith 
463abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
464ef20d060SBarry Smith @*/
4650910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
466ef20d060SBarry Smith {
467ef20d060SBarry Smith   PetscErrorCode     ierr;
468ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
469ef20d060SBarry Smith   void               *ctx;
470ef20d060SBarry Smith   DM                 dm;
471ef20d060SBarry Smith 
472ef20d060SBarry Smith   PetscFunctionBegin;
473ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4740910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
475ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
476ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
477ef20d060SBarry Smith 
478ef20d060SBarry Smith   if (solutionfunction) {
4799b7cd975SBarry Smith     PetscStackPush("TS user solution function");
4800910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
481ef20d060SBarry Smith     PetscStackPop;
482ef20d060SBarry Smith   }
483ef20d060SBarry Smith   PetscFunctionReturn(0);
484ef20d060SBarry Smith }
4859b7cd975SBarry Smith #undef __FUNCT__
4869b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
4879b7cd975SBarry Smith /*@
4889b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
4899b7cd975SBarry Smith 
4909b7cd975SBarry Smith    Collective on TS and Vec
4919b7cd975SBarry Smith 
4929b7cd975SBarry Smith    Input Parameters:
4939b7cd975SBarry Smith +  ts - the TS context
4949b7cd975SBarry Smith -  t - current time
4959b7cd975SBarry Smith 
4969b7cd975SBarry Smith    Output Parameter:
4979b7cd975SBarry Smith .  U - the function value
4989b7cd975SBarry Smith 
4999b7cd975SBarry Smith    Note:
5009b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5019b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5029b7cd975SBarry Smith 
5039b7cd975SBarry Smith    Level: developer
5049b7cd975SBarry Smith 
5059b7cd975SBarry Smith .keywords: TS, compute
5069b7cd975SBarry Smith 
5079b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5089b7cd975SBarry Smith @*/
5099b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5109b7cd975SBarry Smith {
5119b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5129b7cd975SBarry Smith   void               *ctx;
5139b7cd975SBarry Smith   DM                 dm;
5149b7cd975SBarry Smith 
5159b7cd975SBarry Smith   PetscFunctionBegin;
5169b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5179b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5189b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5199b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5209b7cd975SBarry Smith 
5219b7cd975SBarry Smith   if (forcing) {
5229b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5239b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5249b7cd975SBarry Smith     PetscStackPop;
5259b7cd975SBarry Smith   }
5269b7cd975SBarry Smith   PetscFunctionReturn(0);
5279b7cd975SBarry Smith }
528ef20d060SBarry Smith 
529ef20d060SBarry Smith #undef __FUNCT__
530214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
531214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
532214bc6a2SJed Brown {
5332dd45cf8SJed Brown   Vec            F;
534214bc6a2SJed Brown   PetscErrorCode ierr;
535214bc6a2SJed Brown 
536214bc6a2SJed Brown   PetscFunctionBegin;
5370298fd71SBarry Smith   *Frhs = NULL;
5380298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
539214bc6a2SJed Brown   if (!ts->Frhs) {
5402dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
541214bc6a2SJed Brown   }
542214bc6a2SJed Brown   *Frhs = ts->Frhs;
543214bc6a2SJed Brown   PetscFunctionReturn(0);
544214bc6a2SJed Brown }
545214bc6a2SJed Brown 
546214bc6a2SJed Brown #undef __FUNCT__
547214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
548214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
549214bc6a2SJed Brown {
550214bc6a2SJed Brown   Mat            A,B;
5512dd45cf8SJed Brown   PetscErrorCode ierr;
552214bc6a2SJed Brown 
553214bc6a2SJed Brown   PetscFunctionBegin;
5540298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
555214bc6a2SJed Brown   if (Arhs) {
556214bc6a2SJed Brown     if (!ts->Arhs) {
557214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
558214bc6a2SJed Brown     }
559214bc6a2SJed Brown     *Arhs = ts->Arhs;
560214bc6a2SJed Brown   }
561214bc6a2SJed Brown   if (Brhs) {
562214bc6a2SJed Brown     if (!ts->Brhs) {
563214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
564214bc6a2SJed Brown     }
565214bc6a2SJed Brown     *Brhs = ts->Brhs;
566214bc6a2SJed Brown   }
567214bc6a2SJed Brown   PetscFunctionReturn(0);
568214bc6a2SJed Brown }
569214bc6a2SJed Brown 
570214bc6a2SJed Brown #undef __FUNCT__
571316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
572316643e7SJed Brown /*@
5730910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
574316643e7SJed Brown 
575316643e7SJed Brown    Collective on TS and Vec
576316643e7SJed Brown 
577316643e7SJed Brown    Input Parameters:
578316643e7SJed Brown +  ts - the TS context
579316643e7SJed Brown .  t - current time
5800910c330SBarry Smith .  U - state vector
5810910c330SBarry Smith .  Udot - time derivative of state vector
582214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
583316643e7SJed Brown 
584316643e7SJed Brown    Output Parameter:
585316643e7SJed Brown .  Y - right hand side
586316643e7SJed Brown 
587316643e7SJed Brown    Note:
588316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
589316643e7SJed Brown    is used internally within the nonlinear solvers.
590316643e7SJed Brown 
591316643e7SJed Brown    If the user did did not write their equations in implicit form, this
592316643e7SJed Brown    function recasts them in implicit form.
593316643e7SJed Brown 
594316643e7SJed Brown    Level: developer
595316643e7SJed Brown 
596316643e7SJed Brown .keywords: TS, compute
597316643e7SJed Brown 
598316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
599316643e7SJed Brown @*/
6000910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
601316643e7SJed Brown {
602316643e7SJed Brown   PetscErrorCode ierr;
60324989b8cSPeter Brune   TSIFunction    ifunction;
60424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60524989b8cSPeter Brune   void           *ctx;
60624989b8cSPeter Brune   DM             dm;
607316643e7SJed Brown 
608316643e7SJed Brown   PetscFunctionBegin;
6090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6100910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6110910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6120700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
613316643e7SJed Brown 
61424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6160298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
61724989b8cSPeter Brune 
618ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
619d90be118SSean Farley 
6200910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
62124989b8cSPeter Brune   if (ifunction) {
622316643e7SJed Brown     PetscStackPush("TS user implicit function");
6230910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
624316643e7SJed Brown     PetscStackPop;
625214bc6a2SJed Brown   }
626214bc6a2SJed Brown   if (imex) {
62724989b8cSPeter Brune     if (!ifunction) {
6280910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6292dd45cf8SJed Brown     }
63024989b8cSPeter Brune   } else if (rhsfunction) {
63124989b8cSPeter Brune     if (ifunction) {
632214bc6a2SJed Brown       Vec Frhs;
633214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6340910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
635214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6362dd45cf8SJed Brown     } else {
6370910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6380910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
639316643e7SJed Brown     }
6404a6899ffSJed Brown   }
6410910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
642316643e7SJed Brown   PetscFunctionReturn(0);
643316643e7SJed Brown }
644316643e7SJed Brown 
645316643e7SJed Brown #undef __FUNCT__
646316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
647316643e7SJed Brown /*@
648316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
649316643e7SJed Brown 
650316643e7SJed Brown    Collective on TS and Vec
651316643e7SJed Brown 
652316643e7SJed Brown    Input
653316643e7SJed Brown       Input Parameters:
654316643e7SJed Brown +  ts - the TS context
655316643e7SJed Brown .  t - current timestep
6560910c330SBarry Smith .  U - state vector
6570910c330SBarry Smith .  Udot - time derivative of state vector
658214bc6a2SJed Brown .  shift - shift to apply, see note below
659214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
660316643e7SJed Brown 
661316643e7SJed Brown    Output Parameters:
662316643e7SJed Brown +  A - Jacobian matrix
663316643e7SJed Brown .  B - optional preconditioning matrix
664316643e7SJed Brown -  flag - flag indicating matrix structure
665316643e7SJed Brown 
666316643e7SJed Brown    Notes:
6670910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
668316643e7SJed Brown 
6690910c330SBarry Smith    dF/dU + shift*dF/dUdot
670316643e7SJed Brown 
671316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
672316643e7SJed Brown    is used internally within the nonlinear solvers.
673316643e7SJed Brown 
674316643e7SJed Brown    Level: developer
675316643e7SJed Brown 
676316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
677316643e7SJed Brown 
678316643e7SJed Brown .seealso:  TSSetIJacobian()
67963495f91SJed Brown @*/
6800910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
681316643e7SJed Brown {
6820910c330SBarry Smith   PetscInt       Ustate, Udotstate;
683316643e7SJed Brown   PetscErrorCode ierr;
68424989b8cSPeter Brune   TSIJacobian    ijacobian;
68524989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
68624989b8cSPeter Brune   DM             dm;
68724989b8cSPeter Brune   void           *ctx;
688316643e7SJed Brown 
689316643e7SJed Brown   PetscFunctionBegin;
6900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6910910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6920910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
693316643e7SJed Brown   PetscValidPointer(A,6);
6940700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
695316643e7SJed Brown   PetscValidPointer(B,7);
6960700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
697316643e7SJed Brown   PetscValidPointer(flg,8);
69824989b8cSPeter Brune 
69924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
70024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7010298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
70224989b8cSPeter Brune 
7030910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
7040910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
7050910c330SBarry Smith   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == U && ts->ijacobian.Xstate == Ustate && ts->ijacobian.Xdot == Udot && ts->ijacobian.Xdotstate == Udotstate && ts->ijacobian.imex == imex))) {
7060026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
7070026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
7080026cea9SSean Farley     PetscFunctionReturn(0);
7090026cea9SSean Farley   }
710316643e7SJed Brown 
711ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
712316643e7SJed Brown 
7134e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7140910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
71524989b8cSPeter Brune   if (ijacobian) {
7162dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
717316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7180910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
719316643e7SJed Brown     PetscStackPop;
720214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
721214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
722214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7234a6899ffSJed Brown   }
724214bc6a2SJed Brown   if (imex) {
725b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
726214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7272dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
728214bc6a2SJed Brown       if (*A != *B) {
729214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
730214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
731214bc6a2SJed Brown       }
732214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
733214bc6a2SJed Brown     }
734214bc6a2SJed Brown   } else {
73524989b8cSPeter Brune     if (!ijacobian) {
7360910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
737214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
738214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
739316643e7SJed Brown       if (*A != *B) {
740316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
741316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
742316643e7SJed Brown       }
74324989b8cSPeter Brune     } else if (rhsjacobian) {
744214bc6a2SJed Brown       Mat          Arhs,Brhs;
745214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
746214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
7470910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
748214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
749214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
750214bc6a2SJed Brown       if (*A != *B) {
751214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
752214bc6a2SJed Brown       }
753214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
754214bc6a2SJed Brown     }
755316643e7SJed Brown   }
7560026cea9SSean Farley 
7570026cea9SSean Farley   ts->ijacobian.time = t;
7580910c330SBarry Smith   ts->ijacobian.X    = U;
7590910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
760bbd56ea5SKarl Rupp 
7610910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
7620910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
763bbd56ea5SKarl Rupp 
7640026cea9SSean Farley   ts->ijacobian.shift      = shift;
7650026cea9SSean Farley   ts->ijacobian.imex       = imex;
7660026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
767bbd56ea5SKarl Rupp 
7680910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
769316643e7SJed Brown   PetscFunctionReturn(0);
770316643e7SJed Brown }
771316643e7SJed Brown 
772316643e7SJed Brown #undef __FUNCT__
7734a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
774d763cef2SBarry Smith /*@C
775d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
776b5abc632SBarry Smith     where U_t = G(t,u).
777d763cef2SBarry Smith 
7783f9fe445SBarry Smith     Logically Collective on TS
779d763cef2SBarry Smith 
780d763cef2SBarry Smith     Input Parameters:
781d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
7820298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
783d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
784d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
7850298fd71SBarry Smith           function evaluation routine (may be NULL)
786d763cef2SBarry Smith 
787d763cef2SBarry Smith     Calling sequence of func:
78887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
789d763cef2SBarry Smith 
790d763cef2SBarry Smith +   t - current timestep
791d763cef2SBarry Smith .   u - input vector
792d763cef2SBarry Smith .   F - function vector
793d763cef2SBarry Smith -   ctx - [optional] user-defined function context
794d763cef2SBarry Smith 
795d763cef2SBarry Smith     Level: beginner
796d763cef2SBarry Smith 
797d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
798d763cef2SBarry Smith 
799d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
800d763cef2SBarry Smith @*/
801089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
802d763cef2SBarry Smith {
803089b2837SJed Brown   PetscErrorCode ierr;
804089b2837SJed Brown   SNES           snes;
8050298fd71SBarry Smith   Vec            ralloc = NULL;
80624989b8cSPeter Brune   DM             dm;
807d763cef2SBarry Smith 
808089b2837SJed Brown   PetscFunctionBegin;
8090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
810ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
81124989b8cSPeter Brune 
81224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81324989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
814089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
815e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
816e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
817e856ceecSJed Brown     r    = ralloc;
818e856ceecSJed Brown   }
819089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
820e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
821d763cef2SBarry Smith   PetscFunctionReturn(0);
822d763cef2SBarry Smith }
823d763cef2SBarry Smith 
8244a2ae208SSatish Balay #undef __FUNCT__
825ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
826ef20d060SBarry Smith /*@C
827abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
828ef20d060SBarry Smith 
829ef20d060SBarry Smith     Logically Collective on TS
830ef20d060SBarry Smith 
831ef20d060SBarry Smith     Input Parameters:
832ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
833ef20d060SBarry Smith .   f - routine for evaluating the solution
834ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8350298fd71SBarry Smith           function evaluation routine (may be NULL)
836ef20d060SBarry Smith 
837ef20d060SBarry Smith     Calling sequence of func:
838ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
839ef20d060SBarry Smith 
840ef20d060SBarry Smith +   t - current timestep
841ef20d060SBarry Smith .   u - output vector
842ef20d060SBarry Smith -   ctx - [optional] user-defined function context
843ef20d060SBarry Smith 
844abd5a294SJed Brown     Notes:
845abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
846abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
847abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
848abd5a294SJed Brown 
8494f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
850abd5a294SJed Brown 
851ef20d060SBarry Smith     Level: beginner
852ef20d060SBarry Smith 
853ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
854ef20d060SBarry Smith 
8559b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
856ef20d060SBarry Smith @*/
857ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
858ef20d060SBarry Smith {
859ef20d060SBarry Smith   PetscErrorCode ierr;
860ef20d060SBarry Smith   DM             dm;
861ef20d060SBarry Smith 
862ef20d060SBarry Smith   PetscFunctionBegin;
863ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
864ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
865ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
866ef20d060SBarry Smith   PetscFunctionReturn(0);
867ef20d060SBarry Smith }
868ef20d060SBarry Smith 
869ef20d060SBarry Smith #undef __FUNCT__
8709b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8719b7cd975SBarry Smith /*@C
8729b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8739b7cd975SBarry Smith 
8749b7cd975SBarry Smith     Logically Collective on TS
8759b7cd975SBarry Smith 
8769b7cd975SBarry Smith     Input Parameters:
8779b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
8789b7cd975SBarry Smith .   f - routine for evaluating the forcing function
8799b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
8800298fd71SBarry Smith           function evaluation routine (may be NULL)
8819b7cd975SBarry Smith 
8829b7cd975SBarry Smith     Calling sequence of func:
8839b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
8849b7cd975SBarry Smith 
8859b7cd975SBarry Smith +   t - current timestep
8869b7cd975SBarry Smith .   u - output vector
8879b7cd975SBarry Smith -   ctx - [optional] user-defined function context
8889b7cd975SBarry Smith 
8899b7cd975SBarry Smith     Notes:
8909b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
8919b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
8929b7cd975SBarry Smith 
8939b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
8949b7cd975SBarry Smith 
8959b7cd975SBarry Smith     Level: beginner
8969b7cd975SBarry Smith 
8979b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
8989b7cd975SBarry Smith 
8999b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9009b7cd975SBarry Smith @*/
9019b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9029b7cd975SBarry Smith {
9039b7cd975SBarry Smith   PetscErrorCode ierr;
9049b7cd975SBarry Smith   DM             dm;
9059b7cd975SBarry Smith 
9069b7cd975SBarry Smith   PetscFunctionBegin;
9079b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9089b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9099b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9109b7cd975SBarry Smith   PetscFunctionReturn(0);
9119b7cd975SBarry Smith }
9129b7cd975SBarry Smith 
9139b7cd975SBarry Smith #undef __FUNCT__
9144a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
915d763cef2SBarry Smith /*@C
916d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
917b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
918d763cef2SBarry Smith 
9193f9fe445SBarry Smith    Logically Collective on TS
920d763cef2SBarry Smith 
921d763cef2SBarry Smith    Input Parameters:
922d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
923e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
924e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
925d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
926d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9270298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
928d763cef2SBarry Smith 
929d763cef2SBarry Smith    Calling sequence of func:
93087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
931d763cef2SBarry Smith 
932d763cef2SBarry Smith +  t - current timestep
933d763cef2SBarry Smith .  u - input vector
934e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
935e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
936d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
93794b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
938d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
939d763cef2SBarry Smith 
940d763cef2SBarry Smith    Notes:
94194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
942d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
943d763cef2SBarry Smith 
944d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
945d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
94656335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
947d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
948d763cef2SBarry Smith    throughout the global iterations.
949d763cef2SBarry Smith 
950d763cef2SBarry Smith    Level: beginner
951d763cef2SBarry Smith 
952d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
953d763cef2SBarry Smith 
9548d359177SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction()
955d763cef2SBarry Smith 
956d763cef2SBarry Smith @*/
957e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
958d763cef2SBarry Smith {
959277b19d0SLisandro Dalcin   PetscErrorCode ierr;
960089b2837SJed Brown   SNES           snes;
96124989b8cSPeter Brune   DM             dm;
96224989b8cSPeter Brune   TSIJacobian    ijacobian;
963277b19d0SLisandro Dalcin 
964d763cef2SBarry Smith   PetscFunctionBegin;
9650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
966e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
967e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
968e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
969e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
970d763cef2SBarry Smith 
97124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
97224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
9730298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
97424989b8cSPeter Brune 
975089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
9765f659677SPeter Brune   if (!ijacobian) {
977e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
9780e4ef248SJed Brown   }
979e5d3d808SBarry Smith   if (Amat) {
980e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
9810e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
982bbd56ea5SKarl Rupp 
983e5d3d808SBarry Smith     ts->Arhs = Amat;
9840e4ef248SJed Brown   }
985e5d3d808SBarry Smith   if (Pmat) {
986e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
9870e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
988bbd56ea5SKarl Rupp 
989e5d3d808SBarry Smith     ts->Brhs = Pmat;
9900e4ef248SJed Brown   }
991d763cef2SBarry Smith   PetscFunctionReturn(0);
992d763cef2SBarry Smith }
993d763cef2SBarry Smith 
994316643e7SJed Brown 
995316643e7SJed Brown #undef __FUNCT__
996316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
997316643e7SJed Brown /*@C
998b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
999316643e7SJed Brown 
10003f9fe445SBarry Smith    Logically Collective on TS
1001316643e7SJed Brown 
1002316643e7SJed Brown    Input Parameters:
1003316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10040298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1005316643e7SJed Brown .  f   - the function evaluation routine
10060298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1007316643e7SJed Brown 
1008316643e7SJed Brown    Calling sequence of f:
1009316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1010316643e7SJed Brown 
1011316643e7SJed Brown +  t   - time at step/stage being solved
1012316643e7SJed Brown .  u   - state vector
1013316643e7SJed Brown .  u_t - time derivative of state vector
1014316643e7SJed Brown .  F   - function vector
1015316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1016316643e7SJed Brown 
1017316643e7SJed Brown    Important:
1018d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1019316643e7SJed Brown 
1020316643e7SJed Brown    Level: beginner
1021316643e7SJed Brown 
1022316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1023316643e7SJed Brown 
1024d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1025316643e7SJed Brown @*/
1026089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1027316643e7SJed Brown {
1028089b2837SJed Brown   PetscErrorCode ierr;
1029089b2837SJed Brown   SNES           snes;
10300298fd71SBarry Smith   Vec            resalloc = NULL;
103124989b8cSPeter Brune   DM             dm;
1032316643e7SJed Brown 
1033316643e7SJed Brown   PetscFunctionBegin;
10340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1035ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
103624989b8cSPeter Brune 
103724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
103924989b8cSPeter Brune 
1040089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1041e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1042e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1043e856ceecSJed Brown     res  = resalloc;
1044e856ceecSJed Brown   }
1045089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1046e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1047089b2837SJed Brown   PetscFunctionReturn(0);
1048089b2837SJed Brown }
1049089b2837SJed Brown 
1050089b2837SJed Brown #undef __FUNCT__
1051089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1052089b2837SJed Brown /*@C
1053089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1054089b2837SJed Brown 
1055089b2837SJed Brown    Not Collective
1056089b2837SJed Brown 
1057089b2837SJed Brown    Input Parameter:
1058089b2837SJed Brown .  ts - the TS context
1059089b2837SJed Brown 
1060089b2837SJed Brown    Output Parameter:
10610298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10620298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10630298fd71SBarry Smith -  ctx - the function context (or NULL)
1064089b2837SJed Brown 
1065089b2837SJed Brown    Level: advanced
1066089b2837SJed Brown 
1067089b2837SJed Brown .keywords: TS, nonlinear, get, function
1068089b2837SJed Brown 
1069089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1070089b2837SJed Brown @*/
1071089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1072089b2837SJed Brown {
1073089b2837SJed Brown   PetscErrorCode ierr;
1074089b2837SJed Brown   SNES           snes;
107524989b8cSPeter Brune   DM             dm;
1076089b2837SJed Brown 
1077089b2837SJed Brown   PetscFunctionBegin;
1078089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1079089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10800298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
108124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
108224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1083089b2837SJed Brown   PetscFunctionReturn(0);
1084089b2837SJed Brown }
1085089b2837SJed Brown 
1086089b2837SJed Brown #undef __FUNCT__
1087089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1088089b2837SJed Brown /*@C
1089089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1090089b2837SJed Brown 
1091089b2837SJed Brown    Not Collective
1092089b2837SJed Brown 
1093089b2837SJed Brown    Input Parameter:
1094089b2837SJed Brown .  ts - the TS context
1095089b2837SJed Brown 
1096089b2837SJed Brown    Output Parameter:
10970298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
10980298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
10990298fd71SBarry Smith -  ctx - the function context (or NULL)
1100089b2837SJed Brown 
1101089b2837SJed Brown    Level: advanced
1102089b2837SJed Brown 
1103089b2837SJed Brown .keywords: TS, nonlinear, get, function
1104089b2837SJed Brown 
1105089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1106089b2837SJed Brown @*/
1107089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1108089b2837SJed Brown {
1109089b2837SJed Brown   PetscErrorCode ierr;
1110089b2837SJed Brown   SNES           snes;
111124989b8cSPeter Brune   DM             dm;
1112089b2837SJed Brown 
1113089b2837SJed Brown   PetscFunctionBegin;
1114089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1115089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11160298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
111724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
111824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1119316643e7SJed Brown   PetscFunctionReturn(0);
1120316643e7SJed Brown }
1121316643e7SJed Brown 
1122316643e7SJed Brown #undef __FUNCT__
1123316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1124316643e7SJed Brown /*@C
1125a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1126a4f0a591SBarry Smith         you provided with TSSetIFunction().
1127316643e7SJed Brown 
11283f9fe445SBarry Smith    Logically Collective on TS
1129316643e7SJed Brown 
1130316643e7SJed Brown    Input Parameters:
1131316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1132e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1133e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1134316643e7SJed Brown .  f   - the Jacobian evaluation routine
11350298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1136316643e7SJed Brown 
1137316643e7SJed Brown    Calling sequence of f:
1138e5d3d808SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx);
1139316643e7SJed Brown 
1140316643e7SJed Brown +  t    - time at step/stage being solved
11411b4a444bSJed Brown .  U    - state vector
11421b4a444bSJed Brown .  U_t  - time derivative of state vector
1143316643e7SJed Brown .  a    - shift
1144e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1145e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1146316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1147316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1148316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1149316643e7SJed Brown 
1150316643e7SJed Brown    Notes:
1151e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1152316643e7SJed Brown 
1153a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1154b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1155a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1156a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1157a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1158a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1159a4f0a591SBarry Smith 
1160316643e7SJed Brown    Level: beginner
1161316643e7SJed Brown 
1162316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1163316643e7SJed Brown 
11648d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault()
1165316643e7SJed Brown 
1166316643e7SJed Brown @*/
1167e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1168316643e7SJed Brown {
1169316643e7SJed Brown   PetscErrorCode ierr;
1170089b2837SJed Brown   SNES           snes;
117124989b8cSPeter Brune   DM             dm;
1172316643e7SJed Brown 
1173316643e7SJed Brown   PetscFunctionBegin;
11740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1175e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1176e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1177e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1178e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
117924989b8cSPeter Brune 
118024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118124989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
118224989b8cSPeter Brune 
1183089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1184e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1185316643e7SJed Brown   PetscFunctionReturn(0);
1186316643e7SJed Brown }
1187316643e7SJed Brown 
11884a2ae208SSatish Balay #undef __FUNCT__
118955849f57SBarry Smith #define __FUNCT__ "TSLoad"
119055849f57SBarry Smith /*@C
119155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
119255849f57SBarry Smith 
119355849f57SBarry Smith   Collective on PetscViewer
119455849f57SBarry Smith 
119555849f57SBarry Smith   Input Parameters:
119655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
119755849f57SBarry Smith            some related function before a call to TSLoad().
119855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
119955849f57SBarry Smith 
120055849f57SBarry Smith    Level: intermediate
120155849f57SBarry Smith 
120255849f57SBarry Smith   Notes:
120355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
120455849f57SBarry Smith 
120555849f57SBarry Smith   Notes for advanced users:
120655849f57SBarry Smith   Most users should not need to know the details of the binary storage
120755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
120855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
120955849f57SBarry Smith   format is
121055849f57SBarry Smith .vb
121155849f57SBarry Smith      has not yet been determined
121255849f57SBarry Smith .ve
121355849f57SBarry Smith 
121455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
121555849f57SBarry Smith @*/
1216f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
121755849f57SBarry Smith {
121855849f57SBarry Smith   PetscErrorCode ierr;
121955849f57SBarry Smith   PetscBool      isbinary;
122055849f57SBarry Smith   PetscInt       classid;
122155849f57SBarry Smith   char           type[256];
12222d53ad75SBarry Smith   DMTS           sdm;
1223ad6bc421SBarry Smith   DM             dm;
122455849f57SBarry Smith 
122555849f57SBarry Smith   PetscFunctionBegin;
1226f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
122755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
122855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
122955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
123055849f57SBarry Smith 
123155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1232ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
123355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1234f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1235f2c2a1b9SBarry Smith   if (ts->ops->load) {
1236f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1237f2c2a1b9SBarry Smith   }
1238ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1239ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1240ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1241f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1242f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12432d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12442d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
124555849f57SBarry Smith   PetscFunctionReturn(0);
124655849f57SBarry Smith }
124755849f57SBarry Smith 
12489804daf3SBarry Smith #include <petscdraw.h>
1249f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1250f05ece33SBarry Smith #include <petscviewerams.h>
1251f05ece33SBarry Smith #endif
125255849f57SBarry Smith #undef __FUNCT__
12534a2ae208SSatish Balay #define __FUNCT__ "TSView"
12547e2c5f70SBarry Smith /*@C
1255d763cef2SBarry Smith     TSView - Prints the TS data structure.
1256d763cef2SBarry Smith 
12574c49b128SBarry Smith     Collective on TS
1258d763cef2SBarry Smith 
1259d763cef2SBarry Smith     Input Parameters:
1260d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1261d763cef2SBarry Smith -   viewer - visualization context
1262d763cef2SBarry Smith 
1263d763cef2SBarry Smith     Options Database Key:
1264d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1265d763cef2SBarry Smith 
1266d763cef2SBarry Smith     Notes:
1267d763cef2SBarry Smith     The available visualization contexts include
1268b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1269b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1270d763cef2SBarry Smith          output where only the first processor opens
1271d763cef2SBarry Smith          the file.  All other processors send their
1272d763cef2SBarry Smith          data to the first processor to print.
1273d763cef2SBarry Smith 
1274d763cef2SBarry Smith     The user can open an alternative visualization context with
1275b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1276d763cef2SBarry Smith 
1277d763cef2SBarry Smith     Level: beginner
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith .keywords: TS, timestep, view
1280d763cef2SBarry Smith 
1281b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1282d763cef2SBarry Smith @*/
12837087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1284d763cef2SBarry Smith {
1285dfbe8321SBarry Smith   PetscErrorCode ierr;
128619fd82e9SBarry Smith   TSType         type;
12872b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
12882d53ad75SBarry Smith   DMTS           sdm;
1289f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1290f05ece33SBarry Smith   PetscBool      isams;
1291f05ece33SBarry Smith #endif
1292d763cef2SBarry Smith 
1293d763cef2SBarry Smith   PetscFunctionBegin;
12940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12953050cee2SBarry Smith   if (!viewer) {
1296ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
12973050cee2SBarry Smith   }
12980700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1299c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1300fd16b177SBarry Smith 
1301251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1302251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
130355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13042b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1305f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1306f05ece33SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERAMS,&isams);CHKERRQ(ierr);
1307f05ece33SBarry Smith #endif
130832077d6dSBarry Smith   if (iascii) {
1309317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
131077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1311a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1312d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13135ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1314c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1315d763cef2SBarry Smith     }
13165ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1317193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13182d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13192d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1320d52bd9f3SBarry Smith     if (ts->ops->view) {
1321d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1322d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1323d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1324d52bd9f3SBarry Smith     }
13250f5bd95cSBarry Smith   } else if (isstring) {
1326a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1327b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
132855849f57SBarry Smith   } else if (isbinary) {
132955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
133055849f57SBarry Smith     MPI_Comm    comm;
133155849f57SBarry Smith     PetscMPIInt rank;
133255849f57SBarry Smith     char        type[256];
133355849f57SBarry Smith 
133455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
133555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
133655849f57SBarry Smith     if (!rank) {
133755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
133855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
133955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
134055849f57SBarry Smith     }
134155849f57SBarry Smith     if (ts->ops->view) {
134255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
134355849f57SBarry Smith     }
1344f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1345f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13462d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13472d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13482b0a91c0SBarry Smith   } else if (isdraw) {
13492b0a91c0SBarry Smith     PetscDraw draw;
13502b0a91c0SBarry Smith     char      str[36];
135189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
13522b0a91c0SBarry Smith 
13532b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
13542b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
13552b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
13562b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
13570298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
135889fd9fafSBarry Smith     bottom = y - h;
13592b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
13602b0a91c0SBarry Smith     if (ts->ops->view) {
13612b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13622b0a91c0SBarry Smith     }
13632b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1364f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1365f05ece33SBarry Smith   } else if (isams) {
1366f05ece33SBarry Smith     if (((PetscObject)ts)->amsmem == -1) {
1367f05ece33SBarry Smith       ierr = PetscObjectViewAMS((PetscObject)ts,viewer);CHKERRQ(ierr);
1368f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_take_access,(((PetscObject)ts)->amsmem));
1369f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time step",&ts->steps,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1370f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time",&ts->ptime,1,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1371f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_grant_access,(((PetscObject)ts)->amsmem));
1372d763cef2SBarry Smith     }
13730acecf5bSBarry Smith     if (ts->ops->view) {
13740acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
13750acecf5bSBarry Smith     }
1376f05ece33SBarry Smith #endif
1377f05ece33SBarry Smith   }
1378f05ece33SBarry Smith 
1379b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1380251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1381b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1382d763cef2SBarry Smith   PetscFunctionReturn(0);
1383d763cef2SBarry Smith }
1384d763cef2SBarry Smith 
1385d763cef2SBarry Smith 
13864a2ae208SSatish Balay #undef __FUNCT__
13874a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1388b07ff414SBarry Smith /*@
1389d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1390d763cef2SBarry Smith    the timesteppers.
1391d763cef2SBarry Smith 
13923f9fe445SBarry Smith    Logically Collective on TS
1393d763cef2SBarry Smith 
1394d763cef2SBarry Smith    Input Parameters:
1395d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1396d763cef2SBarry Smith -  usrP - optional user context
1397d763cef2SBarry Smith 
1398d763cef2SBarry Smith    Level: intermediate
1399d763cef2SBarry Smith 
1400d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1401d763cef2SBarry Smith 
1402d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1403d763cef2SBarry Smith @*/
14047087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1405d763cef2SBarry Smith {
1406d763cef2SBarry Smith   PetscFunctionBegin;
14070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1408d763cef2SBarry Smith   ts->user = usrP;
1409d763cef2SBarry Smith   PetscFunctionReturn(0);
1410d763cef2SBarry Smith }
1411d763cef2SBarry Smith 
14124a2ae208SSatish Balay #undef __FUNCT__
14134a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1414b07ff414SBarry Smith /*@
1415d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1416d763cef2SBarry Smith     timestepper.
1417d763cef2SBarry Smith 
1418d763cef2SBarry Smith     Not Collective
1419d763cef2SBarry Smith 
1420d763cef2SBarry Smith     Input Parameter:
1421d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith     Output Parameter:
1424d763cef2SBarry Smith .   usrP - user context
1425d763cef2SBarry Smith 
1426d763cef2SBarry Smith     Level: intermediate
1427d763cef2SBarry Smith 
1428d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1429d763cef2SBarry Smith 
1430d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1431d763cef2SBarry Smith @*/
1432e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1433d763cef2SBarry Smith {
1434d763cef2SBarry Smith   PetscFunctionBegin;
14350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1436e71120c6SJed Brown   *(void**)usrP = ts->user;
1437d763cef2SBarry Smith   PetscFunctionReturn(0);
1438d763cef2SBarry Smith }
1439d763cef2SBarry Smith 
14404a2ae208SSatish Balay #undef __FUNCT__
14414a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1442d763cef2SBarry Smith /*@
1443b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1444d763cef2SBarry Smith 
1445d763cef2SBarry Smith    Not Collective
1446d763cef2SBarry Smith 
1447d763cef2SBarry Smith    Input Parameter:
1448d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Output Parameter:
1451b8123daeSJed Brown .  iter - number of steps completed so far
1452d763cef2SBarry Smith 
1453d763cef2SBarry Smith    Level: intermediate
1454d763cef2SBarry Smith 
1455d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1456b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1457d763cef2SBarry Smith @*/
14587087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1459d763cef2SBarry Smith {
1460d763cef2SBarry Smith   PetscFunctionBegin;
14610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14624482741eSBarry Smith   PetscValidIntPointer(iter,2);
1463d763cef2SBarry Smith   *iter = ts->steps;
1464d763cef2SBarry Smith   PetscFunctionReturn(0);
1465d763cef2SBarry Smith }
1466d763cef2SBarry Smith 
14674a2ae208SSatish Balay #undef __FUNCT__
14684a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1469d763cef2SBarry Smith /*@
1470d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1471d763cef2SBarry Smith    as well as the initial time.
1472d763cef2SBarry Smith 
14733f9fe445SBarry Smith    Logically Collective on TS
1474d763cef2SBarry Smith 
1475d763cef2SBarry Smith    Input Parameters:
1476d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1477d763cef2SBarry Smith .  initial_time - the initial time
1478d763cef2SBarry Smith -  time_step - the size of the timestep
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith    Level: intermediate
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1483d763cef2SBarry Smith 
1484d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1485d763cef2SBarry Smith @*/
14867087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1487d763cef2SBarry Smith {
1488e144a568SJed Brown   PetscErrorCode ierr;
1489e144a568SJed Brown 
1490d763cef2SBarry Smith   PetscFunctionBegin;
14910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1492e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1493d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1494d763cef2SBarry Smith   PetscFunctionReturn(0);
1495d763cef2SBarry Smith }
1496d763cef2SBarry Smith 
14974a2ae208SSatish Balay #undef __FUNCT__
14984a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1499d763cef2SBarry Smith /*@
1500d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1501d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
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 -  time_step - the size of the timestep
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith    Level: intermediate
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1512d763cef2SBarry Smith 
1513d763cef2SBarry Smith .keywords: TS, set, timestep
1514d763cef2SBarry Smith @*/
15157087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1516d763cef2SBarry Smith {
1517d763cef2SBarry Smith   PetscFunctionBegin;
15180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1519c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1520d763cef2SBarry Smith   ts->time_step      = time_step;
152131748224SBarry Smith   ts->time_step_orig = time_step;
1522d763cef2SBarry Smith   PetscFunctionReturn(0);
1523d763cef2SBarry Smith }
1524d763cef2SBarry Smith 
15254a2ae208SSatish Balay #undef __FUNCT__
1526a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1527a43b19c4SJed Brown /*@
152849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
152949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
153049354f04SShri Abhyankar      or just return at the final time TS computed.
1531a43b19c4SJed Brown 
1532a43b19c4SJed Brown   Logically Collective on TS
1533a43b19c4SJed Brown 
1534a43b19c4SJed Brown    Input Parameter:
1535a43b19c4SJed Brown +   ts - the time-step context
153649354f04SShri Abhyankar -   eftopt - exact final time option
1537a43b19c4SJed Brown 
1538a43b19c4SJed Brown    Level: beginner
1539a43b19c4SJed Brown 
1540a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1541a43b19c4SJed Brown @*/
154249354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1543a43b19c4SJed Brown {
1544a43b19c4SJed Brown   PetscFunctionBegin;
1545a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
154649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
154749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1548a43b19c4SJed Brown   PetscFunctionReturn(0);
1549a43b19c4SJed Brown }
1550a43b19c4SJed Brown 
1551a43b19c4SJed Brown #undef __FUNCT__
15524a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1553d763cef2SBarry Smith /*@
1554d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1555d763cef2SBarry Smith 
1556d763cef2SBarry Smith    Not Collective
1557d763cef2SBarry Smith 
1558d763cef2SBarry Smith    Input Parameter:
1559d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1560d763cef2SBarry Smith 
1561d763cef2SBarry Smith    Output Parameter:
1562d763cef2SBarry Smith .  dt - the current timestep size
1563d763cef2SBarry Smith 
1564d763cef2SBarry Smith    Level: intermediate
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith .keywords: TS, get, timestep
1569d763cef2SBarry Smith @*/
15707087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1571d763cef2SBarry Smith {
1572d763cef2SBarry Smith   PetscFunctionBegin;
15730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1574f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1575d763cef2SBarry Smith   *dt = ts->time_step;
1576d763cef2SBarry Smith   PetscFunctionReturn(0);
1577d763cef2SBarry Smith }
1578d763cef2SBarry Smith 
15794a2ae208SSatish Balay #undef __FUNCT__
15804a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1581d8e5e3e6SSatish Balay /*@
1582d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1583d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1584d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1585d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1586d763cef2SBarry Smith 
1587d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1588d763cef2SBarry Smith 
1589d763cef2SBarry Smith    Input Parameter:
1590d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1591d763cef2SBarry Smith 
1592d763cef2SBarry Smith    Output Parameter:
1593d763cef2SBarry Smith .  v - the vector containing the solution
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith    Level: intermediate
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith .seealso: TSGetTimeStep()
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1600d763cef2SBarry Smith @*/
16017087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1602d763cef2SBarry Smith {
1603d763cef2SBarry Smith   PetscFunctionBegin;
16040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16054482741eSBarry Smith   PetscValidPointer(v,2);
16068737fe31SLisandro Dalcin   *v = ts->vec_sol;
1607d763cef2SBarry Smith   PetscFunctionReturn(0);
1608d763cef2SBarry Smith }
1609d763cef2SBarry Smith 
1610bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
16114a2ae208SSatish Balay #undef __FUNCT__
1612bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1613d8e5e3e6SSatish Balay /*@
1614bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1615d763cef2SBarry Smith 
1616bdad233fSMatthew Knepley   Not collective
1617d763cef2SBarry Smith 
1618bdad233fSMatthew Knepley   Input Parameters:
1619bdad233fSMatthew Knepley + ts   - The TS
1620bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1621d763cef2SBarry Smith .vb
16220910c330SBarry Smith          U_t - A U = 0      (linear)
16230910c330SBarry Smith          U_t - A(t) U = 0   (linear)
16240910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1625d763cef2SBarry Smith .ve
1626d763cef2SBarry Smith 
1627d763cef2SBarry Smith    Level: beginner
1628d763cef2SBarry Smith 
1629bdad233fSMatthew Knepley .keywords: TS, problem type
1630bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1631d763cef2SBarry Smith @*/
16327087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1633a7cc72afSBarry Smith {
16349e2a6581SJed Brown   PetscErrorCode ierr;
16359e2a6581SJed Brown 
1636d763cef2SBarry Smith   PetscFunctionBegin;
16370700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1638bdad233fSMatthew Knepley   ts->problem_type = type;
16399e2a6581SJed Brown   if (type == TS_LINEAR) {
16409e2a6581SJed Brown     SNES snes;
16419e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
16429e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
16439e2a6581SJed Brown   }
1644d763cef2SBarry Smith   PetscFunctionReturn(0);
1645d763cef2SBarry Smith }
1646d763cef2SBarry Smith 
1647bdad233fSMatthew Knepley #undef __FUNCT__
1648bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1649bdad233fSMatthew Knepley /*@C
1650bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1651bdad233fSMatthew Knepley 
1652bdad233fSMatthew Knepley   Not collective
1653bdad233fSMatthew Knepley 
1654bdad233fSMatthew Knepley   Input Parameter:
1655bdad233fSMatthew Knepley . ts   - The TS
1656bdad233fSMatthew Knepley 
1657bdad233fSMatthew Knepley   Output Parameter:
1658bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1659bdad233fSMatthew Knepley .vb
1660089b2837SJed Brown          M U_t = A U
1661089b2837SJed Brown          M(t) U_t = A(t) U
1662b5abc632SBarry Smith          F(t,U,U_t)
1663bdad233fSMatthew Knepley .ve
1664bdad233fSMatthew Knepley 
1665bdad233fSMatthew Knepley    Level: beginner
1666bdad233fSMatthew Knepley 
1667bdad233fSMatthew Knepley .keywords: TS, problem type
1668bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1669bdad233fSMatthew Knepley @*/
16707087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1671a7cc72afSBarry Smith {
1672bdad233fSMatthew Knepley   PetscFunctionBegin;
16730700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
16744482741eSBarry Smith   PetscValidIntPointer(type,2);
1675bdad233fSMatthew Knepley   *type = ts->problem_type;
1676bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1677bdad233fSMatthew Knepley }
1678d763cef2SBarry Smith 
16794a2ae208SSatish Balay #undef __FUNCT__
16804a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1681d763cef2SBarry Smith /*@
1682d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1683d763cef2SBarry Smith    of a timestepper.
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith    Collective on TS
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Input Parameter:
1688d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Notes:
1691d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1692d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1693d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1694d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1695d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1696d763cef2SBarry Smith 
1697d763cef2SBarry Smith    Level: advanced
1698d763cef2SBarry Smith 
1699d763cef2SBarry Smith .keywords: TS, timestep, setup
1700d763cef2SBarry Smith 
1701d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1702d763cef2SBarry Smith @*/
17037087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1704d763cef2SBarry Smith {
1705dfbe8321SBarry Smith   PetscErrorCode ierr;
17066c6b9e74SPeter Brune   DM             dm;
17076c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17086c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
17096c6b9e74SPeter Brune   TSIJacobian    ijac;
17106c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1711d763cef2SBarry Smith 
1712d763cef2SBarry Smith   PetscFunctionBegin;
17130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1714277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1715277b19d0SLisandro Dalcin 
17167adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
17179596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1718d763cef2SBarry Smith   }
1719277b19d0SLisandro Dalcin 
1720277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1721277b19d0SLisandro Dalcin 
1722ad6bc421SBarry Smith   ierr = TSGetTSAdapt(ts,&ts->adapt);CHKERRQ(ierr);
17231c3436cfSJed Brown 
1724277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1725000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1726277b19d0SLisandro Dalcin   }
1727277b19d0SLisandro Dalcin 
17286c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
17296c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
17306c6b9e74SPeter Brune    */
17316c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
17320298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
17336c6b9e74SPeter Brune   if (!func) {
17346c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
17356c6b9e74SPeter Brune   }
17366c6b9e74SPeter 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.
17376c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
17386c6b9e74SPeter Brune    */
17390298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
17400298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
17410298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
17426c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
17436c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
17446c6b9e74SPeter Brune   }
1745277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1746277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1747277b19d0SLisandro Dalcin }
1748277b19d0SLisandro Dalcin 
1749277b19d0SLisandro Dalcin #undef __FUNCT__
1750277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1751277b19d0SLisandro Dalcin /*@
1752277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1753277b19d0SLisandro Dalcin 
1754277b19d0SLisandro Dalcin    Collective on TS
1755277b19d0SLisandro Dalcin 
1756277b19d0SLisandro Dalcin    Input Parameter:
1757277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1758277b19d0SLisandro Dalcin 
1759277b19d0SLisandro Dalcin    Level: beginner
1760277b19d0SLisandro Dalcin 
1761277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1762277b19d0SLisandro Dalcin 
1763277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1764277b19d0SLisandro Dalcin @*/
1765277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1766277b19d0SLisandro Dalcin {
1767277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1768277b19d0SLisandro Dalcin 
1769277b19d0SLisandro Dalcin   PetscFunctionBegin;
1770277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1771277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1772277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1773277b19d0SLisandro Dalcin   }
1774277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1775bbd56ea5SKarl Rupp 
17764e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
17774e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1778214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
17796bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1780e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1781e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
178238637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1783*b29392e1SShri   ierr = ISDestroy(&ts->is_diff);CHKERRQ(ierr);
1784bbd56ea5SKarl Rupp 
1785277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1786d763cef2SBarry Smith   PetscFunctionReturn(0);
1787d763cef2SBarry Smith }
1788d763cef2SBarry Smith 
17894a2ae208SSatish Balay #undef __FUNCT__
17904a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1791d8e5e3e6SSatish Balay /*@
1792d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1793d763cef2SBarry Smith    with TSCreate().
1794d763cef2SBarry Smith 
1795d763cef2SBarry Smith    Collective on TS
1796d763cef2SBarry Smith 
1797d763cef2SBarry Smith    Input Parameter:
1798d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1799d763cef2SBarry Smith 
1800d763cef2SBarry Smith    Level: beginner
1801d763cef2SBarry Smith 
1802d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1803d763cef2SBarry Smith 
1804d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1805d763cef2SBarry Smith @*/
18066bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1807d763cef2SBarry Smith {
18086849ba73SBarry Smith   PetscErrorCode ierr;
1809d763cef2SBarry Smith 
1810d763cef2SBarry Smith   PetscFunctionBegin;
18116bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
18126bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
18136bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1814d763cef2SBarry Smith 
18156bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1816277b19d0SLisandro Dalcin 
1817be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
1818f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*ts);CHKERRQ(ierr);
18196bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
18206d4c513bSLisandro Dalcin 
182184df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
18226bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
18236bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
18246bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
18256d4c513bSLisandro Dalcin 
1826a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1827d763cef2SBarry Smith   PetscFunctionReturn(0);
1828d763cef2SBarry Smith }
1829d763cef2SBarry Smith 
18304a2ae208SSatish Balay #undef __FUNCT__
18314a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1832d8e5e3e6SSatish Balay /*@
1833d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1834d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1835d763cef2SBarry Smith 
1836d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1837d763cef2SBarry Smith 
1838d763cef2SBarry Smith    Input Parameter:
1839d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1840d763cef2SBarry Smith 
1841d763cef2SBarry Smith    Output Parameter:
1842d763cef2SBarry Smith .  snes - the nonlinear solver context
1843d763cef2SBarry Smith 
1844d763cef2SBarry Smith    Notes:
1845d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1846d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
184794b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1848d763cef2SBarry Smith 
1849d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
18500298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1851d763cef2SBarry Smith 
1852d763cef2SBarry Smith    Level: beginner
1853d763cef2SBarry Smith 
1854d763cef2SBarry Smith .keywords: timestep, get, SNES
1855d763cef2SBarry Smith @*/
18567087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1857d763cef2SBarry Smith {
1858d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1859d372ba47SLisandro Dalcin 
1860d763cef2SBarry Smith   PetscFunctionBegin;
18610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18624482741eSBarry Smith   PetscValidPointer(snes,2);
1863d372ba47SLisandro Dalcin   if (!ts->snes) {
1864ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
18650298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1866d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1867d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1868496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
18699e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
18709e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
18719e2a6581SJed Brown     }
1872d372ba47SLisandro Dalcin   }
1873d763cef2SBarry Smith   *snes = ts->snes;
1874d763cef2SBarry Smith   PetscFunctionReturn(0);
1875d763cef2SBarry Smith }
1876d763cef2SBarry Smith 
18774a2ae208SSatish Balay #undef __FUNCT__
1878deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1879deb2cd25SJed Brown /*@
1880deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1881deb2cd25SJed Brown 
1882deb2cd25SJed Brown    Collective
1883deb2cd25SJed Brown 
1884deb2cd25SJed Brown    Input Parameter:
1885deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1886deb2cd25SJed Brown -  snes - the nonlinear solver context
1887deb2cd25SJed Brown 
1888deb2cd25SJed Brown    Notes:
1889deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1890deb2cd25SJed Brown 
1891deb2cd25SJed Brown    Level: developer
1892deb2cd25SJed Brown 
1893deb2cd25SJed Brown .keywords: timestep, set, SNES
1894deb2cd25SJed Brown @*/
1895deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1896deb2cd25SJed Brown {
1897deb2cd25SJed Brown   PetscErrorCode ierr;
1898740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1899deb2cd25SJed Brown 
1900deb2cd25SJed Brown   PetscFunctionBegin;
1901deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1902deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1903deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1904deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1905bbd56ea5SKarl Rupp 
1906deb2cd25SJed Brown   ts->snes = snes;
1907bbd56ea5SKarl Rupp 
19080298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19090298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1910740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
19110298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1912740132f1SEmil Constantinescu   }
1913deb2cd25SJed Brown   PetscFunctionReturn(0);
1914deb2cd25SJed Brown }
1915deb2cd25SJed Brown 
1916deb2cd25SJed Brown #undef __FUNCT__
191794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1918d8e5e3e6SSatish Balay /*@
191994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1920d763cef2SBarry Smith    a TS (timestepper) context.
1921d763cef2SBarry Smith 
192294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1923d763cef2SBarry Smith 
1924d763cef2SBarry Smith    Input Parameter:
1925d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1926d763cef2SBarry Smith 
1927d763cef2SBarry Smith    Output Parameter:
192894b7f48cSBarry Smith .  ksp - the nonlinear solver context
1929d763cef2SBarry Smith 
1930d763cef2SBarry Smith    Notes:
193194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1932d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1933d763cef2SBarry Smith    KSP and PC contexts as well.
1934d763cef2SBarry Smith 
193594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
19360298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
1937d763cef2SBarry Smith 
1938d763cef2SBarry Smith    Level: beginner
1939d763cef2SBarry Smith 
194094b7f48cSBarry Smith .keywords: timestep, get, KSP
1941d763cef2SBarry Smith @*/
19427087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1943d763cef2SBarry Smith {
1944d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1945089b2837SJed Brown   SNES           snes;
1946d372ba47SLisandro Dalcin 
1947d763cef2SBarry Smith   PetscFunctionBegin;
19480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19494482741eSBarry Smith   PetscValidPointer(ksp,2);
195017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1951e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1952089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1953089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1954d763cef2SBarry Smith   PetscFunctionReturn(0);
1955d763cef2SBarry Smith }
1956d763cef2SBarry Smith 
1957d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1958d763cef2SBarry Smith 
19594a2ae208SSatish Balay #undef __FUNCT__
1960adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1961adb62b0dSMatthew Knepley /*@
1962adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1963adb62b0dSMatthew Knepley    maximum time for iteration.
1964adb62b0dSMatthew Knepley 
19653f9fe445SBarry Smith    Not Collective
1966adb62b0dSMatthew Knepley 
1967adb62b0dSMatthew Knepley    Input Parameters:
1968adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
19690298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
19700298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
1971adb62b0dSMatthew Knepley 
1972adb62b0dSMatthew Knepley    Level: intermediate
1973adb62b0dSMatthew Knepley 
1974adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1975adb62b0dSMatthew Knepley @*/
19767087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1977adb62b0dSMatthew Knepley {
1978adb62b0dSMatthew Knepley   PetscFunctionBegin;
19790700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1980abc0a331SBarry Smith   if (maxsteps) {
19814482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1982adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1983adb62b0dSMatthew Knepley   }
1984abc0a331SBarry Smith   if (maxtime) {
19854482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1986adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
1987adb62b0dSMatthew Knepley   }
1988adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1989adb62b0dSMatthew Knepley }
1990adb62b0dSMatthew Knepley 
1991adb62b0dSMatthew Knepley #undef __FUNCT__
19924a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1993d763cef2SBarry Smith /*@
1994d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1995d763cef2SBarry Smith    maximum time for iteration.
1996d763cef2SBarry Smith 
19973f9fe445SBarry Smith    Logically Collective on TS
1998d763cef2SBarry Smith 
1999d763cef2SBarry Smith    Input Parameters:
2000d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2001d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2002d763cef2SBarry Smith -  maxtime - final time to iterate to
2003d763cef2SBarry Smith 
2004d763cef2SBarry Smith    Options Database Keys:
2005d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
20063bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2007d763cef2SBarry Smith 
2008d763cef2SBarry Smith    Notes:
2009d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2010d763cef2SBarry Smith 
2011d763cef2SBarry Smith    Level: intermediate
2012d763cef2SBarry Smith 
2013d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2014a43b19c4SJed Brown 
2015a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2016d763cef2SBarry Smith @*/
20177087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2018d763cef2SBarry Smith {
2019d763cef2SBarry Smith   PetscFunctionBegin;
20200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2021c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2022c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
202339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
202439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2025d763cef2SBarry Smith   PetscFunctionReturn(0);
2026d763cef2SBarry Smith }
2027d763cef2SBarry Smith 
20284a2ae208SSatish Balay #undef __FUNCT__
20294a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2030d763cef2SBarry Smith /*@
2031d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2032d763cef2SBarry Smith    for use by the TS routines.
2033d763cef2SBarry Smith 
20343f9fe445SBarry Smith    Logically Collective on TS and Vec
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith    Input Parameters:
2037d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
20380910c330SBarry Smith -  u - the solution vector
2039d763cef2SBarry Smith 
2040d763cef2SBarry Smith    Level: beginner
2041d763cef2SBarry Smith 
2042d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2043d763cef2SBarry Smith @*/
20440910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2045d763cef2SBarry Smith {
20468737fe31SLisandro Dalcin   PetscErrorCode ierr;
2047496e6a7aSJed Brown   DM             dm;
20488737fe31SLisandro Dalcin 
2049d763cef2SBarry Smith   PetscFunctionBegin;
20500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20510910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
20520910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
20536bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2054bbd56ea5SKarl Rupp 
20550910c330SBarry Smith   ts->vec_sol = u;
2056bbd56ea5SKarl Rupp 
2057496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
20580910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2059d763cef2SBarry Smith   PetscFunctionReturn(0);
2060d763cef2SBarry Smith }
2061d763cef2SBarry Smith 
2062e74ef692SMatthew Knepley #undef __FUNCT__
2063e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2064ac226902SBarry Smith /*@C
2065000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
20663f2090d5SJed Brown   called once at the beginning of each time step.
2067000e7ae3SMatthew Knepley 
20683f9fe445SBarry Smith   Logically Collective on TS
2069000e7ae3SMatthew Knepley 
2070000e7ae3SMatthew Knepley   Input Parameters:
2071000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2072000e7ae3SMatthew Knepley - func - The function
2073000e7ae3SMatthew Knepley 
2074000e7ae3SMatthew Knepley   Calling sequence of func:
2075000e7ae3SMatthew Knepley . func (TS ts);
2076000e7ae3SMatthew Knepley 
2077000e7ae3SMatthew Knepley   Level: intermediate
2078000e7ae3SMatthew Knepley 
2079b8123daeSJed Brown   Note:
2080b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2081b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2082b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2083b8123daeSJed Brown 
2084000e7ae3SMatthew Knepley .keywords: TS, timestep
2085b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2086000e7ae3SMatthew Knepley @*/
20877087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2088000e7ae3SMatthew Knepley {
2089000e7ae3SMatthew Knepley   PetscFunctionBegin;
20900700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2091ae60f76fSBarry Smith   ts->prestep = func;
2092000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2093000e7ae3SMatthew Knepley }
2094000e7ae3SMatthew Knepley 
2095e74ef692SMatthew Knepley #undef __FUNCT__
20963f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
209709ee8438SJed Brown /*@
20983f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
20993f2090d5SJed Brown 
21003f2090d5SJed Brown   Collective on TS
21013f2090d5SJed Brown 
21023f2090d5SJed Brown   Input Parameters:
21033f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21043f2090d5SJed Brown 
21053f2090d5SJed Brown   Notes:
21063f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
21073f2090d5SJed Brown   so most users would not generally call this routine themselves.
21083f2090d5SJed Brown 
21093f2090d5SJed Brown   Level: developer
21103f2090d5SJed Brown 
21113f2090d5SJed Brown .keywords: TS, timestep
2112b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
21133f2090d5SJed Brown @*/
21147087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
21153f2090d5SJed Brown {
21163f2090d5SJed Brown   PetscErrorCode ierr;
21173f2090d5SJed Brown 
21183f2090d5SJed Brown   PetscFunctionBegin;
21190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2120ae60f76fSBarry Smith   if (ts->prestep) {
2121ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2122312ce896SJed Brown   }
21233f2090d5SJed Brown   PetscFunctionReturn(0);
21243f2090d5SJed Brown }
21253f2090d5SJed Brown 
21263f2090d5SJed Brown #undef __FUNCT__
2127b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2128b8123daeSJed Brown /*@C
2129b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2130b8123daeSJed Brown   called once at the beginning of each stage.
2131b8123daeSJed Brown 
2132b8123daeSJed Brown   Logically Collective on TS
2133b8123daeSJed Brown 
2134b8123daeSJed Brown   Input Parameters:
2135b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2136b8123daeSJed Brown - func - The function
2137b8123daeSJed Brown 
2138b8123daeSJed Brown   Calling sequence of func:
2139b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2140b8123daeSJed Brown 
2141b8123daeSJed Brown   Level: intermediate
2142b8123daeSJed Brown 
2143b8123daeSJed Brown   Note:
2144b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2145b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2146b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2147b8123daeSJed Brown 
2148b8123daeSJed Brown .keywords: TS, timestep
2149b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2150b8123daeSJed Brown @*/
2151b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2152b8123daeSJed Brown {
2153b8123daeSJed Brown   PetscFunctionBegin;
2154b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2155ae60f76fSBarry Smith   ts->prestage = func;
2156b8123daeSJed Brown   PetscFunctionReturn(0);
2157b8123daeSJed Brown }
2158b8123daeSJed Brown 
2159b8123daeSJed Brown #undef __FUNCT__
2160b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2161b8123daeSJed Brown /*@
2162b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2163b8123daeSJed Brown 
2164b8123daeSJed Brown   Collective on TS
2165b8123daeSJed Brown 
2166b8123daeSJed Brown   Input Parameters:
2167b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2168b8123daeSJed Brown 
2169b8123daeSJed Brown   Notes:
2170b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2171b8123daeSJed Brown   most users would not generally call this routine themselves.
2172b8123daeSJed Brown 
2173b8123daeSJed Brown   Level: developer
2174b8123daeSJed Brown 
2175b8123daeSJed Brown .keywords: TS, timestep
2176b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2177b8123daeSJed Brown @*/
2178b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2179b8123daeSJed Brown {
2180b8123daeSJed Brown   PetscErrorCode ierr;
2181b8123daeSJed Brown 
2182b8123daeSJed Brown   PetscFunctionBegin;
2183b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2184ae60f76fSBarry Smith   if (ts->prestage) {
2185ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2186b8123daeSJed Brown   }
2187b8123daeSJed Brown   PetscFunctionReturn(0);
2188b8123daeSJed Brown }
2189b8123daeSJed Brown 
2190b8123daeSJed Brown #undef __FUNCT__
2191e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2192ac226902SBarry Smith /*@C
2193000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
21943f2090d5SJed Brown   called once at the end of each time step.
2195000e7ae3SMatthew Knepley 
21963f9fe445SBarry Smith   Logically Collective on TS
2197000e7ae3SMatthew Knepley 
2198000e7ae3SMatthew Knepley   Input Parameters:
2199000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2200000e7ae3SMatthew Knepley - func - The function
2201000e7ae3SMatthew Knepley 
2202000e7ae3SMatthew Knepley   Calling sequence of func:
2203b8123daeSJed Brown $ func (TS ts);
2204000e7ae3SMatthew Knepley 
2205000e7ae3SMatthew Knepley   Level: intermediate
2206000e7ae3SMatthew Knepley 
2207000e7ae3SMatthew Knepley .keywords: TS, timestep
2208b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2209000e7ae3SMatthew Knepley @*/
22107087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2211000e7ae3SMatthew Knepley {
2212000e7ae3SMatthew Knepley   PetscFunctionBegin;
22130700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2214ae60f76fSBarry Smith   ts->poststep = func;
2215000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2216000e7ae3SMatthew Knepley }
2217000e7ae3SMatthew Knepley 
2218e74ef692SMatthew Knepley #undef __FUNCT__
22193f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
222009ee8438SJed Brown /*@
22213f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
22223f2090d5SJed Brown 
22233f2090d5SJed Brown   Collective on TS
22243f2090d5SJed Brown 
22253f2090d5SJed Brown   Input Parameters:
22263f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
22273f2090d5SJed Brown 
22283f2090d5SJed Brown   Notes:
22293f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
22303f2090d5SJed Brown   so most users would not generally call this routine themselves.
22313f2090d5SJed Brown 
22323f2090d5SJed Brown   Level: developer
22333f2090d5SJed Brown 
22343f2090d5SJed Brown .keywords: TS, timestep
22353f2090d5SJed Brown @*/
22367087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
22373f2090d5SJed Brown {
22383f2090d5SJed Brown   PetscErrorCode ierr;
22393f2090d5SJed Brown 
22403f2090d5SJed Brown   PetscFunctionBegin;
22410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2242ae60f76fSBarry Smith   if (ts->poststep) {
2243ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
224472ac3e02SJed Brown   }
22453f2090d5SJed Brown   PetscFunctionReturn(0);
22463f2090d5SJed Brown }
22473f2090d5SJed Brown 
2248d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2249d763cef2SBarry Smith 
22504a2ae208SSatish Balay #undef __FUNCT__
2251a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2252d763cef2SBarry Smith /*@C
2253a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2254d763cef2SBarry Smith    timestep to display the iteration's  progress.
2255d763cef2SBarry Smith 
22563f9fe445SBarry Smith    Logically Collective on TS
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith    Input Parameters:
2259d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2260e213d8f1SJed Brown .  monitor - monitoring routine
2261329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
22620298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2263b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
22640298fd71SBarry Smith           (may be NULL)
2265d763cef2SBarry Smith 
2266e213d8f1SJed Brown    Calling sequence of monitor:
22670910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2268d763cef2SBarry Smith 
2269d763cef2SBarry Smith +    ts - the TS context
227088c05cc5SBarry 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
227188c05cc5SBarry Smith                                been interpolated to)
22721f06c33eSBarry Smith .    time - current time
22730910c330SBarry Smith .    u - current iterate
2274d763cef2SBarry Smith -    mctx - [optional] monitoring context
2275d763cef2SBarry Smith 
2276d763cef2SBarry Smith    Notes:
2277d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2278d763cef2SBarry Smith    already has been loaded.
2279d763cef2SBarry Smith 
2280025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2281025f1a04SBarry Smith 
2282d763cef2SBarry Smith    Level: intermediate
2283d763cef2SBarry Smith 
2284d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2285d763cef2SBarry Smith 
2286a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2287d763cef2SBarry Smith @*/
2288c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2289d763cef2SBarry Smith {
2290d763cef2SBarry Smith   PetscFunctionBegin;
22910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
229217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2293d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
22948704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2295d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2296d763cef2SBarry Smith   PetscFunctionReturn(0);
2297d763cef2SBarry Smith }
2298d763cef2SBarry Smith 
22994a2ae208SSatish Balay #undef __FUNCT__
2300a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2301d763cef2SBarry Smith /*@C
2302a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2303d763cef2SBarry Smith 
23043f9fe445SBarry Smith    Logically Collective on TS
2305d763cef2SBarry Smith 
2306d763cef2SBarry Smith    Input Parameters:
2307d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2308d763cef2SBarry Smith 
2309d763cef2SBarry Smith    Notes:
2310d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2311d763cef2SBarry Smith 
2312d763cef2SBarry Smith    Level: intermediate
2313d763cef2SBarry Smith 
2314d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2315d763cef2SBarry Smith 
2316a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2317d763cef2SBarry Smith @*/
23187087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2319d763cef2SBarry Smith {
2320d952e501SBarry Smith   PetscErrorCode ierr;
2321d952e501SBarry Smith   PetscInt       i;
2322d952e501SBarry Smith 
2323d763cef2SBarry Smith   PetscFunctionBegin;
23240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2325d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
23268704b422SBarry Smith     if (ts->monitordestroy[i]) {
23278704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2328d952e501SBarry Smith     }
2329d952e501SBarry Smith   }
2330d763cef2SBarry Smith   ts->numbermonitors = 0;
2331d763cef2SBarry Smith   PetscFunctionReturn(0);
2332d763cef2SBarry Smith }
2333d763cef2SBarry Smith 
23344a2ae208SSatish Balay #undef __FUNCT__
2335a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2336d8e5e3e6SSatish Balay /*@
2337a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
23385516499fSSatish Balay 
23395516499fSSatish Balay    Level: intermediate
234041251cbbSSatish Balay 
23415516499fSSatish Balay .keywords: TS, set, monitor
23425516499fSSatish Balay 
234341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
234441251cbbSSatish Balay @*/
2345649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2346d763cef2SBarry Smith {
2347dfbe8321SBarry Smith   PetscErrorCode ierr;
2348ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2349d132466eSBarry Smith 
2350d763cef2SBarry Smith   PetscFunctionBegin;
2351649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2352971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2353649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2354d763cef2SBarry Smith   PetscFunctionReturn(0);
2355d763cef2SBarry Smith }
2356d763cef2SBarry Smith 
23574a2ae208SSatish Balay #undef __FUNCT__
2358cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2359cd652676SJed Brown /*@
2360cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2361cd652676SJed Brown 
2362cd652676SJed Brown    Logically Collective on TS
2363cd652676SJed Brown 
2364cd652676SJed Brown    Input Argument:
2365cd652676SJed Brown .  ts - time stepping context
2366cd652676SJed Brown 
2367cd652676SJed Brown    Output Argument:
2368cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2369cd652676SJed Brown 
2370cd652676SJed Brown    Level: intermediate
2371cd652676SJed Brown 
2372cd652676SJed Brown .keywords: TS, set
2373cd652676SJed Brown 
2374cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2375cd652676SJed Brown @*/
2376cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2377cd652676SJed Brown {
2378cd652676SJed Brown   PetscFunctionBegin;
2379cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2380cd652676SJed Brown   ts->retain_stages = flg;
2381cd652676SJed Brown   PetscFunctionReturn(0);
2382cd652676SJed Brown }
2383cd652676SJed Brown 
2384cd652676SJed Brown #undef __FUNCT__
2385cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2386cd652676SJed Brown /*@
2387cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2388cd652676SJed Brown 
2389cd652676SJed Brown    Collective on TS
2390cd652676SJed Brown 
2391cd652676SJed Brown    Input Argument:
2392cd652676SJed Brown +  ts - time stepping context
2393cd652676SJed Brown -  t - time to interpolate to
2394cd652676SJed Brown 
2395cd652676SJed Brown    Output Argument:
23960910c330SBarry Smith .  U - state at given time
2397cd652676SJed Brown 
2398cd652676SJed Brown    Notes:
2399cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2400cd652676SJed Brown 
2401cd652676SJed Brown    Level: intermediate
2402cd652676SJed Brown 
2403cd652676SJed Brown    Developer Notes:
2404cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2405cd652676SJed Brown 
2406cd652676SJed Brown .keywords: TS, set
2407cd652676SJed Brown 
2408cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2409cd652676SJed Brown @*/
24100910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2411cd652676SJed Brown {
2412cd652676SJed Brown   PetscErrorCode ierr;
2413cd652676SJed Brown 
2414cd652676SJed Brown   PetscFunctionBegin;
2415cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2416ce94432eSBarry 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);
2417ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
24180910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2419cd652676SJed Brown   PetscFunctionReturn(0);
2420cd652676SJed Brown }
2421cd652676SJed Brown 
2422cd652676SJed Brown #undef __FUNCT__
24234a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2424d763cef2SBarry Smith /*@
24256d9e5789SSean Farley    TSStep - Steps one time step
2426d763cef2SBarry Smith 
2427d763cef2SBarry Smith    Collective on TS
2428d763cef2SBarry Smith 
2429d763cef2SBarry Smith    Input Parameter:
2430d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2431d763cef2SBarry Smith 
24326d9e5789SSean Farley    Level: intermediate
2433d763cef2SBarry Smith 
2434b8123daeSJed Brown    Notes:
2435b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2436b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2437b8123daeSJed Brown 
243825cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
243925cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
244025cb2221SBarry Smith 
2441d763cef2SBarry Smith .keywords: TS, timestep, solve
2442d763cef2SBarry Smith 
244325cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2444d763cef2SBarry Smith @*/
2445193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2446d763cef2SBarry Smith {
2447362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2448dfbe8321SBarry Smith   PetscErrorCode ierr;
2449d763cef2SBarry Smith 
2450d763cef2SBarry Smith   PetscFunctionBegin;
24510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2452d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2453d405a339SMatthew Knepley 
2454362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2455362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2456bbd56ea5SKarl Rupp 
2457d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2458193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2459d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2460bbd56ea5SKarl Rupp 
2461362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2462362cd11cSLisandro Dalcin 
2463362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2464cef5090cSJed Brown     if (ts->errorifstepfailed) {
2465cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2466ce94432eSBarry 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]);
2467ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2468cef5090cSJed Brown     }
2469362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2470db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2471db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2472362cd11cSLisandro Dalcin   }
2473d763cef2SBarry Smith   PetscFunctionReturn(0);
2474d763cef2SBarry Smith }
2475d763cef2SBarry Smith 
24764a2ae208SSatish Balay #undef __FUNCT__
247705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
247805175c85SJed Brown /*@
247905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
248005175c85SJed Brown 
24811c3436cfSJed Brown    Collective on TS
248205175c85SJed Brown 
248305175c85SJed Brown    Input Arguments:
24841c3436cfSJed Brown +  ts - time stepping context
24851c3436cfSJed Brown .  order - desired order of accuracy
24860298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
248705175c85SJed Brown 
248805175c85SJed Brown    Output Arguments:
24890910c330SBarry Smith .  U - state at the end of the current step
249005175c85SJed Brown 
249105175c85SJed Brown    Level: advanced
249205175c85SJed Brown 
2493108c343cSJed Brown    Notes:
2494108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2495108c343cSJed 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.
2496108c343cSJed Brown 
24971c3436cfSJed Brown .seealso: TSStep(), TSAdapt
249805175c85SJed Brown @*/
24990910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
250005175c85SJed Brown {
250105175c85SJed Brown   PetscErrorCode ierr;
250205175c85SJed Brown 
250305175c85SJed Brown   PetscFunctionBegin;
250405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
250505175c85SJed Brown   PetscValidType(ts,1);
25060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2507ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
25080910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
250905175c85SJed Brown   PetscFunctionReturn(0);
251005175c85SJed Brown }
251105175c85SJed Brown 
251205175c85SJed Brown #undef __FUNCT__
25136a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
25146a4d4014SLisandro Dalcin /*@
25156a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
25166a4d4014SLisandro Dalcin 
25176a4d4014SLisandro Dalcin    Collective on TS
25186a4d4014SLisandro Dalcin 
25196a4d4014SLisandro Dalcin    Input Parameter:
25206a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2521cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
25225a3a76d0SJed Brown 
25236a4d4014SLisandro Dalcin    Level: beginner
25246a4d4014SLisandro Dalcin 
25255a3a76d0SJed Brown    Notes:
25265a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
25275a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
25285a3a76d0SJed Brown    stepped over the final time.
25295a3a76d0SJed Brown 
25306a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
25316a4d4014SLisandro Dalcin 
25326a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
25336a4d4014SLisandro Dalcin @*/
2534cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
25356a4d4014SLisandro Dalcin {
25364d7d938eSLisandro Dalcin   PetscBool         flg;
25374d7d938eSLisandro Dalcin   PetscViewer       viewer;
25386a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2539cffb1e40SBarry Smith   PetscViewerFormat format;
2540f22f69f0SBarry Smith 
25416a4d4014SLisandro Dalcin   PetscFunctionBegin;
25420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2543f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
254449354f04SShri 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 */
25450910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
25465a3a76d0SJed Brown       Vec y;
25470910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
25485a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
25495a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
25505a3a76d0SJed Brown     }
2551f2c2a1b9SBarry Smith     if (u) {
25520910c330SBarry Smith       ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2553f2c2a1b9SBarry Smith     }
2554bbd56ea5SKarl Rupp   } else if (u) {
25550910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
25565a3a76d0SJed Brown   }
2557b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
25586a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2559193ac0bcSJed Brown   ts->steps             = 0;
25605ef26d82SJed Brown   ts->ksp_its           = 0;
25615ef26d82SJed Brown   ts->snes_its          = 0;
2562c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2563c610991cSLisandro Dalcin   ts->reject            = 0;
2564193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2565193ac0bcSJed Brown 
2566f05ece33SBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view_pre",&viewer,&format,&flg);CHKERRQ(ierr);
2567f05ece33SBarry Smith   if (flg && !PetscPreLoadingOn) {
2568f05ece33SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
2569f05ece33SBarry Smith     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2570f05ece33SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2571f05ece33SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2572f05ece33SBarry Smith   }
2573f05ece33SBarry Smith 
2574193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2575193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
25760910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2577bbd56ea5SKarl Rupp 
2578cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2579193ac0bcSJed Brown   } else {
25806a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2581362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2582db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2583db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2584e1a7a14fSJed Brown     while (!ts->reason) {
2585193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2586193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2587193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2588193ac0bcSJed Brown     }
258949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
25900910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2591bbd56ea5SKarl Rupp 
2592cc708dedSBarry Smith       ts->solvetime = ts->max_time;
25930574a7fbSJed Brown     } else {
2594ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2595cc708dedSBarry Smith       ts->solvetime = ts->ptime;
25960574a7fbSJed Brown     }
2597193ac0bcSJed Brown   }
25983923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2599ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
26004d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2601cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
26024d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2603cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2604cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
26052b0a91c0SBarry Smith   }
26066a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26076a4d4014SLisandro Dalcin }
26086a4d4014SLisandro Dalcin 
26096a4d4014SLisandro Dalcin #undef __FUNCT__
26104a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2611228d79bcSJed Brown /*@
2612228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2613228d79bcSJed Brown 
2614228d79bcSJed Brown    Collective on TS
2615228d79bcSJed Brown 
2616228d79bcSJed Brown    Input Parameters:
2617228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2618228d79bcSJed Brown .  step - step number that has just completed
2619228d79bcSJed Brown .  ptime - model time of the state
26200910c330SBarry Smith -  u - state at the current model time
2621228d79bcSJed Brown 
2622228d79bcSJed Brown    Notes:
2623228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2624228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2625228d79bcSJed Brown 
2626228d79bcSJed Brown    Level: advanced
2627228d79bcSJed Brown 
2628228d79bcSJed Brown .keywords: TS, timestep
2629228d79bcSJed Brown @*/
26300910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2631d763cef2SBarry Smith {
26326849ba73SBarry Smith   PetscErrorCode ierr;
2633a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2634d763cef2SBarry Smith 
2635d763cef2SBarry Smith   PetscFunctionBegin;
2636d763cef2SBarry Smith   for (i=0; i<n; i++) {
26370910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2638d763cef2SBarry Smith   }
2639d763cef2SBarry Smith   PetscFunctionReturn(0);
2640d763cef2SBarry Smith }
2641d763cef2SBarry Smith 
2642d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
26430b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
26440b039ecaSBarry Smith   PetscDrawLG lg;
26450b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2646201da799SBarry Smith   PetscInt    ksp_its,snes_its;
26470b039ecaSBarry Smith };
26480b039ecaSBarry Smith 
2649d763cef2SBarry Smith 
26504a2ae208SSatish Balay #undef __FUNCT__
2651a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2652d763cef2SBarry Smith /*@C
2653a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2654a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith    Collective on TS
2657d763cef2SBarry Smith 
2658d763cef2SBarry Smith    Input Parameters:
2659d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2660d763cef2SBarry Smith .  label - the title to put in the title bar
26617c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2662a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2663a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith    Output Parameter:
26660b039ecaSBarry Smith .  ctx - the context
2667d763cef2SBarry Smith 
2668d763cef2SBarry Smith    Options Database Key:
26694f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
26704f09c107SBarry Smith .  -ts_monitor_lg_solution -
267199fdda47SBarry Smith .  -ts_monitor_lg_error -
267299fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
267399fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
267499fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2675d763cef2SBarry Smith 
2676d763cef2SBarry Smith    Notes:
2677a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2678d763cef2SBarry Smith 
2679d763cef2SBarry Smith    Level: intermediate
2680d763cef2SBarry Smith 
26817c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2682d763cef2SBarry Smith 
26834f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
26847c922b88SBarry Smith 
2685d763cef2SBarry Smith @*/
2686a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2687d763cef2SBarry Smith {
2688b0a32e0cSBarry Smith   PetscDraw      win;
2689dfbe8321SBarry Smith   PetscErrorCode ierr;
269099fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2691d763cef2SBarry Smith 
2692d763cef2SBarry Smith   PetscFunctionBegin;
2693201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2694a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
26953fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
26960b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
26970298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
269899fdda47SBarry Smith   if (flg) {
26990b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
270099fdda47SBarry Smith   }
27010b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2702a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2703d763cef2SBarry Smith   PetscFunctionReturn(0);
2704d763cef2SBarry Smith }
2705d763cef2SBarry Smith 
27064a2ae208SSatish Balay #undef __FUNCT__
27074f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
27084f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2709d763cef2SBarry Smith {
27100b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2711c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2712dfbe8321SBarry Smith   PetscErrorCode ierr;
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith   PetscFunctionBegin;
2715a9f9c1f6SBarry Smith   if (!n) {
2716a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2717a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2718a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2719a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2720a9f9c1f6SBarry Smith   }
2721c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
27220b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
272399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))) {
27240b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
27253923b477SBarry Smith   }
2726d763cef2SBarry Smith   PetscFunctionReturn(0);
2727d763cef2SBarry Smith }
2728d763cef2SBarry Smith 
27294a2ae208SSatish Balay #undef __FUNCT__
2730a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2731d763cef2SBarry Smith /*@C
2732a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2733a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2734d763cef2SBarry Smith 
27350b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith    Input Parameter:
27380b039ecaSBarry Smith .  ctx - the monitor context
2739d763cef2SBarry Smith 
2740d763cef2SBarry Smith    Level: intermediate
2741d763cef2SBarry Smith 
2742d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2743d763cef2SBarry Smith 
27444f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2745d763cef2SBarry Smith @*/
2746a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2747d763cef2SBarry Smith {
2748b0a32e0cSBarry Smith   PetscDraw      draw;
2749dfbe8321SBarry Smith   PetscErrorCode ierr;
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith   PetscFunctionBegin;
27520b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
27536bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
27540b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
27550b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2756d763cef2SBarry Smith   PetscFunctionReturn(0);
2757d763cef2SBarry Smith }
2758d763cef2SBarry Smith 
27594a2ae208SSatish Balay #undef __FUNCT__
27604a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2761d763cef2SBarry Smith /*@
2762b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2763d763cef2SBarry Smith 
2764d763cef2SBarry Smith    Not Collective
2765d763cef2SBarry Smith 
2766d763cef2SBarry Smith    Input Parameter:
2767d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2768d763cef2SBarry Smith 
2769d763cef2SBarry Smith    Output Parameter:
2770d763cef2SBarry Smith .  t  - the current time
2771d763cef2SBarry Smith 
2772d763cef2SBarry Smith    Level: beginner
2773d763cef2SBarry Smith 
2774b8123daeSJed Brown    Note:
2775b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2776b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2777b8123daeSJed Brown 
2778d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2779d763cef2SBarry Smith 
2780d763cef2SBarry Smith .keywords: TS, get, time
2781d763cef2SBarry Smith @*/
27827087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2783d763cef2SBarry Smith {
2784d763cef2SBarry Smith   PetscFunctionBegin;
27850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2786f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2787d763cef2SBarry Smith   *t = ts->ptime;
2788d763cef2SBarry Smith   PetscFunctionReturn(0);
2789d763cef2SBarry Smith }
2790d763cef2SBarry Smith 
27914a2ae208SSatish Balay #undef __FUNCT__
27926a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
27936a4d4014SLisandro Dalcin /*@
27946a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
27956a4d4014SLisandro Dalcin 
27963f9fe445SBarry Smith    Logically Collective on TS
27976a4d4014SLisandro Dalcin 
27986a4d4014SLisandro Dalcin    Input Parameters:
27996a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
28006a4d4014SLisandro Dalcin -  time - the time
28016a4d4014SLisandro Dalcin 
28026a4d4014SLisandro Dalcin    Level: intermediate
28036a4d4014SLisandro Dalcin 
28046a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
28056a4d4014SLisandro Dalcin 
28066a4d4014SLisandro Dalcin .keywords: TS, set, time
28076a4d4014SLisandro Dalcin @*/
28087087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
28096a4d4014SLisandro Dalcin {
28106a4d4014SLisandro Dalcin   PetscFunctionBegin;
28110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2812c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
28136a4d4014SLisandro Dalcin   ts->ptime = t;
28146a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
28156a4d4014SLisandro Dalcin }
28166a4d4014SLisandro Dalcin 
28176a4d4014SLisandro Dalcin #undef __FUNCT__
28184a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2819d763cef2SBarry Smith /*@C
2820d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2821d763cef2SBarry Smith    TS options in the database.
2822d763cef2SBarry Smith 
28233f9fe445SBarry Smith    Logically Collective on TS
2824d763cef2SBarry Smith 
2825d763cef2SBarry Smith    Input Parameter:
2826d763cef2SBarry Smith +  ts     - The TS context
2827d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2828d763cef2SBarry Smith 
2829d763cef2SBarry Smith    Notes:
2830d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2831d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2832d763cef2SBarry Smith    hyphen.
2833d763cef2SBarry Smith 
2834d763cef2SBarry Smith    Level: advanced
2835d763cef2SBarry Smith 
2836d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith .seealso: TSSetFromOptions()
2839d763cef2SBarry Smith 
2840d763cef2SBarry Smith @*/
28417087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2842d763cef2SBarry Smith {
2843dfbe8321SBarry Smith   PetscErrorCode ierr;
2844089b2837SJed Brown   SNES           snes;
2845d763cef2SBarry Smith 
2846d763cef2SBarry Smith   PetscFunctionBegin;
28470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2848d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2849089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2850089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2851d763cef2SBarry Smith   PetscFunctionReturn(0);
2852d763cef2SBarry Smith }
2853d763cef2SBarry Smith 
2854d763cef2SBarry Smith 
28554a2ae208SSatish Balay #undef __FUNCT__
28564a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2857d763cef2SBarry Smith /*@C
2858d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2859d763cef2SBarry Smith    TS options in the database.
2860d763cef2SBarry Smith 
28613f9fe445SBarry Smith    Logically Collective on TS
2862d763cef2SBarry Smith 
2863d763cef2SBarry Smith    Input Parameter:
2864d763cef2SBarry Smith +  ts     - The TS context
2865d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2866d763cef2SBarry Smith 
2867d763cef2SBarry Smith    Notes:
2868d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2869d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2870d763cef2SBarry Smith    hyphen.
2871d763cef2SBarry Smith 
2872d763cef2SBarry Smith    Level: advanced
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2875d763cef2SBarry Smith 
2876d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2877d763cef2SBarry Smith 
2878d763cef2SBarry Smith @*/
28797087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2880d763cef2SBarry Smith {
2881dfbe8321SBarry Smith   PetscErrorCode ierr;
2882089b2837SJed Brown   SNES           snes;
2883d763cef2SBarry Smith 
2884d763cef2SBarry Smith   PetscFunctionBegin;
28850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2886d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2887089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2888089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2889d763cef2SBarry Smith   PetscFunctionReturn(0);
2890d763cef2SBarry Smith }
2891d763cef2SBarry Smith 
28924a2ae208SSatish Balay #undef __FUNCT__
28934a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2894d763cef2SBarry Smith /*@C
2895d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2896d763cef2SBarry Smith    TS options in the database.
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith    Not Collective
2899d763cef2SBarry Smith 
2900d763cef2SBarry Smith    Input Parameter:
2901d763cef2SBarry Smith .  ts - The TS context
2902d763cef2SBarry Smith 
2903d763cef2SBarry Smith    Output Parameter:
2904d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2905d763cef2SBarry Smith 
2906d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2907d763cef2SBarry Smith    sufficient length to hold the prefix.
2908d763cef2SBarry Smith 
2909d763cef2SBarry Smith    Level: intermediate
2910d763cef2SBarry Smith 
2911d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2912d763cef2SBarry Smith 
2913d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2914d763cef2SBarry Smith @*/
29157087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2916d763cef2SBarry Smith {
2917dfbe8321SBarry Smith   PetscErrorCode ierr;
2918d763cef2SBarry Smith 
2919d763cef2SBarry Smith   PetscFunctionBegin;
29200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29214482741eSBarry Smith   PetscValidPointer(prefix,2);
2922d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2923d763cef2SBarry Smith   PetscFunctionReturn(0);
2924d763cef2SBarry Smith }
2925d763cef2SBarry Smith 
29264a2ae208SSatish Balay #undef __FUNCT__
29274a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2928d763cef2SBarry Smith /*@C
2929d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2930d763cef2SBarry Smith 
2931d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2932d763cef2SBarry Smith 
2933d763cef2SBarry Smith    Input Parameter:
2934d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2935d763cef2SBarry Smith 
2936d763cef2SBarry Smith    Output Parameters:
2937e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
2938e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
2939e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
2940e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
2941d763cef2SBarry Smith 
29420298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
2943d763cef2SBarry Smith 
2944d763cef2SBarry Smith    Level: intermediate
2945d763cef2SBarry Smith 
294626d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2947d763cef2SBarry Smith 
2948d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2949d763cef2SBarry Smith @*/
2950e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
2951d763cef2SBarry Smith {
2952089b2837SJed Brown   PetscErrorCode ierr;
2953089b2837SJed Brown   SNES           snes;
295424989b8cSPeter Brune   DM             dm;
2955089b2837SJed Brown 
2956d763cef2SBarry Smith   PetscFunctionBegin;
2957089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2958e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
295924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
296024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2961d763cef2SBarry Smith   PetscFunctionReturn(0);
2962d763cef2SBarry Smith }
2963d763cef2SBarry Smith 
29641713a123SBarry Smith #undef __FUNCT__
29652eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
29662eca1d9cSJed Brown /*@C
29672eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
29682eca1d9cSJed Brown 
29692eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
29702eca1d9cSJed Brown 
29712eca1d9cSJed Brown    Input Parameter:
29722eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
29732eca1d9cSJed Brown 
29742eca1d9cSJed Brown    Output Parameters:
2975e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
2976e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
29772eca1d9cSJed Brown .  f   - The function to compute the matrices
29782eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
29792eca1d9cSJed Brown 
29800298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
29812eca1d9cSJed Brown 
29822eca1d9cSJed Brown    Level: advanced
29832eca1d9cSJed Brown 
29842eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
29852eca1d9cSJed Brown 
29862eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
29872eca1d9cSJed Brown @*/
2988e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
29892eca1d9cSJed Brown {
2990089b2837SJed Brown   PetscErrorCode ierr;
2991089b2837SJed Brown   SNES           snes;
299224989b8cSPeter Brune   DM             dm;
29930910c330SBarry Smith 
29942eca1d9cSJed Brown   PetscFunctionBegin;
2995089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2996f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
2997e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
299824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
299924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
30002eca1d9cSJed Brown   PetscFunctionReturn(0);
30012eca1d9cSJed Brown }
30022eca1d9cSJed Brown 
30036083293cSBarry Smith 
30042eca1d9cSJed Brown #undef __FUNCT__
300583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
30061713a123SBarry Smith /*@C
300783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
30081713a123SBarry Smith    VecView() for the solution at each timestep
30091713a123SBarry Smith 
30101713a123SBarry Smith    Collective on TS
30111713a123SBarry Smith 
30121713a123SBarry Smith    Input Parameters:
30131713a123SBarry Smith +  ts - the TS context
30141713a123SBarry Smith .  step - current time-step
3015142b95e3SSatish Balay .  ptime - current time
30160298fd71SBarry Smith -  dummy - either a viewer or NULL
30171713a123SBarry Smith 
301899fdda47SBarry Smith    Options Database:
301999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
302099fdda47SBarry Smith 
302199fdda47SBarry 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
302299fdda47SBarry Smith        will look bad
302399fdda47SBarry Smith 
30241713a123SBarry Smith    Level: intermediate
30251713a123SBarry Smith 
30261713a123SBarry Smith .keywords: TS,  vector, monitor, view
30271713a123SBarry Smith 
3028a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30291713a123SBarry Smith @*/
30300910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30311713a123SBarry Smith {
3032dfbe8321SBarry Smith   PetscErrorCode   ierr;
303383a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3034473a3ab2SBarry Smith   PetscDraw        draw;
30351713a123SBarry Smith 
30361713a123SBarry Smith   PetscFunctionBegin;
30376083293cSBarry Smith   if (!step && ictx->showinitial) {
30386083293cSBarry Smith     if (!ictx->initialsolution) {
30390910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
30401713a123SBarry Smith     }
30410910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
30426083293cSBarry Smith   }
30433fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
30440dcf80beSBarry Smith 
30456083293cSBarry Smith   if (ictx->showinitial) {
30466083293cSBarry Smith     PetscReal pause;
30476083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
30486083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
30496083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
30506083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
30516083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
30526083293cSBarry Smith   }
30530910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3054473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3055473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3056473a3ab2SBarry Smith     char      time[32];
3057473a3ab2SBarry Smith     size_t    len;
3058473a3ab2SBarry Smith 
3059473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3060473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3061473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3062473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
30630298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3064473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3065473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3066473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3067473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3068473a3ab2SBarry Smith   }
3069473a3ab2SBarry Smith 
30706083293cSBarry Smith   if (ictx->showinitial) {
30716083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
30726083293cSBarry Smith   }
30731713a123SBarry Smith   PetscFunctionReturn(0);
30741713a123SBarry Smith }
30751713a123SBarry Smith 
30762d5ee99bSBarry Smith #undef __FUNCT__
30772d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
30782d5ee99bSBarry Smith /*@C
30792d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
30802d5ee99bSBarry Smith 
30812d5ee99bSBarry Smith    Collective on TS
30822d5ee99bSBarry Smith 
30832d5ee99bSBarry Smith    Input Parameters:
30842d5ee99bSBarry Smith +  ts - the TS context
30852d5ee99bSBarry Smith .  step - current time-step
30862d5ee99bSBarry Smith .  ptime - current time
30872d5ee99bSBarry Smith -  dummy - either a viewer or NULL
30882d5ee99bSBarry Smith 
30892d5ee99bSBarry Smith    Level: intermediate
30902d5ee99bSBarry Smith 
30912d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
30922d5ee99bSBarry Smith 
30932d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30942d5ee99bSBarry Smith @*/
30952d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
30962d5ee99bSBarry Smith {
30972d5ee99bSBarry Smith   PetscErrorCode    ierr;
30982d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
30992d5ee99bSBarry Smith   PetscDraw         draw;
31002d5ee99bSBarry Smith   MPI_Comm          comm;
31012d5ee99bSBarry Smith   PetscInt          n;
31022d5ee99bSBarry Smith   PetscMPIInt       size;
31032d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
31042d5ee99bSBarry Smith   char              time[32];
31052d5ee99bSBarry Smith   size_t            len;
31062d5ee99bSBarry Smith   const PetscScalar *U;
31072d5ee99bSBarry Smith 
31082d5ee99bSBarry Smith   PetscFunctionBegin;
31092d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
31102d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
31112d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
31122d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
31132d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
31142d5ee99bSBarry Smith 
31152d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
31162d5ee99bSBarry Smith 
31172d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
31184b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3119ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3120a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3121a4494fc1SBarry Smith       PetscFunctionReturn(0);
3122a4494fc1SBarry Smith   }
31232d5ee99bSBarry Smith   if (!step) ictx->color++;
31242d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
31252d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
31262d5ee99bSBarry Smith 
31272d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
31284b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
31292d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
31302d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
31312d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
31322d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
31332d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
31342d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
31352d5ee99bSBarry Smith   }
31362d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
31372d5ee99bSBarry Smith   PetscFunctionReturn(0);
31382d5ee99bSBarry Smith }
31392d5ee99bSBarry Smith 
31401713a123SBarry Smith 
31416c699258SBarry Smith #undef __FUNCT__
314283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
31436083293cSBarry Smith /*@C
314483a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
31456083293cSBarry Smith 
31466083293cSBarry Smith    Collective on TS
31476083293cSBarry Smith 
31486083293cSBarry Smith    Input Parameters:
31496083293cSBarry Smith .    ctx - the monitor context
31506083293cSBarry Smith 
31516083293cSBarry Smith    Level: intermediate
31526083293cSBarry Smith 
31536083293cSBarry Smith .keywords: TS,  vector, monitor, view
31546083293cSBarry Smith 
315583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
31566083293cSBarry Smith @*/
315783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
31586083293cSBarry Smith {
31596083293cSBarry Smith   PetscErrorCode ierr;
31606083293cSBarry Smith 
31616083293cSBarry Smith   PetscFunctionBegin;
31624b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
316383a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
316483a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
316583a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
31666083293cSBarry Smith   PetscFunctionReturn(0);
31676083293cSBarry Smith }
31686083293cSBarry Smith 
31696083293cSBarry Smith #undef __FUNCT__
317083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
31716083293cSBarry Smith /*@C
317283a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
31736083293cSBarry Smith 
31746083293cSBarry Smith    Collective on TS
31756083293cSBarry Smith 
31766083293cSBarry Smith    Input Parameter:
31776083293cSBarry Smith .    ts - time-step context
31786083293cSBarry Smith 
31796083293cSBarry Smith    Output Patameter:
31806083293cSBarry Smith .    ctx - the monitor context
31816083293cSBarry Smith 
318299fdda47SBarry Smith    Options Database:
318399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
318499fdda47SBarry Smith 
31856083293cSBarry Smith    Level: intermediate
31866083293cSBarry Smith 
31876083293cSBarry Smith .keywords: TS,  vector, monitor, view
31886083293cSBarry Smith 
318983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
31906083293cSBarry Smith @*/
319183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
31926083293cSBarry Smith {
31936083293cSBarry Smith   PetscErrorCode   ierr;
31946083293cSBarry Smith 
31956083293cSBarry Smith   PetscFunctionBegin;
319683a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
319783a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3198e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3199bbd56ea5SKarl Rupp 
320083a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3201473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
32020298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3203473a3ab2SBarry Smith 
3204473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
32050298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
32062d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
32076083293cSBarry Smith   PetscFunctionReturn(0);
32086083293cSBarry Smith }
32096083293cSBarry Smith 
32106083293cSBarry Smith #undef __FUNCT__
321183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
32123a471f94SBarry Smith /*@C
321383a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
32143a471f94SBarry Smith    VecView() for the error at each timestep
32153a471f94SBarry Smith 
32163a471f94SBarry Smith    Collective on TS
32173a471f94SBarry Smith 
32183a471f94SBarry Smith    Input Parameters:
32193a471f94SBarry Smith +  ts - the TS context
32203a471f94SBarry Smith .  step - current time-step
32213a471f94SBarry Smith .  ptime - current time
32220298fd71SBarry Smith -  dummy - either a viewer or NULL
32233a471f94SBarry Smith 
32243a471f94SBarry Smith    Level: intermediate
32253a471f94SBarry Smith 
32263a471f94SBarry Smith .keywords: TS,  vector, monitor, view
32273a471f94SBarry Smith 
32283a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32293a471f94SBarry Smith @*/
32300910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
32313a471f94SBarry Smith {
32323a471f94SBarry Smith   PetscErrorCode   ierr;
323383a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
323483a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
32353a471f94SBarry Smith   Vec              work;
32363a471f94SBarry Smith 
32373a471f94SBarry Smith   PetscFunctionBegin;
32383fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
32390910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
32403a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
32410910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
32423a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
32433a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
32443a471f94SBarry Smith   PetscFunctionReturn(0);
32453a471f94SBarry Smith }
32463a471f94SBarry Smith 
32472a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
32483a471f94SBarry Smith #undef __FUNCT__
32496c699258SBarry Smith #define __FUNCT__ "TSSetDM"
32506c699258SBarry Smith /*@
32516c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
32526c699258SBarry Smith 
32533f9fe445SBarry Smith    Logically Collective on TS and DM
32546c699258SBarry Smith 
32556c699258SBarry Smith    Input Parameters:
32566c699258SBarry Smith +  ts - the preconditioner context
32576c699258SBarry Smith -  dm - the dm
32586c699258SBarry Smith 
32596c699258SBarry Smith    Level: intermediate
32606c699258SBarry Smith 
32616c699258SBarry Smith 
32626c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
32636c699258SBarry Smith @*/
32647087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
32656c699258SBarry Smith {
32666c699258SBarry Smith   PetscErrorCode ierr;
3267089b2837SJed Brown   SNES           snes;
3268942e3340SBarry Smith   DMTS           tsdm;
32696c699258SBarry Smith 
32706c699258SBarry Smith   PetscFunctionBegin;
32710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
327270663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3273942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
32742a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3275942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3276942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
327724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
327824989b8cSPeter Brune         tsdm->originaldm = dm;
327924989b8cSPeter Brune       }
328024989b8cSPeter Brune     }
32816bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
328224989b8cSPeter Brune   }
32836c699258SBarry Smith   ts->dm = dm;
3284bbd56ea5SKarl Rupp 
3285089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3286089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
32876c699258SBarry Smith   PetscFunctionReturn(0);
32886c699258SBarry Smith }
32896c699258SBarry Smith 
32906c699258SBarry Smith #undef __FUNCT__
32916c699258SBarry Smith #define __FUNCT__ "TSGetDM"
32926c699258SBarry Smith /*@
32936c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
32946c699258SBarry Smith 
32953f9fe445SBarry Smith    Not Collective
32966c699258SBarry Smith 
32976c699258SBarry Smith    Input Parameter:
32986c699258SBarry Smith . ts - the preconditioner context
32996c699258SBarry Smith 
33006c699258SBarry Smith    Output Parameter:
33016c699258SBarry Smith .  dm - the dm
33026c699258SBarry Smith 
33036c699258SBarry Smith    Level: intermediate
33046c699258SBarry Smith 
33056c699258SBarry Smith 
33066c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
33076c699258SBarry Smith @*/
33087087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
33096c699258SBarry Smith {
3310496e6a7aSJed Brown   PetscErrorCode ierr;
3311496e6a7aSJed Brown 
33126c699258SBarry Smith   PetscFunctionBegin;
33130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3314496e6a7aSJed Brown   if (!ts->dm) {
3315ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3316496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3317496e6a7aSJed Brown   }
33186c699258SBarry Smith   *dm = ts->dm;
33196c699258SBarry Smith   PetscFunctionReturn(0);
33206c699258SBarry Smith }
33211713a123SBarry Smith 
33220f5c6efeSJed Brown #undef __FUNCT__
33230f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
33240f5c6efeSJed Brown /*@
33250f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
33260f5c6efeSJed Brown 
33273f9fe445SBarry Smith    Logically Collective on SNES
33280f5c6efeSJed Brown 
33290f5c6efeSJed Brown    Input Parameter:
3330d42a1c89SJed Brown + snes - nonlinear solver
33310910c330SBarry Smith . U - the current state at which to evaluate the residual
3332d42a1c89SJed Brown - ctx - user context, must be a TS
33330f5c6efeSJed Brown 
33340f5c6efeSJed Brown    Output Parameter:
33350f5c6efeSJed Brown . F - the nonlinear residual
33360f5c6efeSJed Brown 
33370f5c6efeSJed Brown    Notes:
33380f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
33390f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
33400f5c6efeSJed Brown 
33410f5c6efeSJed Brown    Level: advanced
33420f5c6efeSJed Brown 
33430f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
33440f5c6efeSJed Brown @*/
33450910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
33460f5c6efeSJed Brown {
33470f5c6efeSJed Brown   TS             ts = (TS)ctx;
33480f5c6efeSJed Brown   PetscErrorCode ierr;
33490f5c6efeSJed Brown 
33500f5c6efeSJed Brown   PetscFunctionBegin;
33510f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33520910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
33530f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
33540f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
33550910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
33560f5c6efeSJed Brown   PetscFunctionReturn(0);
33570f5c6efeSJed Brown }
33580f5c6efeSJed Brown 
33590f5c6efeSJed Brown #undef __FUNCT__
33600f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
33610f5c6efeSJed Brown /*@
33620f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
33630f5c6efeSJed Brown 
33640f5c6efeSJed Brown    Collective on SNES
33650f5c6efeSJed Brown 
33660f5c6efeSJed Brown    Input Parameter:
33670f5c6efeSJed Brown + snes - nonlinear solver
33680910c330SBarry Smith . U - the current state at which to evaluate the residual
33690f5c6efeSJed Brown - ctx - user context, must be a TS
33700f5c6efeSJed Brown 
33710f5c6efeSJed Brown    Output Parameter:
33720f5c6efeSJed Brown + A - the Jacobian
33730f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
33740f5c6efeSJed Brown - flag - indicates any structure change in the matrix
33750f5c6efeSJed Brown 
33760f5c6efeSJed Brown    Notes:
33770f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
33780f5c6efeSJed Brown 
33790f5c6efeSJed Brown    Level: developer
33800f5c6efeSJed Brown 
33810f5c6efeSJed Brown .seealso: SNESSetJacobian()
33820f5c6efeSJed Brown @*/
33830910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
33840f5c6efeSJed Brown {
33850f5c6efeSJed Brown   TS             ts = (TS)ctx;
33860f5c6efeSJed Brown   PetscErrorCode ierr;
33870f5c6efeSJed Brown 
33880f5c6efeSJed Brown   PetscFunctionBegin;
33890f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
33900910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
33910f5c6efeSJed Brown   PetscValidPointer(A,3);
33920f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
33930f5c6efeSJed Brown   PetscValidPointer(B,4);
33940f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
33950f5c6efeSJed Brown   PetscValidPointer(flag,5);
33960f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
33970910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
33980f5c6efeSJed Brown   PetscFunctionReturn(0);
33990f5c6efeSJed Brown }
3400325fc9f4SBarry Smith 
34010e4ef248SJed Brown #undef __FUNCT__
34020e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
34030e4ef248SJed Brown /*@C
34040e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
34050e4ef248SJed Brown 
34060e4ef248SJed Brown    Collective on TS
34070e4ef248SJed Brown 
34080e4ef248SJed Brown    Input Arguments:
34090e4ef248SJed Brown +  ts - time stepping context
34100e4ef248SJed Brown .  t - time at which to evaluate
34110910c330SBarry Smith .  U - state at which to evaluate
34120e4ef248SJed Brown -  ctx - context
34130e4ef248SJed Brown 
34140e4ef248SJed Brown    Output Arguments:
34150e4ef248SJed Brown .  F - right hand side
34160e4ef248SJed Brown 
34170e4ef248SJed Brown    Level: intermediate
34180e4ef248SJed Brown 
34190e4ef248SJed Brown    Notes:
34200e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
34210e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
34220e4ef248SJed Brown 
34230e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
34240e4ef248SJed Brown @*/
34250910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
34260e4ef248SJed Brown {
34270e4ef248SJed Brown   PetscErrorCode ierr;
34280e4ef248SJed Brown   Mat            Arhs,Brhs;
34290e4ef248SJed Brown   MatStructure   flg2;
34300e4ef248SJed Brown 
34310e4ef248SJed Brown   PetscFunctionBegin;
34320e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
34330910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
34340910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
34350e4ef248SJed Brown   PetscFunctionReturn(0);
34360e4ef248SJed Brown }
34370e4ef248SJed Brown 
34380e4ef248SJed Brown #undef __FUNCT__
34390e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
34400e4ef248SJed Brown /*@C
34410e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
34420e4ef248SJed Brown 
34430e4ef248SJed Brown    Collective on TS
34440e4ef248SJed Brown 
34450e4ef248SJed Brown    Input Arguments:
34460e4ef248SJed Brown +  ts - time stepping context
34470e4ef248SJed Brown .  t - time at which to evaluate
34480910c330SBarry Smith .  U - state at which to evaluate
34490e4ef248SJed Brown -  ctx - context
34500e4ef248SJed Brown 
34510e4ef248SJed Brown    Output Arguments:
34520e4ef248SJed Brown +  A - pointer to operator
34530e4ef248SJed Brown .  B - pointer to preconditioning matrix
34540e4ef248SJed Brown -  flg - matrix structure flag
34550e4ef248SJed Brown 
34560e4ef248SJed Brown    Level: intermediate
34570e4ef248SJed Brown 
34580e4ef248SJed Brown    Notes:
34590e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
34600e4ef248SJed Brown 
34610e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
34620e4ef248SJed Brown @*/
34630910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
34640e4ef248SJed Brown {
34650e4ef248SJed Brown   PetscFunctionBegin;
34660e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
34670e4ef248SJed Brown   PetscFunctionReturn(0);
34680e4ef248SJed Brown }
34690e4ef248SJed Brown 
34700026cea9SSean Farley #undef __FUNCT__
34710026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
34720026cea9SSean Farley /*@C
34730026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
34740026cea9SSean Farley 
34750026cea9SSean Farley    Collective on TS
34760026cea9SSean Farley 
34770026cea9SSean Farley    Input Arguments:
34780026cea9SSean Farley +  ts - time stepping context
34790026cea9SSean Farley .  t - time at which to evaluate
34800910c330SBarry Smith .  U - state at which to evaluate
34810910c330SBarry Smith .  Udot - time derivative of state vector
34820026cea9SSean Farley -  ctx - context
34830026cea9SSean Farley 
34840026cea9SSean Farley    Output Arguments:
34850026cea9SSean Farley .  F - left hand side
34860026cea9SSean Farley 
34870026cea9SSean Farley    Level: intermediate
34880026cea9SSean Farley 
34890026cea9SSean Farley    Notes:
34900910c330SBarry 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
34910026cea9SSean Farley    user is required to write their own TSComputeIFunction.
34920026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
34930026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
34940026cea9SSean Farley 
34950026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
34960026cea9SSean Farley @*/
34970910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
34980026cea9SSean Farley {
34990026cea9SSean Farley   PetscErrorCode ierr;
35000026cea9SSean Farley   Mat            A,B;
35010026cea9SSean Farley   MatStructure   flg2;
35020026cea9SSean Farley 
35030026cea9SSean Farley   PetscFunctionBegin;
35040298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
35050910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
35060910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
35070026cea9SSean Farley   PetscFunctionReturn(0);
35080026cea9SSean Farley }
35090026cea9SSean Farley 
35100026cea9SSean Farley #undef __FUNCT__
35110026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
35120026cea9SSean Farley /*@C
351324e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
35140026cea9SSean Farley 
35150026cea9SSean Farley    Collective on TS
35160026cea9SSean Farley 
35170026cea9SSean Farley    Input Arguments:
35180026cea9SSean Farley +  ts - time stepping context
35190026cea9SSean Farley .  t - time at which to evaluate
35200910c330SBarry Smith .  U - state at which to evaluate
35210910c330SBarry Smith .  Udot - time derivative of state vector
35220026cea9SSean Farley .  shift - shift to apply
35230026cea9SSean Farley -  ctx - context
35240026cea9SSean Farley 
35250026cea9SSean Farley    Output Arguments:
35260026cea9SSean Farley +  A - pointer to operator
35270026cea9SSean Farley .  B - pointer to preconditioning matrix
35280026cea9SSean Farley -  flg - matrix structure flag
35290026cea9SSean Farley 
35300026cea9SSean Farley    Level: intermediate
35310026cea9SSean Farley 
35320026cea9SSean Farley    Notes:
35330026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
35340026cea9SSean Farley 
35350026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
35360026cea9SSean Farley @*/
35370910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
35380026cea9SSean Farley {
35390026cea9SSean Farley   PetscFunctionBegin;
35400026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
35410026cea9SSean Farley   PetscFunctionReturn(0);
35420026cea9SSean Farley }
3543e817cc15SEmil Constantinescu #undef __FUNCT__
3544e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3545e817cc15SEmil Constantinescu /*@
3546e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3547e817cc15SEmil Constantinescu 
3548e817cc15SEmil Constantinescu    Not Collective
3549e817cc15SEmil Constantinescu 
3550e817cc15SEmil Constantinescu    Input Parameter:
3551e817cc15SEmil Constantinescu .  ts - the TS context
3552e817cc15SEmil Constantinescu 
3553e817cc15SEmil Constantinescu    Output Parameter:
35544e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3555e817cc15SEmil Constantinescu 
3556e817cc15SEmil Constantinescu    Level: beginner
3557e817cc15SEmil Constantinescu 
3558e817cc15SEmil Constantinescu .keywords: TS, equation type
3559e817cc15SEmil Constantinescu 
3560e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3561e817cc15SEmil Constantinescu @*/
3562e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3563e817cc15SEmil Constantinescu {
3564e817cc15SEmil Constantinescu   PetscFunctionBegin;
3565e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3566e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3567e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3568e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3569e817cc15SEmil Constantinescu }
3570e817cc15SEmil Constantinescu 
3571e817cc15SEmil Constantinescu #undef __FUNCT__
3572e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3573e817cc15SEmil Constantinescu /*@
3574e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3575e817cc15SEmil Constantinescu 
3576e817cc15SEmil Constantinescu    Not Collective
3577e817cc15SEmil Constantinescu 
3578e817cc15SEmil Constantinescu    Input Parameter:
3579e817cc15SEmil Constantinescu +  ts - the TS context
35804e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3581e817cc15SEmil Constantinescu 
3582e817cc15SEmil Constantinescu    Level: advanced
3583e817cc15SEmil Constantinescu 
3584e817cc15SEmil Constantinescu .keywords: TS, equation type
3585e817cc15SEmil Constantinescu 
3586e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3587e817cc15SEmil Constantinescu @*/
3588e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3589e817cc15SEmil Constantinescu {
3590e817cc15SEmil Constantinescu   PetscFunctionBegin;
3591e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3592e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3593e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3594e817cc15SEmil Constantinescu }
35950026cea9SSean Farley 
35964af1b03aSJed Brown #undef __FUNCT__
35974af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
35984af1b03aSJed Brown /*@
35994af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
36004af1b03aSJed Brown 
36014af1b03aSJed Brown    Not Collective
36024af1b03aSJed Brown 
36034af1b03aSJed Brown    Input Parameter:
36044af1b03aSJed Brown .  ts - the TS context
36054af1b03aSJed Brown 
36064af1b03aSJed Brown    Output Parameter:
36074af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
36084af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
36094af1b03aSJed Brown 
3610487e0bb9SJed Brown    Level: beginner
36114af1b03aSJed Brown 
3612cd652676SJed Brown    Notes:
3613cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
36144af1b03aSJed Brown 
36154af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
36164af1b03aSJed Brown 
36174af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
36184af1b03aSJed Brown @*/
36194af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
36204af1b03aSJed Brown {
36214af1b03aSJed Brown   PetscFunctionBegin;
36224af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36234af1b03aSJed Brown   PetscValidPointer(reason,2);
36244af1b03aSJed Brown   *reason = ts->reason;
36254af1b03aSJed Brown   PetscFunctionReturn(0);
36264af1b03aSJed Brown }
36274af1b03aSJed Brown 
3628fb1732b5SBarry Smith #undef __FUNCT__
3629d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3630d6ad946cSShri Abhyankar /*@
3631d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3632d6ad946cSShri Abhyankar 
3633d6ad946cSShri Abhyankar    Not Collective
3634d6ad946cSShri Abhyankar 
3635d6ad946cSShri Abhyankar    Input Parameter:
3636d6ad946cSShri Abhyankar +  ts - the TS context
3637d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3638d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3639d6ad946cSShri Abhyankar 
3640f5abba47SShri Abhyankar    Level: advanced
3641d6ad946cSShri Abhyankar 
3642d6ad946cSShri Abhyankar    Notes:
3643d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3644d6ad946cSShri Abhyankar 
3645d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3646d6ad946cSShri Abhyankar 
3647d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3648d6ad946cSShri Abhyankar @*/
3649d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3650d6ad946cSShri Abhyankar {
3651d6ad946cSShri Abhyankar   PetscFunctionBegin;
3652d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3653d6ad946cSShri Abhyankar   ts->reason = reason;
3654d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3655d6ad946cSShri Abhyankar }
3656d6ad946cSShri Abhyankar 
3657d6ad946cSShri Abhyankar #undef __FUNCT__
3658cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3659cc708dedSBarry Smith /*@
3660cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3661cc708dedSBarry Smith 
3662cc708dedSBarry Smith    Not Collective
3663cc708dedSBarry Smith 
3664cc708dedSBarry Smith    Input Parameter:
3665cc708dedSBarry Smith .  ts - the TS context
3666cc708dedSBarry Smith 
3667cc708dedSBarry Smith    Output Parameter:
3668cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3669cc708dedSBarry Smith 
3670487e0bb9SJed Brown    Level: beginner
3671cc708dedSBarry Smith 
3672cc708dedSBarry Smith    Notes:
3673cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3674cc708dedSBarry Smith 
3675cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3676cc708dedSBarry Smith 
3677cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3678cc708dedSBarry Smith @*/
3679cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3680cc708dedSBarry Smith {
3681cc708dedSBarry Smith   PetscFunctionBegin;
3682cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3683cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3684cc708dedSBarry Smith   *ftime = ts->solvetime;
3685cc708dedSBarry Smith   PetscFunctionReturn(0);
3686cc708dedSBarry Smith }
3687cc708dedSBarry Smith 
3688cc708dedSBarry Smith #undef __FUNCT__
36895ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
36909f67acb7SJed Brown /*@
36915ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
36929f67acb7SJed Brown    used by the time integrator.
36939f67acb7SJed Brown 
36949f67acb7SJed Brown    Not Collective
36959f67acb7SJed Brown 
36969f67acb7SJed Brown    Input Parameter:
36979f67acb7SJed Brown .  ts - TS context
36989f67acb7SJed Brown 
36999f67acb7SJed Brown    Output Parameter:
37009f67acb7SJed Brown .  nits - number of nonlinear iterations
37019f67acb7SJed Brown 
37029f67acb7SJed Brown    Notes:
37039f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
37049f67acb7SJed Brown 
37059f67acb7SJed Brown    Level: intermediate
37069f67acb7SJed Brown 
37079f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
37089f67acb7SJed Brown 
37095ef26d82SJed Brown .seealso:  TSGetKSPIterations()
37109f67acb7SJed Brown @*/
37115ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
37129f67acb7SJed Brown {
37139f67acb7SJed Brown   PetscFunctionBegin;
37149f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37159f67acb7SJed Brown   PetscValidIntPointer(nits,2);
37165ef26d82SJed Brown   *nits = ts->snes_its;
37179f67acb7SJed Brown   PetscFunctionReturn(0);
37189f67acb7SJed Brown }
37199f67acb7SJed Brown 
37209f67acb7SJed Brown #undef __FUNCT__
37215ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
37229f67acb7SJed Brown /*@
37235ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
37249f67acb7SJed Brown    used by the time integrator.
37259f67acb7SJed Brown 
37269f67acb7SJed Brown    Not Collective
37279f67acb7SJed Brown 
37289f67acb7SJed Brown    Input Parameter:
37299f67acb7SJed Brown .  ts - TS context
37309f67acb7SJed Brown 
37319f67acb7SJed Brown    Output Parameter:
37329f67acb7SJed Brown .  lits - number of linear iterations
37339f67acb7SJed Brown 
37349f67acb7SJed Brown    Notes:
37359f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
37369f67acb7SJed Brown 
37379f67acb7SJed Brown    Level: intermediate
37389f67acb7SJed Brown 
37399f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
37409f67acb7SJed Brown 
37415ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
37429f67acb7SJed Brown @*/
37435ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
37449f67acb7SJed Brown {
37459f67acb7SJed Brown   PetscFunctionBegin;
37469f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37479f67acb7SJed Brown   PetscValidIntPointer(lits,2);
37485ef26d82SJed Brown   *lits = ts->ksp_its;
37499f67acb7SJed Brown   PetscFunctionReturn(0);
37509f67acb7SJed Brown }
37519f67acb7SJed Brown 
37529f67acb7SJed Brown #undef __FUNCT__
3753cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3754cef5090cSJed Brown /*@
3755cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3756cef5090cSJed Brown 
3757cef5090cSJed Brown    Not Collective
3758cef5090cSJed Brown 
3759cef5090cSJed Brown    Input Parameter:
3760cef5090cSJed Brown .  ts - TS context
3761cef5090cSJed Brown 
3762cef5090cSJed Brown    Output Parameter:
3763cef5090cSJed Brown .  rejects - number of steps rejected
3764cef5090cSJed Brown 
3765cef5090cSJed Brown    Notes:
3766cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3767cef5090cSJed Brown 
3768cef5090cSJed Brown    Level: intermediate
3769cef5090cSJed Brown 
3770cef5090cSJed Brown .keywords: TS, get, number
3771cef5090cSJed Brown 
37725ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3773cef5090cSJed Brown @*/
3774cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3775cef5090cSJed Brown {
3776cef5090cSJed Brown   PetscFunctionBegin;
3777cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3778cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3779cef5090cSJed Brown   *rejects = ts->reject;
3780cef5090cSJed Brown   PetscFunctionReturn(0);
3781cef5090cSJed Brown }
3782cef5090cSJed Brown 
3783cef5090cSJed Brown #undef __FUNCT__
3784cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3785cef5090cSJed Brown /*@
3786cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3787cef5090cSJed Brown 
3788cef5090cSJed Brown    Not Collective
3789cef5090cSJed Brown 
3790cef5090cSJed Brown    Input Parameter:
3791cef5090cSJed Brown .  ts - TS context
3792cef5090cSJed Brown 
3793cef5090cSJed Brown    Output Parameter:
3794cef5090cSJed Brown .  fails - number of failed nonlinear solves
3795cef5090cSJed Brown 
3796cef5090cSJed Brown    Notes:
3797cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3798cef5090cSJed Brown 
3799cef5090cSJed Brown    Level: intermediate
3800cef5090cSJed Brown 
3801cef5090cSJed Brown .keywords: TS, get, number
3802cef5090cSJed Brown 
38035ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3804cef5090cSJed Brown @*/
3805cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3806cef5090cSJed Brown {
3807cef5090cSJed Brown   PetscFunctionBegin;
3808cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3809cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3810cef5090cSJed Brown   *fails = ts->num_snes_failures;
3811cef5090cSJed Brown   PetscFunctionReturn(0);
3812cef5090cSJed Brown }
3813cef5090cSJed Brown 
3814cef5090cSJed Brown #undef __FUNCT__
3815cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3816cef5090cSJed Brown /*@
3817cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3818cef5090cSJed Brown 
3819cef5090cSJed Brown    Not Collective
3820cef5090cSJed Brown 
3821cef5090cSJed Brown    Input Parameter:
3822cef5090cSJed Brown +  ts - TS context
3823cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3824cef5090cSJed Brown 
3825cef5090cSJed Brown    Notes:
3826cef5090cSJed Brown    The counter is reset to zero for each step
3827cef5090cSJed Brown 
3828cef5090cSJed Brown    Options Database Key:
3829cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3830cef5090cSJed Brown 
3831cef5090cSJed Brown    Level: intermediate
3832cef5090cSJed Brown 
3833cef5090cSJed Brown .keywords: TS, set, maximum, number
3834cef5090cSJed Brown 
38355ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3836cef5090cSJed Brown @*/
3837cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3838cef5090cSJed Brown {
3839cef5090cSJed Brown   PetscFunctionBegin;
3840cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3841cef5090cSJed Brown   ts->max_reject = rejects;
3842cef5090cSJed Brown   PetscFunctionReturn(0);
3843cef5090cSJed Brown }
3844cef5090cSJed Brown 
3845cef5090cSJed Brown #undef __FUNCT__
3846cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3847cef5090cSJed Brown /*@
3848cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3849cef5090cSJed Brown 
3850cef5090cSJed Brown    Not Collective
3851cef5090cSJed Brown 
3852cef5090cSJed Brown    Input Parameter:
3853cef5090cSJed Brown +  ts - TS context
3854cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3855cef5090cSJed Brown 
3856cef5090cSJed Brown    Notes:
3857cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3858cef5090cSJed Brown 
3859cef5090cSJed Brown    Options Database Key:
3860cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3861cef5090cSJed Brown 
3862cef5090cSJed Brown    Level: intermediate
3863cef5090cSJed Brown 
3864cef5090cSJed Brown .keywords: TS, set, maximum, number
3865cef5090cSJed Brown 
38665ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3867cef5090cSJed Brown @*/
3868cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3869cef5090cSJed Brown {
3870cef5090cSJed Brown   PetscFunctionBegin;
3871cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3872cef5090cSJed Brown   ts->max_snes_failures = fails;
3873cef5090cSJed Brown   PetscFunctionReturn(0);
3874cef5090cSJed Brown }
3875cef5090cSJed Brown 
3876cef5090cSJed Brown #undef __FUNCT__
3877cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3878cef5090cSJed Brown /*@
3879cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3880cef5090cSJed Brown 
3881cef5090cSJed Brown    Not Collective
3882cef5090cSJed Brown 
3883cef5090cSJed Brown    Input Parameter:
3884cef5090cSJed Brown +  ts - TS context
3885cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3886cef5090cSJed Brown 
3887cef5090cSJed Brown    Options Database Key:
3888cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3889cef5090cSJed Brown 
3890cef5090cSJed Brown    Level: intermediate
3891cef5090cSJed Brown 
3892cef5090cSJed Brown .keywords: TS, set, error
3893cef5090cSJed Brown 
38945ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3895cef5090cSJed Brown @*/
3896cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3897cef5090cSJed Brown {
3898cef5090cSJed Brown   PetscFunctionBegin;
3899cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3900cef5090cSJed Brown   ts->errorifstepfailed = err;
3901cef5090cSJed Brown   PetscFunctionReturn(0);
3902cef5090cSJed Brown }
3903cef5090cSJed Brown 
3904cef5090cSJed Brown #undef __FUNCT__
3905fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3906fb1732b5SBarry Smith /*@C
3907fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3908fb1732b5SBarry Smith 
3909fb1732b5SBarry Smith    Collective on TS
3910fb1732b5SBarry Smith 
3911fb1732b5SBarry Smith    Input Parameters:
3912fb1732b5SBarry Smith +  ts - the TS context
3913fb1732b5SBarry Smith .  step - current time-step
3914fb1732b5SBarry Smith .  ptime - current time
39150910c330SBarry Smith .  u - current state
3916fb1732b5SBarry Smith -  viewer - binary viewer
3917fb1732b5SBarry Smith 
3918fb1732b5SBarry Smith    Level: intermediate
3919fb1732b5SBarry Smith 
3920fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3921fb1732b5SBarry Smith 
3922fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3923fb1732b5SBarry Smith @*/
39240910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3925fb1732b5SBarry Smith {
3926fb1732b5SBarry Smith   PetscErrorCode ierr;
3927ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3928fb1732b5SBarry Smith 
3929fb1732b5SBarry Smith   PetscFunctionBegin;
39300910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3931ed81e22dSJed Brown   PetscFunctionReturn(0);
3932ed81e22dSJed Brown }
3933ed81e22dSJed Brown 
3934ed81e22dSJed Brown #undef __FUNCT__
3935ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3936ed81e22dSJed Brown /*@C
3937ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3938ed81e22dSJed Brown 
3939ed81e22dSJed Brown    Collective on TS
3940ed81e22dSJed Brown 
3941ed81e22dSJed Brown    Input Parameters:
3942ed81e22dSJed Brown +  ts - the TS context
3943ed81e22dSJed Brown .  step - current time-step
3944ed81e22dSJed Brown .  ptime - current time
39450910c330SBarry Smith .  u - current state
3946ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3947ed81e22dSJed Brown 
3948ed81e22dSJed Brown    Level: intermediate
3949ed81e22dSJed Brown 
3950ed81e22dSJed Brown    Notes:
3951ed81e22dSJed 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.
3952ed81e22dSJed Brown    These are named according to the file name template.
3953ed81e22dSJed Brown 
3954ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3955ed81e22dSJed Brown 
3956ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3957ed81e22dSJed Brown 
3958ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3959ed81e22dSJed Brown @*/
39600910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3961ed81e22dSJed Brown {
3962ed81e22dSJed Brown   PetscErrorCode ierr;
3963ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3964ed81e22dSJed Brown   PetscViewer    viewer;
3965ed81e22dSJed Brown 
3966ed81e22dSJed Brown   PetscFunctionBegin;
39678caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3968ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
39690910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3970ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3971ed81e22dSJed Brown   PetscFunctionReturn(0);
3972ed81e22dSJed Brown }
3973ed81e22dSJed Brown 
3974ed81e22dSJed Brown #undef __FUNCT__
3975ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3976ed81e22dSJed Brown /*@C
3977ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3978ed81e22dSJed Brown 
3979ed81e22dSJed Brown    Collective on TS
3980ed81e22dSJed Brown 
3981ed81e22dSJed Brown    Input Parameters:
3982ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3983ed81e22dSJed Brown 
3984ed81e22dSJed Brown    Level: intermediate
3985ed81e22dSJed Brown 
3986ed81e22dSJed Brown    Note:
3987ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3988ed81e22dSJed Brown 
3989ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3990ed81e22dSJed Brown 
3991ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3992ed81e22dSJed Brown @*/
3993ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3994ed81e22dSJed Brown {
3995ed81e22dSJed Brown   PetscErrorCode ierr;
3996ed81e22dSJed Brown 
3997ed81e22dSJed Brown   PetscFunctionBegin;
3998ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3999fb1732b5SBarry Smith   PetscFunctionReturn(0);
4000fb1732b5SBarry Smith }
4001fb1732b5SBarry Smith 
400284df9cb4SJed Brown #undef __FUNCT__
4003ad6bc421SBarry Smith #define __FUNCT__ "TSGetTSAdapt"
400484df9cb4SJed Brown /*@
4005ad6bc421SBarry Smith    TSGetTSAdapt - Get the adaptive controller context for the current method
400684df9cb4SJed Brown 
4007ed81e22dSJed Brown    Collective on TS if controller has not been created yet
400884df9cb4SJed Brown 
400984df9cb4SJed Brown    Input Arguments:
4010ed81e22dSJed Brown .  ts - time stepping context
401184df9cb4SJed Brown 
401284df9cb4SJed Brown    Output Arguments:
4013ed81e22dSJed Brown .  adapt - adaptive controller
401484df9cb4SJed Brown 
401584df9cb4SJed Brown    Level: intermediate
401684df9cb4SJed Brown 
4017ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
401884df9cb4SJed Brown @*/
4019ad6bc421SBarry Smith PetscErrorCode TSGetTSAdapt(TS ts,TSAdapt *adapt)
402084df9cb4SJed Brown {
402184df9cb4SJed Brown   PetscErrorCode ierr;
402284df9cb4SJed Brown 
402384df9cb4SJed Brown   PetscFunctionBegin;
402484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
402584df9cb4SJed Brown   PetscValidPointer(adapt,2);
402684df9cb4SJed Brown   if (!ts->adapt) {
4027ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
40281c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
40291c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
403084df9cb4SJed Brown   }
403184df9cb4SJed Brown   *adapt = ts->adapt;
403284df9cb4SJed Brown   PetscFunctionReturn(0);
403384df9cb4SJed Brown }
4034d6ebe24aSShri Abhyankar 
4035d6ebe24aSShri Abhyankar #undef __FUNCT__
40361c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
40371c3436cfSJed Brown /*@
40381c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
40391c3436cfSJed Brown 
40401c3436cfSJed Brown    Logically Collective
40411c3436cfSJed Brown 
40421c3436cfSJed Brown    Input Arguments:
40431c3436cfSJed Brown +  ts - time integration context
40441c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
40450298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
40461c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
40470298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
40481c3436cfSJed Brown 
40491c3436cfSJed Brown    Level: beginner
40501c3436cfSJed Brown 
4051c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
40521c3436cfSJed Brown @*/
40531c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
40541c3436cfSJed Brown {
40551c3436cfSJed Brown   PetscErrorCode ierr;
40561c3436cfSJed Brown 
40571c3436cfSJed Brown   PetscFunctionBegin;
4058c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
40591c3436cfSJed Brown   if (vatol) {
40601c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
40611c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4062bbd56ea5SKarl Rupp 
40631c3436cfSJed Brown     ts->vatol = vatol;
40641c3436cfSJed Brown   }
4065c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
40661c3436cfSJed Brown   if (vrtol) {
40671c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
40681c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4069bbd56ea5SKarl Rupp 
40701c3436cfSJed Brown     ts->vrtol = vrtol;
40711c3436cfSJed Brown   }
40721c3436cfSJed Brown   PetscFunctionReturn(0);
40731c3436cfSJed Brown }
40741c3436cfSJed Brown 
40751c3436cfSJed Brown #undef __FUNCT__
4076c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4077c5033834SJed Brown /*@
4078c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4079c5033834SJed Brown 
4080c5033834SJed Brown    Logically Collective
4081c5033834SJed Brown 
4082c5033834SJed Brown    Input Arguments:
4083c5033834SJed Brown .  ts - time integration context
4084c5033834SJed Brown 
4085c5033834SJed Brown    Output Arguments:
40860298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
40870298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
40880298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
40890298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4090c5033834SJed Brown 
4091c5033834SJed Brown    Level: beginner
4092c5033834SJed Brown 
4093c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4094c5033834SJed Brown @*/
4095c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4096c5033834SJed Brown {
4097c5033834SJed Brown   PetscFunctionBegin;
4098c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4099c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4100c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4101c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4102c5033834SJed Brown   PetscFunctionReturn(0);
4103c5033834SJed Brown }
4104c5033834SJed Brown 
4105c5033834SJed Brown #undef __FUNCT__
4106*b29392e1SShri #define __FUNCT__ "TSSetDifferentialEquationsIS"
4107*b29392e1SShri /*@
4108*b29392e1SShri    TSSetDifferentialEquationsIS - Sets an IS containing locations of differential equations in a DAE
4109*b29392e1SShri 
4110*b29392e1SShri    Not Collective
4111*b29392e1SShri 
4112*b29392e1SShri    Input Arguments:
4113*b29392e1SShri +  ts - time stepping context
4114*b29392e1SShri -  is_diff - Index set for differential equations
4115*b29392e1SShri 
4116*b29392e1SShri    Level: advanced
4117*b29392e1SShri 
4118*b29392e1SShri @*/
4119*b29392e1SShri PetscErrorCode TSSetDifferentialEquationsIS(TS ts, IS is_diff)
4120*b29392e1SShri {
4121*b29392e1SShri   PetscErrorCode ierr;
4122*b29392e1SShri 
4123*b29392e1SShri   PetscFunctionBegin;
4124*b29392e1SShri   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4125*b29392e1SShri   PetscValidHeaderSpecific(is_diff,IS_CLASSID,2);
4126*b29392e1SShri   PetscCheckSameComm(ts,1,is_diff,2);
4127*b29392e1SShri   ierr = PetscObjectReference((PetscObject)is_diff);CHKERRQ(ierr);
4128*b29392e1SShri   ierr = ISDestroy(&ts->is_diff);CHKERRQ(ierr);
4129*b29392e1SShri   ts->is_diff = is_diff;
4130*b29392e1SShri   PetscFunctionReturn(0);
4131*b29392e1SShri }
4132*b29392e1SShri 
4133*b29392e1SShri #undef __FUNCT__
41341c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
41351c3436cfSJed Brown /*@
41361c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
41371c3436cfSJed Brown 
41381c3436cfSJed Brown    Collective on TS
41391c3436cfSJed Brown 
41401c3436cfSJed Brown    Input Arguments:
41411c3436cfSJed Brown +  ts - time stepping context
41421c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
41431c3436cfSJed Brown 
41441c3436cfSJed Brown    Output Arguments:
41451c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
41461c3436cfSJed Brown 
41471c3436cfSJed Brown    Level: developer
41481c3436cfSJed Brown 
41491c3436cfSJed Brown .seealso: TSSetTolerances()
41501c3436cfSJed Brown @*/
41511c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
41521c3436cfSJed Brown {
41531c3436cfSJed Brown   PetscErrorCode    ierr;
4154*b29392e1SShri   PetscInt          i,n,N,rstart;
41550910c330SBarry Smith   const PetscScalar *u,*y;
41560910c330SBarry Smith   Vec               U;
41571c3436cfSJed Brown   PetscReal         sum,gsum;
41581c3436cfSJed Brown 
41591c3436cfSJed Brown   PetscFunctionBegin;
41601c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41611c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
41621c3436cfSJed Brown   PetscValidPointer(norm,3);
41630910c330SBarry Smith   U = ts->vec_sol;
41640910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4165ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
41661c3436cfSJed Brown 
41670910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
41680910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
4169*b29392e1SShri   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
41700910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
41711c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
41721c3436cfSJed Brown   sum  = 0.;
4173ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4174ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4175ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4176ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4177ceefc113SJed Brown     for (i=0; i<n; i++) {
41780910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41790910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4180ceefc113SJed Brown     }
4181ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4182ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4183ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4184ceefc113SJed Brown     const PetscScalar *atol;
4185ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4186ceefc113SJed Brown     for (i=0; i<n; i++) {
41870910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41880910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4189ceefc113SJed Brown     }
4190ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4191ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4192ceefc113SJed Brown     const PetscScalar *rtol;
4193ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4194ceefc113SJed Brown     for (i=0; i<n; i++) {
41950910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
41960910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4197ceefc113SJed Brown     }
4198ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4199ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
4200*b29392e1SShri     if (ts->is_diff) {
4201*b29392e1SShri       const PetscInt *idx;
4202*b29392e1SShri       PetscInt k;
4203*b29392e1SShri       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
4204*b29392e1SShri       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
4205*b29392e1SShri       for (i=0; i<n; i++) {
4206*b29392e1SShri 	k = idx[i] - rstart;
4207*b29392e1SShri 	PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
4208*b29392e1SShri 	sum += PetscSqr(PetscAbsScalar(y[k] - u[k]) / tol);
4209*b29392e1SShri       }
4210*b29392e1SShri     } else {
42111c3436cfSJed Brown       for (i=0; i<n; i++) {
42120910c330SBarry Smith 	PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42130910c330SBarry Smith 	sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
42141c3436cfSJed Brown       }
4215ceefc113SJed Brown     }
4216*b29392e1SShri   }
42170910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
42181c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
42191c3436cfSJed Brown 
4220ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
42211c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4222ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
42231c3436cfSJed Brown   PetscFunctionReturn(0);
42241c3436cfSJed Brown }
42251c3436cfSJed Brown 
42261c3436cfSJed Brown #undef __FUNCT__
42278d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
42288d59e960SJed Brown /*@
42298d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
42308d59e960SJed Brown 
42318d59e960SJed Brown    Logically Collective on TS
42328d59e960SJed Brown 
42338d59e960SJed Brown    Input Arguments:
42348d59e960SJed Brown +  ts - time stepping context
42358d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
42368d59e960SJed Brown 
42378d59e960SJed Brown    Note:
42388d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
42398d59e960SJed Brown 
42408d59e960SJed Brown    Level: intermediate
42418d59e960SJed Brown 
42428d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
42438d59e960SJed Brown @*/
42448d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
42458d59e960SJed Brown {
42468d59e960SJed Brown   PetscFunctionBegin;
42478d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42488d59e960SJed Brown   ts->cfltime_local = cfltime;
42498d59e960SJed Brown   ts->cfltime       = -1.;
42508d59e960SJed Brown   PetscFunctionReturn(0);
42518d59e960SJed Brown }
42528d59e960SJed Brown 
42538d59e960SJed Brown #undef __FUNCT__
42548d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
42558d59e960SJed Brown /*@
42568d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
42578d59e960SJed Brown 
42588d59e960SJed Brown    Collective on TS
42598d59e960SJed Brown 
42608d59e960SJed Brown    Input Arguments:
42618d59e960SJed Brown .  ts - time stepping context
42628d59e960SJed Brown 
42638d59e960SJed Brown    Output Arguments:
42648d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
42658d59e960SJed Brown 
42668d59e960SJed Brown    Level: advanced
42678d59e960SJed Brown 
42688d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
42698d59e960SJed Brown @*/
42708d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
42718d59e960SJed Brown {
42728d59e960SJed Brown   PetscErrorCode ierr;
42738d59e960SJed Brown 
42748d59e960SJed Brown   PetscFunctionBegin;
42758d59e960SJed Brown   if (ts->cfltime < 0) {
4276ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
42778d59e960SJed Brown   }
42788d59e960SJed Brown   *cfltime = ts->cfltime;
42798d59e960SJed Brown   PetscFunctionReturn(0);
42808d59e960SJed Brown }
42818d59e960SJed Brown 
42828d59e960SJed Brown #undef __FUNCT__
4283d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4284d6ebe24aSShri Abhyankar /*@
4285d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4286d6ebe24aSShri Abhyankar 
4287d6ebe24aSShri Abhyankar    Input Parameters:
4288d6ebe24aSShri Abhyankar .  ts   - the TS context.
4289d6ebe24aSShri Abhyankar .  xl   - lower bound.
4290d6ebe24aSShri Abhyankar .  xu   - upper bound.
4291d6ebe24aSShri Abhyankar 
4292d6ebe24aSShri Abhyankar    Notes:
4293d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
429433c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4295d6ebe24aSShri Abhyankar 
42962bd2b0e6SSatish Balay    Level: advanced
42972bd2b0e6SSatish Balay 
4298d6ebe24aSShri Abhyankar @*/
4299d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4300d6ebe24aSShri Abhyankar {
4301d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4302d6ebe24aSShri Abhyankar   SNES           snes;
4303d6ebe24aSShri Abhyankar 
4304d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4305d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4306d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4307d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4308d6ebe24aSShri Abhyankar }
4309d6ebe24aSShri Abhyankar 
4310325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4311c6db04a5SJed Brown #include <mex.h>
4312325fc9f4SBarry Smith 
4313325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4314325fc9f4SBarry Smith 
4315325fc9f4SBarry Smith #undef __FUNCT__
4316325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4317325fc9f4SBarry Smith /*
4318325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4319325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4320325fc9f4SBarry Smith 
4321325fc9f4SBarry Smith    Collective on TS
4322325fc9f4SBarry Smith 
4323325fc9f4SBarry Smith    Input Parameters:
4324325fc9f4SBarry Smith +  snes - the TS context
43250910c330SBarry Smith -  u - input vector
4326325fc9f4SBarry Smith 
4327325fc9f4SBarry Smith    Output Parameter:
4328325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4329325fc9f4SBarry Smith 
4330325fc9f4SBarry Smith    Notes:
4331325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4332325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4333325fc9f4SBarry Smith    themselves.
4334325fc9f4SBarry Smith 
4335325fc9f4SBarry Smith    Level: developer
4336325fc9f4SBarry Smith 
4337325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4338325fc9f4SBarry Smith 
4339325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4340325fc9f4SBarry Smith */
43410910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4342325fc9f4SBarry Smith {
4343325fc9f4SBarry Smith   PetscErrorCode  ierr;
4344325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4345325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4346325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4347325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4348325fc9f4SBarry Smith 
4349325fc9f4SBarry Smith   PetscFunctionBegin;
4350325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
43510910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
43520910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4353325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
43540910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4355325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4356325fc9f4SBarry Smith 
4357325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
43580910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43590910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
43600910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4361bbd56ea5SKarl Rupp 
4362325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4363325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4364325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4365325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4366325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4367325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4368325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4369325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4370325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4371325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4372325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4373325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4374325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4375325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4376325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4377325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4378325fc9f4SBarry Smith   PetscFunctionReturn(0);
4379325fc9f4SBarry Smith }
4380325fc9f4SBarry Smith 
4381325fc9f4SBarry Smith 
4382325fc9f4SBarry Smith #undef __FUNCT__
4383325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4384325fc9f4SBarry Smith /*
4385325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4386325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4387e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4388325fc9f4SBarry Smith 
4389325fc9f4SBarry Smith    Logically Collective on TS
4390325fc9f4SBarry Smith 
4391325fc9f4SBarry Smith    Input Parameters:
4392325fc9f4SBarry Smith +  ts - the TS context
4393325fc9f4SBarry Smith -  func - function evaluation routine
4394325fc9f4SBarry Smith 
4395325fc9f4SBarry Smith    Calling sequence of func:
43960910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4397325fc9f4SBarry Smith 
4398325fc9f4SBarry Smith    Level: beginner
4399325fc9f4SBarry Smith 
4400325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4401325fc9f4SBarry Smith 
4402325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4403325fc9f4SBarry Smith */
4404cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4405325fc9f4SBarry Smith {
4406325fc9f4SBarry Smith   PetscErrorCode  ierr;
4407325fc9f4SBarry Smith   TSMatlabContext *sctx;
4408325fc9f4SBarry Smith 
4409325fc9f4SBarry Smith   PetscFunctionBegin;
4410325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4411325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4412325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4413325fc9f4SBarry Smith   /*
4414325fc9f4SBarry Smith      This should work, but it doesn't
4415325fc9f4SBarry Smith   sctx->ctx = ctx;
4416325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4417325fc9f4SBarry Smith   */
4418325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4419bbd56ea5SKarl Rupp 
44200298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4421325fc9f4SBarry Smith   PetscFunctionReturn(0);
4422325fc9f4SBarry Smith }
4423325fc9f4SBarry Smith 
4424325fc9f4SBarry Smith #undef __FUNCT__
4425325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4426325fc9f4SBarry Smith /*
4427325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4428325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4429325fc9f4SBarry Smith 
4430325fc9f4SBarry Smith    Collective on TS
4431325fc9f4SBarry Smith 
4432325fc9f4SBarry Smith    Input Parameters:
4433cdcf91faSSean Farley +  ts - the TS context
44340910c330SBarry Smith .  u - input vector
4435325fc9f4SBarry Smith .  A, B - the matrices
4436325fc9f4SBarry Smith -  ctx - user context
4437325fc9f4SBarry Smith 
4438325fc9f4SBarry Smith    Output Parameter:
4439325fc9f4SBarry Smith .  flag - structure of the matrix
4440325fc9f4SBarry Smith 
4441325fc9f4SBarry Smith    Level: developer
4442325fc9f4SBarry Smith 
4443325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4444325fc9f4SBarry Smith 
4445325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4446325fc9f4SBarry Smith @*/
44470910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4448325fc9f4SBarry Smith {
4449325fc9f4SBarry Smith   PetscErrorCode  ierr;
4450325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4451325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4452325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4453325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4454325fc9f4SBarry Smith 
4455325fc9f4SBarry Smith   PetscFunctionBegin;
4456cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44570910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4458325fc9f4SBarry Smith 
44590910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4460325fc9f4SBarry Smith 
4461cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
44620910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
44630910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
44640910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
44650910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4466bbd56ea5SKarl Rupp 
4467325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4468325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4469325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4470325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4471325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4472325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4473325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4474325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4475325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4476325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4477325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4478325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4479325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4480325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4481325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4482325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4483325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4484325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4485325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4486325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4487325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4488325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4489325fc9f4SBarry Smith   PetscFunctionReturn(0);
4490325fc9f4SBarry Smith }
4491325fc9f4SBarry Smith 
4492325fc9f4SBarry Smith 
4493325fc9f4SBarry Smith #undef __FUNCT__
4494325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4495325fc9f4SBarry Smith /*
4496325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4497e3c5b3baSBarry 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
4498325fc9f4SBarry Smith 
4499325fc9f4SBarry Smith    Logically Collective on TS
4500325fc9f4SBarry Smith 
4501325fc9f4SBarry Smith    Input Parameters:
4502cdcf91faSSean Farley +  ts - the TS context
4503325fc9f4SBarry Smith .  A,B - Jacobian matrices
4504325fc9f4SBarry Smith .  func - function evaluation routine
4505325fc9f4SBarry Smith -  ctx - user context
4506325fc9f4SBarry Smith 
4507325fc9f4SBarry Smith    Calling sequence of func:
45080910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4509325fc9f4SBarry Smith 
4510325fc9f4SBarry Smith 
4511325fc9f4SBarry Smith    Level: developer
4512325fc9f4SBarry Smith 
4513325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4514325fc9f4SBarry Smith 
4515325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4516325fc9f4SBarry Smith */
4517cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4518325fc9f4SBarry Smith {
4519325fc9f4SBarry Smith   PetscErrorCode  ierr;
4520325fc9f4SBarry Smith   TSMatlabContext *sctx;
4521325fc9f4SBarry Smith 
4522325fc9f4SBarry Smith   PetscFunctionBegin;
4523325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4524325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4525325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4526325fc9f4SBarry Smith   /*
4527325fc9f4SBarry Smith      This should work, but it doesn't
4528325fc9f4SBarry Smith   sctx->ctx = ctx;
4529325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4530325fc9f4SBarry Smith   */
4531325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4532bbd56ea5SKarl Rupp 
4533cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4534325fc9f4SBarry Smith   PetscFunctionReturn(0);
4535325fc9f4SBarry Smith }
4536325fc9f4SBarry Smith 
4537b5b1a830SBarry Smith #undef __FUNCT__
4538b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4539b5b1a830SBarry Smith /*
4540b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4541b5b1a830SBarry Smith 
4542b5b1a830SBarry Smith    Collective on TS
4543b5b1a830SBarry Smith 
4544b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4545b5b1a830SBarry Smith @*/
45460910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4547b5b1a830SBarry Smith {
4548b5b1a830SBarry Smith   PetscErrorCode  ierr;
4549b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4550a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4551b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4552b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4553b5b1a830SBarry Smith 
4554b5b1a830SBarry Smith   PetscFunctionBegin;
4555cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45560910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4557b5b1a830SBarry Smith 
4558cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
45590910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4560bbd56ea5SKarl Rupp 
4561b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4562b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4563b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4564b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4565b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4566b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4567b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4568b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4569b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4570b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4571b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4572b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4573b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4574b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4575b5b1a830SBarry Smith   PetscFunctionReturn(0);
4576b5b1a830SBarry Smith }
4577b5b1a830SBarry Smith 
4578b5b1a830SBarry Smith 
4579b5b1a830SBarry Smith #undef __FUNCT__
4580b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4581b5b1a830SBarry Smith /*
4582b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4583b5b1a830SBarry Smith 
4584b5b1a830SBarry Smith    Level: developer
4585b5b1a830SBarry Smith 
4586b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4587b5b1a830SBarry Smith 
4588b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4589b5b1a830SBarry Smith */
4590cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4591b5b1a830SBarry Smith {
4592b5b1a830SBarry Smith   PetscErrorCode  ierr;
4593b5b1a830SBarry Smith   TSMatlabContext *sctx;
4594b5b1a830SBarry Smith 
4595b5b1a830SBarry Smith   PetscFunctionBegin;
4596b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4597b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4598b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4599b5b1a830SBarry Smith   /*
4600b5b1a830SBarry Smith      This should work, but it doesn't
4601b5b1a830SBarry Smith   sctx->ctx = ctx;
4602b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4603b5b1a830SBarry Smith   */
4604b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4605bbd56ea5SKarl Rupp 
46060298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4607b5b1a830SBarry Smith   PetscFunctionReturn(0);
4608b5b1a830SBarry Smith }
4609325fc9f4SBarry Smith #endif
4610b3603a34SBarry Smith 
46110b039ecaSBarry Smith 
46120b039ecaSBarry Smith 
4613b3603a34SBarry Smith #undef __FUNCT__
46144f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4615b3603a34SBarry Smith /*@C
46164f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4617b3603a34SBarry Smith        in a time based line graph
4618b3603a34SBarry Smith 
4619b3603a34SBarry Smith    Collective on TS
4620b3603a34SBarry Smith 
4621b3603a34SBarry Smith    Input Parameters:
4622b3603a34SBarry Smith +  ts - the TS context
4623b3603a34SBarry Smith .  step - current time-step
4624b3603a34SBarry Smith .  ptime - current time
4625b3603a34SBarry Smith -  lg - a line graph object
4626b3603a34SBarry Smith 
4627b3603a34SBarry Smith    Level: intermediate
4628b3603a34SBarry Smith 
46290b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4630b3603a34SBarry Smith 
4631b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4632b3603a34SBarry Smith 
4633b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4634b3603a34SBarry Smith @*/
46350910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4636b3603a34SBarry Smith {
4637b3603a34SBarry Smith   PetscErrorCode    ierr;
46380b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4639b3603a34SBarry Smith   const PetscScalar *yy;
464058ff32f7SBarry Smith   PetscInt          dim;
4641b3603a34SBarry Smith 
4642b3603a34SBarry Smith   PetscFunctionBegin;
464358ff32f7SBarry Smith   if (!step) {
4644a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4645a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4646a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
46470910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
46480b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
46490b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
465058ff32f7SBarry Smith   }
46510910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4652e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4653e3efe391SJed Brown   {
4654e3efe391SJed Brown     PetscReal *yreal;
4655e3efe391SJed Brown     PetscInt  i,n;
46560910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4657e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4658e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
46590b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4660e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4661e3efe391SJed Brown   }
4662e3efe391SJed Brown #else
46630b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4664e3efe391SJed Brown #endif
46650910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
46663fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
46670b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46683923b477SBarry Smith   }
4669b3603a34SBarry Smith   PetscFunctionReturn(0);
4670b3603a34SBarry Smith }
4671b3603a34SBarry Smith 
4672b3603a34SBarry Smith #undef __FUNCT__
46734f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4674ef20d060SBarry Smith /*@C
46754f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4676ef20d060SBarry Smith        in a time based line graph
4677ef20d060SBarry Smith 
4678ef20d060SBarry Smith    Collective on TS
4679ef20d060SBarry Smith 
4680ef20d060SBarry Smith    Input Parameters:
4681ef20d060SBarry Smith +  ts - the TS context
4682ef20d060SBarry Smith .  step - current time-step
4683ef20d060SBarry Smith .  ptime - current time
4684ef20d060SBarry Smith -  lg - a line graph object
4685ef20d060SBarry Smith 
4686ef20d060SBarry Smith    Level: intermediate
4687ef20d060SBarry Smith 
4688abd5a294SJed Brown    Notes:
4689abd5a294SJed Brown    Only for sequential solves.
4690abd5a294SJed Brown 
4691abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4692abd5a294SJed Brown 
4693abd5a294SJed Brown    Options Database Keys:
46944f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4695ef20d060SBarry Smith 
4696ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4697ef20d060SBarry Smith 
4698abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4699ef20d060SBarry Smith @*/
47000910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4701ef20d060SBarry Smith {
4702ef20d060SBarry Smith   PetscErrorCode    ierr;
47030b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4704ef20d060SBarry Smith   const PetscScalar *yy;
4705ef20d060SBarry Smith   Vec               y;
4706a9f9c1f6SBarry Smith   PetscInt          dim;
4707ef20d060SBarry Smith 
4708ef20d060SBarry Smith   PetscFunctionBegin;
4709a9f9c1f6SBarry Smith   if (!step) {
4710a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4711a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
47121ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
47130910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4714a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4715a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4716a9f9c1f6SBarry Smith   }
47170910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4718ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
47190910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4720ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4721e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4722e3efe391SJed Brown   {
4723e3efe391SJed Brown     PetscReal *yreal;
4724e3efe391SJed Brown     PetscInt  i,n;
4725e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4726e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4727e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
47280b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4729e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4730e3efe391SJed Brown   }
4731e3efe391SJed Brown #else
47320b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4733e3efe391SJed Brown #endif
4734ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4735ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
47363fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))) {
47370b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
47383923b477SBarry Smith   }
4739ef20d060SBarry Smith   PetscFunctionReturn(0);
4740ef20d060SBarry Smith }
4741ef20d060SBarry Smith 
4742201da799SBarry Smith #undef __FUNCT__
4743201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4744201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4745201da799SBarry Smith {
4746201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4747201da799SBarry Smith   PetscReal      x   = ptime,y;
4748201da799SBarry Smith   PetscErrorCode ierr;
4749201da799SBarry Smith   PetscInt       its;
4750201da799SBarry Smith 
4751201da799SBarry Smith   PetscFunctionBegin;
4752201da799SBarry Smith   if (!n) {
4753201da799SBarry Smith     PetscDrawAxis axis;
4754bbd56ea5SKarl Rupp 
4755201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4756201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4757201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4758bbd56ea5SKarl Rupp 
4759201da799SBarry Smith     ctx->snes_its = 0;
4760201da799SBarry Smith   }
4761201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4762201da799SBarry Smith   y    = its - ctx->snes_its;
4763201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
47643fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4765201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4766201da799SBarry Smith   }
4767201da799SBarry Smith   ctx->snes_its = its;
4768201da799SBarry Smith   PetscFunctionReturn(0);
4769201da799SBarry Smith }
4770201da799SBarry Smith 
4771201da799SBarry Smith #undef __FUNCT__
4772201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4773201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4774201da799SBarry Smith {
4775201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4776201da799SBarry Smith   PetscReal      x   = ptime,y;
4777201da799SBarry Smith   PetscErrorCode ierr;
4778201da799SBarry Smith   PetscInt       its;
4779201da799SBarry Smith 
4780201da799SBarry Smith   PetscFunctionBegin;
4781201da799SBarry Smith   if (!n) {
4782201da799SBarry Smith     PetscDrawAxis axis;
4783bbd56ea5SKarl Rupp 
4784201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4785201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4786201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4787bbd56ea5SKarl Rupp 
4788201da799SBarry Smith     ctx->ksp_its = 0;
4789201da799SBarry Smith   }
4790201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4791201da799SBarry Smith   y    = its - ctx->ksp_its;
4792201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
479399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4794201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4795201da799SBarry Smith   }
4796201da799SBarry Smith   ctx->ksp_its = its;
4797201da799SBarry Smith   PetscFunctionReturn(0);
4798201da799SBarry Smith }
4799f9c1d6abSBarry Smith 
4800f9c1d6abSBarry Smith #undef __FUNCT__
4801f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4802f9c1d6abSBarry Smith /*@
4803f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4804f9c1d6abSBarry Smith 
4805f9c1d6abSBarry Smith    Collective on TS and Vec
4806f9c1d6abSBarry Smith 
4807f9c1d6abSBarry Smith    Input Parameters:
4808f9c1d6abSBarry Smith +  ts - the TS context
4809f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4810f9c1d6abSBarry Smith 
4811f9c1d6abSBarry Smith    Output Parameters:
4812f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4813f9c1d6abSBarry Smith 
4814f9c1d6abSBarry Smith    Level: developer
4815f9c1d6abSBarry Smith 
4816f9c1d6abSBarry Smith .keywords: TS, compute
4817f9c1d6abSBarry Smith 
4818f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4819f9c1d6abSBarry Smith @*/
4820f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4821f9c1d6abSBarry Smith {
4822f9c1d6abSBarry Smith   PetscErrorCode ierr;
4823f9c1d6abSBarry Smith 
4824f9c1d6abSBarry Smith   PetscFunctionBegin;
4825f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4826ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4827f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4828f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4829f9c1d6abSBarry Smith }
4830