xref: /petsc/src/ts/interface/ts.c (revision e1244c694be870577ddfeb27d69a78d09ad24070)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
144a2ae208SSatish Balay #undef __FUNCT__
15bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
16bdad233fSMatthew Knepley /*
17bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Collective on TS
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Input Parameter:
22bdad233fSMatthew Knepley . ts - The ts
23bdad233fSMatthew Knepley 
24bdad233fSMatthew Knepley   Level: intermediate
25bdad233fSMatthew Knepley 
26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
28bdad233fSMatthew Knepley */
296849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
30bdad233fSMatthew Knepley {
31ace3abfcSBarry Smith   PetscBool      opt;
322fc52814SBarry Smith   const char     *defaultType;
33bdad233fSMatthew Knepley   char           typeName[256];
34dfbe8321SBarry Smith   PetscErrorCode ierr;
35bdad233fSMatthew Knepley 
36bdad233fSMatthew Knepley   PetscFunctionBegin;
37bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
38bbd56ea5SKarl Rupp   else defaultType = TSEULER;
39bdad233fSMatthew Knepley 
40607a6623SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);}
41bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
42a7cc72afSBarry Smith   if (opt) {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   } else {
45bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
46bdad233fSMatthew Knepley   }
47bdad233fSMatthew Knepley   PetscFunctionReturn(0);
48bdad233fSMatthew Knepley }
49bdad233fSMatthew Knepley 
502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
512d5ee99bSBarry Smith   PetscViewer   viewer;
524b363babSBarry Smith   PetscDrawAxis axis;
532d5ee99bSBarry Smith   Vec           initialsolution;
542d5ee99bSBarry Smith   PetscBool     showinitial;
552d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
562d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
572d5ee99bSBarry Smith   int           color;
582d5ee99bSBarry Smith };
592d5ee99bSBarry Smith 
60bdad233fSMatthew Knepley #undef __FUNCT__
61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
62bdad233fSMatthew Knepley /*@
63bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Collective on TS
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley    Input Parameter:
68bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
69bdad233fSMatthew Knepley 
70bdad233fSMatthew Knepley    Options Database Keys:
714d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
72bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
733bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
74bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
75bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
76de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
77de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
78de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
79de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
80de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
81de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
82de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
832d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
84de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
8591b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
8691b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
8791b97e58SBarry Smith 
8891b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
89bdad233fSMatthew Knepley 
90bdad233fSMatthew Knepley    Level: beginner
91bdad233fSMatthew Knepley 
92bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
93bdad233fSMatthew Knepley 
94a313700dSBarry Smith .seealso: TSGetType()
95bdad233fSMatthew Knepley @*/
967087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
97bdad233fSMatthew Knepley {
98ace3abfcSBarry Smith   PetscBool              opt,flg;
99dfbe8321SBarry Smith   PetscErrorCode         ierr;
100649052a6SBarry Smith   PetscViewer            monviewer;
101eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
102089b2837SJed Brown   SNES                   snes;
1031c3436cfSJed Brown   TSAdapt                adapt;
10431748224SBarry Smith   PetscReal              time_step;
10549354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
106d1212d36SBarry Smith   char                   dir[16];
107bdad233fSMatthew Knepley 
108bdad233fSMatthew Knepley   PetscFunctionBegin;
1090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1103194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
111a43b19c4SJed Brown   /* Handle TS type options */
112a43b19c4SJed Brown   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley   /* Handle generic TS options */
1150298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1160298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1170298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11831748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
11931748224SBarry Smith   if (flg) {
12031748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12131748224SBarry Smith   }
12249354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
12349354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1240298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1250298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1260298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1280298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
129bdad233fSMatthew Knepley 
130bdad233fSMatthew Knepley   /* Monitor options */
131a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
132eabae89aSBarry Smith   if (flg) {
133ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
134649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
135bdad233fSMatthew Knepley   }
1365180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1375180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1385180491cSLisandro Dalcin 
1394f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
140a7cc72afSBarry Smith   if (opt) {
1410b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1423923b477SBarry Smith     PetscInt       howoften = 1;
143a80ad3e0SBarry Smith 
1440298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
145ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1464f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
147b3603a34SBarry Smith   }
1484f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
149b3603a34SBarry Smith   if (opt) {
1500b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1513923b477SBarry Smith     PetscInt       howoften = 1;
152b3603a34SBarry Smith 
1530298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
15422d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1554f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
156bdad233fSMatthew Knepley   }
1574f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
158ef20d060SBarry Smith   if (opt) {
1590b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1603923b477SBarry Smith     PetscInt       howoften = 1;
161ef20d060SBarry Smith 
1620298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
16322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1644f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
165ef20d060SBarry Smith   }
166201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
167201da799SBarry Smith   if (opt) {
168201da799SBarry Smith     TSMonitorLGCtx ctx;
169201da799SBarry Smith     PetscInt       howoften = 1;
170201da799SBarry Smith 
1710298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
17222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
173201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
174201da799SBarry Smith   }
175201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
176201da799SBarry Smith   if (opt) {
177201da799SBarry Smith     TSMonitorLGCtx ctx;
178201da799SBarry Smith     PetscInt       howoften = 1;
179201da799SBarry Smith 
1800298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
18122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
182201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
183201da799SBarry Smith   }
1848189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1858189c53fSBarry Smith   if (opt) {
1868189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1878189c53fSBarry Smith     PetscInt          howoften = 1;
1888189c53fSBarry Smith 
1890298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
19022d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1918189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1928189c53fSBarry Smith   }
193ef20d060SBarry Smith   opt  = PETSC_FALSE;
1940dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
195a7cc72afSBarry Smith   if (opt) {
19683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19783a4ac43SBarry Smith     PetscInt         howoften = 1;
198a80ad3e0SBarry Smith 
1990298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
200ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
20183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
202bdad233fSMatthew Knepley   }
203fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2042d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2052d5ee99bSBarry Smith   if (opt) {
2062d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2072d5ee99bSBarry Smith     PetscReal        bounds[4];
2082d5ee99bSBarry Smith     PetscInt         n = 4;
2092d5ee99bSBarry Smith     PetscDraw        draw;
2102d5ee99bSBarry Smith 
2112d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2122d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2132d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2142d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2152d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2164b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2174b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
218f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2194b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
22007995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2212d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2222d5ee99bSBarry Smith   }
2232d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2240dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2253a471f94SBarry Smith   if (opt) {
22683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
22783a4ac43SBarry Smith     PetscInt         howoften = 1;
2283a471f94SBarry Smith 
2290298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
230ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
23183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2323a471f94SBarry Smith   }
2333a471f94SBarry Smith   opt  = PETSC_FALSE;
23491b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
235fb1732b5SBarry Smith   if (flg) {
236fb1732b5SBarry Smith     PetscViewer ctx;
237fb1732b5SBarry Smith     if (monfilename[0]) {
238ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
239c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
240fb1732b5SBarry Smith     } else {
241ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2420298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
243fb1732b5SBarry Smith     }
244fb1732b5SBarry Smith   }
245ed81e22dSJed Brown   opt  = PETSC_FALSE;
24691b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
247ed81e22dSJed Brown   if (flg) {
248ed81e22dSJed Brown     const char *ptr,*ptr2;
249ed81e22dSJed Brown     char       *filetemplate;
250ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
251ed81e22dSJed Brown     /* Do some cursory validation of the input. */
252ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
253ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
254ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
255ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
256ce94432eSBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
257ed81e22dSJed Brown       if (ptr2) break;
258ed81e22dSJed Brown     }
259ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
260ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
261ed81e22dSJed Brown   }
262bdad233fSMatthew Knepley 
263d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
264d1212d36SBarry Smith   if (flg) {
265d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
266d1212d36SBarry Smith     int                 ray = 0;
267d1212d36SBarry Smith     DMDADirection       ddir;
268d1212d36SBarry Smith     DM                  da;
269d1212d36SBarry Smith     PetscMPIInt         rank;
270d1212d36SBarry Smith 
271ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
272d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
273d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
274ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
275d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
276d1212d36SBarry Smith 
277d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
278d1212d36SBarry Smith     ierr = PetscNew(TSMonitorDMDARayCtx,&rayctx);CHKERRQ(ierr);
279d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
280d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
281ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
282d1212d36SBarry Smith     if (!rank) {
283d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
284d1212d36SBarry Smith     }
285d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
286d1212d36SBarry Smith   }
287d1212d36SBarry Smith 
288552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2891c3436cfSJed Brown   ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2901c3436cfSJed Brown 
291d52bd9f3SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
292d52bd9f3SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
293d52bd9f3SBarry Smith 
294bdad233fSMatthew Knepley   /* Handle specific TS options */
295abc0a331SBarry Smith   if (ts->ops->setfromoptions) {
296bdad233fSMatthew Knepley     ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
297bdad233fSMatthew Knepley   }
2985d973c19SBarry Smith 
2995d973c19SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3005d973c19SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
301bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
302bdad233fSMatthew Knepley   PetscFunctionReturn(0);
303bdad233fSMatthew Knepley }
304bdad233fSMatthew Knepley 
305bdad233fSMatthew Knepley #undef __FUNCT__
306cdcf91faSSean Farley #undef __FUNCT__
3074a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
308a7a1495cSBarry Smith /*@
3098c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
310a7a1495cSBarry Smith       set with TSSetRHSJacobian().
311a7a1495cSBarry Smith 
312a7a1495cSBarry Smith    Collective on TS and Vec
313a7a1495cSBarry Smith 
314a7a1495cSBarry Smith    Input Parameters:
315316643e7SJed Brown +  ts - the TS context
316a7a1495cSBarry Smith .  t - current timestep
3170910c330SBarry Smith -  U - input vector
318a7a1495cSBarry Smith 
319a7a1495cSBarry Smith    Output Parameters:
320a7a1495cSBarry Smith +  A - Jacobian matrix
321a7a1495cSBarry Smith .  B - optional preconditioning matrix
322a7a1495cSBarry Smith -  flag - flag indicating matrix structure
323a7a1495cSBarry Smith 
324a7a1495cSBarry Smith    Notes:
325a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
326a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
327a7a1495cSBarry Smith 
32894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
329a7a1495cSBarry Smith    flag parameter.
330a7a1495cSBarry Smith 
331a7a1495cSBarry Smith    Level: developer
332a7a1495cSBarry Smith 
333a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
334a7a1495cSBarry Smith 
33594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
336a7a1495cSBarry Smith @*/
3370910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
338a7a1495cSBarry Smith {
339dfbe8321SBarry Smith   PetscErrorCode ierr;
3400910c330SBarry Smith   PetscInt       Ustate;
34124989b8cSPeter Brune   DM             dm;
342942e3340SBarry Smith   DMTS           tsdm;
34324989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
34424989b8cSPeter Brune   void           *ctx;
34524989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
346a7a1495cSBarry Smith 
347a7a1495cSBarry Smith   PetscFunctionBegin;
3480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3490910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3500910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
35124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
352942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
35324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
3540298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
3550910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
3560910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
3570e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
3580e4ef248SJed Brown     PetscFunctionReturn(0);
359f8ede8e7SJed Brown   }
360d90be118SSean Farley 
361ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
362d90be118SSean Farley 
363*e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
364*e1244c69SJed Brown     ierr = MatShift(*A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
365*e1244c69SJed Brown     ierr = MatScale(*A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
366*e1244c69SJed Brown     if (*A != *B) {
367*e1244c69SJed Brown       ierr = MatShift(*B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
368*e1244c69SJed Brown       ierr = MatScale(*B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
369*e1244c69SJed Brown     }
370*e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
371*e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
372*e1244c69SJed Brown   }
373*e1244c69SJed Brown 
37424989b8cSPeter Brune   if (rhsjacobianfunc) {
3750910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
376a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
377a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3780910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
379a7a1495cSBarry Smith     PetscStackPop;
3800910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
381a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3820700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3830700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
384ef66eb69SBarry Smith   } else {
385214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
386214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
387214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
388ef66eb69SBarry Smith   }
3890e4ef248SJed Brown   ts->rhsjacobian.time       = t;
3900910c330SBarry Smith   ts->rhsjacobian.X          = U;
3910910c330SBarry Smith   ierr                       = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3920e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
393a7a1495cSBarry Smith   PetscFunctionReturn(0);
394a7a1495cSBarry Smith }
395a7a1495cSBarry Smith 
3964a2ae208SSatish Balay #undef __FUNCT__
3974a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
398316643e7SJed Brown /*@
399d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
400d763cef2SBarry Smith 
401316643e7SJed Brown    Collective on TS and Vec
402316643e7SJed Brown 
403316643e7SJed Brown    Input Parameters:
404316643e7SJed Brown +  ts - the TS context
405316643e7SJed Brown .  t - current time
4060910c330SBarry Smith -  U - state vector
407316643e7SJed Brown 
408316643e7SJed Brown    Output Parameter:
409316643e7SJed Brown .  y - right hand side
410316643e7SJed Brown 
411316643e7SJed Brown    Note:
412316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
413316643e7SJed Brown    is used internally within the nonlinear solvers.
414316643e7SJed Brown 
415316643e7SJed Brown    Level: developer
416316643e7SJed Brown 
417316643e7SJed Brown .keywords: TS, compute
418316643e7SJed Brown 
419316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
420316643e7SJed Brown @*/
4210910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
422d763cef2SBarry Smith {
423dfbe8321SBarry Smith   PetscErrorCode ierr;
42424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
42524989b8cSPeter Brune   TSIFunction    ifunction;
42624989b8cSPeter Brune   void           *ctx;
42724989b8cSPeter Brune   DM             dm;
42824989b8cSPeter Brune 
429d763cef2SBarry Smith   PetscFunctionBegin;
4300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4310910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4320700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
43324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
43424989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4350298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
436d763cef2SBarry Smith 
437ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
438d763cef2SBarry Smith 
4390910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
44024989b8cSPeter Brune   if (rhsfunction) {
441d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4420910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
443d763cef2SBarry Smith     PetscStackPop;
444214bc6a2SJed Brown   } else {
445214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
446b2cd27e8SJed Brown   }
44744a41b28SSean Farley 
4480910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
449d763cef2SBarry Smith   PetscFunctionReturn(0);
450d763cef2SBarry Smith }
451d763cef2SBarry Smith 
4524a2ae208SSatish Balay #undef __FUNCT__
453ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
454ef20d060SBarry Smith /*@
455ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
456ef20d060SBarry Smith 
457ef20d060SBarry Smith    Collective on TS and Vec
458ef20d060SBarry Smith 
459ef20d060SBarry Smith    Input Parameters:
460ef20d060SBarry Smith +  ts - the TS context
461ef20d060SBarry Smith -  t - current time
462ef20d060SBarry Smith 
463ef20d060SBarry Smith    Output Parameter:
4640910c330SBarry Smith .  U - the solution
465ef20d060SBarry Smith 
466ef20d060SBarry Smith    Note:
467ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
468ef20d060SBarry Smith    is used internally within the nonlinear solvers.
469ef20d060SBarry Smith 
470ef20d060SBarry Smith    Level: developer
471ef20d060SBarry Smith 
472ef20d060SBarry Smith .keywords: TS, compute
473ef20d060SBarry Smith 
474abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
475ef20d060SBarry Smith @*/
4760910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
477ef20d060SBarry Smith {
478ef20d060SBarry Smith   PetscErrorCode     ierr;
479ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
480ef20d060SBarry Smith   void               *ctx;
481ef20d060SBarry Smith   DM                 dm;
482ef20d060SBarry Smith 
483ef20d060SBarry Smith   PetscFunctionBegin;
484ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4850910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
486ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
487ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
488ef20d060SBarry Smith 
489ef20d060SBarry Smith   if (solutionfunction) {
4909b7cd975SBarry Smith     PetscStackPush("TS user solution function");
4910910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
492ef20d060SBarry Smith     PetscStackPop;
493ef20d060SBarry Smith   }
494ef20d060SBarry Smith   PetscFunctionReturn(0);
495ef20d060SBarry Smith }
4969b7cd975SBarry Smith #undef __FUNCT__
4979b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
4989b7cd975SBarry Smith /*@
4999b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5009b7cd975SBarry Smith 
5019b7cd975SBarry Smith    Collective on TS and Vec
5029b7cd975SBarry Smith 
5039b7cd975SBarry Smith    Input Parameters:
5049b7cd975SBarry Smith +  ts - the TS context
5059b7cd975SBarry Smith -  t - current time
5069b7cd975SBarry Smith 
5079b7cd975SBarry Smith    Output Parameter:
5089b7cd975SBarry Smith .  U - the function value
5099b7cd975SBarry Smith 
5109b7cd975SBarry Smith    Note:
5119b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5129b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5139b7cd975SBarry Smith 
5149b7cd975SBarry Smith    Level: developer
5159b7cd975SBarry Smith 
5169b7cd975SBarry Smith .keywords: TS, compute
5179b7cd975SBarry Smith 
5189b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5199b7cd975SBarry Smith @*/
5209b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5219b7cd975SBarry Smith {
5229b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5239b7cd975SBarry Smith   void               *ctx;
5249b7cd975SBarry Smith   DM                 dm;
5259b7cd975SBarry Smith 
5269b7cd975SBarry Smith   PetscFunctionBegin;
5279b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5289b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5299b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5309b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5319b7cd975SBarry Smith 
5329b7cd975SBarry Smith   if (forcing) {
5339b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5349b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5359b7cd975SBarry Smith     PetscStackPop;
5369b7cd975SBarry Smith   }
5379b7cd975SBarry Smith   PetscFunctionReturn(0);
5389b7cd975SBarry Smith }
539ef20d060SBarry Smith 
540ef20d060SBarry Smith #undef __FUNCT__
541214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
542214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
543214bc6a2SJed Brown {
5442dd45cf8SJed Brown   Vec            F;
545214bc6a2SJed Brown   PetscErrorCode ierr;
546214bc6a2SJed Brown 
547214bc6a2SJed Brown   PetscFunctionBegin;
5480298fd71SBarry Smith   *Frhs = NULL;
5490298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
550214bc6a2SJed Brown   if (!ts->Frhs) {
5512dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
552214bc6a2SJed Brown   }
553214bc6a2SJed Brown   *Frhs = ts->Frhs;
554214bc6a2SJed Brown   PetscFunctionReturn(0);
555214bc6a2SJed Brown }
556214bc6a2SJed Brown 
557214bc6a2SJed Brown #undef __FUNCT__
558214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
559214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
560214bc6a2SJed Brown {
561214bc6a2SJed Brown   Mat            A,B;
5622dd45cf8SJed Brown   PetscErrorCode ierr;
563214bc6a2SJed Brown 
564214bc6a2SJed Brown   PetscFunctionBegin;
5650298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
566214bc6a2SJed Brown   if (Arhs) {
567214bc6a2SJed Brown     if (!ts->Arhs) {
568214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
569214bc6a2SJed Brown     }
570214bc6a2SJed Brown     *Arhs = ts->Arhs;
571214bc6a2SJed Brown   }
572214bc6a2SJed Brown   if (Brhs) {
573214bc6a2SJed Brown     if (!ts->Brhs) {
574214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
575214bc6a2SJed Brown     }
576214bc6a2SJed Brown     *Brhs = ts->Brhs;
577214bc6a2SJed Brown   }
578214bc6a2SJed Brown   PetscFunctionReturn(0);
579214bc6a2SJed Brown }
580214bc6a2SJed Brown 
581214bc6a2SJed Brown #undef __FUNCT__
582316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
583316643e7SJed Brown /*@
5840910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
585316643e7SJed Brown 
586316643e7SJed Brown    Collective on TS and Vec
587316643e7SJed Brown 
588316643e7SJed Brown    Input Parameters:
589316643e7SJed Brown +  ts - the TS context
590316643e7SJed Brown .  t - current time
5910910c330SBarry Smith .  U - state vector
5920910c330SBarry Smith .  Udot - time derivative of state vector
593214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
594316643e7SJed Brown 
595316643e7SJed Brown    Output Parameter:
596316643e7SJed Brown .  Y - right hand side
597316643e7SJed Brown 
598316643e7SJed Brown    Note:
599316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
600316643e7SJed Brown    is used internally within the nonlinear solvers.
601316643e7SJed Brown 
602316643e7SJed Brown    If the user did did not write their equations in implicit form, this
603316643e7SJed Brown    function recasts them in implicit form.
604316643e7SJed Brown 
605316643e7SJed Brown    Level: developer
606316643e7SJed Brown 
607316643e7SJed Brown .keywords: TS, compute
608316643e7SJed Brown 
609316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
610316643e7SJed Brown @*/
6110910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
612316643e7SJed Brown {
613316643e7SJed Brown   PetscErrorCode ierr;
61424989b8cSPeter Brune   TSIFunction    ifunction;
61524989b8cSPeter Brune   TSRHSFunction  rhsfunction;
61624989b8cSPeter Brune   void           *ctx;
61724989b8cSPeter Brune   DM             dm;
618316643e7SJed Brown 
619316643e7SJed Brown   PetscFunctionBegin;
6200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6210910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6220910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6230700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
624316643e7SJed Brown 
62524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
62624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6270298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
62824989b8cSPeter Brune 
629ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
630d90be118SSean Farley 
6310910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
63224989b8cSPeter Brune   if (ifunction) {
633316643e7SJed Brown     PetscStackPush("TS user implicit function");
6340910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
635316643e7SJed Brown     PetscStackPop;
636214bc6a2SJed Brown   }
637214bc6a2SJed Brown   if (imex) {
63824989b8cSPeter Brune     if (!ifunction) {
6390910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
6402dd45cf8SJed Brown     }
64124989b8cSPeter Brune   } else if (rhsfunction) {
64224989b8cSPeter Brune     if (ifunction) {
643214bc6a2SJed Brown       Vec Frhs;
644214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
6450910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
646214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
6472dd45cf8SJed Brown     } else {
6480910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
6490910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
650316643e7SJed Brown     }
6514a6899ffSJed Brown   }
6520910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
653316643e7SJed Brown   PetscFunctionReturn(0);
654316643e7SJed Brown }
655316643e7SJed Brown 
656316643e7SJed Brown #undef __FUNCT__
657316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
658316643e7SJed Brown /*@
659316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
660316643e7SJed Brown 
661316643e7SJed Brown    Collective on TS and Vec
662316643e7SJed Brown 
663316643e7SJed Brown    Input
664316643e7SJed Brown       Input Parameters:
665316643e7SJed Brown +  ts - the TS context
666316643e7SJed Brown .  t - current timestep
6670910c330SBarry Smith .  U - state vector
6680910c330SBarry Smith .  Udot - time derivative of state vector
669214bc6a2SJed Brown .  shift - shift to apply, see note below
670214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
671316643e7SJed Brown 
672316643e7SJed Brown    Output Parameters:
673316643e7SJed Brown +  A - Jacobian matrix
674316643e7SJed Brown .  B - optional preconditioning matrix
675316643e7SJed Brown -  flag - flag indicating matrix structure
676316643e7SJed Brown 
677316643e7SJed Brown    Notes:
6780910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
679316643e7SJed Brown 
6800910c330SBarry Smith    dF/dU + shift*dF/dUdot
681316643e7SJed Brown 
682316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
683316643e7SJed Brown    is used internally within the nonlinear solvers.
684316643e7SJed Brown 
685316643e7SJed Brown    Level: developer
686316643e7SJed Brown 
687316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
688316643e7SJed Brown 
689316643e7SJed Brown .seealso:  TSSetIJacobian()
69063495f91SJed Brown @*/
6910910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
692316643e7SJed Brown {
6930910c330SBarry Smith   PetscInt       Ustate, Udotstate;
694316643e7SJed Brown   PetscErrorCode ierr;
69524989b8cSPeter Brune   TSIJacobian    ijacobian;
69624989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
69724989b8cSPeter Brune   DM             dm;
69824989b8cSPeter Brune   void           *ctx;
699316643e7SJed Brown 
700316643e7SJed Brown   PetscFunctionBegin;
7010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7020910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7030910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
704316643e7SJed Brown   PetscValidPointer(A,6);
7050700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
706316643e7SJed Brown   PetscValidPointer(B,7);
7070700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
708316643e7SJed Brown   PetscValidPointer(flg,8);
70924989b8cSPeter Brune 
71024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
71124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7120298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
71324989b8cSPeter Brune 
7140910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
7150910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
7160910c330SBarry 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))) {
7170026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
7180026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
7190026cea9SSean Farley     PetscFunctionReturn(0);
7200026cea9SSean Farley   }
721316643e7SJed Brown 
722ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
723316643e7SJed Brown 
7244e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
7250910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
72624989b8cSPeter Brune   if (ijacobian) {
7272dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
728316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
7290910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
730316643e7SJed Brown     PetscStackPop;
731214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
732214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
733214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
7344a6899ffSJed Brown   }
735214bc6a2SJed Brown   if (imex) {
736b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
737214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
7382dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
739214bc6a2SJed Brown       if (*A != *B) {
740214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
741214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
742214bc6a2SJed Brown       }
743214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
744214bc6a2SJed Brown     }
745214bc6a2SJed Brown   } else {
746*e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
747*e1244c69SJed Brown     MatStructure flg2;
748*e1244c69SJed Brown     if (rhsjacobian) {
749*e1244c69SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
750*e1244c69SJed Brown       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
751*e1244c69SJed Brown     }
752*e1244c69SJed Brown     if (Arhs == *A) {           /* No IJacobian, so we only have the RHS matrix */
753*e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
754*e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
755214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
756214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
757316643e7SJed Brown       if (*A != *B) {
758316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
759316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
760316643e7SJed Brown       }
761*e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
762*e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
763*e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
764*e1244c69SJed Brown         ierr = MatZeroEntries(*A);CHKERRQ(ierr);
765*e1244c69SJed Brown         ierr = MatShift(*A,shift);CHKERRQ(ierr);
766*e1244c69SJed Brown         if (*A != *B) {
767*e1244c69SJed Brown           ierr = MatZeroEntries(*B);CHKERRQ(ierr);
768*e1244c69SJed Brown           ierr = MatShift(*B,shift);CHKERRQ(ierr);
769*e1244c69SJed Brown         }
770*e1244c69SJed Brown       }
771214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
772214bc6a2SJed Brown       if (*A != *B) {
773214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
774214bc6a2SJed Brown       }
775214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
776214bc6a2SJed Brown     }
777316643e7SJed Brown   }
7780026cea9SSean Farley 
7790026cea9SSean Farley   ts->ijacobian.time = t;
7800910c330SBarry Smith   ts->ijacobian.X    = U;
7810910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
782bbd56ea5SKarl Rupp 
7830910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
7840910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
785bbd56ea5SKarl Rupp 
7860026cea9SSean Farley   ts->ijacobian.shift      = shift;
7870026cea9SSean Farley   ts->ijacobian.imex       = imex;
7880026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
789bbd56ea5SKarl Rupp 
7900910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
791316643e7SJed Brown   PetscFunctionReturn(0);
792316643e7SJed Brown }
793316643e7SJed Brown 
794316643e7SJed Brown #undef __FUNCT__
7954a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
796d763cef2SBarry Smith /*@C
797d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
798b5abc632SBarry Smith     where U_t = G(t,u).
799d763cef2SBarry Smith 
8003f9fe445SBarry Smith     Logically Collective on TS
801d763cef2SBarry Smith 
802d763cef2SBarry Smith     Input Parameters:
803d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8040298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
805d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
806d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8070298fd71SBarry Smith           function evaluation routine (may be NULL)
808d763cef2SBarry Smith 
809d763cef2SBarry Smith     Calling sequence of func:
81087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
811d763cef2SBarry Smith 
812d763cef2SBarry Smith +   t - current timestep
813d763cef2SBarry Smith .   u - input vector
814d763cef2SBarry Smith .   F - function vector
815d763cef2SBarry Smith -   ctx - [optional] user-defined function context
816d763cef2SBarry Smith 
817d763cef2SBarry Smith     Level: beginner
818d763cef2SBarry Smith 
819d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
820d763cef2SBarry Smith 
821d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
822d763cef2SBarry Smith @*/
823089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
824d763cef2SBarry Smith {
825089b2837SJed Brown   PetscErrorCode ierr;
826089b2837SJed Brown   SNES           snes;
8270298fd71SBarry Smith   Vec            ralloc = NULL;
82824989b8cSPeter Brune   DM             dm;
829d763cef2SBarry Smith 
830089b2837SJed Brown   PetscFunctionBegin;
8310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
832ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
83324989b8cSPeter Brune 
83424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
83524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
836089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
837e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
838e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
839e856ceecSJed Brown     r    = ralloc;
840e856ceecSJed Brown   }
841089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
842e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
843d763cef2SBarry Smith   PetscFunctionReturn(0);
844d763cef2SBarry Smith }
845d763cef2SBarry Smith 
8464a2ae208SSatish Balay #undef __FUNCT__
847ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
848ef20d060SBarry Smith /*@C
849abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
850ef20d060SBarry Smith 
851ef20d060SBarry Smith     Logically Collective on TS
852ef20d060SBarry Smith 
853ef20d060SBarry Smith     Input Parameters:
854ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
855ef20d060SBarry Smith .   f - routine for evaluating the solution
856ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8570298fd71SBarry Smith           function evaluation routine (may be NULL)
858ef20d060SBarry Smith 
859ef20d060SBarry Smith     Calling sequence of func:
860ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
861ef20d060SBarry Smith 
862ef20d060SBarry Smith +   t - current timestep
863ef20d060SBarry Smith .   u - output vector
864ef20d060SBarry Smith -   ctx - [optional] user-defined function context
865ef20d060SBarry Smith 
866abd5a294SJed Brown     Notes:
867abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
868abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
869abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
870abd5a294SJed Brown 
8714f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
872abd5a294SJed Brown 
873ef20d060SBarry Smith     Level: beginner
874ef20d060SBarry Smith 
875ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
876ef20d060SBarry Smith 
8779b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
878ef20d060SBarry Smith @*/
879ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
880ef20d060SBarry Smith {
881ef20d060SBarry Smith   PetscErrorCode ierr;
882ef20d060SBarry Smith   DM             dm;
883ef20d060SBarry Smith 
884ef20d060SBarry Smith   PetscFunctionBegin;
885ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
886ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
887ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
888ef20d060SBarry Smith   PetscFunctionReturn(0);
889ef20d060SBarry Smith }
890ef20d060SBarry Smith 
891ef20d060SBarry Smith #undef __FUNCT__
8929b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
8939b7cd975SBarry Smith /*@C
8949b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
8959b7cd975SBarry Smith 
8969b7cd975SBarry Smith     Logically Collective on TS
8979b7cd975SBarry Smith 
8989b7cd975SBarry Smith     Input Parameters:
8999b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9009b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9019b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9020298fd71SBarry Smith           function evaluation routine (may be NULL)
9039b7cd975SBarry Smith 
9049b7cd975SBarry Smith     Calling sequence of func:
9059b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9069b7cd975SBarry Smith 
9079b7cd975SBarry Smith +   t - current timestep
9089b7cd975SBarry Smith .   u - output vector
9099b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9109b7cd975SBarry Smith 
9119b7cd975SBarry Smith     Notes:
9129b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9139b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9149b7cd975SBarry Smith 
9159b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9169b7cd975SBarry Smith 
9179b7cd975SBarry Smith     Level: beginner
9189b7cd975SBarry Smith 
9199b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9209b7cd975SBarry Smith 
9219b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9229b7cd975SBarry Smith @*/
9239b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9249b7cd975SBarry Smith {
9259b7cd975SBarry Smith   PetscErrorCode ierr;
9269b7cd975SBarry Smith   DM             dm;
9279b7cd975SBarry Smith 
9289b7cd975SBarry Smith   PetscFunctionBegin;
9299b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9309b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9319b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9329b7cd975SBarry Smith   PetscFunctionReturn(0);
9339b7cd975SBarry Smith }
9349b7cd975SBarry Smith 
9359b7cd975SBarry Smith #undef __FUNCT__
9364a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
937d763cef2SBarry Smith /*@C
938d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
939b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
940d763cef2SBarry Smith 
9413f9fe445SBarry Smith    Logically Collective on TS
942d763cef2SBarry Smith 
943d763cef2SBarry Smith    Input Parameters:
944d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
945e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
946e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
947d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
948d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9490298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
950d763cef2SBarry Smith 
951d763cef2SBarry Smith    Calling sequence of func:
95287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
953d763cef2SBarry Smith 
954d763cef2SBarry Smith +  t - current timestep
955d763cef2SBarry Smith .  u - input vector
956e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
957e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
958d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
95994b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
960d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
961d763cef2SBarry Smith 
962d763cef2SBarry Smith    Notes:
96394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
964d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
965d763cef2SBarry Smith 
966d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
967d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
96856335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
969d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
970d763cef2SBarry Smith    throughout the global iterations.
971d763cef2SBarry Smith 
972d763cef2SBarry Smith    Level: beginner
973d763cef2SBarry Smith 
974d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
975d763cef2SBarry Smith 
976*e1244c69SJed Brown .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse()
977d763cef2SBarry Smith 
978d763cef2SBarry Smith @*/
979e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
980d763cef2SBarry Smith {
981277b19d0SLisandro Dalcin   PetscErrorCode ierr;
982089b2837SJed Brown   SNES           snes;
98324989b8cSPeter Brune   DM             dm;
98424989b8cSPeter Brune   TSIJacobian    ijacobian;
985277b19d0SLisandro Dalcin 
986d763cef2SBarry Smith   PetscFunctionBegin;
9870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
988e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
989e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
990e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
991e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
992d763cef2SBarry Smith 
99324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
99424989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
995*e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
996*e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
997*e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
998*e1244c69SJed Brown   }
9990298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1000089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10015f659677SPeter Brune   if (!ijacobian) {
1002e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10030e4ef248SJed Brown   }
1004e5d3d808SBarry Smith   if (Amat) {
1005e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10060e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1007bbd56ea5SKarl Rupp 
1008e5d3d808SBarry Smith     ts->Arhs = Amat;
10090e4ef248SJed Brown   }
1010e5d3d808SBarry Smith   if (Pmat) {
1011e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10120e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1013bbd56ea5SKarl Rupp 
1014e5d3d808SBarry Smith     ts->Brhs = Pmat;
10150e4ef248SJed Brown   }
1016d763cef2SBarry Smith   PetscFunctionReturn(0);
1017d763cef2SBarry Smith }
1018d763cef2SBarry Smith 
1019316643e7SJed Brown 
1020316643e7SJed Brown #undef __FUNCT__
1021316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1022316643e7SJed Brown /*@C
1023b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1024316643e7SJed Brown 
10253f9fe445SBarry Smith    Logically Collective on TS
1026316643e7SJed Brown 
1027316643e7SJed Brown    Input Parameters:
1028316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10290298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1030316643e7SJed Brown .  f   - the function evaluation routine
10310298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1032316643e7SJed Brown 
1033316643e7SJed Brown    Calling sequence of f:
1034316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1035316643e7SJed Brown 
1036316643e7SJed Brown +  t   - time at step/stage being solved
1037316643e7SJed Brown .  u   - state vector
1038316643e7SJed Brown .  u_t - time derivative of state vector
1039316643e7SJed Brown .  F   - function vector
1040316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1041316643e7SJed Brown 
1042316643e7SJed Brown    Important:
1043d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1044316643e7SJed Brown 
1045316643e7SJed Brown    Level: beginner
1046316643e7SJed Brown 
1047316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1048316643e7SJed Brown 
1049d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1050316643e7SJed Brown @*/
1051089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1052316643e7SJed Brown {
1053089b2837SJed Brown   PetscErrorCode ierr;
1054089b2837SJed Brown   SNES           snes;
10550298fd71SBarry Smith   Vec            resalloc = NULL;
105624989b8cSPeter Brune   DM             dm;
1057316643e7SJed Brown 
1058316643e7SJed Brown   PetscFunctionBegin;
10590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1060ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
106124989b8cSPeter Brune 
106224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106324989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
106424989b8cSPeter Brune 
1065089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1066e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1067e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1068e856ceecSJed Brown     res  = resalloc;
1069e856ceecSJed Brown   }
1070089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1071e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1072089b2837SJed Brown   PetscFunctionReturn(0);
1073089b2837SJed Brown }
1074089b2837SJed Brown 
1075089b2837SJed Brown #undef __FUNCT__
1076089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1077089b2837SJed Brown /*@C
1078089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1079089b2837SJed Brown 
1080089b2837SJed Brown    Not Collective
1081089b2837SJed Brown 
1082089b2837SJed Brown    Input Parameter:
1083089b2837SJed Brown .  ts - the TS context
1084089b2837SJed Brown 
1085089b2837SJed Brown    Output Parameter:
10860298fd71SBarry Smith +  r - vector to hold residual (or NULL)
10870298fd71SBarry Smith .  func - the function to compute residual (or NULL)
10880298fd71SBarry Smith -  ctx - the function context (or NULL)
1089089b2837SJed Brown 
1090089b2837SJed Brown    Level: advanced
1091089b2837SJed Brown 
1092089b2837SJed Brown .keywords: TS, nonlinear, get, function
1093089b2837SJed Brown 
1094089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1095089b2837SJed Brown @*/
1096089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1097089b2837SJed Brown {
1098089b2837SJed Brown   PetscErrorCode ierr;
1099089b2837SJed Brown   SNES           snes;
110024989b8cSPeter Brune   DM             dm;
1101089b2837SJed Brown 
1102089b2837SJed Brown   PetscFunctionBegin;
1103089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1104089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11050298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
110624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1108089b2837SJed Brown   PetscFunctionReturn(0);
1109089b2837SJed Brown }
1110089b2837SJed Brown 
1111089b2837SJed Brown #undef __FUNCT__
1112089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1113089b2837SJed Brown /*@C
1114089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1115089b2837SJed Brown 
1116089b2837SJed Brown    Not Collective
1117089b2837SJed Brown 
1118089b2837SJed Brown    Input Parameter:
1119089b2837SJed Brown .  ts - the TS context
1120089b2837SJed Brown 
1121089b2837SJed Brown    Output Parameter:
11220298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11230298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11240298fd71SBarry Smith -  ctx - the function context (or NULL)
1125089b2837SJed Brown 
1126089b2837SJed Brown    Level: advanced
1127089b2837SJed Brown 
1128089b2837SJed Brown .keywords: TS, nonlinear, get, function
1129089b2837SJed Brown 
1130089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1131089b2837SJed Brown @*/
1132089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1133089b2837SJed Brown {
1134089b2837SJed Brown   PetscErrorCode ierr;
1135089b2837SJed Brown   SNES           snes;
113624989b8cSPeter Brune   DM             dm;
1137089b2837SJed Brown 
1138089b2837SJed Brown   PetscFunctionBegin;
1139089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1140089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11410298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
114224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
114324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1144316643e7SJed Brown   PetscFunctionReturn(0);
1145316643e7SJed Brown }
1146316643e7SJed Brown 
1147316643e7SJed Brown #undef __FUNCT__
1148316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1149316643e7SJed Brown /*@C
1150a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1151a4f0a591SBarry Smith         you provided with TSSetIFunction().
1152316643e7SJed Brown 
11533f9fe445SBarry Smith    Logically Collective on TS
1154316643e7SJed Brown 
1155316643e7SJed Brown    Input Parameters:
1156316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1157e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1158e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1159316643e7SJed Brown .  f   - the Jacobian evaluation routine
11600298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1161316643e7SJed Brown 
1162316643e7SJed Brown    Calling sequence of f:
1163e5d3d808SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *Amat,Mat *Pmat,MatStructure *flag,void *ctx);
1164316643e7SJed Brown 
1165316643e7SJed Brown +  t    - time at step/stage being solved
11661b4a444bSJed Brown .  U    - state vector
11671b4a444bSJed Brown .  U_t  - time derivative of state vector
1168316643e7SJed Brown .  a    - shift
1169e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1170e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1171316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
1172316643e7SJed Brown           structure (same as flag in KSPSetOperators())
1173316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1174316643e7SJed Brown 
1175316643e7SJed Brown    Notes:
1176e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1177316643e7SJed Brown 
1178a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1179b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1180a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1181a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1182a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1183a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1184a4f0a591SBarry Smith 
1185316643e7SJed Brown    Level: beginner
1186316643e7SJed Brown 
1187316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1188316643e7SJed Brown 
11898d359177SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault()
1190316643e7SJed Brown 
1191316643e7SJed Brown @*/
1192e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1193316643e7SJed Brown {
1194316643e7SJed Brown   PetscErrorCode ierr;
1195089b2837SJed Brown   SNES           snes;
119624989b8cSPeter Brune   DM             dm;
1197316643e7SJed Brown 
1198316643e7SJed Brown   PetscFunctionBegin;
11990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1200e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1201e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1202e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1203e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
120424989b8cSPeter Brune 
120524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
120724989b8cSPeter Brune 
1208089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1209e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1210316643e7SJed Brown   PetscFunctionReturn(0);
1211316643e7SJed Brown }
1212316643e7SJed Brown 
12134a2ae208SSatish Balay #undef __FUNCT__
1214*e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1215*e1244c69SJed Brown /*@
1216*e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1217*e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1218*e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1219*e1244c69SJed Brown    not been changed by the TS.
1220*e1244c69SJed Brown 
1221*e1244c69SJed Brown    Logically Collective
1222*e1244c69SJed Brown 
1223*e1244c69SJed Brown    Input Arguments:
1224*e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1225*e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1226*e1244c69SJed Brown 
1227*e1244c69SJed Brown    Level: intermediate
1228*e1244c69SJed Brown 
1229*e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1230*e1244c69SJed Brown @*/
1231*e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1232*e1244c69SJed Brown {
1233*e1244c69SJed Brown   PetscFunctionBegin;
1234*e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1235*e1244c69SJed Brown   PetscFunctionReturn(0);
1236*e1244c69SJed Brown }
1237*e1244c69SJed Brown 
1238*e1244c69SJed Brown #undef __FUNCT__
123955849f57SBarry Smith #define __FUNCT__ "TSLoad"
124055849f57SBarry Smith /*@C
124155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
124255849f57SBarry Smith 
124355849f57SBarry Smith   Collective on PetscViewer
124455849f57SBarry Smith 
124555849f57SBarry Smith   Input Parameters:
124655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
124755849f57SBarry Smith            some related function before a call to TSLoad().
124855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
124955849f57SBarry Smith 
125055849f57SBarry Smith    Level: intermediate
125155849f57SBarry Smith 
125255849f57SBarry Smith   Notes:
125355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
125455849f57SBarry Smith 
125555849f57SBarry Smith   Notes for advanced users:
125655849f57SBarry Smith   Most users should not need to know the details of the binary storage
125755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
125855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
125955849f57SBarry Smith   format is
126055849f57SBarry Smith .vb
126155849f57SBarry Smith      has not yet been determined
126255849f57SBarry Smith .ve
126355849f57SBarry Smith 
126455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
126555849f57SBarry Smith @*/
1266f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
126755849f57SBarry Smith {
126855849f57SBarry Smith   PetscErrorCode ierr;
126955849f57SBarry Smith   PetscBool      isbinary;
127055849f57SBarry Smith   PetscInt       classid;
127155849f57SBarry Smith   char           type[256];
12722d53ad75SBarry Smith   DMTS           sdm;
1273ad6bc421SBarry Smith   DM             dm;
127455849f57SBarry Smith 
127555849f57SBarry Smith   PetscFunctionBegin;
1276f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
127755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
127855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
127955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
128055849f57SBarry Smith 
128155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1282ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
128355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1284f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1285f2c2a1b9SBarry Smith   if (ts->ops->load) {
1286f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1287f2c2a1b9SBarry Smith   }
1288ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1289ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1290ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1291f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1292f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
12932d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
12942d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
129555849f57SBarry Smith   PetscFunctionReturn(0);
129655849f57SBarry Smith }
129755849f57SBarry Smith 
12989804daf3SBarry Smith #include <petscdraw.h>
1299f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1300f05ece33SBarry Smith #include <petscviewerams.h>
1301f05ece33SBarry Smith #endif
130255849f57SBarry Smith #undef __FUNCT__
13034a2ae208SSatish Balay #define __FUNCT__ "TSView"
13047e2c5f70SBarry Smith /*@C
1305d763cef2SBarry Smith     TSView - Prints the TS data structure.
1306d763cef2SBarry Smith 
13074c49b128SBarry Smith     Collective on TS
1308d763cef2SBarry Smith 
1309d763cef2SBarry Smith     Input Parameters:
1310d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1311d763cef2SBarry Smith -   viewer - visualization context
1312d763cef2SBarry Smith 
1313d763cef2SBarry Smith     Options Database Key:
1314d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1315d763cef2SBarry Smith 
1316d763cef2SBarry Smith     Notes:
1317d763cef2SBarry Smith     The available visualization contexts include
1318b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1319b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1320d763cef2SBarry Smith          output where only the first processor opens
1321d763cef2SBarry Smith          the file.  All other processors send their
1322d763cef2SBarry Smith          data to the first processor to print.
1323d763cef2SBarry Smith 
1324d763cef2SBarry Smith     The user can open an alternative visualization context with
1325b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1326d763cef2SBarry Smith 
1327d763cef2SBarry Smith     Level: beginner
1328d763cef2SBarry Smith 
1329d763cef2SBarry Smith .keywords: TS, timestep, view
1330d763cef2SBarry Smith 
1331b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1332d763cef2SBarry Smith @*/
13337087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1334d763cef2SBarry Smith {
1335dfbe8321SBarry Smith   PetscErrorCode ierr;
133619fd82e9SBarry Smith   TSType         type;
13372b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13382d53ad75SBarry Smith   DMTS           sdm;
1339f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1340f05ece33SBarry Smith   PetscBool      isams;
1341f05ece33SBarry Smith #endif
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith   PetscFunctionBegin;
13440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13453050cee2SBarry Smith   if (!viewer) {
1346ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13473050cee2SBarry Smith   }
13480700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1349c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1350fd16b177SBarry Smith 
1351251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1352251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
135355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13542b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1355f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1356f05ece33SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERAMS,&isams);CHKERRQ(ierr);
1357f05ece33SBarry Smith #endif
135832077d6dSBarry Smith   if (iascii) {
1359317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
136077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1361a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1362d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13635ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1364c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1365d763cef2SBarry Smith     }
13665ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1367193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13682d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13692d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1370d52bd9f3SBarry Smith     if (ts->ops->view) {
1371d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1372d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1373d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1374d52bd9f3SBarry Smith     }
13750f5bd95cSBarry Smith   } else if (isstring) {
1376a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1377b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
137855849f57SBarry Smith   } else if (isbinary) {
137955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
138055849f57SBarry Smith     MPI_Comm    comm;
138155849f57SBarry Smith     PetscMPIInt rank;
138255849f57SBarry Smith     char        type[256];
138355849f57SBarry Smith 
138455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
138555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
138655849f57SBarry Smith     if (!rank) {
138755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
138855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
138955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
139055849f57SBarry Smith     }
139155849f57SBarry Smith     if (ts->ops->view) {
139255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
139355849f57SBarry Smith     }
1394f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1395f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
13962d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13972d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
13982b0a91c0SBarry Smith   } else if (isdraw) {
13992b0a91c0SBarry Smith     PetscDraw draw;
14002b0a91c0SBarry Smith     char      str[36];
140189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14022b0a91c0SBarry Smith 
14032b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14042b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14052b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14062b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14070298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
140889fd9fafSBarry Smith     bottom = y - h;
14092b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14102b0a91c0SBarry Smith     if (ts->ops->view) {
14112b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14122b0a91c0SBarry Smith     }
14132b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1414f05ece33SBarry Smith #if defined(PETSC_HAVE_AMS)
1415f05ece33SBarry Smith   } else if (isams) {
1416f05ece33SBarry Smith     if (((PetscObject)ts)->amsmem == -1) {
1417f05ece33SBarry Smith       ierr = PetscObjectViewAMS((PetscObject)ts,viewer);CHKERRQ(ierr);
1418f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_take_access,(((PetscObject)ts)->amsmem));
1419f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time step",&ts->steps,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1420f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_add_field,(((PetscObject)ts)->amsmem,"time",&ts->ptime,1,AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
1421f05ece33SBarry Smith       PetscStackCallAMS(AMS_Memory_grant_access,(((PetscObject)ts)->amsmem));
1422d763cef2SBarry Smith     }
14230acecf5bSBarry Smith     if (ts->ops->view) {
14240acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14250acecf5bSBarry Smith     }
1426f05ece33SBarry Smith #endif
1427f05ece33SBarry Smith   }
1428f05ece33SBarry Smith 
1429b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1430251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1431b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1432d763cef2SBarry Smith   PetscFunctionReturn(0);
1433d763cef2SBarry Smith }
1434d763cef2SBarry Smith 
1435d763cef2SBarry Smith 
14364a2ae208SSatish Balay #undef __FUNCT__
14374a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1438b07ff414SBarry Smith /*@
1439d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1440d763cef2SBarry Smith    the timesteppers.
1441d763cef2SBarry Smith 
14423f9fe445SBarry Smith    Logically Collective on TS
1443d763cef2SBarry Smith 
1444d763cef2SBarry Smith    Input Parameters:
1445d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1446d763cef2SBarry Smith -  usrP - optional user context
1447d763cef2SBarry Smith 
1448d763cef2SBarry Smith    Level: intermediate
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1451d763cef2SBarry Smith 
1452d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1453d763cef2SBarry Smith @*/
14547087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1455d763cef2SBarry Smith {
1456d763cef2SBarry Smith   PetscFunctionBegin;
14570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1458d763cef2SBarry Smith   ts->user = usrP;
1459d763cef2SBarry Smith   PetscFunctionReturn(0);
1460d763cef2SBarry Smith }
1461d763cef2SBarry Smith 
14624a2ae208SSatish Balay #undef __FUNCT__
14634a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1464b07ff414SBarry Smith /*@
1465d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1466d763cef2SBarry Smith     timestepper.
1467d763cef2SBarry Smith 
1468d763cef2SBarry Smith     Not Collective
1469d763cef2SBarry Smith 
1470d763cef2SBarry Smith     Input Parameter:
1471d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1472d763cef2SBarry Smith 
1473d763cef2SBarry Smith     Output Parameter:
1474d763cef2SBarry Smith .   usrP - user context
1475d763cef2SBarry Smith 
1476d763cef2SBarry Smith     Level: intermediate
1477d763cef2SBarry Smith 
1478d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1479d763cef2SBarry Smith 
1480d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1481d763cef2SBarry Smith @*/
1482e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1483d763cef2SBarry Smith {
1484d763cef2SBarry Smith   PetscFunctionBegin;
14850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1486e71120c6SJed Brown   *(void**)usrP = ts->user;
1487d763cef2SBarry Smith   PetscFunctionReturn(0);
1488d763cef2SBarry Smith }
1489d763cef2SBarry Smith 
14904a2ae208SSatish Balay #undef __FUNCT__
14914a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1492d763cef2SBarry Smith /*@
1493b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1494d763cef2SBarry Smith 
1495d763cef2SBarry Smith    Not Collective
1496d763cef2SBarry Smith 
1497d763cef2SBarry Smith    Input Parameter:
1498d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1499d763cef2SBarry Smith 
1500d763cef2SBarry Smith    Output Parameter:
1501b8123daeSJed Brown .  iter - number of steps completed so far
1502d763cef2SBarry Smith 
1503d763cef2SBarry Smith    Level: intermediate
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1506b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1507d763cef2SBarry Smith @*/
15087087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1509d763cef2SBarry Smith {
1510d763cef2SBarry Smith   PetscFunctionBegin;
15110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15124482741eSBarry Smith   PetscValidIntPointer(iter,2);
1513d763cef2SBarry Smith   *iter = ts->steps;
1514d763cef2SBarry Smith   PetscFunctionReturn(0);
1515d763cef2SBarry Smith }
1516d763cef2SBarry Smith 
15174a2ae208SSatish Balay #undef __FUNCT__
15184a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1519d763cef2SBarry Smith /*@
1520d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1521d763cef2SBarry Smith    as well as the initial time.
1522d763cef2SBarry Smith 
15233f9fe445SBarry Smith    Logically Collective on TS
1524d763cef2SBarry Smith 
1525d763cef2SBarry Smith    Input Parameters:
1526d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1527d763cef2SBarry Smith .  initial_time - the initial time
1528d763cef2SBarry Smith -  time_step - the size of the timestep
1529d763cef2SBarry Smith 
1530d763cef2SBarry Smith    Level: intermediate
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1535d763cef2SBarry Smith @*/
15367087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1537d763cef2SBarry Smith {
1538e144a568SJed Brown   PetscErrorCode ierr;
1539e144a568SJed Brown 
1540d763cef2SBarry Smith   PetscFunctionBegin;
15410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1542e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1543d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1544d763cef2SBarry Smith   PetscFunctionReturn(0);
1545d763cef2SBarry Smith }
1546d763cef2SBarry Smith 
15474a2ae208SSatish Balay #undef __FUNCT__
15484a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1549d763cef2SBarry Smith /*@
1550d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1551d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1552d763cef2SBarry Smith 
15533f9fe445SBarry Smith    Logically Collective on TS
1554d763cef2SBarry Smith 
1555d763cef2SBarry Smith    Input Parameters:
1556d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1557d763cef2SBarry Smith -  time_step - the size of the timestep
1558d763cef2SBarry Smith 
1559d763cef2SBarry Smith    Level: intermediate
1560d763cef2SBarry Smith 
1561d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith .keywords: TS, set, timestep
1564d763cef2SBarry Smith @*/
15657087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1566d763cef2SBarry Smith {
1567d763cef2SBarry Smith   PetscFunctionBegin;
15680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1569c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1570d763cef2SBarry Smith   ts->time_step      = time_step;
157131748224SBarry Smith   ts->time_step_orig = time_step;
1572d763cef2SBarry Smith   PetscFunctionReturn(0);
1573d763cef2SBarry Smith }
1574d763cef2SBarry Smith 
15754a2ae208SSatish Balay #undef __FUNCT__
1576a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1577a43b19c4SJed Brown /*@
157849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
157949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
158049354f04SShri Abhyankar      or just return at the final time TS computed.
1581a43b19c4SJed Brown 
1582a43b19c4SJed Brown   Logically Collective on TS
1583a43b19c4SJed Brown 
1584a43b19c4SJed Brown    Input Parameter:
1585a43b19c4SJed Brown +   ts - the time-step context
158649354f04SShri Abhyankar -   eftopt - exact final time option
1587a43b19c4SJed Brown 
1588a43b19c4SJed Brown    Level: beginner
1589a43b19c4SJed Brown 
1590a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1591a43b19c4SJed Brown @*/
159249354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1593a43b19c4SJed Brown {
1594a43b19c4SJed Brown   PetscFunctionBegin;
1595a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
159649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
159749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1598a43b19c4SJed Brown   PetscFunctionReturn(0);
1599a43b19c4SJed Brown }
1600a43b19c4SJed Brown 
1601a43b19c4SJed Brown #undef __FUNCT__
16024a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1603d763cef2SBarry Smith /*@
1604d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1605d763cef2SBarry Smith 
1606d763cef2SBarry Smith    Not Collective
1607d763cef2SBarry Smith 
1608d763cef2SBarry Smith    Input Parameter:
1609d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1610d763cef2SBarry Smith 
1611d763cef2SBarry Smith    Output Parameter:
1612d763cef2SBarry Smith .  dt - the current timestep size
1613d763cef2SBarry Smith 
1614d763cef2SBarry Smith    Level: intermediate
1615d763cef2SBarry Smith 
1616d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1617d763cef2SBarry Smith 
1618d763cef2SBarry Smith .keywords: TS, get, timestep
1619d763cef2SBarry Smith @*/
16207087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1621d763cef2SBarry Smith {
1622d763cef2SBarry Smith   PetscFunctionBegin;
16230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1624f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1625d763cef2SBarry Smith   *dt = ts->time_step;
1626d763cef2SBarry Smith   PetscFunctionReturn(0);
1627d763cef2SBarry Smith }
1628d763cef2SBarry Smith 
16294a2ae208SSatish Balay #undef __FUNCT__
16304a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1631d8e5e3e6SSatish Balay /*@
1632d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1633d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1634d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1635d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1636d763cef2SBarry Smith 
1637d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1638d763cef2SBarry Smith 
1639d763cef2SBarry Smith    Input Parameter:
1640d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1641d763cef2SBarry Smith 
1642d763cef2SBarry Smith    Output Parameter:
1643d763cef2SBarry Smith .  v - the vector containing the solution
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith    Level: intermediate
1646d763cef2SBarry Smith 
1647d763cef2SBarry Smith .seealso: TSGetTimeStep()
1648d763cef2SBarry Smith 
1649d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1650d763cef2SBarry Smith @*/
16517087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1652d763cef2SBarry Smith {
1653d763cef2SBarry Smith   PetscFunctionBegin;
16540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16554482741eSBarry Smith   PetscValidPointer(v,2);
16568737fe31SLisandro Dalcin   *v = ts->vec_sol;
1657d763cef2SBarry Smith   PetscFunctionReturn(0);
1658d763cef2SBarry Smith }
1659d763cef2SBarry Smith 
1660bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
16614a2ae208SSatish Balay #undef __FUNCT__
1662bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1663d8e5e3e6SSatish Balay /*@
1664bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1665d763cef2SBarry Smith 
1666bdad233fSMatthew Knepley   Not collective
1667d763cef2SBarry Smith 
1668bdad233fSMatthew Knepley   Input Parameters:
1669bdad233fSMatthew Knepley + ts   - The TS
1670bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1671d763cef2SBarry Smith .vb
16720910c330SBarry Smith          U_t - A U = 0      (linear)
16730910c330SBarry Smith          U_t - A(t) U = 0   (linear)
16740910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1675d763cef2SBarry Smith .ve
1676d763cef2SBarry Smith 
1677d763cef2SBarry Smith    Level: beginner
1678d763cef2SBarry Smith 
1679bdad233fSMatthew Knepley .keywords: TS, problem type
1680bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1681d763cef2SBarry Smith @*/
16827087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1683a7cc72afSBarry Smith {
16849e2a6581SJed Brown   PetscErrorCode ierr;
16859e2a6581SJed Brown 
1686d763cef2SBarry Smith   PetscFunctionBegin;
16870700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1688bdad233fSMatthew Knepley   ts->problem_type = type;
16899e2a6581SJed Brown   if (type == TS_LINEAR) {
16909e2a6581SJed Brown     SNES snes;
16919e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
16929e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
16939e2a6581SJed Brown   }
1694d763cef2SBarry Smith   PetscFunctionReturn(0);
1695d763cef2SBarry Smith }
1696d763cef2SBarry Smith 
1697bdad233fSMatthew Knepley #undef __FUNCT__
1698bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1699bdad233fSMatthew Knepley /*@C
1700bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1701bdad233fSMatthew Knepley 
1702bdad233fSMatthew Knepley   Not collective
1703bdad233fSMatthew Knepley 
1704bdad233fSMatthew Knepley   Input Parameter:
1705bdad233fSMatthew Knepley . ts   - The TS
1706bdad233fSMatthew Knepley 
1707bdad233fSMatthew Knepley   Output Parameter:
1708bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1709bdad233fSMatthew Knepley .vb
1710089b2837SJed Brown          M U_t = A U
1711089b2837SJed Brown          M(t) U_t = A(t) U
1712b5abc632SBarry Smith          F(t,U,U_t)
1713bdad233fSMatthew Knepley .ve
1714bdad233fSMatthew Knepley 
1715bdad233fSMatthew Knepley    Level: beginner
1716bdad233fSMatthew Knepley 
1717bdad233fSMatthew Knepley .keywords: TS, problem type
1718bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1719bdad233fSMatthew Knepley @*/
17207087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1721a7cc72afSBarry Smith {
1722bdad233fSMatthew Knepley   PetscFunctionBegin;
17230700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17244482741eSBarry Smith   PetscValidIntPointer(type,2);
1725bdad233fSMatthew Knepley   *type = ts->problem_type;
1726bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1727bdad233fSMatthew Knepley }
1728d763cef2SBarry Smith 
17294a2ae208SSatish Balay #undef __FUNCT__
17304a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1731d763cef2SBarry Smith /*@
1732d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1733d763cef2SBarry Smith    of a timestepper.
1734d763cef2SBarry Smith 
1735d763cef2SBarry Smith    Collective on TS
1736d763cef2SBarry Smith 
1737d763cef2SBarry Smith    Input Parameter:
1738d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1739d763cef2SBarry Smith 
1740d763cef2SBarry Smith    Notes:
1741d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1742d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1743d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1744d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1745d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1746d763cef2SBarry Smith 
1747d763cef2SBarry Smith    Level: advanced
1748d763cef2SBarry Smith 
1749d763cef2SBarry Smith .keywords: TS, timestep, setup
1750d763cef2SBarry Smith 
1751d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1752d763cef2SBarry Smith @*/
17537087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1754d763cef2SBarry Smith {
1755dfbe8321SBarry Smith   PetscErrorCode ierr;
17566c6b9e74SPeter Brune   DM             dm;
17576c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
17586c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
17596c6b9e74SPeter Brune   TSIJacobian    ijac;
17606c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1761d763cef2SBarry Smith 
1762d763cef2SBarry Smith   PetscFunctionBegin;
17630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1764277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1765277b19d0SLisandro Dalcin 
17667adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
17679596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1768d763cef2SBarry Smith   }
1769277b19d0SLisandro Dalcin 
1770277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1771277b19d0SLisandro Dalcin 
1772552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
17731c3436cfSJed Brown 
1774*e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1775*e1244c69SJed Brown     Mat Amat,Pmat;
1776*e1244c69SJed Brown     SNES snes;
1777*e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1778*e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1779*e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1780*e1244c69SJed Brown      * have displaced the RHS matrix */
1781*e1244c69SJed Brown     if (Amat == ts->Arhs) {
1782*e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1783*e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1784*e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1785*e1244c69SJed Brown     }
1786*e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1787*e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1788*e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1789*e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1790*e1244c69SJed Brown     }
1791*e1244c69SJed Brown   }
1792*e1244c69SJed Brown 
1793277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1794000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1795277b19d0SLisandro Dalcin   }
1796277b19d0SLisandro Dalcin 
17976c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
17986c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
17996c6b9e74SPeter Brune    */
18006c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18010298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18026c6b9e74SPeter Brune   if (!func) {
18036c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18046c6b9e74SPeter Brune   }
18056c6b9e74SPeter 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.
18066c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18076c6b9e74SPeter Brune    */
18080298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18090298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18100298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18116c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18126c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18136c6b9e74SPeter Brune   }
1814277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1815277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1816277b19d0SLisandro Dalcin }
1817277b19d0SLisandro Dalcin 
1818277b19d0SLisandro Dalcin #undef __FUNCT__
1819277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1820277b19d0SLisandro Dalcin /*@
1821277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1822277b19d0SLisandro Dalcin 
1823277b19d0SLisandro Dalcin    Collective on TS
1824277b19d0SLisandro Dalcin 
1825277b19d0SLisandro Dalcin    Input Parameter:
1826277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1827277b19d0SLisandro Dalcin 
1828277b19d0SLisandro Dalcin    Level: beginner
1829277b19d0SLisandro Dalcin 
1830277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1831277b19d0SLisandro Dalcin 
1832277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1833277b19d0SLisandro Dalcin @*/
1834277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1835277b19d0SLisandro Dalcin {
1836277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1837277b19d0SLisandro Dalcin 
1838277b19d0SLisandro Dalcin   PetscFunctionBegin;
1839277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1840277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1841277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1842277b19d0SLisandro Dalcin   }
1843277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1844bbd56ea5SKarl Rupp 
18454e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
18464e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1847214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
18486bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1849e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1850e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
185138637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1852bbd56ea5SKarl Rupp 
1853277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1854d763cef2SBarry Smith   PetscFunctionReturn(0);
1855d763cef2SBarry Smith }
1856d763cef2SBarry Smith 
18574a2ae208SSatish Balay #undef __FUNCT__
18584a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1859d8e5e3e6SSatish Balay /*@
1860d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1861d763cef2SBarry Smith    with TSCreate().
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith    Collective on TS
1864d763cef2SBarry Smith 
1865d763cef2SBarry Smith    Input Parameter:
1866d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1867d763cef2SBarry Smith 
1868d763cef2SBarry Smith    Level: beginner
1869d763cef2SBarry Smith 
1870d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1871d763cef2SBarry Smith 
1872d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1873d763cef2SBarry Smith @*/
18746bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1875d763cef2SBarry Smith {
18766849ba73SBarry Smith   PetscErrorCode ierr;
1877d763cef2SBarry Smith 
1878d763cef2SBarry Smith   PetscFunctionBegin;
18796bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
18806bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
18816bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1882d763cef2SBarry Smith 
18836bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1884277b19d0SLisandro Dalcin 
1885be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
1886f05ece33SBarry Smith   ierr = PetscObjectAMSViewOff((PetscObject)*ts);CHKERRQ(ierr);
18876bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
18886d4c513bSLisandro Dalcin 
188984df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
18906bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
18916bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
18926bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
18936d4c513bSLisandro Dalcin 
1894a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1895d763cef2SBarry Smith   PetscFunctionReturn(0);
1896d763cef2SBarry Smith }
1897d763cef2SBarry Smith 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1900d8e5e3e6SSatish Balay /*@
1901d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1902d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1903d763cef2SBarry Smith 
1904d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1905d763cef2SBarry Smith 
1906d763cef2SBarry Smith    Input Parameter:
1907d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1908d763cef2SBarry Smith 
1909d763cef2SBarry Smith    Output Parameter:
1910d763cef2SBarry Smith .  snes - the nonlinear solver context
1911d763cef2SBarry Smith 
1912d763cef2SBarry Smith    Notes:
1913d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1914d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
191594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1916d763cef2SBarry Smith 
1917d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
19180298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
1919d763cef2SBarry Smith 
1920d763cef2SBarry Smith    Level: beginner
1921d763cef2SBarry Smith 
1922d763cef2SBarry Smith .keywords: timestep, get, SNES
1923d763cef2SBarry Smith @*/
19247087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1925d763cef2SBarry Smith {
1926d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1927d372ba47SLisandro Dalcin 
1928d763cef2SBarry Smith   PetscFunctionBegin;
19290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19304482741eSBarry Smith   PetscValidPointer(snes,2);
1931d372ba47SLisandro Dalcin   if (!ts->snes) {
1932ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
19330298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1934d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1935d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1936496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
19379e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
19389e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
19399e2a6581SJed Brown     }
1940d372ba47SLisandro Dalcin   }
1941d763cef2SBarry Smith   *snes = ts->snes;
1942d763cef2SBarry Smith   PetscFunctionReturn(0);
1943d763cef2SBarry Smith }
1944d763cef2SBarry Smith 
19454a2ae208SSatish Balay #undef __FUNCT__
1946deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
1947deb2cd25SJed Brown /*@
1948deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
1949deb2cd25SJed Brown 
1950deb2cd25SJed Brown    Collective
1951deb2cd25SJed Brown 
1952deb2cd25SJed Brown    Input Parameter:
1953deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
1954deb2cd25SJed Brown -  snes - the nonlinear solver context
1955deb2cd25SJed Brown 
1956deb2cd25SJed Brown    Notes:
1957deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
1958deb2cd25SJed Brown 
1959deb2cd25SJed Brown    Level: developer
1960deb2cd25SJed Brown 
1961deb2cd25SJed Brown .keywords: timestep, set, SNES
1962deb2cd25SJed Brown @*/
1963deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
1964deb2cd25SJed Brown {
1965deb2cd25SJed Brown   PetscErrorCode ierr;
1966740132f1SEmil Constantinescu   PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
1967deb2cd25SJed Brown 
1968deb2cd25SJed Brown   PetscFunctionBegin;
1969deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1970deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
1971deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
1972deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
1973bbd56ea5SKarl Rupp 
1974deb2cd25SJed Brown   ts->snes = snes;
1975bbd56ea5SKarl Rupp 
19760298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
19770298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
1978740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
19790298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1980740132f1SEmil Constantinescu   }
1981deb2cd25SJed Brown   PetscFunctionReturn(0);
1982deb2cd25SJed Brown }
1983deb2cd25SJed Brown 
1984deb2cd25SJed Brown #undef __FUNCT__
198594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1986d8e5e3e6SSatish Balay /*@
198794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1988d763cef2SBarry Smith    a TS (timestepper) context.
1989d763cef2SBarry Smith 
199094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1991d763cef2SBarry Smith 
1992d763cef2SBarry Smith    Input Parameter:
1993d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1994d763cef2SBarry Smith 
1995d763cef2SBarry Smith    Output Parameter:
199694b7f48cSBarry Smith .  ksp - the nonlinear solver context
1997d763cef2SBarry Smith 
1998d763cef2SBarry Smith    Notes:
199994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2000d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2001d763cef2SBarry Smith    KSP and PC contexts as well.
2002d763cef2SBarry Smith 
200394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
20040298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2005d763cef2SBarry Smith 
2006d763cef2SBarry Smith    Level: beginner
2007d763cef2SBarry Smith 
200894b7f48cSBarry Smith .keywords: timestep, get, KSP
2009d763cef2SBarry Smith @*/
20107087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2011d763cef2SBarry Smith {
2012d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2013089b2837SJed Brown   SNES           snes;
2014d372ba47SLisandro Dalcin 
2015d763cef2SBarry Smith   PetscFunctionBegin;
20160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20174482741eSBarry Smith   PetscValidPointer(ksp,2);
201817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2019e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2020089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2021089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2022d763cef2SBarry Smith   PetscFunctionReturn(0);
2023d763cef2SBarry Smith }
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2026d763cef2SBarry Smith 
20274a2ae208SSatish Balay #undef __FUNCT__
2028adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2029adb62b0dSMatthew Knepley /*@
2030adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2031adb62b0dSMatthew Knepley    maximum time for iteration.
2032adb62b0dSMatthew Knepley 
20333f9fe445SBarry Smith    Not Collective
2034adb62b0dSMatthew Knepley 
2035adb62b0dSMatthew Knepley    Input Parameters:
2036adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
20370298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
20380298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2039adb62b0dSMatthew Knepley 
2040adb62b0dSMatthew Knepley    Level: intermediate
2041adb62b0dSMatthew Knepley 
2042adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2043adb62b0dSMatthew Knepley @*/
20447087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2045adb62b0dSMatthew Knepley {
2046adb62b0dSMatthew Knepley   PetscFunctionBegin;
20470700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2048abc0a331SBarry Smith   if (maxsteps) {
20494482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2050adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2051adb62b0dSMatthew Knepley   }
2052abc0a331SBarry Smith   if (maxtime) {
20534482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2054adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2055adb62b0dSMatthew Knepley   }
2056adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2057adb62b0dSMatthew Knepley }
2058adb62b0dSMatthew Knepley 
2059adb62b0dSMatthew Knepley #undef __FUNCT__
20604a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2061d763cef2SBarry Smith /*@
2062d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2063d763cef2SBarry Smith    maximum time for iteration.
2064d763cef2SBarry Smith 
20653f9fe445SBarry Smith    Logically Collective on TS
2066d763cef2SBarry Smith 
2067d763cef2SBarry Smith    Input Parameters:
2068d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2069d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2070d763cef2SBarry Smith -  maxtime - final time to iterate to
2071d763cef2SBarry Smith 
2072d763cef2SBarry Smith    Options Database Keys:
2073d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
20743bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith    Notes:
2077d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith    Level: intermediate
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2082a43b19c4SJed Brown 
2083a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2084d763cef2SBarry Smith @*/
20857087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2086d763cef2SBarry Smith {
2087d763cef2SBarry Smith   PetscFunctionBegin;
20880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2089c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2090c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
209139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
209239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2093d763cef2SBarry Smith   PetscFunctionReturn(0);
2094d763cef2SBarry Smith }
2095d763cef2SBarry Smith 
20964a2ae208SSatish Balay #undef __FUNCT__
20974a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2098d763cef2SBarry Smith /*@
2099d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2100d763cef2SBarry Smith    for use by the TS routines.
2101d763cef2SBarry Smith 
21023f9fe445SBarry Smith    Logically Collective on TS and Vec
2103d763cef2SBarry Smith 
2104d763cef2SBarry Smith    Input Parameters:
2105d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
21060910c330SBarry Smith -  u - the solution vector
2107d763cef2SBarry Smith 
2108d763cef2SBarry Smith    Level: beginner
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2111d763cef2SBarry Smith @*/
21120910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2113d763cef2SBarry Smith {
21148737fe31SLisandro Dalcin   PetscErrorCode ierr;
2115496e6a7aSJed Brown   DM             dm;
21168737fe31SLisandro Dalcin 
2117d763cef2SBarry Smith   PetscFunctionBegin;
21180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21190910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
21200910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
21216bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2122bbd56ea5SKarl Rupp 
21230910c330SBarry Smith   ts->vec_sol = u;
2124bbd56ea5SKarl Rupp 
2125496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
21260910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2127d763cef2SBarry Smith   PetscFunctionReturn(0);
2128d763cef2SBarry Smith }
2129d763cef2SBarry Smith 
2130e74ef692SMatthew Knepley #undef __FUNCT__
2131e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2132ac226902SBarry Smith /*@C
2133000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
21343f2090d5SJed Brown   called once at the beginning of each time step.
2135000e7ae3SMatthew Knepley 
21363f9fe445SBarry Smith   Logically Collective on TS
2137000e7ae3SMatthew Knepley 
2138000e7ae3SMatthew Knepley   Input Parameters:
2139000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2140000e7ae3SMatthew Knepley - func - The function
2141000e7ae3SMatthew Knepley 
2142000e7ae3SMatthew Knepley   Calling sequence of func:
2143000e7ae3SMatthew Knepley . func (TS ts);
2144000e7ae3SMatthew Knepley 
2145000e7ae3SMatthew Knepley   Level: intermediate
2146000e7ae3SMatthew Knepley 
2147b8123daeSJed Brown   Note:
2148b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2149b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2150b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2151b8123daeSJed Brown 
2152000e7ae3SMatthew Knepley .keywords: TS, timestep
2153b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
2154000e7ae3SMatthew Knepley @*/
21557087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2156000e7ae3SMatthew Knepley {
2157000e7ae3SMatthew Knepley   PetscFunctionBegin;
21580700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2159ae60f76fSBarry Smith   ts->prestep = func;
2160000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2161000e7ae3SMatthew Knepley }
2162000e7ae3SMatthew Knepley 
2163e74ef692SMatthew Knepley #undef __FUNCT__
21643f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
216509ee8438SJed Brown /*@
21663f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
21673f2090d5SJed Brown 
21683f2090d5SJed Brown   Collective on TS
21693f2090d5SJed Brown 
21703f2090d5SJed Brown   Input Parameters:
21713f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
21723f2090d5SJed Brown 
21733f2090d5SJed Brown   Notes:
21743f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
21753f2090d5SJed Brown   so most users would not generally call this routine themselves.
21763f2090d5SJed Brown 
21773f2090d5SJed Brown   Level: developer
21783f2090d5SJed Brown 
21793f2090d5SJed Brown .keywords: TS, timestep
2180b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
21813f2090d5SJed Brown @*/
21827087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
21833f2090d5SJed Brown {
21843f2090d5SJed Brown   PetscErrorCode ierr;
21853f2090d5SJed Brown 
21863f2090d5SJed Brown   PetscFunctionBegin;
21870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2188ae60f76fSBarry Smith   if (ts->prestep) {
2189ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2190312ce896SJed Brown   }
21913f2090d5SJed Brown   PetscFunctionReturn(0);
21923f2090d5SJed Brown }
21933f2090d5SJed Brown 
21943f2090d5SJed Brown #undef __FUNCT__
2195b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2196b8123daeSJed Brown /*@C
2197b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2198b8123daeSJed Brown   called once at the beginning of each stage.
2199b8123daeSJed Brown 
2200b8123daeSJed Brown   Logically Collective on TS
2201b8123daeSJed Brown 
2202b8123daeSJed Brown   Input Parameters:
2203b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2204b8123daeSJed Brown - func - The function
2205b8123daeSJed Brown 
2206b8123daeSJed Brown   Calling sequence of func:
2207b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2208b8123daeSJed Brown 
2209b8123daeSJed Brown   Level: intermediate
2210b8123daeSJed Brown 
2211b8123daeSJed Brown   Note:
2212b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2213b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2214b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2215b8123daeSJed Brown 
2216b8123daeSJed Brown .keywords: TS, timestep
2217b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2218b8123daeSJed Brown @*/
2219b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2220b8123daeSJed Brown {
2221b8123daeSJed Brown   PetscFunctionBegin;
2222b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2223ae60f76fSBarry Smith   ts->prestage = func;
2224b8123daeSJed Brown   PetscFunctionReturn(0);
2225b8123daeSJed Brown }
2226b8123daeSJed Brown 
2227b8123daeSJed Brown #undef __FUNCT__
2228b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2229b8123daeSJed Brown /*@
2230b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2231b8123daeSJed Brown 
2232b8123daeSJed Brown   Collective on TS
2233b8123daeSJed Brown 
2234b8123daeSJed Brown   Input Parameters:
2235b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
2236b8123daeSJed Brown 
2237b8123daeSJed Brown   Notes:
2238b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2239b8123daeSJed Brown   most users would not generally call this routine themselves.
2240b8123daeSJed Brown 
2241b8123daeSJed Brown   Level: developer
2242b8123daeSJed Brown 
2243b8123daeSJed Brown .keywords: TS, timestep
2244b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
2245b8123daeSJed Brown @*/
2246b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2247b8123daeSJed Brown {
2248b8123daeSJed Brown   PetscErrorCode ierr;
2249b8123daeSJed Brown 
2250b8123daeSJed Brown   PetscFunctionBegin;
2251b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2252ae60f76fSBarry Smith   if (ts->prestage) {
2253ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2254b8123daeSJed Brown   }
2255b8123daeSJed Brown   PetscFunctionReturn(0);
2256b8123daeSJed Brown }
2257b8123daeSJed Brown 
2258b8123daeSJed Brown #undef __FUNCT__
2259e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2260ac226902SBarry Smith /*@C
2261000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
22623f2090d5SJed Brown   called once at the end of each time step.
2263000e7ae3SMatthew Knepley 
22643f9fe445SBarry Smith   Logically Collective on TS
2265000e7ae3SMatthew Knepley 
2266000e7ae3SMatthew Knepley   Input Parameters:
2267000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2268000e7ae3SMatthew Knepley - func - The function
2269000e7ae3SMatthew Knepley 
2270000e7ae3SMatthew Knepley   Calling sequence of func:
2271b8123daeSJed Brown $ func (TS ts);
2272000e7ae3SMatthew Knepley 
2273000e7ae3SMatthew Knepley   Level: intermediate
2274000e7ae3SMatthew Knepley 
2275000e7ae3SMatthew Knepley .keywords: TS, timestep
2276b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2277000e7ae3SMatthew Knepley @*/
22787087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2279000e7ae3SMatthew Knepley {
2280000e7ae3SMatthew Knepley   PetscFunctionBegin;
22810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2282ae60f76fSBarry Smith   ts->poststep = func;
2283000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2284000e7ae3SMatthew Knepley }
2285000e7ae3SMatthew Knepley 
2286e74ef692SMatthew Knepley #undef __FUNCT__
22873f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
228809ee8438SJed Brown /*@
22893f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
22903f2090d5SJed Brown 
22913f2090d5SJed Brown   Collective on TS
22923f2090d5SJed Brown 
22933f2090d5SJed Brown   Input Parameters:
22943f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
22953f2090d5SJed Brown 
22963f2090d5SJed Brown   Notes:
22973f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
22983f2090d5SJed Brown   so most users would not generally call this routine themselves.
22993f2090d5SJed Brown 
23003f2090d5SJed Brown   Level: developer
23013f2090d5SJed Brown 
23023f2090d5SJed Brown .keywords: TS, timestep
23033f2090d5SJed Brown @*/
23047087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
23053f2090d5SJed Brown {
23063f2090d5SJed Brown   PetscErrorCode ierr;
23073f2090d5SJed Brown 
23083f2090d5SJed Brown   PetscFunctionBegin;
23090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2310ae60f76fSBarry Smith   if (ts->poststep) {
2311ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
231272ac3e02SJed Brown   }
23133f2090d5SJed Brown   PetscFunctionReturn(0);
23143f2090d5SJed Brown }
23153f2090d5SJed Brown 
2316d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2317d763cef2SBarry Smith 
23184a2ae208SSatish Balay #undef __FUNCT__
2319a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2320d763cef2SBarry Smith /*@C
2321a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2322d763cef2SBarry Smith    timestep to display the iteration's  progress.
2323d763cef2SBarry Smith 
23243f9fe445SBarry Smith    Logically Collective on TS
2325d763cef2SBarry Smith 
2326d763cef2SBarry Smith    Input Parameters:
2327d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2328e213d8f1SJed Brown .  monitor - monitoring routine
2329329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
23300298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2331b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
23320298fd71SBarry Smith           (may be NULL)
2333d763cef2SBarry Smith 
2334e213d8f1SJed Brown    Calling sequence of monitor:
23350910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2336d763cef2SBarry Smith 
2337d763cef2SBarry Smith +    ts - the TS context
233888c05cc5SBarry 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
233988c05cc5SBarry Smith                                been interpolated to)
23401f06c33eSBarry Smith .    time - current time
23410910c330SBarry Smith .    u - current iterate
2342d763cef2SBarry Smith -    mctx - [optional] monitoring context
2343d763cef2SBarry Smith 
2344d763cef2SBarry Smith    Notes:
2345d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2346d763cef2SBarry Smith    already has been loaded.
2347d763cef2SBarry Smith 
2348025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2349025f1a04SBarry Smith 
2350d763cef2SBarry Smith    Level: intermediate
2351d763cef2SBarry Smith 
2352d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2353d763cef2SBarry Smith 
2354a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2355d763cef2SBarry Smith @*/
2356c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2357d763cef2SBarry Smith {
2358d763cef2SBarry Smith   PetscFunctionBegin;
23590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
236017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2361d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
23628704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2363d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2364d763cef2SBarry Smith   PetscFunctionReturn(0);
2365d763cef2SBarry Smith }
2366d763cef2SBarry Smith 
23674a2ae208SSatish Balay #undef __FUNCT__
2368a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2369d763cef2SBarry Smith /*@C
2370a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2371d763cef2SBarry Smith 
23723f9fe445SBarry Smith    Logically Collective on TS
2373d763cef2SBarry Smith 
2374d763cef2SBarry Smith    Input Parameters:
2375d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2376d763cef2SBarry Smith 
2377d763cef2SBarry Smith    Notes:
2378d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2379d763cef2SBarry Smith 
2380d763cef2SBarry Smith    Level: intermediate
2381d763cef2SBarry Smith 
2382d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2383d763cef2SBarry Smith 
2384a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2385d763cef2SBarry Smith @*/
23867087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2387d763cef2SBarry Smith {
2388d952e501SBarry Smith   PetscErrorCode ierr;
2389d952e501SBarry Smith   PetscInt       i;
2390d952e501SBarry Smith 
2391d763cef2SBarry Smith   PetscFunctionBegin;
23920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2393d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
23948704b422SBarry Smith     if (ts->monitordestroy[i]) {
23958704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2396d952e501SBarry Smith     }
2397d952e501SBarry Smith   }
2398d763cef2SBarry Smith   ts->numbermonitors = 0;
2399d763cef2SBarry Smith   PetscFunctionReturn(0);
2400d763cef2SBarry Smith }
2401d763cef2SBarry Smith 
24024a2ae208SSatish Balay #undef __FUNCT__
2403a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2404d8e5e3e6SSatish Balay /*@
2405a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
24065516499fSSatish Balay 
24075516499fSSatish Balay    Level: intermediate
240841251cbbSSatish Balay 
24095516499fSSatish Balay .keywords: TS, set, monitor
24105516499fSSatish Balay 
241141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
241241251cbbSSatish Balay @*/
2413649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2414d763cef2SBarry Smith {
2415dfbe8321SBarry Smith   PetscErrorCode ierr;
2416ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2417d132466eSBarry Smith 
2418d763cef2SBarry Smith   PetscFunctionBegin;
2419649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2420971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2421649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2422d763cef2SBarry Smith   PetscFunctionReturn(0);
2423d763cef2SBarry Smith }
2424d763cef2SBarry Smith 
24254a2ae208SSatish Balay #undef __FUNCT__
2426cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2427cd652676SJed Brown /*@
2428cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2429cd652676SJed Brown 
2430cd652676SJed Brown    Logically Collective on TS
2431cd652676SJed Brown 
2432cd652676SJed Brown    Input Argument:
2433cd652676SJed Brown .  ts - time stepping context
2434cd652676SJed Brown 
2435cd652676SJed Brown    Output Argument:
2436cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2437cd652676SJed Brown 
2438cd652676SJed Brown    Level: intermediate
2439cd652676SJed Brown 
2440cd652676SJed Brown .keywords: TS, set
2441cd652676SJed Brown 
2442cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2443cd652676SJed Brown @*/
2444cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2445cd652676SJed Brown {
2446cd652676SJed Brown   PetscFunctionBegin;
2447cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2448cd652676SJed Brown   ts->retain_stages = flg;
2449cd652676SJed Brown   PetscFunctionReturn(0);
2450cd652676SJed Brown }
2451cd652676SJed Brown 
2452cd652676SJed Brown #undef __FUNCT__
2453cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2454cd652676SJed Brown /*@
2455cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2456cd652676SJed Brown 
2457cd652676SJed Brown    Collective on TS
2458cd652676SJed Brown 
2459cd652676SJed Brown    Input Argument:
2460cd652676SJed Brown +  ts - time stepping context
2461cd652676SJed Brown -  t - time to interpolate to
2462cd652676SJed Brown 
2463cd652676SJed Brown    Output Argument:
24640910c330SBarry Smith .  U - state at given time
2465cd652676SJed Brown 
2466cd652676SJed Brown    Notes:
2467cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2468cd652676SJed Brown 
2469cd652676SJed Brown    Level: intermediate
2470cd652676SJed Brown 
2471cd652676SJed Brown    Developer Notes:
2472cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2473cd652676SJed Brown 
2474cd652676SJed Brown .keywords: TS, set
2475cd652676SJed Brown 
2476cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2477cd652676SJed Brown @*/
24780910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2479cd652676SJed Brown {
2480cd652676SJed Brown   PetscErrorCode ierr;
2481cd652676SJed Brown 
2482cd652676SJed Brown   PetscFunctionBegin;
2483cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2484b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2485ce94432eSBarry 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);
2486ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
24870910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2488cd652676SJed Brown   PetscFunctionReturn(0);
2489cd652676SJed Brown }
2490cd652676SJed Brown 
2491cd652676SJed Brown #undef __FUNCT__
24924a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2493d763cef2SBarry Smith /*@
24946d9e5789SSean Farley    TSStep - Steps one time step
2495d763cef2SBarry Smith 
2496d763cef2SBarry Smith    Collective on TS
2497d763cef2SBarry Smith 
2498d763cef2SBarry Smith    Input Parameter:
2499d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2500d763cef2SBarry Smith 
25016d9e5789SSean Farley    Level: intermediate
2502d763cef2SBarry Smith 
2503b8123daeSJed Brown    Notes:
2504b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2505b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2506b8123daeSJed Brown 
250725cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
250825cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
250925cb2221SBarry Smith 
2510d763cef2SBarry Smith .keywords: TS, timestep, solve
2511d763cef2SBarry Smith 
251225cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2513d763cef2SBarry Smith @*/
2514193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2515d763cef2SBarry Smith {
2516362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2517dfbe8321SBarry Smith   PetscErrorCode ierr;
2518d763cef2SBarry Smith 
2519d763cef2SBarry Smith   PetscFunctionBegin;
25200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2521d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2522d405a339SMatthew Knepley 
2523362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2524362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2525bbd56ea5SKarl Rupp 
2526d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2527193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2528d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2529bbd56ea5SKarl Rupp 
2530362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2531362cd11cSLisandro Dalcin 
2532362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2533cef5090cSJed Brown     if (ts->errorifstepfailed) {
2534cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2535ce94432eSBarry 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]);
2536ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2537cef5090cSJed Brown     }
2538362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2539db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2540db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2541362cd11cSLisandro Dalcin   }
2542d763cef2SBarry Smith   PetscFunctionReturn(0);
2543d763cef2SBarry Smith }
2544d763cef2SBarry Smith 
25454a2ae208SSatish Balay #undef __FUNCT__
254605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
254705175c85SJed Brown /*@
254805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
254905175c85SJed Brown 
25501c3436cfSJed Brown    Collective on TS
255105175c85SJed Brown 
255205175c85SJed Brown    Input Arguments:
25531c3436cfSJed Brown +  ts - time stepping context
25541c3436cfSJed Brown .  order - desired order of accuracy
25550298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
255605175c85SJed Brown 
255705175c85SJed Brown    Output Arguments:
25580910c330SBarry Smith .  U - state at the end of the current step
255905175c85SJed Brown 
256005175c85SJed Brown    Level: advanced
256105175c85SJed Brown 
2562108c343cSJed Brown    Notes:
2563108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2564108c343cSJed 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.
2565108c343cSJed Brown 
25661c3436cfSJed Brown .seealso: TSStep(), TSAdapt
256705175c85SJed Brown @*/
25680910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
256905175c85SJed Brown {
257005175c85SJed Brown   PetscErrorCode ierr;
257105175c85SJed Brown 
257205175c85SJed Brown   PetscFunctionBegin;
257305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
257405175c85SJed Brown   PetscValidType(ts,1);
25750910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2576ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
25770910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
257805175c85SJed Brown   PetscFunctionReturn(0);
257905175c85SJed Brown }
258005175c85SJed Brown 
258105175c85SJed Brown #undef __FUNCT__
25826a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
25836a4d4014SLisandro Dalcin /*@
25846a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
25856a4d4014SLisandro Dalcin 
25866a4d4014SLisandro Dalcin    Collective on TS
25876a4d4014SLisandro Dalcin 
25886a4d4014SLisandro Dalcin    Input Parameter:
25896a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2590cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
25915a3a76d0SJed Brown 
25926a4d4014SLisandro Dalcin    Level: beginner
25936a4d4014SLisandro Dalcin 
25945a3a76d0SJed Brown    Notes:
25955a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
25965a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
25975a3a76d0SJed Brown    stepped over the final time.
25985a3a76d0SJed Brown 
25996a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
26006a4d4014SLisandro Dalcin 
26016a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
26026a4d4014SLisandro Dalcin @*/
2603cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
26046a4d4014SLisandro Dalcin {
26054d7d938eSLisandro Dalcin   PetscBool         flg;
26064d7d938eSLisandro Dalcin   PetscViewer       viewer;
2607b06615a5SLisandro Dalcin   Vec               solution;
26086a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2609cffb1e40SBarry Smith   PetscViewerFormat format;
2610f22f69f0SBarry Smith 
26116a4d4014SLisandro Dalcin   PetscFunctionBegin;
26120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2613f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
261449354f04SShri 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 */
2615b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
26160910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
2617b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
2618b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
2619b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
26205a3a76d0SJed Brown     }
26210910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2622bbd56ea5SKarl Rupp   } else if (u) {
26230910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
26245a3a76d0SJed Brown   }
2625b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
26266a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2627193ac0bcSJed Brown   ts->steps             = 0;
26285ef26d82SJed Brown   ts->ksp_its           = 0;
26295ef26d82SJed Brown   ts->snes_its          = 0;
2630c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2631c610991cSLisandro Dalcin   ts->reject            = 0;
2632193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2633193ac0bcSJed Brown 
2634f05ece33SBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view_pre",&viewer,&format,&flg);CHKERRQ(ierr);
2635f05ece33SBarry Smith   if (flg && !PetscPreLoadingOn) {
2636f05ece33SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
2637f05ece33SBarry Smith     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2638f05ece33SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2639f05ece33SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2640f05ece33SBarry Smith   }
2641f05ece33SBarry Smith 
2642193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2643193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
26440910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2645cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2646193ac0bcSJed Brown   } else {
26476a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2648db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2649db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2650e1a7a14fSJed Brown     while (!ts->reason) {
2651b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2652193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2653193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2654193ac0bcSJed Brown     }
265549354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
26560910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2657cc708dedSBarry Smith       ts->solvetime = ts->max_time;
2658b06615a5SLisandro Dalcin       solution = u;
26590574a7fbSJed Brown     } else {
2660ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2661cc708dedSBarry Smith       ts->solvetime = ts->ptime;
2662b06615a5SLisandro Dalcin       solution = ts->vec_sol;
26630574a7fbSJed Brown     }
2664b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
2665193ac0bcSJed Brown   }
2666ce94432eSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,"-ts_view",&viewer,&format,&flg);CHKERRQ(ierr);
26674d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
2668cffb1e40SBarry Smith     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
26694d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
2670cffb1e40SBarry Smith     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
2671cffb1e40SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
26722b0a91c0SBarry Smith   }
26736a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
26746a4d4014SLisandro Dalcin }
26756a4d4014SLisandro Dalcin 
26766a4d4014SLisandro Dalcin #undef __FUNCT__
26774a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2678228d79bcSJed Brown /*@
2679228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2680228d79bcSJed Brown 
2681228d79bcSJed Brown    Collective on TS
2682228d79bcSJed Brown 
2683228d79bcSJed Brown    Input Parameters:
2684228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2685228d79bcSJed Brown .  step - step number that has just completed
2686228d79bcSJed Brown .  ptime - model time of the state
26870910c330SBarry Smith -  u - state at the current model time
2688228d79bcSJed Brown 
2689228d79bcSJed Brown    Notes:
2690228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2691228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2692228d79bcSJed Brown 
2693228d79bcSJed Brown    Level: advanced
2694228d79bcSJed Brown 
2695228d79bcSJed Brown .keywords: TS, timestep
2696228d79bcSJed Brown @*/
26970910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2698d763cef2SBarry Smith {
26996849ba73SBarry Smith   PetscErrorCode ierr;
2700a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2701d763cef2SBarry Smith 
2702d763cef2SBarry Smith   PetscFunctionBegin;
2703b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2704b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
2705d763cef2SBarry Smith   for (i=0; i<n; i++) {
27060910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2707d763cef2SBarry Smith   }
2708d763cef2SBarry Smith   PetscFunctionReturn(0);
2709d763cef2SBarry Smith }
2710d763cef2SBarry Smith 
2711d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
27120b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
27130b039ecaSBarry Smith   PetscDrawLG lg;
27140b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2715201da799SBarry Smith   PetscInt    ksp_its,snes_its;
27160b039ecaSBarry Smith };
27170b039ecaSBarry Smith 
2718d763cef2SBarry Smith 
27194a2ae208SSatish Balay #undef __FUNCT__
2720a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2721d763cef2SBarry Smith /*@C
2722a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2723a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2724d763cef2SBarry Smith 
2725d763cef2SBarry Smith    Collective on TS
2726d763cef2SBarry Smith 
2727d763cef2SBarry Smith    Input Parameters:
2728d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2729d763cef2SBarry Smith .  label - the title to put in the title bar
27307c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2731a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2732a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2733d763cef2SBarry Smith 
2734d763cef2SBarry Smith    Output Parameter:
27350b039ecaSBarry Smith .  ctx - the context
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith    Options Database Key:
27384f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
27394f09c107SBarry Smith .  -ts_monitor_lg_solution -
274099fdda47SBarry Smith .  -ts_monitor_lg_error -
274199fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
274299fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
274399fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2744d763cef2SBarry Smith 
2745d763cef2SBarry Smith    Notes:
2746a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2747d763cef2SBarry Smith 
2748d763cef2SBarry Smith    Level: intermediate
2749d763cef2SBarry Smith 
27507c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2751d763cef2SBarry Smith 
27524f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
27537c922b88SBarry Smith 
2754d763cef2SBarry Smith @*/
2755a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2756d763cef2SBarry Smith {
2757b0a32e0cSBarry Smith   PetscDraw      win;
2758dfbe8321SBarry Smith   PetscErrorCode ierr;
275999fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2760d763cef2SBarry Smith 
2761d763cef2SBarry Smith   PetscFunctionBegin;
2762201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2763a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
27643fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
27650b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
27660298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-lg_indicate_data_points",&flg,NULL);CHKERRQ(ierr);
276799fdda47SBarry Smith   if (flg) {
27680b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
276999fdda47SBarry Smith   }
27700b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2771a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2772d763cef2SBarry Smith   PetscFunctionReturn(0);
2773d763cef2SBarry Smith }
2774d763cef2SBarry Smith 
27754a2ae208SSatish Balay #undef __FUNCT__
27764f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
2777b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
2778d763cef2SBarry Smith {
27790b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2780c32365f1SBarry Smith   PetscReal      x   = ptime,y;
2781dfbe8321SBarry Smith   PetscErrorCode ierr;
2782d763cef2SBarry Smith 
2783d763cef2SBarry Smith   PetscFunctionBegin;
2784b06615a5SLisandro Dalcin   if (!step) {
2785a9f9c1f6SBarry Smith     PetscDrawAxis axis;
2786a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2787a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2788a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2789a9f9c1f6SBarry Smith   }
2790c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
27910b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
2792b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
27930b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
27943923b477SBarry Smith   }
2795d763cef2SBarry Smith   PetscFunctionReturn(0);
2796d763cef2SBarry Smith }
2797d763cef2SBarry Smith 
27984a2ae208SSatish Balay #undef __FUNCT__
2799a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2800d763cef2SBarry Smith /*@C
2801a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2802a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2803d763cef2SBarry Smith 
28040b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2805d763cef2SBarry Smith 
2806d763cef2SBarry Smith    Input Parameter:
28070b039ecaSBarry Smith .  ctx - the monitor context
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith    Level: intermediate
2810d763cef2SBarry Smith 
2811d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2812d763cef2SBarry Smith 
28134f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2814d763cef2SBarry Smith @*/
2815a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2816d763cef2SBarry Smith {
2817b0a32e0cSBarry Smith   PetscDraw      draw;
2818dfbe8321SBarry Smith   PetscErrorCode ierr;
2819d763cef2SBarry Smith 
2820d763cef2SBarry Smith   PetscFunctionBegin;
28210b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
28226bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
28230b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
28240b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2825d763cef2SBarry Smith   PetscFunctionReturn(0);
2826d763cef2SBarry Smith }
2827d763cef2SBarry Smith 
28284a2ae208SSatish Balay #undef __FUNCT__
28294a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2830d763cef2SBarry Smith /*@
2831b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2832d763cef2SBarry Smith 
2833d763cef2SBarry Smith    Not Collective
2834d763cef2SBarry Smith 
2835d763cef2SBarry Smith    Input Parameter:
2836d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2837d763cef2SBarry Smith 
2838d763cef2SBarry Smith    Output Parameter:
2839d763cef2SBarry Smith .  t  - the current time
2840d763cef2SBarry Smith 
2841d763cef2SBarry Smith    Level: beginner
2842d763cef2SBarry Smith 
2843b8123daeSJed Brown    Note:
2844b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2845b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2846b8123daeSJed Brown 
2847d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2848d763cef2SBarry Smith 
2849d763cef2SBarry Smith .keywords: TS, get, time
2850d763cef2SBarry Smith @*/
28517087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
2852d763cef2SBarry Smith {
2853d763cef2SBarry Smith   PetscFunctionBegin;
28540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2855f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2856d763cef2SBarry Smith   *t = ts->ptime;
2857d763cef2SBarry Smith   PetscFunctionReturn(0);
2858d763cef2SBarry Smith }
2859d763cef2SBarry Smith 
28604a2ae208SSatish Balay #undef __FUNCT__
28616a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
28626a4d4014SLisandro Dalcin /*@
28636a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
28646a4d4014SLisandro Dalcin 
28653f9fe445SBarry Smith    Logically Collective on TS
28666a4d4014SLisandro Dalcin 
28676a4d4014SLisandro Dalcin    Input Parameters:
28686a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
28696a4d4014SLisandro Dalcin -  time - the time
28706a4d4014SLisandro Dalcin 
28716a4d4014SLisandro Dalcin    Level: intermediate
28726a4d4014SLisandro Dalcin 
28736a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
28746a4d4014SLisandro Dalcin 
28756a4d4014SLisandro Dalcin .keywords: TS, set, time
28766a4d4014SLisandro Dalcin @*/
28777087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
28786a4d4014SLisandro Dalcin {
28796a4d4014SLisandro Dalcin   PetscFunctionBegin;
28800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2881c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
28826a4d4014SLisandro Dalcin   ts->ptime = t;
28836a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
28846a4d4014SLisandro Dalcin }
28856a4d4014SLisandro Dalcin 
28866a4d4014SLisandro Dalcin #undef __FUNCT__
28874a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2888d763cef2SBarry Smith /*@C
2889d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2890d763cef2SBarry Smith    TS options in the database.
2891d763cef2SBarry Smith 
28923f9fe445SBarry Smith    Logically Collective on TS
2893d763cef2SBarry Smith 
2894d763cef2SBarry Smith    Input Parameter:
2895d763cef2SBarry Smith +  ts     - The TS context
2896d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith    Notes:
2899d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2900d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2901d763cef2SBarry Smith    hyphen.
2902d763cef2SBarry Smith 
2903d763cef2SBarry Smith    Level: advanced
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2906d763cef2SBarry Smith 
2907d763cef2SBarry Smith .seealso: TSSetFromOptions()
2908d763cef2SBarry Smith 
2909d763cef2SBarry Smith @*/
29107087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2911d763cef2SBarry Smith {
2912dfbe8321SBarry Smith   PetscErrorCode ierr;
2913089b2837SJed Brown   SNES           snes;
2914d763cef2SBarry Smith 
2915d763cef2SBarry Smith   PetscFunctionBegin;
29160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2917d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2918089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2919089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2920d763cef2SBarry Smith   PetscFunctionReturn(0);
2921d763cef2SBarry Smith }
2922d763cef2SBarry Smith 
2923d763cef2SBarry Smith 
29244a2ae208SSatish Balay #undef __FUNCT__
29254a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2926d763cef2SBarry Smith /*@C
2927d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2928d763cef2SBarry Smith    TS options in the database.
2929d763cef2SBarry Smith 
29303f9fe445SBarry Smith    Logically Collective on TS
2931d763cef2SBarry Smith 
2932d763cef2SBarry Smith    Input Parameter:
2933d763cef2SBarry Smith +  ts     - The TS context
2934d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2935d763cef2SBarry Smith 
2936d763cef2SBarry Smith    Notes:
2937d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2938d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2939d763cef2SBarry Smith    hyphen.
2940d763cef2SBarry Smith 
2941d763cef2SBarry Smith    Level: advanced
2942d763cef2SBarry Smith 
2943d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2944d763cef2SBarry Smith 
2945d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2946d763cef2SBarry Smith 
2947d763cef2SBarry Smith @*/
29487087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2949d763cef2SBarry Smith {
2950dfbe8321SBarry Smith   PetscErrorCode ierr;
2951089b2837SJed Brown   SNES           snes;
2952d763cef2SBarry Smith 
2953d763cef2SBarry Smith   PetscFunctionBegin;
29540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2955d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2956089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2957089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2958d763cef2SBarry Smith   PetscFunctionReturn(0);
2959d763cef2SBarry Smith }
2960d763cef2SBarry Smith 
29614a2ae208SSatish Balay #undef __FUNCT__
29624a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2963d763cef2SBarry Smith /*@C
2964d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2965d763cef2SBarry Smith    TS options in the database.
2966d763cef2SBarry Smith 
2967d763cef2SBarry Smith    Not Collective
2968d763cef2SBarry Smith 
2969d763cef2SBarry Smith    Input Parameter:
2970d763cef2SBarry Smith .  ts - The TS context
2971d763cef2SBarry Smith 
2972d763cef2SBarry Smith    Output Parameter:
2973d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2974d763cef2SBarry Smith 
2975d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2976d763cef2SBarry Smith    sufficient length to hold the prefix.
2977d763cef2SBarry Smith 
2978d763cef2SBarry Smith    Level: intermediate
2979d763cef2SBarry Smith 
2980d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2981d763cef2SBarry Smith 
2982d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2983d763cef2SBarry Smith @*/
29847087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2985d763cef2SBarry Smith {
2986dfbe8321SBarry Smith   PetscErrorCode ierr;
2987d763cef2SBarry Smith 
2988d763cef2SBarry Smith   PetscFunctionBegin;
29890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29904482741eSBarry Smith   PetscValidPointer(prefix,2);
2991d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2992d763cef2SBarry Smith   PetscFunctionReturn(0);
2993d763cef2SBarry Smith }
2994d763cef2SBarry Smith 
29954a2ae208SSatish Balay #undef __FUNCT__
29964a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2997d763cef2SBarry Smith /*@C
2998d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2999d763cef2SBarry Smith 
3000d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3001d763cef2SBarry Smith 
3002d763cef2SBarry Smith    Input Parameter:
3003d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3004d763cef2SBarry Smith 
3005d763cef2SBarry Smith    Output Parameters:
3006e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3007e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3008e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3009e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3010d763cef2SBarry Smith 
30110298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3012d763cef2SBarry Smith 
3013d763cef2SBarry Smith    Level: intermediate
3014d763cef2SBarry Smith 
301526d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3016d763cef2SBarry Smith 
3017d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3018d763cef2SBarry Smith @*/
3019e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3020d763cef2SBarry Smith {
3021089b2837SJed Brown   PetscErrorCode ierr;
3022089b2837SJed Brown   SNES           snes;
302324989b8cSPeter Brune   DM             dm;
3024089b2837SJed Brown 
3025d763cef2SBarry Smith   PetscFunctionBegin;
3026089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3027e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
302824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
302924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3030d763cef2SBarry Smith   PetscFunctionReturn(0);
3031d763cef2SBarry Smith }
3032d763cef2SBarry Smith 
30331713a123SBarry Smith #undef __FUNCT__
30342eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
30352eca1d9cSJed Brown /*@C
30362eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
30372eca1d9cSJed Brown 
30382eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
30392eca1d9cSJed Brown 
30402eca1d9cSJed Brown    Input Parameter:
30412eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
30422eca1d9cSJed Brown 
30432eca1d9cSJed Brown    Output Parameters:
3044e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3045e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
30462eca1d9cSJed Brown .  f   - The function to compute the matrices
30472eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
30482eca1d9cSJed Brown 
30490298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
30502eca1d9cSJed Brown 
30512eca1d9cSJed Brown    Level: advanced
30522eca1d9cSJed Brown 
30532eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
30542eca1d9cSJed Brown 
30552eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
30562eca1d9cSJed Brown @*/
3057e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
30582eca1d9cSJed Brown {
3059089b2837SJed Brown   PetscErrorCode ierr;
3060089b2837SJed Brown   SNES           snes;
306124989b8cSPeter Brune   DM             dm;
30620910c330SBarry Smith 
30632eca1d9cSJed Brown   PetscFunctionBegin;
3064089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3065f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3066e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
306724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
306824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
30692eca1d9cSJed Brown   PetscFunctionReturn(0);
30702eca1d9cSJed Brown }
30712eca1d9cSJed Brown 
30726083293cSBarry Smith 
30732eca1d9cSJed Brown #undef __FUNCT__
307483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
30751713a123SBarry Smith /*@C
307683a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
30771713a123SBarry Smith    VecView() for the solution at each timestep
30781713a123SBarry Smith 
30791713a123SBarry Smith    Collective on TS
30801713a123SBarry Smith 
30811713a123SBarry Smith    Input Parameters:
30821713a123SBarry Smith +  ts - the TS context
30831713a123SBarry Smith .  step - current time-step
3084142b95e3SSatish Balay .  ptime - current time
30850298fd71SBarry Smith -  dummy - either a viewer or NULL
30861713a123SBarry Smith 
308799fdda47SBarry Smith    Options Database:
308899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
308999fdda47SBarry Smith 
309099fdda47SBarry 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
309199fdda47SBarry Smith        will look bad
309299fdda47SBarry Smith 
30931713a123SBarry Smith    Level: intermediate
30941713a123SBarry Smith 
30951713a123SBarry Smith .keywords: TS,  vector, monitor, view
30961713a123SBarry Smith 
3097a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
30981713a123SBarry Smith @*/
30990910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31001713a123SBarry Smith {
3101dfbe8321SBarry Smith   PetscErrorCode   ierr;
310283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3103473a3ab2SBarry Smith   PetscDraw        draw;
31041713a123SBarry Smith 
31051713a123SBarry Smith   PetscFunctionBegin;
31066083293cSBarry Smith   if (!step && ictx->showinitial) {
31076083293cSBarry Smith     if (!ictx->initialsolution) {
31080910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
31091713a123SBarry Smith     }
31100910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
31116083293cSBarry Smith   }
3112b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
31130dcf80beSBarry Smith 
31146083293cSBarry Smith   if (ictx->showinitial) {
31156083293cSBarry Smith     PetscReal pause;
31166083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
31176083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
31186083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
31196083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
31206083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
31216083293cSBarry Smith   }
31220910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3123473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3124473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3125473a3ab2SBarry Smith     char      time[32];
3126473a3ab2SBarry Smith     size_t    len;
3127473a3ab2SBarry Smith 
3128473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3129473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3130473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3131473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
31320298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3133473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3134473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3135473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3136473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3137473a3ab2SBarry Smith   }
3138473a3ab2SBarry Smith 
31396083293cSBarry Smith   if (ictx->showinitial) {
31406083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
31416083293cSBarry Smith   }
31421713a123SBarry Smith   PetscFunctionReturn(0);
31431713a123SBarry Smith }
31441713a123SBarry Smith 
31452d5ee99bSBarry Smith #undef __FUNCT__
31462d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
31472d5ee99bSBarry Smith /*@C
31482d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
31492d5ee99bSBarry Smith 
31502d5ee99bSBarry Smith    Collective on TS
31512d5ee99bSBarry Smith 
31522d5ee99bSBarry Smith    Input Parameters:
31532d5ee99bSBarry Smith +  ts - the TS context
31542d5ee99bSBarry Smith .  step - current time-step
31552d5ee99bSBarry Smith .  ptime - current time
31562d5ee99bSBarry Smith -  dummy - either a viewer or NULL
31572d5ee99bSBarry Smith 
31582d5ee99bSBarry Smith    Level: intermediate
31592d5ee99bSBarry Smith 
31602d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
31612d5ee99bSBarry Smith 
31622d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
31632d5ee99bSBarry Smith @*/
31642d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
31652d5ee99bSBarry Smith {
31662d5ee99bSBarry Smith   PetscErrorCode    ierr;
31672d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
31682d5ee99bSBarry Smith   PetscDraw         draw;
31692d5ee99bSBarry Smith   MPI_Comm          comm;
31702d5ee99bSBarry Smith   PetscInt          n;
31712d5ee99bSBarry Smith   PetscMPIInt       size;
31722d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
31732d5ee99bSBarry Smith   char              time[32];
31742d5ee99bSBarry Smith   size_t            len;
31752d5ee99bSBarry Smith   const PetscScalar *U;
31762d5ee99bSBarry Smith 
31772d5ee99bSBarry Smith   PetscFunctionBegin;
31782d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
31792d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
31802d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
31812d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
31822d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
31832d5ee99bSBarry Smith 
31842d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
31852d5ee99bSBarry Smith 
31862d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
31874b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3188ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3189a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3190a4494fc1SBarry Smith       PetscFunctionReturn(0);
3191a4494fc1SBarry Smith   }
31922d5ee99bSBarry Smith   if (!step) ictx->color++;
31932d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
31942d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
31952d5ee99bSBarry Smith 
31962d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
31974b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
31982d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
31992d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
32002d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
32012d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
32022d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
32032d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
32042d5ee99bSBarry Smith   }
32052d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
32062d5ee99bSBarry Smith   PetscFunctionReturn(0);
32072d5ee99bSBarry Smith }
32082d5ee99bSBarry Smith 
32091713a123SBarry Smith 
32106c699258SBarry Smith #undef __FUNCT__
321183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
32126083293cSBarry Smith /*@C
321383a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
32146083293cSBarry Smith 
32156083293cSBarry Smith    Collective on TS
32166083293cSBarry Smith 
32176083293cSBarry Smith    Input Parameters:
32186083293cSBarry Smith .    ctx - the monitor context
32196083293cSBarry Smith 
32206083293cSBarry Smith    Level: intermediate
32216083293cSBarry Smith 
32226083293cSBarry Smith .keywords: TS,  vector, monitor, view
32236083293cSBarry Smith 
322483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
32256083293cSBarry Smith @*/
322683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
32276083293cSBarry Smith {
32286083293cSBarry Smith   PetscErrorCode ierr;
32296083293cSBarry Smith 
32306083293cSBarry Smith   PetscFunctionBegin;
32314b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
323283a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
323383a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
323483a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
32356083293cSBarry Smith   PetscFunctionReturn(0);
32366083293cSBarry Smith }
32376083293cSBarry Smith 
32386083293cSBarry Smith #undef __FUNCT__
323983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
32406083293cSBarry Smith /*@C
324183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
32426083293cSBarry Smith 
32436083293cSBarry Smith    Collective on TS
32446083293cSBarry Smith 
32456083293cSBarry Smith    Input Parameter:
32466083293cSBarry Smith .    ts - time-step context
32476083293cSBarry Smith 
32486083293cSBarry Smith    Output Patameter:
32496083293cSBarry Smith .    ctx - the monitor context
32506083293cSBarry Smith 
325199fdda47SBarry Smith    Options Database:
325299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
325399fdda47SBarry Smith 
32546083293cSBarry Smith    Level: intermediate
32556083293cSBarry Smith 
32566083293cSBarry Smith .keywords: TS,  vector, monitor, view
32576083293cSBarry Smith 
325883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
32596083293cSBarry Smith @*/
326083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
32616083293cSBarry Smith {
32626083293cSBarry Smith   PetscErrorCode   ierr;
32636083293cSBarry Smith 
32646083293cSBarry Smith   PetscFunctionBegin;
326583a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
326683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3267e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3268bbd56ea5SKarl Rupp 
326983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3270473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
32710298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3272473a3ab2SBarry Smith 
3273473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
32740298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
32752d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
32766083293cSBarry Smith   PetscFunctionReturn(0);
32776083293cSBarry Smith }
32786083293cSBarry Smith 
32796083293cSBarry Smith #undef __FUNCT__
328083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
32813a471f94SBarry Smith /*@C
328283a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
32833a471f94SBarry Smith    VecView() for the error at each timestep
32843a471f94SBarry Smith 
32853a471f94SBarry Smith    Collective on TS
32863a471f94SBarry Smith 
32873a471f94SBarry Smith    Input Parameters:
32883a471f94SBarry Smith +  ts - the TS context
32893a471f94SBarry Smith .  step - current time-step
32903a471f94SBarry Smith .  ptime - current time
32910298fd71SBarry Smith -  dummy - either a viewer or NULL
32923a471f94SBarry Smith 
32933a471f94SBarry Smith    Level: intermediate
32943a471f94SBarry Smith 
32953a471f94SBarry Smith .keywords: TS,  vector, monitor, view
32963a471f94SBarry Smith 
32973a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
32983a471f94SBarry Smith @*/
32990910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
33003a471f94SBarry Smith {
33013a471f94SBarry Smith   PetscErrorCode   ierr;
330283a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
330383a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
33043a471f94SBarry Smith   Vec              work;
33053a471f94SBarry Smith 
33063a471f94SBarry Smith   PetscFunctionBegin;
3307b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
33080910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
33093a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
33100910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
33113a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
33123a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
33133a471f94SBarry Smith   PetscFunctionReturn(0);
33143a471f94SBarry Smith }
33153a471f94SBarry Smith 
33162a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
33173a471f94SBarry Smith #undef __FUNCT__
33186c699258SBarry Smith #define __FUNCT__ "TSSetDM"
33196c699258SBarry Smith /*@
33206c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
33216c699258SBarry Smith 
33223f9fe445SBarry Smith    Logically Collective on TS and DM
33236c699258SBarry Smith 
33246c699258SBarry Smith    Input Parameters:
33256c699258SBarry Smith +  ts - the preconditioner context
33266c699258SBarry Smith -  dm - the dm
33276c699258SBarry Smith 
33286c699258SBarry Smith    Level: intermediate
33296c699258SBarry Smith 
33306c699258SBarry Smith 
33316c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
33326c699258SBarry Smith @*/
33337087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
33346c699258SBarry Smith {
33356c699258SBarry Smith   PetscErrorCode ierr;
3336089b2837SJed Brown   SNES           snes;
3337942e3340SBarry Smith   DMTS           tsdm;
33386c699258SBarry Smith 
33396c699258SBarry Smith   PetscFunctionBegin;
33400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
334170663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3342942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
33432a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3344942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3345942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
334624989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
334724989b8cSPeter Brune         tsdm->originaldm = dm;
334824989b8cSPeter Brune       }
334924989b8cSPeter Brune     }
33506bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
335124989b8cSPeter Brune   }
33526c699258SBarry Smith   ts->dm = dm;
3353bbd56ea5SKarl Rupp 
3354089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3355089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
33566c699258SBarry Smith   PetscFunctionReturn(0);
33576c699258SBarry Smith }
33586c699258SBarry Smith 
33596c699258SBarry Smith #undef __FUNCT__
33606c699258SBarry Smith #define __FUNCT__ "TSGetDM"
33616c699258SBarry Smith /*@
33626c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
33636c699258SBarry Smith 
33643f9fe445SBarry Smith    Not Collective
33656c699258SBarry Smith 
33666c699258SBarry Smith    Input Parameter:
33676c699258SBarry Smith . ts - the preconditioner context
33686c699258SBarry Smith 
33696c699258SBarry Smith    Output Parameter:
33706c699258SBarry Smith .  dm - the dm
33716c699258SBarry Smith 
33726c699258SBarry Smith    Level: intermediate
33736c699258SBarry Smith 
33746c699258SBarry Smith 
33756c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
33766c699258SBarry Smith @*/
33777087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
33786c699258SBarry Smith {
3379496e6a7aSJed Brown   PetscErrorCode ierr;
3380496e6a7aSJed Brown 
33816c699258SBarry Smith   PetscFunctionBegin;
33820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3383496e6a7aSJed Brown   if (!ts->dm) {
3384ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3385496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3386496e6a7aSJed Brown   }
33876c699258SBarry Smith   *dm = ts->dm;
33886c699258SBarry Smith   PetscFunctionReturn(0);
33896c699258SBarry Smith }
33901713a123SBarry Smith 
33910f5c6efeSJed Brown #undef __FUNCT__
33920f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
33930f5c6efeSJed Brown /*@
33940f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
33950f5c6efeSJed Brown 
33963f9fe445SBarry Smith    Logically Collective on SNES
33970f5c6efeSJed Brown 
33980f5c6efeSJed Brown    Input Parameter:
3399d42a1c89SJed Brown + snes - nonlinear solver
34000910c330SBarry Smith . U - the current state at which to evaluate the residual
3401d42a1c89SJed Brown - ctx - user context, must be a TS
34020f5c6efeSJed Brown 
34030f5c6efeSJed Brown    Output Parameter:
34040f5c6efeSJed Brown . F - the nonlinear residual
34050f5c6efeSJed Brown 
34060f5c6efeSJed Brown    Notes:
34070f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
34080f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
34090f5c6efeSJed Brown 
34100f5c6efeSJed Brown    Level: advanced
34110f5c6efeSJed Brown 
34120f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
34130f5c6efeSJed Brown @*/
34140910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
34150f5c6efeSJed Brown {
34160f5c6efeSJed Brown   TS             ts = (TS)ctx;
34170f5c6efeSJed Brown   PetscErrorCode ierr;
34180f5c6efeSJed Brown 
34190f5c6efeSJed Brown   PetscFunctionBegin;
34200f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34210910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34220f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
34230f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
34240910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
34250f5c6efeSJed Brown   PetscFunctionReturn(0);
34260f5c6efeSJed Brown }
34270f5c6efeSJed Brown 
34280f5c6efeSJed Brown #undef __FUNCT__
34290f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
34300f5c6efeSJed Brown /*@
34310f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
34320f5c6efeSJed Brown 
34330f5c6efeSJed Brown    Collective on SNES
34340f5c6efeSJed Brown 
34350f5c6efeSJed Brown    Input Parameter:
34360f5c6efeSJed Brown + snes - nonlinear solver
34370910c330SBarry Smith . U - the current state at which to evaluate the residual
34380f5c6efeSJed Brown - ctx - user context, must be a TS
34390f5c6efeSJed Brown 
34400f5c6efeSJed Brown    Output Parameter:
34410f5c6efeSJed Brown + A - the Jacobian
34420f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
34430f5c6efeSJed Brown - flag - indicates any structure change in the matrix
34440f5c6efeSJed Brown 
34450f5c6efeSJed Brown    Notes:
34460f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
34470f5c6efeSJed Brown 
34480f5c6efeSJed Brown    Level: developer
34490f5c6efeSJed Brown 
34500f5c6efeSJed Brown .seealso: SNESSetJacobian()
34510f5c6efeSJed Brown @*/
34520910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
34530f5c6efeSJed Brown {
34540f5c6efeSJed Brown   TS             ts = (TS)ctx;
34550f5c6efeSJed Brown   PetscErrorCode ierr;
34560f5c6efeSJed Brown 
34570f5c6efeSJed Brown   PetscFunctionBegin;
34580f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
34590910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
34600f5c6efeSJed Brown   PetscValidPointer(A,3);
34610f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
34620f5c6efeSJed Brown   PetscValidPointer(B,4);
34630f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
34640f5c6efeSJed Brown   PetscValidPointer(flag,5);
34650f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
34660910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
34670f5c6efeSJed Brown   PetscFunctionReturn(0);
34680f5c6efeSJed Brown }
3469325fc9f4SBarry Smith 
34700e4ef248SJed Brown #undef __FUNCT__
34710e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
34720e4ef248SJed Brown /*@C
34730e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
34740e4ef248SJed Brown 
34750e4ef248SJed Brown    Collective on TS
34760e4ef248SJed Brown 
34770e4ef248SJed Brown    Input Arguments:
34780e4ef248SJed Brown +  ts - time stepping context
34790e4ef248SJed Brown .  t - time at which to evaluate
34800910c330SBarry Smith .  U - state at which to evaluate
34810e4ef248SJed Brown -  ctx - context
34820e4ef248SJed Brown 
34830e4ef248SJed Brown    Output Arguments:
34840e4ef248SJed Brown .  F - right hand side
34850e4ef248SJed Brown 
34860e4ef248SJed Brown    Level: intermediate
34870e4ef248SJed Brown 
34880e4ef248SJed Brown    Notes:
34890e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
34900e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
34910e4ef248SJed Brown 
34920e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
34930e4ef248SJed Brown @*/
34940910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
34950e4ef248SJed Brown {
34960e4ef248SJed Brown   PetscErrorCode ierr;
34970e4ef248SJed Brown   Mat            Arhs,Brhs;
34980e4ef248SJed Brown   MatStructure   flg2;
34990e4ef248SJed Brown 
35000e4ef248SJed Brown   PetscFunctionBegin;
35010e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
35020910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
35030910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
35040e4ef248SJed Brown   PetscFunctionReturn(0);
35050e4ef248SJed Brown }
35060e4ef248SJed Brown 
35070e4ef248SJed Brown #undef __FUNCT__
35080e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
35090e4ef248SJed Brown /*@C
35100e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
35110e4ef248SJed Brown 
35120e4ef248SJed Brown    Collective on TS
35130e4ef248SJed Brown 
35140e4ef248SJed Brown    Input Arguments:
35150e4ef248SJed Brown +  ts - time stepping context
35160e4ef248SJed Brown .  t - time at which to evaluate
35170910c330SBarry Smith .  U - state at which to evaluate
35180e4ef248SJed Brown -  ctx - context
35190e4ef248SJed Brown 
35200e4ef248SJed Brown    Output Arguments:
35210e4ef248SJed Brown +  A - pointer to operator
35220e4ef248SJed Brown .  B - pointer to preconditioning matrix
35230e4ef248SJed Brown -  flg - matrix structure flag
35240e4ef248SJed Brown 
35250e4ef248SJed Brown    Level: intermediate
35260e4ef248SJed Brown 
35270e4ef248SJed Brown    Notes:
35280e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
35290e4ef248SJed Brown 
35300e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
35310e4ef248SJed Brown @*/
35320910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
35330e4ef248SJed Brown {
35340e4ef248SJed Brown   PetscFunctionBegin;
35350e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
35360e4ef248SJed Brown   PetscFunctionReturn(0);
35370e4ef248SJed Brown }
35380e4ef248SJed Brown 
35390026cea9SSean Farley #undef __FUNCT__
35400026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
35410026cea9SSean Farley /*@C
35420026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
35430026cea9SSean Farley 
35440026cea9SSean Farley    Collective on TS
35450026cea9SSean Farley 
35460026cea9SSean Farley    Input Arguments:
35470026cea9SSean Farley +  ts - time stepping context
35480026cea9SSean Farley .  t - time at which to evaluate
35490910c330SBarry Smith .  U - state at which to evaluate
35500910c330SBarry Smith .  Udot - time derivative of state vector
35510026cea9SSean Farley -  ctx - context
35520026cea9SSean Farley 
35530026cea9SSean Farley    Output Arguments:
35540026cea9SSean Farley .  F - left hand side
35550026cea9SSean Farley 
35560026cea9SSean Farley    Level: intermediate
35570026cea9SSean Farley 
35580026cea9SSean Farley    Notes:
35590910c330SBarry 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
35600026cea9SSean Farley    user is required to write their own TSComputeIFunction.
35610026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
35620026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
35630026cea9SSean Farley 
35640026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
35650026cea9SSean Farley @*/
35660910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
35670026cea9SSean Farley {
35680026cea9SSean Farley   PetscErrorCode ierr;
35690026cea9SSean Farley   Mat            A,B;
35700026cea9SSean Farley   MatStructure   flg2;
35710026cea9SSean Farley 
35720026cea9SSean Farley   PetscFunctionBegin;
35730298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
35740910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
35750910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
35760026cea9SSean Farley   PetscFunctionReturn(0);
35770026cea9SSean Farley }
35780026cea9SSean Farley 
35790026cea9SSean Farley #undef __FUNCT__
35800026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
35810026cea9SSean Farley /*@C
358224e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
35830026cea9SSean Farley 
35840026cea9SSean Farley    Collective on TS
35850026cea9SSean Farley 
35860026cea9SSean Farley    Input Arguments:
35870026cea9SSean Farley +  ts - time stepping context
35880026cea9SSean Farley .  t - time at which to evaluate
35890910c330SBarry Smith .  U - state at which to evaluate
35900910c330SBarry Smith .  Udot - time derivative of state vector
35910026cea9SSean Farley .  shift - shift to apply
35920026cea9SSean Farley -  ctx - context
35930026cea9SSean Farley 
35940026cea9SSean Farley    Output Arguments:
35950026cea9SSean Farley +  A - pointer to operator
35960026cea9SSean Farley .  B - pointer to preconditioning matrix
35970026cea9SSean Farley -  flg - matrix structure flag
35980026cea9SSean Farley 
35990026cea9SSean Farley    Level: intermediate
36000026cea9SSean Farley 
36010026cea9SSean Farley    Notes:
36020026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
36030026cea9SSean Farley 
36040026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
36050026cea9SSean Farley @*/
36060910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
36070026cea9SSean Farley {
36080026cea9SSean Farley   PetscFunctionBegin;
36090026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
36100026cea9SSean Farley   PetscFunctionReturn(0);
36110026cea9SSean Farley }
3612e817cc15SEmil Constantinescu #undef __FUNCT__
3613e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3614e817cc15SEmil Constantinescu /*@
3615e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3616e817cc15SEmil Constantinescu 
3617e817cc15SEmil Constantinescu    Not Collective
3618e817cc15SEmil Constantinescu 
3619e817cc15SEmil Constantinescu    Input Parameter:
3620e817cc15SEmil Constantinescu .  ts - the TS context
3621e817cc15SEmil Constantinescu 
3622e817cc15SEmil Constantinescu    Output Parameter:
36234e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3624e817cc15SEmil Constantinescu 
3625e817cc15SEmil Constantinescu    Level: beginner
3626e817cc15SEmil Constantinescu 
3627e817cc15SEmil Constantinescu .keywords: TS, equation type
3628e817cc15SEmil Constantinescu 
3629e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3630e817cc15SEmil Constantinescu @*/
3631e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3632e817cc15SEmil Constantinescu {
3633e817cc15SEmil Constantinescu   PetscFunctionBegin;
3634e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3635e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3636e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3637e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3638e817cc15SEmil Constantinescu }
3639e817cc15SEmil Constantinescu 
3640e817cc15SEmil Constantinescu #undef __FUNCT__
3641e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3642e817cc15SEmil Constantinescu /*@
3643e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3644e817cc15SEmil Constantinescu 
3645e817cc15SEmil Constantinescu    Not Collective
3646e817cc15SEmil Constantinescu 
3647e817cc15SEmil Constantinescu    Input Parameter:
3648e817cc15SEmil Constantinescu +  ts - the TS context
36494e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3650e817cc15SEmil Constantinescu 
3651e817cc15SEmil Constantinescu    Level: advanced
3652e817cc15SEmil Constantinescu 
3653e817cc15SEmil Constantinescu .keywords: TS, equation type
3654e817cc15SEmil Constantinescu 
3655e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3656e817cc15SEmil Constantinescu @*/
3657e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3658e817cc15SEmil Constantinescu {
3659e817cc15SEmil Constantinescu   PetscFunctionBegin;
3660e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3661e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3662e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3663e817cc15SEmil Constantinescu }
36640026cea9SSean Farley 
36654af1b03aSJed Brown #undef __FUNCT__
36664af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
36674af1b03aSJed Brown /*@
36684af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
36694af1b03aSJed Brown 
36704af1b03aSJed Brown    Not Collective
36714af1b03aSJed Brown 
36724af1b03aSJed Brown    Input Parameter:
36734af1b03aSJed Brown .  ts - the TS context
36744af1b03aSJed Brown 
36754af1b03aSJed Brown    Output Parameter:
36764af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
36774af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
36784af1b03aSJed Brown 
3679487e0bb9SJed Brown    Level: beginner
36804af1b03aSJed Brown 
3681cd652676SJed Brown    Notes:
3682cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
36834af1b03aSJed Brown 
36844af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
36854af1b03aSJed Brown 
36864af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
36874af1b03aSJed Brown @*/
36884af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
36894af1b03aSJed Brown {
36904af1b03aSJed Brown   PetscFunctionBegin;
36914af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36924af1b03aSJed Brown   PetscValidPointer(reason,2);
36934af1b03aSJed Brown   *reason = ts->reason;
36944af1b03aSJed Brown   PetscFunctionReturn(0);
36954af1b03aSJed Brown }
36964af1b03aSJed Brown 
3697fb1732b5SBarry Smith #undef __FUNCT__
3698d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3699d6ad946cSShri Abhyankar /*@
3700d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3701d6ad946cSShri Abhyankar 
3702d6ad946cSShri Abhyankar    Not Collective
3703d6ad946cSShri Abhyankar 
3704d6ad946cSShri Abhyankar    Input Parameter:
3705d6ad946cSShri Abhyankar +  ts - the TS context
3706d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3707d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3708d6ad946cSShri Abhyankar 
3709f5abba47SShri Abhyankar    Level: advanced
3710d6ad946cSShri Abhyankar 
3711d6ad946cSShri Abhyankar    Notes:
3712d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3713d6ad946cSShri Abhyankar 
3714d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3715d6ad946cSShri Abhyankar 
3716d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3717d6ad946cSShri Abhyankar @*/
3718d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3719d6ad946cSShri Abhyankar {
3720d6ad946cSShri Abhyankar   PetscFunctionBegin;
3721d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3722d6ad946cSShri Abhyankar   ts->reason = reason;
3723d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3724d6ad946cSShri Abhyankar }
3725d6ad946cSShri Abhyankar 
3726d6ad946cSShri Abhyankar #undef __FUNCT__
3727cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3728cc708dedSBarry Smith /*@
3729cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3730cc708dedSBarry Smith 
3731cc708dedSBarry Smith    Not Collective
3732cc708dedSBarry Smith 
3733cc708dedSBarry Smith    Input Parameter:
3734cc708dedSBarry Smith .  ts - the TS context
3735cc708dedSBarry Smith 
3736cc708dedSBarry Smith    Output Parameter:
3737cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3738cc708dedSBarry Smith 
3739487e0bb9SJed Brown    Level: beginner
3740cc708dedSBarry Smith 
3741cc708dedSBarry Smith    Notes:
3742cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3743cc708dedSBarry Smith 
3744cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3745cc708dedSBarry Smith 
3746cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3747cc708dedSBarry Smith @*/
3748cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3749cc708dedSBarry Smith {
3750cc708dedSBarry Smith   PetscFunctionBegin;
3751cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3752cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3753cc708dedSBarry Smith   *ftime = ts->solvetime;
3754cc708dedSBarry Smith   PetscFunctionReturn(0);
3755cc708dedSBarry Smith }
3756cc708dedSBarry Smith 
3757cc708dedSBarry Smith #undef __FUNCT__
37585ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
37599f67acb7SJed Brown /*@
37605ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
37619f67acb7SJed Brown    used by the time integrator.
37629f67acb7SJed Brown 
37639f67acb7SJed Brown    Not Collective
37649f67acb7SJed Brown 
37659f67acb7SJed Brown    Input Parameter:
37669f67acb7SJed Brown .  ts - TS context
37679f67acb7SJed Brown 
37689f67acb7SJed Brown    Output Parameter:
37699f67acb7SJed Brown .  nits - number of nonlinear iterations
37709f67acb7SJed Brown 
37719f67acb7SJed Brown    Notes:
37729f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
37739f67acb7SJed Brown 
37749f67acb7SJed Brown    Level: intermediate
37759f67acb7SJed Brown 
37769f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
37779f67acb7SJed Brown 
37785ef26d82SJed Brown .seealso:  TSGetKSPIterations()
37799f67acb7SJed Brown @*/
37805ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
37819f67acb7SJed Brown {
37829f67acb7SJed Brown   PetscFunctionBegin;
37839f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37849f67acb7SJed Brown   PetscValidIntPointer(nits,2);
37855ef26d82SJed Brown   *nits = ts->snes_its;
37869f67acb7SJed Brown   PetscFunctionReturn(0);
37879f67acb7SJed Brown }
37889f67acb7SJed Brown 
37899f67acb7SJed Brown #undef __FUNCT__
37905ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
37919f67acb7SJed Brown /*@
37925ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
37939f67acb7SJed Brown    used by the time integrator.
37949f67acb7SJed Brown 
37959f67acb7SJed Brown    Not Collective
37969f67acb7SJed Brown 
37979f67acb7SJed Brown    Input Parameter:
37989f67acb7SJed Brown .  ts - TS context
37999f67acb7SJed Brown 
38009f67acb7SJed Brown    Output Parameter:
38019f67acb7SJed Brown .  lits - number of linear iterations
38029f67acb7SJed Brown 
38039f67acb7SJed Brown    Notes:
38049f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
38059f67acb7SJed Brown 
38069f67acb7SJed Brown    Level: intermediate
38079f67acb7SJed Brown 
38089f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
38099f67acb7SJed Brown 
38105ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
38119f67acb7SJed Brown @*/
38125ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
38139f67acb7SJed Brown {
38149f67acb7SJed Brown   PetscFunctionBegin;
38159f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
38169f67acb7SJed Brown   PetscValidIntPointer(lits,2);
38175ef26d82SJed Brown   *lits = ts->ksp_its;
38189f67acb7SJed Brown   PetscFunctionReturn(0);
38199f67acb7SJed Brown }
38209f67acb7SJed Brown 
38219f67acb7SJed Brown #undef __FUNCT__
3822cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3823cef5090cSJed Brown /*@
3824cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3825cef5090cSJed Brown 
3826cef5090cSJed Brown    Not Collective
3827cef5090cSJed Brown 
3828cef5090cSJed Brown    Input Parameter:
3829cef5090cSJed Brown .  ts - TS context
3830cef5090cSJed Brown 
3831cef5090cSJed Brown    Output Parameter:
3832cef5090cSJed Brown .  rejects - number of steps rejected
3833cef5090cSJed Brown 
3834cef5090cSJed Brown    Notes:
3835cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3836cef5090cSJed Brown 
3837cef5090cSJed Brown    Level: intermediate
3838cef5090cSJed Brown 
3839cef5090cSJed Brown .keywords: TS, get, number
3840cef5090cSJed Brown 
38415ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3842cef5090cSJed Brown @*/
3843cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3844cef5090cSJed Brown {
3845cef5090cSJed Brown   PetscFunctionBegin;
3846cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3847cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3848cef5090cSJed Brown   *rejects = ts->reject;
3849cef5090cSJed Brown   PetscFunctionReturn(0);
3850cef5090cSJed Brown }
3851cef5090cSJed Brown 
3852cef5090cSJed Brown #undef __FUNCT__
3853cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3854cef5090cSJed Brown /*@
3855cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3856cef5090cSJed Brown 
3857cef5090cSJed Brown    Not Collective
3858cef5090cSJed Brown 
3859cef5090cSJed Brown    Input Parameter:
3860cef5090cSJed Brown .  ts - TS context
3861cef5090cSJed Brown 
3862cef5090cSJed Brown    Output Parameter:
3863cef5090cSJed Brown .  fails - number of failed nonlinear solves
3864cef5090cSJed Brown 
3865cef5090cSJed Brown    Notes:
3866cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3867cef5090cSJed Brown 
3868cef5090cSJed Brown    Level: intermediate
3869cef5090cSJed Brown 
3870cef5090cSJed Brown .keywords: TS, get, number
3871cef5090cSJed Brown 
38725ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3873cef5090cSJed Brown @*/
3874cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3875cef5090cSJed Brown {
3876cef5090cSJed Brown   PetscFunctionBegin;
3877cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3878cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3879cef5090cSJed Brown   *fails = ts->num_snes_failures;
3880cef5090cSJed Brown   PetscFunctionReturn(0);
3881cef5090cSJed Brown }
3882cef5090cSJed Brown 
3883cef5090cSJed Brown #undef __FUNCT__
3884cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3885cef5090cSJed Brown /*@
3886cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3887cef5090cSJed Brown 
3888cef5090cSJed Brown    Not Collective
3889cef5090cSJed Brown 
3890cef5090cSJed Brown    Input Parameter:
3891cef5090cSJed Brown +  ts - TS context
3892cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3893cef5090cSJed Brown 
3894cef5090cSJed Brown    Notes:
3895cef5090cSJed Brown    The counter is reset to zero for each step
3896cef5090cSJed Brown 
3897cef5090cSJed Brown    Options Database Key:
3898cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3899cef5090cSJed Brown 
3900cef5090cSJed Brown    Level: intermediate
3901cef5090cSJed Brown 
3902cef5090cSJed Brown .keywords: TS, set, maximum, number
3903cef5090cSJed Brown 
39045ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3905cef5090cSJed Brown @*/
3906cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3907cef5090cSJed Brown {
3908cef5090cSJed Brown   PetscFunctionBegin;
3909cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3910cef5090cSJed Brown   ts->max_reject = rejects;
3911cef5090cSJed Brown   PetscFunctionReturn(0);
3912cef5090cSJed Brown }
3913cef5090cSJed Brown 
3914cef5090cSJed Brown #undef __FUNCT__
3915cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3916cef5090cSJed Brown /*@
3917cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3918cef5090cSJed Brown 
3919cef5090cSJed Brown    Not Collective
3920cef5090cSJed Brown 
3921cef5090cSJed Brown    Input Parameter:
3922cef5090cSJed Brown +  ts - TS context
3923cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3924cef5090cSJed Brown 
3925cef5090cSJed Brown    Notes:
3926cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3927cef5090cSJed Brown 
3928cef5090cSJed Brown    Options Database Key:
3929cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3930cef5090cSJed Brown 
3931cef5090cSJed Brown    Level: intermediate
3932cef5090cSJed Brown 
3933cef5090cSJed Brown .keywords: TS, set, maximum, number
3934cef5090cSJed Brown 
39355ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3936cef5090cSJed Brown @*/
3937cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3938cef5090cSJed Brown {
3939cef5090cSJed Brown   PetscFunctionBegin;
3940cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3941cef5090cSJed Brown   ts->max_snes_failures = fails;
3942cef5090cSJed Brown   PetscFunctionReturn(0);
3943cef5090cSJed Brown }
3944cef5090cSJed Brown 
3945cef5090cSJed Brown #undef __FUNCT__
3946cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3947cef5090cSJed Brown /*@
3948cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3949cef5090cSJed Brown 
3950cef5090cSJed Brown    Not Collective
3951cef5090cSJed Brown 
3952cef5090cSJed Brown    Input Parameter:
3953cef5090cSJed Brown +  ts - TS context
3954cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3955cef5090cSJed Brown 
3956cef5090cSJed Brown    Options Database Key:
3957cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3958cef5090cSJed Brown 
3959cef5090cSJed Brown    Level: intermediate
3960cef5090cSJed Brown 
3961cef5090cSJed Brown .keywords: TS, set, error
3962cef5090cSJed Brown 
39635ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3964cef5090cSJed Brown @*/
3965cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3966cef5090cSJed Brown {
3967cef5090cSJed Brown   PetscFunctionBegin;
3968cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3969cef5090cSJed Brown   ts->errorifstepfailed = err;
3970cef5090cSJed Brown   PetscFunctionReturn(0);
3971cef5090cSJed Brown }
3972cef5090cSJed Brown 
3973cef5090cSJed Brown #undef __FUNCT__
3974fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3975fb1732b5SBarry Smith /*@C
3976fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3977fb1732b5SBarry Smith 
3978fb1732b5SBarry Smith    Collective on TS
3979fb1732b5SBarry Smith 
3980fb1732b5SBarry Smith    Input Parameters:
3981fb1732b5SBarry Smith +  ts - the TS context
3982fb1732b5SBarry Smith .  step - current time-step
3983fb1732b5SBarry Smith .  ptime - current time
39840910c330SBarry Smith .  u - current state
3985fb1732b5SBarry Smith -  viewer - binary viewer
3986fb1732b5SBarry Smith 
3987fb1732b5SBarry Smith    Level: intermediate
3988fb1732b5SBarry Smith 
3989fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3990fb1732b5SBarry Smith 
3991fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3992fb1732b5SBarry Smith @*/
39930910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3994fb1732b5SBarry Smith {
3995fb1732b5SBarry Smith   PetscErrorCode ierr;
3996ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3997fb1732b5SBarry Smith 
3998fb1732b5SBarry Smith   PetscFunctionBegin;
39990910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4000ed81e22dSJed Brown   PetscFunctionReturn(0);
4001ed81e22dSJed Brown }
4002ed81e22dSJed Brown 
4003ed81e22dSJed Brown #undef __FUNCT__
4004ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4005ed81e22dSJed Brown /*@C
4006ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4007ed81e22dSJed Brown 
4008ed81e22dSJed Brown    Collective on TS
4009ed81e22dSJed Brown 
4010ed81e22dSJed Brown    Input Parameters:
4011ed81e22dSJed Brown +  ts - the TS context
4012ed81e22dSJed Brown .  step - current time-step
4013ed81e22dSJed Brown .  ptime - current time
40140910c330SBarry Smith .  u - current state
4015ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4016ed81e22dSJed Brown 
4017ed81e22dSJed Brown    Level: intermediate
4018ed81e22dSJed Brown 
4019ed81e22dSJed Brown    Notes:
4020ed81e22dSJed 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.
4021ed81e22dSJed Brown    These are named according to the file name template.
4022ed81e22dSJed Brown 
4023ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4024ed81e22dSJed Brown 
4025ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4026ed81e22dSJed Brown 
4027ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4028ed81e22dSJed Brown @*/
40290910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4030ed81e22dSJed Brown {
4031ed81e22dSJed Brown   PetscErrorCode ierr;
4032ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4033ed81e22dSJed Brown   PetscViewer    viewer;
4034ed81e22dSJed Brown 
4035ed81e22dSJed Brown   PetscFunctionBegin;
40368caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4037ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
40380910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4039ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4040ed81e22dSJed Brown   PetscFunctionReturn(0);
4041ed81e22dSJed Brown }
4042ed81e22dSJed Brown 
4043ed81e22dSJed Brown #undef __FUNCT__
4044ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4045ed81e22dSJed Brown /*@C
4046ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4047ed81e22dSJed Brown 
4048ed81e22dSJed Brown    Collective on TS
4049ed81e22dSJed Brown 
4050ed81e22dSJed Brown    Input Parameters:
4051ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4052ed81e22dSJed Brown 
4053ed81e22dSJed Brown    Level: intermediate
4054ed81e22dSJed Brown 
4055ed81e22dSJed Brown    Note:
4056ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4057ed81e22dSJed Brown 
4058ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4059ed81e22dSJed Brown 
4060ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4061ed81e22dSJed Brown @*/
4062ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4063ed81e22dSJed Brown {
4064ed81e22dSJed Brown   PetscErrorCode ierr;
4065ed81e22dSJed Brown 
4066ed81e22dSJed Brown   PetscFunctionBegin;
4067ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4068fb1732b5SBarry Smith   PetscFunctionReturn(0);
4069fb1732b5SBarry Smith }
4070fb1732b5SBarry Smith 
407184df9cb4SJed Brown #undef __FUNCT__
4072552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
407384df9cb4SJed Brown /*@
4074552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
407584df9cb4SJed Brown 
4076ed81e22dSJed Brown    Collective on TS if controller has not been created yet
407784df9cb4SJed Brown 
407884df9cb4SJed Brown    Input Arguments:
4079ed81e22dSJed Brown .  ts - time stepping context
408084df9cb4SJed Brown 
408184df9cb4SJed Brown    Output Arguments:
4082ed81e22dSJed Brown .  adapt - adaptive controller
408384df9cb4SJed Brown 
408484df9cb4SJed Brown    Level: intermediate
408584df9cb4SJed Brown 
4086ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
408784df9cb4SJed Brown @*/
4088552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
408984df9cb4SJed Brown {
409084df9cb4SJed Brown   PetscErrorCode ierr;
409184df9cb4SJed Brown 
409284df9cb4SJed Brown   PetscFunctionBegin;
409384df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
409484df9cb4SJed Brown   PetscValidPointer(adapt,2);
409584df9cb4SJed Brown   if (!ts->adapt) {
4096ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
40971c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
40981c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
409984df9cb4SJed Brown   }
410084df9cb4SJed Brown   *adapt = ts->adapt;
410184df9cb4SJed Brown   PetscFunctionReturn(0);
410284df9cb4SJed Brown }
4103d6ebe24aSShri Abhyankar 
4104d6ebe24aSShri Abhyankar #undef __FUNCT__
41051c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
41061c3436cfSJed Brown /*@
41071c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
41081c3436cfSJed Brown 
41091c3436cfSJed Brown    Logically Collective
41101c3436cfSJed Brown 
41111c3436cfSJed Brown    Input Arguments:
41121c3436cfSJed Brown +  ts - time integration context
41131c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
41140298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
41151c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
41160298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
41171c3436cfSJed Brown 
41181c3436cfSJed Brown    Level: beginner
41191c3436cfSJed Brown 
4120c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
41211c3436cfSJed Brown @*/
41221c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
41231c3436cfSJed Brown {
41241c3436cfSJed Brown   PetscErrorCode ierr;
41251c3436cfSJed Brown 
41261c3436cfSJed Brown   PetscFunctionBegin;
4127c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
41281c3436cfSJed Brown   if (vatol) {
41291c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
41301c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4131bbd56ea5SKarl Rupp 
41321c3436cfSJed Brown     ts->vatol = vatol;
41331c3436cfSJed Brown   }
4134c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
41351c3436cfSJed Brown   if (vrtol) {
41361c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
41371c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4138bbd56ea5SKarl Rupp 
41391c3436cfSJed Brown     ts->vrtol = vrtol;
41401c3436cfSJed Brown   }
41411c3436cfSJed Brown   PetscFunctionReturn(0);
41421c3436cfSJed Brown }
41431c3436cfSJed Brown 
41441c3436cfSJed Brown #undef __FUNCT__
4145c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4146c5033834SJed Brown /*@
4147c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4148c5033834SJed Brown 
4149c5033834SJed Brown    Logically Collective
4150c5033834SJed Brown 
4151c5033834SJed Brown    Input Arguments:
4152c5033834SJed Brown .  ts - time integration context
4153c5033834SJed Brown 
4154c5033834SJed Brown    Output Arguments:
41550298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
41560298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
41570298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
41580298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4159c5033834SJed Brown 
4160c5033834SJed Brown    Level: beginner
4161c5033834SJed Brown 
4162c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4163c5033834SJed Brown @*/
4164c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4165c5033834SJed Brown {
4166c5033834SJed Brown   PetscFunctionBegin;
4167c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4168c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4169c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4170c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4171c5033834SJed Brown   PetscFunctionReturn(0);
4172c5033834SJed Brown }
4173c5033834SJed Brown 
4174c5033834SJed Brown #undef __FUNCT__
41751c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
41761c3436cfSJed Brown /*@
41771c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
41781c3436cfSJed Brown 
41791c3436cfSJed Brown    Collective on TS
41801c3436cfSJed Brown 
41811c3436cfSJed Brown    Input Arguments:
41821c3436cfSJed Brown +  ts - time stepping context
41831c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
41841c3436cfSJed Brown 
41851c3436cfSJed Brown    Output Arguments:
41861c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
41871c3436cfSJed Brown 
41881c3436cfSJed Brown    Level: developer
41891c3436cfSJed Brown 
41901c3436cfSJed Brown .seealso: TSSetTolerances()
41911c3436cfSJed Brown @*/
41921c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
41931c3436cfSJed Brown {
41941c3436cfSJed Brown   PetscErrorCode    ierr;
41951c3436cfSJed Brown   PetscInt          i,n,N;
41960910c330SBarry Smith   const PetscScalar *u,*y;
41970910c330SBarry Smith   Vec               U;
41981c3436cfSJed Brown   PetscReal         sum,gsum;
41991c3436cfSJed Brown 
42001c3436cfSJed Brown   PetscFunctionBegin;
42011c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42021c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
42031c3436cfSJed Brown   PetscValidPointer(norm,3);
42040910c330SBarry Smith   U = ts->vec_sol;
42050910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4206ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
42071c3436cfSJed Brown 
42080910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
42090910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
42100910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
42111c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
42121c3436cfSJed Brown   sum  = 0.;
4213ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4214ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4215ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4216ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4217ceefc113SJed Brown     for (i=0; i<n; i++) {
42180910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42190910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4220ceefc113SJed Brown     }
4221ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4222ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4223ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4224ceefc113SJed Brown     const PetscScalar *atol;
4225ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4226ceefc113SJed Brown     for (i=0; i<n; i++) {
42270910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42280910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4229ceefc113SJed Brown     }
4230ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4231ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4232ceefc113SJed Brown     const PetscScalar *rtol;
4233ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4234ceefc113SJed Brown     for (i=0; i<n; i++) {
42350910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42360910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4237ceefc113SJed Brown     }
4238ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4239ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
42401c3436cfSJed Brown     for (i=0; i<n; i++) {
42410910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
42420910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
42431c3436cfSJed Brown     }
4244ceefc113SJed Brown   }
42450910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
42461c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
42471c3436cfSJed Brown 
4248ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
42491c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4250ce94432eSBarry Smith   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
42511c3436cfSJed Brown   PetscFunctionReturn(0);
42521c3436cfSJed Brown }
42531c3436cfSJed Brown 
42541c3436cfSJed Brown #undef __FUNCT__
42558d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
42568d59e960SJed Brown /*@
42578d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
42588d59e960SJed Brown 
42598d59e960SJed Brown    Logically Collective on TS
42608d59e960SJed Brown 
42618d59e960SJed Brown    Input Arguments:
42628d59e960SJed Brown +  ts - time stepping context
42638d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
42648d59e960SJed Brown 
42658d59e960SJed Brown    Note:
42668d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
42678d59e960SJed Brown 
42688d59e960SJed Brown    Level: intermediate
42698d59e960SJed Brown 
42708d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
42718d59e960SJed Brown @*/
42728d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
42738d59e960SJed Brown {
42748d59e960SJed Brown   PetscFunctionBegin;
42758d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42768d59e960SJed Brown   ts->cfltime_local = cfltime;
42778d59e960SJed Brown   ts->cfltime       = -1.;
42788d59e960SJed Brown   PetscFunctionReturn(0);
42798d59e960SJed Brown }
42808d59e960SJed Brown 
42818d59e960SJed Brown #undef __FUNCT__
42828d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
42838d59e960SJed Brown /*@
42848d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
42858d59e960SJed Brown 
42868d59e960SJed Brown    Collective on TS
42878d59e960SJed Brown 
42888d59e960SJed Brown    Input Arguments:
42898d59e960SJed Brown .  ts - time stepping context
42908d59e960SJed Brown 
42918d59e960SJed Brown    Output Arguments:
42928d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
42938d59e960SJed Brown 
42948d59e960SJed Brown    Level: advanced
42958d59e960SJed Brown 
42968d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
42978d59e960SJed Brown @*/
42988d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
42998d59e960SJed Brown {
43008d59e960SJed Brown   PetscErrorCode ierr;
43018d59e960SJed Brown 
43028d59e960SJed Brown   PetscFunctionBegin;
43038d59e960SJed Brown   if (ts->cfltime < 0) {
4304ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
43058d59e960SJed Brown   }
43068d59e960SJed Brown   *cfltime = ts->cfltime;
43078d59e960SJed Brown   PetscFunctionReturn(0);
43088d59e960SJed Brown }
43098d59e960SJed Brown 
43108d59e960SJed Brown #undef __FUNCT__
4311d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4312d6ebe24aSShri Abhyankar /*@
4313d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4314d6ebe24aSShri Abhyankar 
4315d6ebe24aSShri Abhyankar    Input Parameters:
4316d6ebe24aSShri Abhyankar .  ts   - the TS context.
4317d6ebe24aSShri Abhyankar .  xl   - lower bound.
4318d6ebe24aSShri Abhyankar .  xu   - upper bound.
4319d6ebe24aSShri Abhyankar 
4320d6ebe24aSShri Abhyankar    Notes:
4321d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
432233c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
4323d6ebe24aSShri Abhyankar 
43242bd2b0e6SSatish Balay    Level: advanced
43252bd2b0e6SSatish Balay 
4326d6ebe24aSShri Abhyankar @*/
4327d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4328d6ebe24aSShri Abhyankar {
4329d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4330d6ebe24aSShri Abhyankar   SNES           snes;
4331d6ebe24aSShri Abhyankar 
4332d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4333d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4334d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4335d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4336d6ebe24aSShri Abhyankar }
4337d6ebe24aSShri Abhyankar 
4338325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4339c6db04a5SJed Brown #include <mex.h>
4340325fc9f4SBarry Smith 
4341325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4342325fc9f4SBarry Smith 
4343325fc9f4SBarry Smith #undef __FUNCT__
4344325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4345325fc9f4SBarry Smith /*
4346325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4347325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4348325fc9f4SBarry Smith 
4349325fc9f4SBarry Smith    Collective on TS
4350325fc9f4SBarry Smith 
4351325fc9f4SBarry Smith    Input Parameters:
4352325fc9f4SBarry Smith +  snes - the TS context
43530910c330SBarry Smith -  u - input vector
4354325fc9f4SBarry Smith 
4355325fc9f4SBarry Smith    Output Parameter:
4356325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4357325fc9f4SBarry Smith 
4358325fc9f4SBarry Smith    Notes:
4359325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4360325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4361325fc9f4SBarry Smith    themselves.
4362325fc9f4SBarry Smith 
4363325fc9f4SBarry Smith    Level: developer
4364325fc9f4SBarry Smith 
4365325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4366325fc9f4SBarry Smith 
4367325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4368325fc9f4SBarry Smith */
43690910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4370325fc9f4SBarry Smith {
4371325fc9f4SBarry Smith   PetscErrorCode  ierr;
4372325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4373325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4374325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4375325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4376325fc9f4SBarry Smith 
4377325fc9f4SBarry Smith   PetscFunctionBegin;
4378325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
43790910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
43800910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4381325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
43820910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4383325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4384325fc9f4SBarry Smith 
4385325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
43860910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
43870910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
43880910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4389bbd56ea5SKarl Rupp 
4390325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4391325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4392325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4393325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4394325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4395325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4396325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4397325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4398325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4399325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4400325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4401325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4402325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4403325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4404325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4405325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4406325fc9f4SBarry Smith   PetscFunctionReturn(0);
4407325fc9f4SBarry Smith }
4408325fc9f4SBarry Smith 
4409325fc9f4SBarry Smith 
4410325fc9f4SBarry Smith #undef __FUNCT__
4411325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4412325fc9f4SBarry Smith /*
4413325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4414325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4415e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4416325fc9f4SBarry Smith 
4417325fc9f4SBarry Smith    Logically Collective on TS
4418325fc9f4SBarry Smith 
4419325fc9f4SBarry Smith    Input Parameters:
4420325fc9f4SBarry Smith +  ts - the TS context
4421325fc9f4SBarry Smith -  func - function evaluation routine
4422325fc9f4SBarry Smith 
4423325fc9f4SBarry Smith    Calling sequence of func:
44240910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4425325fc9f4SBarry Smith 
4426325fc9f4SBarry Smith    Level: beginner
4427325fc9f4SBarry Smith 
4428325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4429325fc9f4SBarry Smith 
4430325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4431325fc9f4SBarry Smith */
4432cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4433325fc9f4SBarry Smith {
4434325fc9f4SBarry Smith   PetscErrorCode  ierr;
4435325fc9f4SBarry Smith   TSMatlabContext *sctx;
4436325fc9f4SBarry Smith 
4437325fc9f4SBarry Smith   PetscFunctionBegin;
4438325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4439325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4440325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4441325fc9f4SBarry Smith   /*
4442325fc9f4SBarry Smith      This should work, but it doesn't
4443325fc9f4SBarry Smith   sctx->ctx = ctx;
4444325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4445325fc9f4SBarry Smith   */
4446325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4447bbd56ea5SKarl Rupp 
44480298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4449325fc9f4SBarry Smith   PetscFunctionReturn(0);
4450325fc9f4SBarry Smith }
4451325fc9f4SBarry Smith 
4452325fc9f4SBarry Smith #undef __FUNCT__
4453325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4454325fc9f4SBarry Smith /*
4455325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4456325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4457325fc9f4SBarry Smith 
4458325fc9f4SBarry Smith    Collective on TS
4459325fc9f4SBarry Smith 
4460325fc9f4SBarry Smith    Input Parameters:
4461cdcf91faSSean Farley +  ts - the TS context
44620910c330SBarry Smith .  u - input vector
4463325fc9f4SBarry Smith .  A, B - the matrices
4464325fc9f4SBarry Smith -  ctx - user context
4465325fc9f4SBarry Smith 
4466325fc9f4SBarry Smith    Output Parameter:
4467325fc9f4SBarry Smith .  flag - structure of the matrix
4468325fc9f4SBarry Smith 
4469325fc9f4SBarry Smith    Level: developer
4470325fc9f4SBarry Smith 
4471325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4472325fc9f4SBarry Smith 
4473325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4474325fc9f4SBarry Smith @*/
44750910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
4476325fc9f4SBarry Smith {
4477325fc9f4SBarry Smith   PetscErrorCode  ierr;
4478325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4479325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4480325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4481325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4482325fc9f4SBarry Smith 
4483325fc9f4SBarry Smith   PetscFunctionBegin;
4484cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44850910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4486325fc9f4SBarry Smith 
44870910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4488325fc9f4SBarry Smith 
4489cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
44900910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
44910910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
44920910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
44930910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4494bbd56ea5SKarl Rupp 
4495325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4496325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4497325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4498325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4499325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4500325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4501325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4502325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4503325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4504325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4505325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4506325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
4507325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4508325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4509325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4510325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4511325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4512325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4513325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4514325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4515325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4516325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4517325fc9f4SBarry Smith   PetscFunctionReturn(0);
4518325fc9f4SBarry Smith }
4519325fc9f4SBarry Smith 
4520325fc9f4SBarry Smith 
4521325fc9f4SBarry Smith #undef __FUNCT__
4522325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4523325fc9f4SBarry Smith /*
4524325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4525e3c5b3baSBarry 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
4526325fc9f4SBarry Smith 
4527325fc9f4SBarry Smith    Logically Collective on TS
4528325fc9f4SBarry Smith 
4529325fc9f4SBarry Smith    Input Parameters:
4530cdcf91faSSean Farley +  ts - the TS context
4531325fc9f4SBarry Smith .  A,B - Jacobian matrices
4532325fc9f4SBarry Smith .  func - function evaluation routine
4533325fc9f4SBarry Smith -  ctx - user context
4534325fc9f4SBarry Smith 
4535325fc9f4SBarry Smith    Calling sequence of func:
45360910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4537325fc9f4SBarry Smith 
4538325fc9f4SBarry Smith 
4539325fc9f4SBarry Smith    Level: developer
4540325fc9f4SBarry Smith 
4541325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4542325fc9f4SBarry Smith 
4543325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4544325fc9f4SBarry Smith */
4545cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4546325fc9f4SBarry Smith {
4547325fc9f4SBarry Smith   PetscErrorCode  ierr;
4548325fc9f4SBarry Smith   TSMatlabContext *sctx;
4549325fc9f4SBarry Smith 
4550325fc9f4SBarry Smith   PetscFunctionBegin;
4551325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4552325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4553325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4554325fc9f4SBarry Smith   /*
4555325fc9f4SBarry Smith      This should work, but it doesn't
4556325fc9f4SBarry Smith   sctx->ctx = ctx;
4557325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4558325fc9f4SBarry Smith   */
4559325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4560bbd56ea5SKarl Rupp 
4561cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4562325fc9f4SBarry Smith   PetscFunctionReturn(0);
4563325fc9f4SBarry Smith }
4564325fc9f4SBarry Smith 
4565b5b1a830SBarry Smith #undef __FUNCT__
4566b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4567b5b1a830SBarry Smith /*
4568b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4569b5b1a830SBarry Smith 
4570b5b1a830SBarry Smith    Collective on TS
4571b5b1a830SBarry Smith 
4572b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4573b5b1a830SBarry Smith @*/
45740910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4575b5b1a830SBarry Smith {
4576b5b1a830SBarry Smith   PetscErrorCode  ierr;
4577b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4578a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4579b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4580b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4581b5b1a830SBarry Smith 
4582b5b1a830SBarry Smith   PetscFunctionBegin;
4583cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45840910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4585b5b1a830SBarry Smith 
4586cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
45870910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4588bbd56ea5SKarl Rupp 
4589b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4590b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4591b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4592b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4593b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4594b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4595b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4596b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4597b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4598b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4599b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4600b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4601b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4602b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4603b5b1a830SBarry Smith   PetscFunctionReturn(0);
4604b5b1a830SBarry Smith }
4605b5b1a830SBarry Smith 
4606b5b1a830SBarry Smith 
4607b5b1a830SBarry Smith #undef __FUNCT__
4608b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4609b5b1a830SBarry Smith /*
4610b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4611b5b1a830SBarry Smith 
4612b5b1a830SBarry Smith    Level: developer
4613b5b1a830SBarry Smith 
4614b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4615b5b1a830SBarry Smith 
4616b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4617b5b1a830SBarry Smith */
4618cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4619b5b1a830SBarry Smith {
4620b5b1a830SBarry Smith   PetscErrorCode  ierr;
4621b5b1a830SBarry Smith   TSMatlabContext *sctx;
4622b5b1a830SBarry Smith 
4623b5b1a830SBarry Smith   PetscFunctionBegin;
4624b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4625b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4626b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4627b5b1a830SBarry Smith   /*
4628b5b1a830SBarry Smith      This should work, but it doesn't
4629b5b1a830SBarry Smith   sctx->ctx = ctx;
4630b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4631b5b1a830SBarry Smith   */
4632b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4633bbd56ea5SKarl Rupp 
46340298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4635b5b1a830SBarry Smith   PetscFunctionReturn(0);
4636b5b1a830SBarry Smith }
4637325fc9f4SBarry Smith #endif
4638b3603a34SBarry Smith 
46390b039ecaSBarry Smith 
46400b039ecaSBarry Smith 
4641b3603a34SBarry Smith #undef __FUNCT__
46424f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4643b3603a34SBarry Smith /*@C
46444f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4645b3603a34SBarry Smith        in a time based line graph
4646b3603a34SBarry Smith 
4647b3603a34SBarry Smith    Collective on TS
4648b3603a34SBarry Smith 
4649b3603a34SBarry Smith    Input Parameters:
4650b3603a34SBarry Smith +  ts - the TS context
4651b3603a34SBarry Smith .  step - current time-step
4652b3603a34SBarry Smith .  ptime - current time
4653b3603a34SBarry Smith -  lg - a line graph object
4654b3603a34SBarry Smith 
4655b3603a34SBarry Smith    Level: intermediate
4656b3603a34SBarry Smith 
46570b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4658b3603a34SBarry Smith 
4659b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4660b3603a34SBarry Smith 
4661b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4662b3603a34SBarry Smith @*/
46630910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4664b3603a34SBarry Smith {
4665b3603a34SBarry Smith   PetscErrorCode    ierr;
46660b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4667b3603a34SBarry Smith   const PetscScalar *yy;
466858ff32f7SBarry Smith   PetscInt          dim;
4669b3603a34SBarry Smith 
4670b3603a34SBarry Smith   PetscFunctionBegin;
467158ff32f7SBarry Smith   if (!step) {
4672a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4673a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4674a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
46750910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
46760b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
46770b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
467858ff32f7SBarry Smith   }
46790910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4680e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4681e3efe391SJed Brown   {
4682e3efe391SJed Brown     PetscReal *yreal;
4683e3efe391SJed Brown     PetscInt  i,n;
46840910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4685e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4686e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
46870b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4688e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4689e3efe391SJed Brown   }
4690e3efe391SJed Brown #else
46910b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4692e3efe391SJed Brown #endif
46930910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
4694b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
46950b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
46963923b477SBarry Smith   }
4697b3603a34SBarry Smith   PetscFunctionReturn(0);
4698b3603a34SBarry Smith }
4699b3603a34SBarry Smith 
4700b3603a34SBarry Smith #undef __FUNCT__
47014f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4702ef20d060SBarry Smith /*@C
47034f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4704ef20d060SBarry Smith        in a time based line graph
4705ef20d060SBarry Smith 
4706ef20d060SBarry Smith    Collective on TS
4707ef20d060SBarry Smith 
4708ef20d060SBarry Smith    Input Parameters:
4709ef20d060SBarry Smith +  ts - the TS context
4710ef20d060SBarry Smith .  step - current time-step
4711ef20d060SBarry Smith .  ptime - current time
4712ef20d060SBarry Smith -  lg - a line graph object
4713ef20d060SBarry Smith 
4714ef20d060SBarry Smith    Level: intermediate
4715ef20d060SBarry Smith 
4716abd5a294SJed Brown    Notes:
4717abd5a294SJed Brown    Only for sequential solves.
4718abd5a294SJed Brown 
4719abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4720abd5a294SJed Brown 
4721abd5a294SJed Brown    Options Database Keys:
47224f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4723ef20d060SBarry Smith 
4724ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4725ef20d060SBarry Smith 
4726abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4727ef20d060SBarry Smith @*/
47280910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4729ef20d060SBarry Smith {
4730ef20d060SBarry Smith   PetscErrorCode    ierr;
47310b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4732ef20d060SBarry Smith   const PetscScalar *yy;
4733ef20d060SBarry Smith   Vec               y;
4734a9f9c1f6SBarry Smith   PetscInt          dim;
4735ef20d060SBarry Smith 
4736ef20d060SBarry Smith   PetscFunctionBegin;
4737a9f9c1f6SBarry Smith   if (!step) {
4738a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4739a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
47401ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
47410910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4742a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4743a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4744a9f9c1f6SBarry Smith   }
47450910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4746ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
47470910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4748ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4749e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4750e3efe391SJed Brown   {
4751e3efe391SJed Brown     PetscReal *yreal;
4752e3efe391SJed Brown     PetscInt  i,n;
4753e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4754e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4755e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
47560b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4757e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4758e3efe391SJed Brown   }
4759e3efe391SJed Brown #else
47600b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4761e3efe391SJed Brown #endif
4762ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4763ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
4764b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
47650b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
47663923b477SBarry Smith   }
4767ef20d060SBarry Smith   PetscFunctionReturn(0);
4768ef20d060SBarry Smith }
4769ef20d060SBarry Smith 
4770201da799SBarry Smith #undef __FUNCT__
4771201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4772201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4773201da799SBarry Smith {
4774201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4775201da799SBarry Smith   PetscReal      x   = ptime,y;
4776201da799SBarry Smith   PetscErrorCode ierr;
4777201da799SBarry Smith   PetscInt       its;
4778201da799SBarry Smith 
4779201da799SBarry Smith   PetscFunctionBegin;
4780201da799SBarry Smith   if (!n) {
4781201da799SBarry Smith     PetscDrawAxis axis;
4782bbd56ea5SKarl Rupp 
4783201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4784201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4785201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4786bbd56ea5SKarl Rupp 
4787201da799SBarry Smith     ctx->snes_its = 0;
4788201da799SBarry Smith   }
4789201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4790201da799SBarry Smith   y    = its - ctx->snes_its;
4791201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
47923fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4793201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4794201da799SBarry Smith   }
4795201da799SBarry Smith   ctx->snes_its = its;
4796201da799SBarry Smith   PetscFunctionReturn(0);
4797201da799SBarry Smith }
4798201da799SBarry Smith 
4799201da799SBarry Smith #undef __FUNCT__
4800201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4801201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4802201da799SBarry Smith {
4803201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4804201da799SBarry Smith   PetscReal      x   = ptime,y;
4805201da799SBarry Smith   PetscErrorCode ierr;
4806201da799SBarry Smith   PetscInt       its;
4807201da799SBarry Smith 
4808201da799SBarry Smith   PetscFunctionBegin;
4809201da799SBarry Smith   if (!n) {
4810201da799SBarry Smith     PetscDrawAxis axis;
4811bbd56ea5SKarl Rupp 
4812201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4813201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4814201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4815bbd56ea5SKarl Rupp 
4816201da799SBarry Smith     ctx->ksp_its = 0;
4817201da799SBarry Smith   }
4818201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4819201da799SBarry Smith   y    = its - ctx->ksp_its;
4820201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
482199fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
4822201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4823201da799SBarry Smith   }
4824201da799SBarry Smith   ctx->ksp_its = its;
4825201da799SBarry Smith   PetscFunctionReturn(0);
4826201da799SBarry Smith }
4827f9c1d6abSBarry Smith 
4828f9c1d6abSBarry Smith #undef __FUNCT__
4829f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4830f9c1d6abSBarry Smith /*@
4831f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4832f9c1d6abSBarry Smith 
4833f9c1d6abSBarry Smith    Collective on TS and Vec
4834f9c1d6abSBarry Smith 
4835f9c1d6abSBarry Smith    Input Parameters:
4836f9c1d6abSBarry Smith +  ts - the TS context
4837f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4838f9c1d6abSBarry Smith 
4839f9c1d6abSBarry Smith    Output Parameters:
4840f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4841f9c1d6abSBarry Smith 
4842f9c1d6abSBarry Smith    Level: developer
4843f9c1d6abSBarry Smith 
4844f9c1d6abSBarry Smith .keywords: TS, compute
4845f9c1d6abSBarry Smith 
4846f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4847f9c1d6abSBarry Smith @*/
4848f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4849f9c1d6abSBarry Smith {
4850f9c1d6abSBarry Smith   PetscErrorCode ierr;
4851f9c1d6abSBarry Smith 
4852f9c1d6abSBarry Smith   PetscFunctionBegin;
4853f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4854ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4855f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4856f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4857f9c1d6abSBarry Smith }
4858