xref: /petsc/src/ts/interface/ts.c (revision cc708ded1f3df60df60d6b0dd94fb319767e5b2e)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
4d763cef2SBarry Smith 
5d5ba7fb7SMatthew Knepley /* Logging support */
67087cfbeSBarry Smith PetscClassId  TS_CLASSID;
7166c7f25SBarry Smith PetscLogEvent  TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
8d405a339SMatthew Knepley 
94a2ae208SSatish Balay #undef __FUNCT__
10bdad233fSMatthew Knepley #define __FUNCT__ "TSSetTypeFromOptions"
11bdad233fSMatthew Knepley /*
12bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
13bdad233fSMatthew Knepley 
14bdad233fSMatthew Knepley   Collective on TS
15bdad233fSMatthew Knepley 
16bdad233fSMatthew Knepley   Input Parameter:
17bdad233fSMatthew Knepley . ts - The ts
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Level: intermediate
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
22bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
23bdad233fSMatthew Knepley */
246849ba73SBarry Smith static PetscErrorCode TSSetTypeFromOptions(TS ts)
25bdad233fSMatthew Knepley {
26ace3abfcSBarry Smith   PetscBool      opt;
272fc52814SBarry Smith   const char     *defaultType;
28bdad233fSMatthew Knepley   char           typeName[256];
29dfbe8321SBarry Smith   PetscErrorCode ierr;
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley   PetscFunctionBegin;
327adad957SLisandro Dalcin   if (((PetscObject)ts)->type_name) {
337adad957SLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
34bdad233fSMatthew Knepley   } else {
359596e0b4SJed Brown     defaultType = TSEULER;
36bdad233fSMatthew Knepley   }
37bdad233fSMatthew Knepley 
38cce0b1b2SLisandro Dalcin   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
39bdad233fSMatthew Knepley   ierr = PetscOptionsList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
40a7cc72afSBarry Smith   if (opt) {
41bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
42bdad233fSMatthew Knepley   } else {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   }
45bdad233fSMatthew Knepley   PetscFunctionReturn(0);
46bdad233fSMatthew Knepley }
47bdad233fSMatthew Knepley 
48bdad233fSMatthew Knepley #undef __FUNCT__
49bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
50bdad233fSMatthew Knepley /*@
51bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
52bdad233fSMatthew Knepley 
53bdad233fSMatthew Knepley    Collective on TS
54bdad233fSMatthew Knepley 
55bdad233fSMatthew Knepley    Input Parameter:
56bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
57bdad233fSMatthew Knepley 
58bdad233fSMatthew Knepley    Options Database Keys:
594d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
60bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
613bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
62bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
63bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
64de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
65de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
66de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
67de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
68de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
69de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
70de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
71de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
72de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
73de06c3feSJed Brown .  -ts_monitor_draw_solution_binary <filename> - Save each solution to a binary file
74de06c3feSJed Brown -  -ts_monitor_draw_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
75bdad233fSMatthew Knepley 
76bdad233fSMatthew Knepley    Level: beginner
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
79bdad233fSMatthew Knepley 
80a313700dSBarry Smith .seealso: TSGetType()
81bdad233fSMatthew Knepley @*/
827087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
83bdad233fSMatthew Knepley {
84ace3abfcSBarry Smith   PetscBool      opt,flg;
85dfbe8321SBarry Smith   PetscErrorCode ierr;
86649052a6SBarry Smith   PetscViewer    monviewer;
87eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
88089b2837SJed Brown   SNES           snes;
891c3436cfSJed Brown   TSAdapt        adapt;
9031748224SBarry Smith   PetscReal      time_step;
91bdad233fSMatthew Knepley 
92bdad233fSMatthew Knepley   PetscFunctionBegin;
930700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
943194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
95a43b19c4SJed Brown     /* Handle TS type options */
96a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
97bdad233fSMatthew Knepley 
98bdad233fSMatthew Knepley     /* Handle generic TS options */
99bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
1003bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
101d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
10231748224SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10331748224SBarry Smith     if (flg) {
10431748224SBarry Smith       ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10531748224SBarry Smith     }
106a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
107a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
108461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
109cef5090cSJed Brown     ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,PETSC_NULL);CHKERRQ(ierr);
110cef5090cSJed Brown     ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,PETSC_NULL);CHKERRQ(ierr);
111cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1121c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1131c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
114bdad233fSMatthew Knepley 
115bdad233fSMatthew Knepley     /* Monitor options */
116a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
117eabae89aSBarry Smith     if (flg) {
118649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
119649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
120bdad233fSMatthew Knepley     }
1215180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1225180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1235180491cSLisandro Dalcin 
1244f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
125a7cc72afSBarry Smith     if (opt) {
1260b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1273923b477SBarry Smith       PetscInt       howoften = 1;
128a80ad3e0SBarry Smith 
1294f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1301d1e9da1SBarry Smith       ierr = TSMonitorLGCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1314f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
132b3603a34SBarry Smith     }
1334f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
134b3603a34SBarry Smith     if (opt) {
1350b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1363923b477SBarry Smith       PetscInt       howoften = 1;
137b3603a34SBarry Smith 
1384f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1393923b477SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
1404f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
141bdad233fSMatthew Knepley     }
1424f09c107SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
143ef20d060SBarry Smith     if (opt) {
1440b039ecaSBarry Smith       TSMonitorLGCtx ctx;
1453923b477SBarry Smith       PetscInt       howoften = 1;
146ef20d060SBarry Smith 
1474f09c107SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1483923b477SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
1494f09c107SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
150ef20d060SBarry Smith     }
151201da799SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
152201da799SBarry Smith     if (opt) {
153201da799SBarry Smith       TSMonitorLGCtx ctx;
154201da799SBarry Smith       PetscInt       howoften = 1;
155201da799SBarry Smith 
156201da799SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
157201da799SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
158201da799SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
159201da799SBarry Smith     }
160201da799SBarry Smith     ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
161201da799SBarry Smith     if (opt) {
162201da799SBarry Smith       TSMonitorLGCtx ctx;
163201da799SBarry Smith       PetscInt       howoften = 1;
164201da799SBarry Smith 
165201da799SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
166201da799SBarry Smith       ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
167201da799SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
168201da799SBarry Smith     }
1698189c53fSBarry Smith     ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1708189c53fSBarry Smith     if (opt) {
1718189c53fSBarry Smith       TSMonitorSPEigCtx ctx;
1728189c53fSBarry Smith       PetscInt          howoften = 1;
1738189c53fSBarry Smith 
1748189c53fSBarry Smith       ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
1758189c53fSBarry Smith       ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
1768189c53fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1778189c53fSBarry Smith     }
178ef20d060SBarry Smith     opt  = PETSC_FALSE;
1790dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
180a7cc72afSBarry Smith     if (opt) {
18183a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
18283a4ac43SBarry Smith       PetscInt         howoften = 1;
183a80ad3e0SBarry Smith 
18483a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
18583a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
18683a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
187bdad233fSMatthew Knepley     }
188fb1732b5SBarry Smith     opt  = PETSC_FALSE;
1890dcf80beSBarry Smith     ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
1903a471f94SBarry Smith     if (opt) {
19183a4ac43SBarry Smith       TSMonitorDrawCtx ctx;
19283a4ac43SBarry Smith       PetscInt         howoften = 1;
1933a471f94SBarry Smith 
19483a4ac43SBarry Smith       ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,PETSC_NULL);CHKERRQ(ierr);
19583a4ac43SBarry Smith       ierr = TSMonitorDrawCtxCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);
19683a4ac43SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
1973a471f94SBarry Smith     }
1983a471f94SBarry Smith     opt  = PETSC_FALSE;
199de06c3feSJed Brown     ierr = PetscOptionsString("-ts_monitor_draw_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
200fb1732b5SBarry Smith     if (flg) {
201fb1732b5SBarry Smith       PetscViewer ctx;
202fb1732b5SBarry Smith       if (monfilename[0]) {
203fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
204fb1732b5SBarry Smith       } else {
205fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
206fb1732b5SBarry Smith       }
207fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
208fb1732b5SBarry Smith     }
209ed81e22dSJed Brown     opt  = PETSC_FALSE;
210de06c3feSJed Brown     ierr = PetscOptionsString("-ts_monitor_draw_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
211ed81e22dSJed Brown     if (flg) {
212ed81e22dSJed Brown       const char *ptr,*ptr2;
213ed81e22dSJed Brown       char *filetemplate;
214de06c3feSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts");
215ed81e22dSJed Brown       /* Do some cursory validation of the input. */
216ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
217de06c3feSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_draw_solution_vtk requires a file template, e.g. filename-%%03D.vts");
218ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
219ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
220de06c3feSJed Brown         if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_draw_solution_vtk, should look like filename-%%03D.vts");
221ed81e22dSJed Brown         if (ptr2) break;
222ed81e22dSJed Brown       }
223ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
224ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
225ed81e22dSJed Brown     }
226bdad233fSMatthew Knepley 
2271c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
2281c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
2291c3436cfSJed Brown 
230d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
231d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
232d52bd9f3SBarry Smith 
233bdad233fSMatthew Knepley     /* Handle specific TS options */
234abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
235bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
236bdad233fSMatthew Knepley     }
2375d973c19SBarry Smith 
2385d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2395d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
240bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
241bdad233fSMatthew Knepley   PetscFunctionReturn(0);
242bdad233fSMatthew Knepley }
243bdad233fSMatthew Knepley 
244bdad233fSMatthew Knepley #undef __FUNCT__
245cdcf91faSSean Farley #undef __FUNCT__
2464a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
247a7a1495cSBarry Smith /*@
2488c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
249a7a1495cSBarry Smith       set with TSSetRHSJacobian().
250a7a1495cSBarry Smith 
251a7a1495cSBarry Smith    Collective on TS and Vec
252a7a1495cSBarry Smith 
253a7a1495cSBarry Smith    Input Parameters:
254316643e7SJed Brown +  ts - the TS context
255a7a1495cSBarry Smith .  t - current timestep
2560910c330SBarry Smith -  U - input vector
257a7a1495cSBarry Smith 
258a7a1495cSBarry Smith    Output Parameters:
259a7a1495cSBarry Smith +  A - Jacobian matrix
260a7a1495cSBarry Smith .  B - optional preconditioning matrix
261a7a1495cSBarry Smith -  flag - flag indicating matrix structure
262a7a1495cSBarry Smith 
263a7a1495cSBarry Smith    Notes:
264a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
265a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
266a7a1495cSBarry Smith 
26794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
268a7a1495cSBarry Smith    flag parameter.
269a7a1495cSBarry Smith 
270a7a1495cSBarry Smith    Level: developer
271a7a1495cSBarry Smith 
272a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
273a7a1495cSBarry Smith 
27494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
275a7a1495cSBarry Smith @*/
2760910c330SBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg)
277a7a1495cSBarry Smith {
278dfbe8321SBarry Smith   PetscErrorCode ierr;
2790910c330SBarry Smith   PetscInt       Ustate;
28024989b8cSPeter Brune   DM             dm;
28124989b8cSPeter Brune   TSDM           tsdm;
28224989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
28324989b8cSPeter Brune   void           *ctx;
28424989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
285a7a1495cSBarry Smith 
286a7a1495cSBarry Smith   PetscFunctionBegin;
2870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2890910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
29024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29124989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
29224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
29324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2940910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
2950910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
2960e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2970e4ef248SJed Brown     PetscFunctionReturn(0);
298f8ede8e7SJed Brown   }
299d90be118SSean Farley 
30024989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
301d90be118SSean Farley 
30224989b8cSPeter Brune   if (rhsjacobianfunc) {
3030910c330SBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
304a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
305a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
3060910c330SBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,flg,ctx);CHKERRQ(ierr);
307a7a1495cSBarry Smith     PetscStackPop;
3080910c330SBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
309a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
3100700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
3110700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
312ef66eb69SBarry Smith   } else {
313214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
314214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
315214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
316ef66eb69SBarry Smith   }
3170e4ef248SJed Brown   ts->rhsjacobian.time = t;
3180910c330SBarry Smith   ts->rhsjacobian.X = U;
3190910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
3200e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
321a7a1495cSBarry Smith   PetscFunctionReturn(0);
322a7a1495cSBarry Smith }
323a7a1495cSBarry Smith 
3244a2ae208SSatish Balay #undef __FUNCT__
3254a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
326316643e7SJed Brown /*@
327d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
328d763cef2SBarry Smith 
329316643e7SJed Brown    Collective on TS and Vec
330316643e7SJed Brown 
331316643e7SJed Brown    Input Parameters:
332316643e7SJed Brown +  ts - the TS context
333316643e7SJed Brown .  t - current time
3340910c330SBarry Smith -  U - state vector
335316643e7SJed Brown 
336316643e7SJed Brown    Output Parameter:
337316643e7SJed Brown .  y - right hand side
338316643e7SJed Brown 
339316643e7SJed Brown    Note:
340316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
341316643e7SJed Brown    is used internally within the nonlinear solvers.
342316643e7SJed Brown 
343316643e7SJed Brown    Level: developer
344316643e7SJed Brown 
345316643e7SJed Brown .keywords: TS, compute
346316643e7SJed Brown 
347316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
348316643e7SJed Brown @*/
3490910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
350d763cef2SBarry Smith {
351dfbe8321SBarry Smith   PetscErrorCode ierr;
35224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
35324989b8cSPeter Brune   TSIFunction    ifunction;
35424989b8cSPeter Brune   void           *ctx;
35524989b8cSPeter Brune   DM             dm;
35624989b8cSPeter Brune 
357d763cef2SBarry Smith   PetscFunctionBegin;
3580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3590910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3600700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
36124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
36224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
36324989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
364d763cef2SBarry Smith 
36524989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
366d763cef2SBarry Smith 
3670910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
36824989b8cSPeter Brune   if (rhsfunction) {
369d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
3700910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
371d763cef2SBarry Smith     PetscStackPop;
372214bc6a2SJed Brown   } else {
373214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
374b2cd27e8SJed Brown   }
37544a41b28SSean Farley 
3760910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
377d763cef2SBarry Smith   PetscFunctionReturn(0);
378d763cef2SBarry Smith }
379d763cef2SBarry Smith 
3804a2ae208SSatish Balay #undef __FUNCT__
381ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
382ef20d060SBarry Smith /*@
383ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
384ef20d060SBarry Smith 
385ef20d060SBarry Smith    Collective on TS and Vec
386ef20d060SBarry Smith 
387ef20d060SBarry Smith    Input Parameters:
388ef20d060SBarry Smith +  ts - the TS context
389ef20d060SBarry Smith -  t - current time
390ef20d060SBarry Smith 
391ef20d060SBarry Smith    Output Parameter:
3920910c330SBarry Smith .  U - the solution
393ef20d060SBarry Smith 
394ef20d060SBarry Smith    Note:
395ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
396ef20d060SBarry Smith    is used internally within the nonlinear solvers.
397ef20d060SBarry Smith 
398ef20d060SBarry Smith    Level: developer
399ef20d060SBarry Smith 
400ef20d060SBarry Smith .keywords: TS, compute
401ef20d060SBarry Smith 
402abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
403ef20d060SBarry Smith @*/
4040910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
405ef20d060SBarry Smith {
406ef20d060SBarry Smith   PetscErrorCode     ierr;
407ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
408ef20d060SBarry Smith   void               *ctx;
409ef20d060SBarry Smith   DM                 dm;
410ef20d060SBarry Smith 
411ef20d060SBarry Smith   PetscFunctionBegin;
412ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
414ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
415ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
416ef20d060SBarry Smith 
417ef20d060SBarry Smith   if (solutionfunction) {
418ef20d060SBarry Smith     PetscStackPush("TS user right-hand-side function");
4190910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
420ef20d060SBarry Smith     PetscStackPop;
421ef20d060SBarry Smith   }
422ef20d060SBarry Smith   PetscFunctionReturn(0);
423ef20d060SBarry Smith }
424ef20d060SBarry Smith 
425ef20d060SBarry Smith #undef __FUNCT__
426214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
427214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
428214bc6a2SJed Brown {
4292dd45cf8SJed Brown   Vec            F;
430214bc6a2SJed Brown   PetscErrorCode ierr;
431214bc6a2SJed Brown 
432214bc6a2SJed Brown   PetscFunctionBegin;
4330da9a4f0SJed Brown   *Frhs = PETSC_NULL;
4342dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
435214bc6a2SJed Brown   if (!ts->Frhs) {
4362dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
437214bc6a2SJed Brown   }
438214bc6a2SJed Brown   *Frhs = ts->Frhs;
439214bc6a2SJed Brown   PetscFunctionReturn(0);
440214bc6a2SJed Brown }
441214bc6a2SJed Brown 
442214bc6a2SJed Brown #undef __FUNCT__
443214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
444214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
445214bc6a2SJed Brown {
446214bc6a2SJed Brown   Mat            A,B;
4472dd45cf8SJed Brown   PetscErrorCode ierr;
448214bc6a2SJed Brown 
449214bc6a2SJed Brown   PetscFunctionBegin;
450214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
451214bc6a2SJed Brown   if (Arhs) {
452214bc6a2SJed Brown     if (!ts->Arhs) {
453214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
454214bc6a2SJed Brown     }
455214bc6a2SJed Brown     *Arhs = ts->Arhs;
456214bc6a2SJed Brown   }
457214bc6a2SJed Brown   if (Brhs) {
458214bc6a2SJed Brown     if (!ts->Brhs) {
459214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
460214bc6a2SJed Brown     }
461214bc6a2SJed Brown     *Brhs = ts->Brhs;
462214bc6a2SJed Brown   }
463214bc6a2SJed Brown   PetscFunctionReturn(0);
464214bc6a2SJed Brown }
465214bc6a2SJed Brown 
466214bc6a2SJed Brown #undef __FUNCT__
467316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
468316643e7SJed Brown /*@
4690910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
470316643e7SJed Brown 
471316643e7SJed Brown    Collective on TS and Vec
472316643e7SJed Brown 
473316643e7SJed Brown    Input Parameters:
474316643e7SJed Brown +  ts - the TS context
475316643e7SJed Brown .  t - current time
4760910c330SBarry Smith .  U - state vector
4770910c330SBarry Smith .  Udot - time derivative of state vector
478214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
479316643e7SJed Brown 
480316643e7SJed Brown    Output Parameter:
481316643e7SJed Brown .  Y - right hand side
482316643e7SJed Brown 
483316643e7SJed Brown    Note:
484316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
485316643e7SJed Brown    is used internally within the nonlinear solvers.
486316643e7SJed Brown 
487316643e7SJed Brown    If the user did did not write their equations in implicit form, this
488316643e7SJed Brown    function recasts them in implicit form.
489316643e7SJed Brown 
490316643e7SJed Brown    Level: developer
491316643e7SJed Brown 
492316643e7SJed Brown .keywords: TS, compute
493316643e7SJed Brown 
494316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
495316643e7SJed Brown @*/
4960910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
497316643e7SJed Brown {
498316643e7SJed Brown   PetscErrorCode ierr;
49924989b8cSPeter Brune   TSIFunction    ifunction;
50024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
50124989b8cSPeter Brune   void           *ctx;
50224989b8cSPeter Brune   DM             dm;
503316643e7SJed Brown 
504316643e7SJed Brown   PetscFunctionBegin;
5050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5070910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
5080700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
509316643e7SJed Brown 
51024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
51124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
51224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
51324989b8cSPeter Brune 
51424989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
515d90be118SSean Farley 
5160910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
51724989b8cSPeter Brune   if (ifunction) {
518316643e7SJed Brown     PetscStackPush("TS user implicit function");
5190910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
520316643e7SJed Brown     PetscStackPop;
521214bc6a2SJed Brown   }
522214bc6a2SJed Brown   if (imex) {
52324989b8cSPeter Brune     if (!ifunction) {
5240910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
5252dd45cf8SJed Brown     }
52624989b8cSPeter Brune   } else if (rhsfunction) {
52724989b8cSPeter Brune     if (ifunction) {
528214bc6a2SJed Brown       Vec Frhs;
529214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
5300910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
531214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
5322dd45cf8SJed Brown     } else {
5330910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
5340910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
535316643e7SJed Brown     }
5364a6899ffSJed Brown   }
5370910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
538316643e7SJed Brown   PetscFunctionReturn(0);
539316643e7SJed Brown }
540316643e7SJed Brown 
541316643e7SJed Brown #undef __FUNCT__
542316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
543316643e7SJed Brown /*@
544316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
545316643e7SJed Brown 
546316643e7SJed Brown    Collective on TS and Vec
547316643e7SJed Brown 
548316643e7SJed Brown    Input
549316643e7SJed Brown       Input Parameters:
550316643e7SJed Brown +  ts - the TS context
551316643e7SJed Brown .  t - current timestep
5520910c330SBarry Smith .  U - state vector
5530910c330SBarry Smith .  Udot - time derivative of state vector
554214bc6a2SJed Brown .  shift - shift to apply, see note below
555214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
556316643e7SJed Brown 
557316643e7SJed Brown    Output Parameters:
558316643e7SJed Brown +  A - Jacobian matrix
559316643e7SJed Brown .  B - optional preconditioning matrix
560316643e7SJed Brown -  flag - flag indicating matrix structure
561316643e7SJed Brown 
562316643e7SJed Brown    Notes:
5630910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
564316643e7SJed Brown 
5650910c330SBarry Smith    dF/dU + shift*dF/dUdot
566316643e7SJed Brown 
567316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
568316643e7SJed Brown    is used internally within the nonlinear solvers.
569316643e7SJed Brown 
570316643e7SJed Brown    Level: developer
571316643e7SJed Brown 
572316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
573316643e7SJed Brown 
574316643e7SJed Brown .seealso:  TSSetIJacobian()
57563495f91SJed Brown @*/
5760910c330SBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
577316643e7SJed Brown {
5780910c330SBarry Smith   PetscInt       Ustate, Udotstate;
579316643e7SJed Brown   PetscErrorCode ierr;
58024989b8cSPeter Brune   TSIJacobian    ijacobian;
58124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
58224989b8cSPeter Brune   DM             dm;
58324989b8cSPeter Brune   void           *ctx;
584316643e7SJed Brown 
585316643e7SJed Brown   PetscFunctionBegin;
58624989b8cSPeter Brune 
5870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5880910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5890910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
590316643e7SJed Brown   PetscValidPointer(A,6);
5910700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
592316643e7SJed Brown   PetscValidPointer(B,7);
5930700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
594316643e7SJed Brown   PetscValidPointer(flg,8);
59524989b8cSPeter Brune 
59624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
59724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
59824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
59924989b8cSPeter Brune 
6000910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&Ustate);CHKERRQ(ierr);
6010910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&Udotstate);CHKERRQ(ierr);
6020910c330SBarry 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))) {
6030026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
6040026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
6050026cea9SSean Farley     PetscFunctionReturn(0);
6060026cea9SSean Farley   }
607316643e7SJed Brown 
60824989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
609316643e7SJed Brown 
6104e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
6110910c330SBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
61224989b8cSPeter Brune   if (ijacobian) {
6132dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
614316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
6150910c330SBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,flg,ctx);CHKERRQ(ierr);
616316643e7SJed Brown     PetscStackPop;
617214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
618214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
619214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
6204a6899ffSJed Brown   }
621214bc6a2SJed Brown   if (imex) {
622b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
623214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
6242dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
625214bc6a2SJed Brown       if (*A != *B) {
626214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
627214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
628214bc6a2SJed Brown       }
629214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
630214bc6a2SJed Brown     }
631214bc6a2SJed Brown   } else {
63224989b8cSPeter Brune     if (!ijacobian) {
6330910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,A,B,flg);CHKERRQ(ierr);
634214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
635214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
636316643e7SJed Brown       if (*A != *B) {
637316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
638316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
639316643e7SJed Brown       }
64024989b8cSPeter Brune     } else if (rhsjacobian) {
641214bc6a2SJed Brown       Mat Arhs,Brhs;
642214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
643214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
6440910c330SBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
645214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
646214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
647214bc6a2SJed Brown       if (*A != *B) {
648214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
649214bc6a2SJed Brown       }
650214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
651214bc6a2SJed Brown     }
652316643e7SJed Brown   }
6530026cea9SSean Farley 
6540026cea9SSean Farley   ts->ijacobian.time = t;
6550910c330SBarry Smith   ts->ijacobian.X = U;
6560910c330SBarry Smith   ts->ijacobian.Xdot = Udot;
6570910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)U,&ts->ijacobian.Xstate);CHKERRQ(ierr);
6580910c330SBarry Smith   ierr = PetscObjectStateQuery((PetscObject)Udot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
6590026cea9SSean Farley   ts->ijacobian.shift = shift;
6600026cea9SSean Farley   ts->ijacobian.imex = imex;
6610026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
6620910c330SBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,*A,*B);CHKERRQ(ierr);
663316643e7SJed Brown   PetscFunctionReturn(0);
664316643e7SJed Brown }
665316643e7SJed Brown 
666316643e7SJed Brown #undef __FUNCT__
6674a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
668d763cef2SBarry Smith /*@C
669d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
670b5abc632SBarry Smith     where U_t = G(t,u).
671d763cef2SBarry Smith 
6723f9fe445SBarry Smith     Logically Collective on TS
673d763cef2SBarry Smith 
674d763cef2SBarry Smith     Input Parameters:
675d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
676ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
677d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
678d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
679d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
680d763cef2SBarry Smith 
681d763cef2SBarry Smith     Calling sequence of func:
68287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
683d763cef2SBarry Smith 
684d763cef2SBarry Smith +   t - current timestep
685d763cef2SBarry Smith .   u - input vector
686d763cef2SBarry Smith .   F - function vector
687d763cef2SBarry Smith -   ctx - [optional] user-defined function context
688d763cef2SBarry Smith 
689d763cef2SBarry Smith     Level: beginner
690d763cef2SBarry Smith 
691d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
692d763cef2SBarry Smith 
693d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
694d763cef2SBarry Smith @*/
695089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
696d763cef2SBarry Smith {
697089b2837SJed Brown   PetscErrorCode ierr;
698089b2837SJed Brown   SNES           snes;
699e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
70024989b8cSPeter Brune   DM             dm;
701d763cef2SBarry Smith 
702089b2837SJed Brown   PetscFunctionBegin;
7030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
704ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
70524989b8cSPeter Brune 
70624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
70724989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
708089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
709e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
710e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
711e856ceecSJed Brown     r = ralloc;
712e856ceecSJed Brown   }
713089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
714e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
715d763cef2SBarry Smith   PetscFunctionReturn(0);
716d763cef2SBarry Smith }
717d763cef2SBarry Smith 
7184a2ae208SSatish Balay #undef __FUNCT__
719ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
720ef20d060SBarry Smith /*@C
721abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
722ef20d060SBarry Smith 
723ef20d060SBarry Smith     Logically Collective on TS
724ef20d060SBarry Smith 
725ef20d060SBarry Smith     Input Parameters:
726ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
727ef20d060SBarry Smith .   f - routine for evaluating the solution
728ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
729ef20d060SBarry Smith           function evaluation routine (may be PETSC_NULL)
730ef20d060SBarry Smith 
731ef20d060SBarry Smith     Calling sequence of func:
732ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
733ef20d060SBarry Smith 
734ef20d060SBarry Smith +   t - current timestep
735ef20d060SBarry Smith .   u - output vector
736ef20d060SBarry Smith -   ctx - [optional] user-defined function context
737ef20d060SBarry Smith 
738abd5a294SJed Brown     Notes:
739abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
740abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
741abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
742abd5a294SJed Brown 
7434f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
744abd5a294SJed Brown 
745ef20d060SBarry Smith     Level: beginner
746ef20d060SBarry Smith 
747ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
748ef20d060SBarry Smith 
749abd5a294SJed Brown .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction()
750ef20d060SBarry Smith @*/
751ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
752ef20d060SBarry Smith {
753ef20d060SBarry Smith   PetscErrorCode ierr;
754ef20d060SBarry Smith   DM             dm;
755ef20d060SBarry Smith 
756ef20d060SBarry Smith   PetscFunctionBegin;
757ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
758ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
759ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
760ef20d060SBarry Smith   PetscFunctionReturn(0);
761ef20d060SBarry Smith }
762ef20d060SBarry Smith 
763ef20d060SBarry Smith #undef __FUNCT__
7644a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
765d763cef2SBarry Smith /*@C
766d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
767b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
768d763cef2SBarry Smith 
7693f9fe445SBarry Smith    Logically Collective on TS
770d763cef2SBarry Smith 
771d763cef2SBarry Smith    Input Parameters:
772d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
773d763cef2SBarry Smith .  A   - Jacobian matrix
774d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
775d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
776d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
777d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
778d763cef2SBarry Smith 
779d763cef2SBarry Smith    Calling sequence of func:
78087828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
781d763cef2SBarry Smith 
782d763cef2SBarry Smith +  t - current timestep
783d763cef2SBarry Smith .  u - input vector
784d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
785d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
786d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
78794b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
788d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
789d763cef2SBarry Smith 
790d763cef2SBarry Smith    Notes:
79194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
792d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
793d763cef2SBarry Smith 
794d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
795d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
79656335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
797d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
798d763cef2SBarry Smith    throughout the global iterations.
799d763cef2SBarry Smith 
800d763cef2SBarry Smith    Level: beginner
801d763cef2SBarry Smith 
802d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
803d763cef2SBarry Smith 
804ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
805d763cef2SBarry Smith 
806d763cef2SBarry Smith @*/
807089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
808d763cef2SBarry Smith {
809277b19d0SLisandro Dalcin   PetscErrorCode ierr;
810089b2837SJed Brown   SNES           snes;
81124989b8cSPeter Brune   DM             dm;
81224989b8cSPeter Brune   TSIJacobian    ijacobian;
813277b19d0SLisandro Dalcin 
814d763cef2SBarry Smith   PetscFunctionBegin;
8150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
816277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
817277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
818277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
819277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
820d763cef2SBarry Smith 
82124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
82324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
82424989b8cSPeter Brune 
825089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
8265f659677SPeter Brune   if (!ijacobian) {
827089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
8280e4ef248SJed Brown   }
8290e4ef248SJed Brown   if (A) {
8300e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8310e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
8320e4ef248SJed Brown     ts->Arhs = A;
8330e4ef248SJed Brown   }
8340e4ef248SJed Brown   if (B) {
8350e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8360e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
8370e4ef248SJed Brown     ts->Brhs = B;
8380e4ef248SJed Brown   }
839d763cef2SBarry Smith   PetscFunctionReturn(0);
840d763cef2SBarry Smith }
841d763cef2SBarry Smith 
842316643e7SJed Brown 
843316643e7SJed Brown #undef __FUNCT__
844316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
845316643e7SJed Brown /*@C
846b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
847316643e7SJed Brown 
8483f9fe445SBarry Smith    Logically Collective on TS
849316643e7SJed Brown 
850316643e7SJed Brown    Input Parameters:
851316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
852ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
853316643e7SJed Brown .  f   - the function evaluation routine
854316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
855316643e7SJed Brown 
856316643e7SJed Brown    Calling sequence of f:
857316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
858316643e7SJed Brown 
859316643e7SJed Brown +  t   - time at step/stage being solved
860316643e7SJed Brown .  u   - state vector
861316643e7SJed Brown .  u_t - time derivative of state vector
862316643e7SJed Brown .  F   - function vector
863316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
864316643e7SJed Brown 
865316643e7SJed Brown    Important:
866d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
867316643e7SJed Brown 
868316643e7SJed Brown    Level: beginner
869316643e7SJed Brown 
870316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
871316643e7SJed Brown 
872d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
873316643e7SJed Brown @*/
874089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
875316643e7SJed Brown {
876089b2837SJed Brown   PetscErrorCode ierr;
877089b2837SJed Brown   SNES           snes;
878e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
87924989b8cSPeter Brune   DM             dm;
880316643e7SJed Brown 
881316643e7SJed Brown   PetscFunctionBegin;
8820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
883ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
88424989b8cSPeter Brune 
88524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
88724989b8cSPeter Brune 
888089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
889e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
890e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
891e856ceecSJed Brown     res = resalloc;
892e856ceecSJed Brown   }
893089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
894e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
89524989b8cSPeter Brune 
896089b2837SJed Brown   PetscFunctionReturn(0);
897089b2837SJed Brown }
898089b2837SJed Brown 
899089b2837SJed Brown #undef __FUNCT__
900089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
901089b2837SJed Brown /*@C
902089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
903089b2837SJed Brown 
904089b2837SJed Brown    Not Collective
905089b2837SJed Brown 
906089b2837SJed Brown    Input Parameter:
907089b2837SJed Brown .  ts - the TS context
908089b2837SJed Brown 
909089b2837SJed Brown    Output Parameter:
910089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
911089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
912089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
913089b2837SJed Brown 
914089b2837SJed Brown    Level: advanced
915089b2837SJed Brown 
916089b2837SJed Brown .keywords: TS, nonlinear, get, function
917089b2837SJed Brown 
918089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
919089b2837SJed Brown @*/
920089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
921089b2837SJed Brown {
922089b2837SJed Brown   PetscErrorCode ierr;
923089b2837SJed Brown   SNES           snes;
92424989b8cSPeter Brune   DM             dm;
925089b2837SJed Brown 
926089b2837SJed Brown   PetscFunctionBegin;
927089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
928089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
929089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
93024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
93124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
932089b2837SJed Brown   PetscFunctionReturn(0);
933089b2837SJed Brown }
934089b2837SJed Brown 
935089b2837SJed Brown #undef __FUNCT__
936089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
937089b2837SJed Brown /*@C
938089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
939089b2837SJed Brown 
940089b2837SJed Brown    Not Collective
941089b2837SJed Brown 
942089b2837SJed Brown    Input Parameter:
943089b2837SJed Brown .  ts - the TS context
944089b2837SJed Brown 
945089b2837SJed Brown    Output Parameter:
946089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
947089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
948089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
949089b2837SJed Brown 
950089b2837SJed Brown    Level: advanced
951089b2837SJed Brown 
952089b2837SJed Brown .keywords: TS, nonlinear, get, function
953089b2837SJed Brown 
954089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
955089b2837SJed Brown @*/
956089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
957089b2837SJed Brown {
958089b2837SJed Brown   PetscErrorCode ierr;
959089b2837SJed Brown   SNES           snes;
96024989b8cSPeter Brune   DM             dm;
961089b2837SJed Brown 
962089b2837SJed Brown   PetscFunctionBegin;
963089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
964089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
965089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
96624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96724989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
968316643e7SJed Brown   PetscFunctionReturn(0);
969316643e7SJed Brown }
970316643e7SJed Brown 
971316643e7SJed Brown #undef __FUNCT__
972316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
973316643e7SJed Brown /*@C
974a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
975a4f0a591SBarry Smith         you provided with TSSetIFunction().
976316643e7SJed Brown 
9773f9fe445SBarry Smith    Logically Collective on TS
978316643e7SJed Brown 
979316643e7SJed Brown    Input Parameters:
980316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
981316643e7SJed Brown .  A   - Jacobian matrix
982316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
983316643e7SJed Brown .  f   - the Jacobian evaluation routine
984316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
985316643e7SJed Brown 
986316643e7SJed Brown    Calling sequence of f:
9871b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
988316643e7SJed Brown 
989316643e7SJed Brown +  t    - time at step/stage being solved
9901b4a444bSJed Brown .  U    - state vector
9911b4a444bSJed Brown .  U_t  - time derivative of state vector
992316643e7SJed Brown .  a    - shift
993b5abc632SBarry Smith .  A    - Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
994316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
995316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
996316643e7SJed Brown           structure (same as flag in KSPSetOperators())
997316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
998316643e7SJed Brown 
999316643e7SJed Brown    Notes:
1000316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
1001316643e7SJed Brown 
1002a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1003b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1004a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1005a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1006a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1007a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1008a4f0a591SBarry Smith 
1009316643e7SJed Brown    Level: beginner
1010316643e7SJed Brown 
1011316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1012316643e7SJed Brown 
1013ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
1014316643e7SJed Brown 
1015316643e7SJed Brown @*/
10167087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
1017316643e7SJed Brown {
1018316643e7SJed Brown   PetscErrorCode ierr;
1019089b2837SJed Brown   SNES           snes;
102024989b8cSPeter Brune   DM             dm;
1021316643e7SJed Brown 
1022316643e7SJed Brown   PetscFunctionBegin;
10230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10240700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
10250700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1026316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
1027316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
102824989b8cSPeter Brune 
102924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
103124989b8cSPeter Brune 
1032089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1033089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1034316643e7SJed Brown   PetscFunctionReturn(0);
1035316643e7SJed Brown }
1036316643e7SJed Brown 
10374a2ae208SSatish Balay #undef __FUNCT__
10384a2ae208SSatish Balay #define __FUNCT__ "TSView"
10397e2c5f70SBarry Smith /*@C
1040d763cef2SBarry Smith     TSView - Prints the TS data structure.
1041d763cef2SBarry Smith 
10424c49b128SBarry Smith     Collective on TS
1043d763cef2SBarry Smith 
1044d763cef2SBarry Smith     Input Parameters:
1045d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1046d763cef2SBarry Smith -   viewer - visualization context
1047d763cef2SBarry Smith 
1048d763cef2SBarry Smith     Options Database Key:
1049d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1050d763cef2SBarry Smith 
1051d763cef2SBarry Smith     Notes:
1052d763cef2SBarry Smith     The available visualization contexts include
1053b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1054b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1055d763cef2SBarry Smith          output where only the first processor opens
1056d763cef2SBarry Smith          the file.  All other processors send their
1057d763cef2SBarry Smith          data to the first processor to print.
1058d763cef2SBarry Smith 
1059d763cef2SBarry Smith     The user can open an alternative visualization context with
1060b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1061d763cef2SBarry Smith 
1062d763cef2SBarry Smith     Level: beginner
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith .keywords: TS, timestep, view
1065d763cef2SBarry Smith 
1066b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1067d763cef2SBarry Smith @*/
10687087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1069d763cef2SBarry Smith {
1070dfbe8321SBarry Smith   PetscErrorCode ierr;
107119fd82e9SBarry Smith   TSType         type;
1072089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
1073d763cef2SBarry Smith 
1074d763cef2SBarry Smith   PetscFunctionBegin;
10750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10763050cee2SBarry Smith   if (!viewer) {
10777adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
10783050cee2SBarry Smith   }
10790700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1080c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1081fd16b177SBarry Smith 
1082251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1083251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
108432077d6dSBarry Smith   if (iascii) {
1085317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
108677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1087a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
1088d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
10895ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1090c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1091d763cef2SBarry Smith     }
10925ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1093193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1094d52bd9f3SBarry Smith     if (ts->ops->view) {
1095d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1096d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1097d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1098d52bd9f3SBarry Smith     }
10990f5bd95cSBarry Smith   } else if (isstring) {
1100a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1101b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
1102d763cef2SBarry Smith   }
1103b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1104251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1105b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1106d763cef2SBarry Smith   PetscFunctionReturn(0);
1107d763cef2SBarry Smith }
1108d763cef2SBarry Smith 
1109d763cef2SBarry Smith 
11104a2ae208SSatish Balay #undef __FUNCT__
11114a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1112b07ff414SBarry Smith /*@
1113d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1114d763cef2SBarry Smith    the timesteppers.
1115d763cef2SBarry Smith 
11163f9fe445SBarry Smith    Logically Collective on TS
1117d763cef2SBarry Smith 
1118d763cef2SBarry Smith    Input Parameters:
1119d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1120d763cef2SBarry Smith -  usrP - optional user context
1121d763cef2SBarry Smith 
1122d763cef2SBarry Smith    Level: intermediate
1123d763cef2SBarry Smith 
1124d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1125d763cef2SBarry Smith 
1126d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1127d763cef2SBarry Smith @*/
11287087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1129d763cef2SBarry Smith {
1130d763cef2SBarry Smith   PetscFunctionBegin;
11310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1132d763cef2SBarry Smith   ts->user = usrP;
1133d763cef2SBarry Smith   PetscFunctionReturn(0);
1134d763cef2SBarry Smith }
1135d763cef2SBarry Smith 
11364a2ae208SSatish Balay #undef __FUNCT__
11374a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1138b07ff414SBarry Smith /*@
1139d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1140d763cef2SBarry Smith     timestepper.
1141d763cef2SBarry Smith 
1142d763cef2SBarry Smith     Not Collective
1143d763cef2SBarry Smith 
1144d763cef2SBarry Smith     Input Parameter:
1145d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1146d763cef2SBarry Smith 
1147d763cef2SBarry Smith     Output Parameter:
1148d763cef2SBarry Smith .   usrP - user context
1149d763cef2SBarry Smith 
1150d763cef2SBarry Smith     Level: intermediate
1151d763cef2SBarry Smith 
1152d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1153d763cef2SBarry Smith 
1154d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1155d763cef2SBarry Smith @*/
1156e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1157d763cef2SBarry Smith {
1158d763cef2SBarry Smith   PetscFunctionBegin;
11590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1160e71120c6SJed Brown   *(void**)usrP = ts->user;
1161d763cef2SBarry Smith   PetscFunctionReturn(0);
1162d763cef2SBarry Smith }
1163d763cef2SBarry Smith 
11644a2ae208SSatish Balay #undef __FUNCT__
11654a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1166d763cef2SBarry Smith /*@
1167b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1168d763cef2SBarry Smith 
1169d763cef2SBarry Smith    Not Collective
1170d763cef2SBarry Smith 
1171d763cef2SBarry Smith    Input Parameter:
1172d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1173d763cef2SBarry Smith 
1174d763cef2SBarry Smith    Output Parameter:
1175b8123daeSJed Brown .  iter - number of steps completed so far
1176d763cef2SBarry Smith 
1177d763cef2SBarry Smith    Level: intermediate
1178d763cef2SBarry Smith 
1179d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1180b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1181d763cef2SBarry Smith @*/
11827087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1183d763cef2SBarry Smith {
1184d763cef2SBarry Smith   PetscFunctionBegin;
11850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11864482741eSBarry Smith   PetscValidIntPointer(iter,2);
1187d763cef2SBarry Smith   *iter = ts->steps;
1188d763cef2SBarry Smith   PetscFunctionReturn(0);
1189d763cef2SBarry Smith }
1190d763cef2SBarry Smith 
11914a2ae208SSatish Balay #undef __FUNCT__
11924a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1193d763cef2SBarry Smith /*@
1194d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1195d763cef2SBarry Smith    as well as the initial time.
1196d763cef2SBarry Smith 
11973f9fe445SBarry Smith    Logically Collective on TS
1198d763cef2SBarry Smith 
1199d763cef2SBarry Smith    Input Parameters:
1200d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1201d763cef2SBarry Smith .  initial_time - the initial time
1202d763cef2SBarry Smith -  time_step - the size of the timestep
1203d763cef2SBarry Smith 
1204d763cef2SBarry Smith    Level: intermediate
1205d763cef2SBarry Smith 
1206d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1207d763cef2SBarry Smith 
1208d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1209d763cef2SBarry Smith @*/
12107087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1211d763cef2SBarry Smith {
1212e144a568SJed Brown   PetscErrorCode ierr;
1213e144a568SJed Brown 
1214d763cef2SBarry Smith   PetscFunctionBegin;
12150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1216e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1217d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1218d763cef2SBarry Smith   PetscFunctionReturn(0);
1219d763cef2SBarry Smith }
1220d763cef2SBarry Smith 
12214a2ae208SSatish Balay #undef __FUNCT__
12224a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1223d763cef2SBarry Smith /*@
1224d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1225d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1226d763cef2SBarry Smith 
12273f9fe445SBarry Smith    Logically Collective on TS
1228d763cef2SBarry Smith 
1229d763cef2SBarry Smith    Input Parameters:
1230d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1231d763cef2SBarry Smith -  time_step - the size of the timestep
1232d763cef2SBarry Smith 
1233d763cef2SBarry Smith    Level: intermediate
1234d763cef2SBarry Smith 
1235d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1236d763cef2SBarry Smith 
1237d763cef2SBarry Smith .keywords: TS, set, timestep
1238d763cef2SBarry Smith @*/
12397087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1240d763cef2SBarry Smith {
1241d763cef2SBarry Smith   PetscFunctionBegin;
12420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1243c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1244d763cef2SBarry Smith   ts->time_step = time_step;
124531748224SBarry Smith   ts->time_step_orig = time_step;
1246d763cef2SBarry Smith   PetscFunctionReturn(0);
1247d763cef2SBarry Smith }
1248d763cef2SBarry Smith 
12494a2ae208SSatish Balay #undef __FUNCT__
1250a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1251a43b19c4SJed Brown /*@
1252a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1253a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1254a43b19c4SJed Brown       it computed.
1255a43b19c4SJed Brown 
1256a43b19c4SJed Brown   Logically Collective on TS
1257a43b19c4SJed Brown 
1258a43b19c4SJed Brown    Input Parameter:
1259a43b19c4SJed Brown +   ts - the time-step context
1260a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1261a43b19c4SJed Brown 
1262a43b19c4SJed Brown    Level: beginner
1263a43b19c4SJed Brown 
1264a43b19c4SJed Brown .seealso: TSSetDuration()
1265a43b19c4SJed Brown @*/
1266a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1267a43b19c4SJed Brown {
1268a43b19c4SJed Brown   PetscFunctionBegin;
1269a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1270d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1271a43b19c4SJed Brown   ts->exact_final_time = flg;
1272a43b19c4SJed Brown   PetscFunctionReturn(0);
1273a43b19c4SJed Brown }
1274a43b19c4SJed Brown 
1275a43b19c4SJed Brown #undef __FUNCT__
12764a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1277d763cef2SBarry Smith /*@
1278d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1279d763cef2SBarry Smith 
1280d763cef2SBarry Smith    Not Collective
1281d763cef2SBarry Smith 
1282d763cef2SBarry Smith    Input Parameter:
1283d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1284d763cef2SBarry Smith 
1285d763cef2SBarry Smith    Output Parameter:
1286d763cef2SBarry Smith .  dt - the current timestep size
1287d763cef2SBarry Smith 
1288d763cef2SBarry Smith    Level: intermediate
1289d763cef2SBarry Smith 
1290d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1291d763cef2SBarry Smith 
1292d763cef2SBarry Smith .keywords: TS, get, timestep
1293d763cef2SBarry Smith @*/
12947087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1295d763cef2SBarry Smith {
1296d763cef2SBarry Smith   PetscFunctionBegin;
12970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1298f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1299d763cef2SBarry Smith   *dt = ts->time_step;
1300d763cef2SBarry Smith   PetscFunctionReturn(0);
1301d763cef2SBarry Smith }
1302d763cef2SBarry Smith 
13034a2ae208SSatish Balay #undef __FUNCT__
13044a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1305d8e5e3e6SSatish Balay /*@
1306d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1307d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1308d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1309d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1310d763cef2SBarry Smith 
1311d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1312d763cef2SBarry Smith 
1313d763cef2SBarry Smith    Input Parameter:
1314d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1315d763cef2SBarry Smith 
1316d763cef2SBarry Smith    Output Parameter:
1317d763cef2SBarry Smith .  v - the vector containing the solution
1318d763cef2SBarry Smith 
1319d763cef2SBarry Smith    Level: intermediate
1320d763cef2SBarry Smith 
1321d763cef2SBarry Smith .seealso: TSGetTimeStep()
1322d763cef2SBarry Smith 
1323d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1324d763cef2SBarry Smith @*/
13257087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1326d763cef2SBarry Smith {
1327d763cef2SBarry Smith   PetscFunctionBegin;
13280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13294482741eSBarry Smith   PetscValidPointer(v,2);
13308737fe31SLisandro Dalcin   *v = ts->vec_sol;
1331d763cef2SBarry Smith   PetscFunctionReturn(0);
1332d763cef2SBarry Smith }
1333d763cef2SBarry Smith 
1334bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
13354a2ae208SSatish Balay #undef __FUNCT__
1336bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1337d8e5e3e6SSatish Balay /*@
1338bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1339d763cef2SBarry Smith 
1340bdad233fSMatthew Knepley   Not collective
1341d763cef2SBarry Smith 
1342bdad233fSMatthew Knepley   Input Parameters:
1343bdad233fSMatthew Knepley + ts   - The TS
1344bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1345d763cef2SBarry Smith .vb
13460910c330SBarry Smith          U_t - A U = 0      (linear)
13470910c330SBarry Smith          U_t - A(t) U = 0   (linear)
13480910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1349d763cef2SBarry Smith .ve
1350d763cef2SBarry Smith 
1351d763cef2SBarry Smith    Level: beginner
1352d763cef2SBarry Smith 
1353bdad233fSMatthew Knepley .keywords: TS, problem type
1354bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1355d763cef2SBarry Smith @*/
13567087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1357a7cc72afSBarry Smith {
13589e2a6581SJed Brown   PetscErrorCode ierr;
13599e2a6581SJed Brown 
1360d763cef2SBarry Smith   PetscFunctionBegin;
13610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1362bdad233fSMatthew Knepley   ts->problem_type = type;
13639e2a6581SJed Brown   if (type == TS_LINEAR) {
13649e2a6581SJed Brown     SNES snes;
13659e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13669e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
13679e2a6581SJed Brown   }
1368d763cef2SBarry Smith   PetscFunctionReturn(0);
1369d763cef2SBarry Smith }
1370d763cef2SBarry Smith 
1371bdad233fSMatthew Knepley #undef __FUNCT__
1372bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1373bdad233fSMatthew Knepley /*@C
1374bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1375bdad233fSMatthew Knepley 
1376bdad233fSMatthew Knepley   Not collective
1377bdad233fSMatthew Knepley 
1378bdad233fSMatthew Knepley   Input Parameter:
1379bdad233fSMatthew Knepley . ts   - The TS
1380bdad233fSMatthew Knepley 
1381bdad233fSMatthew Knepley   Output Parameter:
1382bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1383bdad233fSMatthew Knepley .vb
1384089b2837SJed Brown          M U_t = A U
1385089b2837SJed Brown          M(t) U_t = A(t) U
1386b5abc632SBarry Smith          F(t,U,U_t)
1387bdad233fSMatthew Knepley .ve
1388bdad233fSMatthew Knepley 
1389bdad233fSMatthew Knepley    Level: beginner
1390bdad233fSMatthew Knepley 
1391bdad233fSMatthew Knepley .keywords: TS, problem type
1392bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1393bdad233fSMatthew Knepley @*/
13947087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1395a7cc72afSBarry Smith {
1396bdad233fSMatthew Knepley   PetscFunctionBegin;
13970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
13984482741eSBarry Smith   PetscValidIntPointer(type,2);
1399bdad233fSMatthew Knepley   *type = ts->problem_type;
1400bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1401bdad233fSMatthew Knepley }
1402d763cef2SBarry Smith 
14034a2ae208SSatish Balay #undef __FUNCT__
14044a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1405d763cef2SBarry Smith /*@
1406d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1407d763cef2SBarry Smith    of a timestepper.
1408d763cef2SBarry Smith 
1409d763cef2SBarry Smith    Collective on TS
1410d763cef2SBarry Smith 
1411d763cef2SBarry Smith    Input Parameter:
1412d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1413d763cef2SBarry Smith 
1414d763cef2SBarry Smith    Notes:
1415d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1416d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1417d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1418d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1419d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith    Level: advanced
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith .keywords: TS, timestep, setup
1424d763cef2SBarry Smith 
1425d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1426d763cef2SBarry Smith @*/
14277087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1428d763cef2SBarry Smith {
1429dfbe8321SBarry Smith   PetscErrorCode ierr;
14306c6b9e74SPeter Brune   DM             dm;
14316c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
14326c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
14336c6b9e74SPeter Brune   TSIJacobian    ijac;
14346c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1435d763cef2SBarry Smith 
1436d763cef2SBarry Smith   PetscFunctionBegin;
14370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1438277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1439277b19d0SLisandro Dalcin 
14407adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
14419596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1442d763cef2SBarry Smith   }
1443a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1444277b19d0SLisandro Dalcin 
1445277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1446277b19d0SLisandro Dalcin 
14471c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
14481c3436cfSJed Brown 
1449277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1450000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1451277b19d0SLisandro Dalcin   }
1452277b19d0SLisandro Dalcin 
14536c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
14546c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
14556c6b9e74SPeter Brune    */
14566c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
14576c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
14586c6b9e74SPeter Brune   if (!func) {
14596c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
14606c6b9e74SPeter Brune   }
14616c6b9e74SPeter 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.
14626c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
14636c6b9e74SPeter Brune    */
14646c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
14656c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
14666c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
14676c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
14686c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
14696c6b9e74SPeter Brune   }
1470277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1471277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1472277b19d0SLisandro Dalcin }
1473277b19d0SLisandro Dalcin 
1474277b19d0SLisandro Dalcin #undef __FUNCT__
1475277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1476277b19d0SLisandro Dalcin /*@
1477277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1478277b19d0SLisandro Dalcin 
1479277b19d0SLisandro Dalcin    Collective on TS
1480277b19d0SLisandro Dalcin 
1481277b19d0SLisandro Dalcin    Input Parameter:
1482277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1483277b19d0SLisandro Dalcin 
1484277b19d0SLisandro Dalcin    Level: beginner
1485277b19d0SLisandro Dalcin 
1486277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1487277b19d0SLisandro Dalcin 
1488277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1489277b19d0SLisandro Dalcin @*/
1490277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1491277b19d0SLisandro Dalcin {
1492277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1493277b19d0SLisandro Dalcin 
1494277b19d0SLisandro Dalcin   PetscFunctionBegin;
1495277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1496277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1497277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1498277b19d0SLisandro Dalcin   }
1499277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
15004e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
15014e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1502214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
15036bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1504e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1505e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
150638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1507277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1508d763cef2SBarry Smith   PetscFunctionReturn(0);
1509d763cef2SBarry Smith }
1510d763cef2SBarry Smith 
15114a2ae208SSatish Balay #undef __FUNCT__
15124a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1513d8e5e3e6SSatish Balay /*@
1514d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1515d763cef2SBarry Smith    with TSCreate().
1516d763cef2SBarry Smith 
1517d763cef2SBarry Smith    Collective on TS
1518d763cef2SBarry Smith 
1519d763cef2SBarry Smith    Input Parameter:
1520d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1521d763cef2SBarry Smith 
1522d763cef2SBarry Smith    Level: beginner
1523d763cef2SBarry Smith 
1524d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1525d763cef2SBarry Smith 
1526d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1527d763cef2SBarry Smith @*/
15286bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1529d763cef2SBarry Smith {
15306849ba73SBarry Smith   PetscErrorCode ierr;
1531d763cef2SBarry Smith 
1532d763cef2SBarry Smith   PetscFunctionBegin;
15336bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
15346bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
15356bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1536d763cef2SBarry Smith 
15376bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1538277b19d0SLisandro Dalcin 
1539be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15406bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
15416bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
15426d4c513bSLisandro Dalcin 
154384df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
15446bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
15456bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
15466bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
15476d4c513bSLisandro Dalcin 
1548a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1549d763cef2SBarry Smith   PetscFunctionReturn(0);
1550d763cef2SBarry Smith }
1551d763cef2SBarry Smith 
15524a2ae208SSatish Balay #undef __FUNCT__
15534a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1554d8e5e3e6SSatish Balay /*@
1555d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1556d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1557d763cef2SBarry Smith 
1558d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1559d763cef2SBarry Smith 
1560d763cef2SBarry Smith    Input Parameter:
1561d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1562d763cef2SBarry Smith 
1563d763cef2SBarry Smith    Output Parameter:
1564d763cef2SBarry Smith .  snes - the nonlinear solver context
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith    Notes:
1567d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1568d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
156994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1570d763cef2SBarry Smith 
1571d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1572d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1573d763cef2SBarry Smith 
1574d763cef2SBarry Smith    Level: beginner
1575d763cef2SBarry Smith 
1576d763cef2SBarry Smith .keywords: timestep, get, SNES
1577d763cef2SBarry Smith @*/
15787087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1579d763cef2SBarry Smith {
1580d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1581d372ba47SLisandro Dalcin 
1582d763cef2SBarry Smith   PetscFunctionBegin;
15830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15844482741eSBarry Smith   PetscValidPointer(snes,2);
1585d372ba47SLisandro Dalcin   if (!ts->snes) {
1586d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
15876c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1588d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1589d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1590496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
15919e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
15929e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
15939e2a6581SJed Brown     }
1594d372ba47SLisandro Dalcin   }
1595d763cef2SBarry Smith   *snes = ts->snes;
1596d763cef2SBarry Smith   PetscFunctionReturn(0);
1597d763cef2SBarry Smith }
1598d763cef2SBarry Smith 
15994a2ae208SSatish Balay #undef __FUNCT__
160094b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1601d8e5e3e6SSatish Balay /*@
160294b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1603d763cef2SBarry Smith    a TS (timestepper) context.
1604d763cef2SBarry Smith 
160594b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1606d763cef2SBarry Smith 
1607d763cef2SBarry Smith    Input Parameter:
1608d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1609d763cef2SBarry Smith 
1610d763cef2SBarry Smith    Output Parameter:
161194b7f48cSBarry Smith .  ksp - the nonlinear solver context
1612d763cef2SBarry Smith 
1613d763cef2SBarry Smith    Notes:
161494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1615d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1616d763cef2SBarry Smith    KSP and PC contexts as well.
1617d763cef2SBarry Smith 
161894b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
161994b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1620d763cef2SBarry Smith 
1621d763cef2SBarry Smith    Level: beginner
1622d763cef2SBarry Smith 
162394b7f48cSBarry Smith .keywords: timestep, get, KSP
1624d763cef2SBarry Smith @*/
16257087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1626d763cef2SBarry Smith {
1627d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1628089b2837SJed Brown   SNES           snes;
1629d372ba47SLisandro Dalcin 
1630d763cef2SBarry Smith   PetscFunctionBegin;
16310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16324482741eSBarry Smith   PetscValidPointer(ksp,2);
163317186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1634e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1635089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1636089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1637d763cef2SBarry Smith   PetscFunctionReturn(0);
1638d763cef2SBarry Smith }
1639d763cef2SBarry Smith 
1640d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1641d763cef2SBarry Smith 
16424a2ae208SSatish Balay #undef __FUNCT__
1643adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1644adb62b0dSMatthew Knepley /*@
1645adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1646adb62b0dSMatthew Knepley    maximum time for iteration.
1647adb62b0dSMatthew Knepley 
16483f9fe445SBarry Smith    Not Collective
1649adb62b0dSMatthew Knepley 
1650adb62b0dSMatthew Knepley    Input Parameters:
1651adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1652adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1653adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1654adb62b0dSMatthew Knepley 
1655adb62b0dSMatthew Knepley    Level: intermediate
1656adb62b0dSMatthew Knepley 
1657adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1658adb62b0dSMatthew Knepley @*/
16597087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1660adb62b0dSMatthew Knepley {
1661adb62b0dSMatthew Knepley   PetscFunctionBegin;
16620700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1663abc0a331SBarry Smith   if (maxsteps) {
16644482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1665adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1666adb62b0dSMatthew Knepley   }
1667abc0a331SBarry Smith   if (maxtime) {
16684482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1669adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1670adb62b0dSMatthew Knepley   }
1671adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1672adb62b0dSMatthew Knepley }
1673adb62b0dSMatthew Knepley 
1674adb62b0dSMatthew Knepley #undef __FUNCT__
16754a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1676d763cef2SBarry Smith /*@
1677d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1678d763cef2SBarry Smith    maximum time for iteration.
1679d763cef2SBarry Smith 
16803f9fe445SBarry Smith    Logically Collective on TS
1681d763cef2SBarry Smith 
1682d763cef2SBarry Smith    Input Parameters:
1683d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1684d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1685d763cef2SBarry Smith -  maxtime - final time to iterate to
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Options Database Keys:
1688d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
16893bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1690d763cef2SBarry Smith 
1691d763cef2SBarry Smith    Notes:
1692d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1693d763cef2SBarry Smith 
1694d763cef2SBarry Smith    Level: intermediate
1695d763cef2SBarry Smith 
1696d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1697a43b19c4SJed Brown 
1698a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1699d763cef2SBarry Smith @*/
17007087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1701d763cef2SBarry Smith {
1702d763cef2SBarry Smith   PetscFunctionBegin;
17030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1704c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1705c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
170639b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
170739b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1708d763cef2SBarry Smith   PetscFunctionReturn(0);
1709d763cef2SBarry Smith }
1710d763cef2SBarry Smith 
17114a2ae208SSatish Balay #undef __FUNCT__
17124a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1713d763cef2SBarry Smith /*@
1714d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1715d763cef2SBarry Smith    for use by the TS routines.
1716d763cef2SBarry Smith 
17173f9fe445SBarry Smith    Logically Collective on TS and Vec
1718d763cef2SBarry Smith 
1719d763cef2SBarry Smith    Input Parameters:
1720d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
17210910c330SBarry Smith -  u - the solution vector
1722d763cef2SBarry Smith 
1723d763cef2SBarry Smith    Level: beginner
1724d763cef2SBarry Smith 
1725d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1726d763cef2SBarry Smith @*/
17270910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
1728d763cef2SBarry Smith {
17298737fe31SLisandro Dalcin   PetscErrorCode ierr;
1730496e6a7aSJed Brown   DM             dm;
17318737fe31SLisandro Dalcin 
1732d763cef2SBarry Smith   PetscFunctionBegin;
17330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17340910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
17350910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
17366bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
17370910c330SBarry Smith   ts->vec_sol = u;
1738496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
17390910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
1740d763cef2SBarry Smith   PetscFunctionReturn(0);
1741d763cef2SBarry Smith }
1742d763cef2SBarry Smith 
1743e74ef692SMatthew Knepley #undef __FUNCT__
1744e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1745ac226902SBarry Smith /*@C
1746000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
17473f2090d5SJed Brown   called once at the beginning of each time step.
1748000e7ae3SMatthew Knepley 
17493f9fe445SBarry Smith   Logically Collective on TS
1750000e7ae3SMatthew Knepley 
1751000e7ae3SMatthew Knepley   Input Parameters:
1752000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1753000e7ae3SMatthew Knepley - func - The function
1754000e7ae3SMatthew Knepley 
1755000e7ae3SMatthew Knepley   Calling sequence of func:
1756000e7ae3SMatthew Knepley . func (TS ts);
1757000e7ae3SMatthew Knepley 
1758000e7ae3SMatthew Knepley   Level: intermediate
1759000e7ae3SMatthew Knepley 
1760b8123daeSJed Brown   Note:
1761b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1762b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1763b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1764b8123daeSJed Brown 
1765000e7ae3SMatthew Knepley .keywords: TS, timestep
1766b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1767000e7ae3SMatthew Knepley @*/
17687087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1769000e7ae3SMatthew Knepley {
1770000e7ae3SMatthew Knepley   PetscFunctionBegin;
17710700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1772000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1773000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1774000e7ae3SMatthew Knepley }
1775000e7ae3SMatthew Knepley 
1776e74ef692SMatthew Knepley #undef __FUNCT__
17773f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
177809ee8438SJed Brown /*@
17793f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
17803f2090d5SJed Brown 
17813f2090d5SJed Brown   Collective on TS
17823f2090d5SJed Brown 
17833f2090d5SJed Brown   Input Parameters:
17843f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
17853f2090d5SJed Brown 
17863f2090d5SJed Brown   Notes:
17873f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
17883f2090d5SJed Brown   so most users would not generally call this routine themselves.
17893f2090d5SJed Brown 
17903f2090d5SJed Brown   Level: developer
17913f2090d5SJed Brown 
17923f2090d5SJed Brown .keywords: TS, timestep
1793b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
17943f2090d5SJed Brown @*/
17957087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
17963f2090d5SJed Brown {
17973f2090d5SJed Brown   PetscErrorCode ierr;
17983f2090d5SJed Brown 
17993f2090d5SJed Brown   PetscFunctionBegin;
18000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
180172ac3e02SJed Brown   if (ts->ops->prestep) {
18023f2090d5SJed Brown     PetscStackPush("TS PreStep function");
18033f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
18043f2090d5SJed Brown     PetscStackPop;
1805312ce896SJed Brown   }
18063f2090d5SJed Brown   PetscFunctionReturn(0);
18073f2090d5SJed Brown }
18083f2090d5SJed Brown 
18093f2090d5SJed Brown #undef __FUNCT__
1810b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1811b8123daeSJed Brown /*@C
1812b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1813b8123daeSJed Brown   called once at the beginning of each stage.
1814b8123daeSJed Brown 
1815b8123daeSJed Brown   Logically Collective on TS
1816b8123daeSJed Brown 
1817b8123daeSJed Brown   Input Parameters:
1818b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1819b8123daeSJed Brown - func - The function
1820b8123daeSJed Brown 
1821b8123daeSJed Brown   Calling sequence of func:
1822b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1823b8123daeSJed Brown 
1824b8123daeSJed Brown   Level: intermediate
1825b8123daeSJed Brown 
1826b8123daeSJed Brown   Note:
1827b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1828b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1829b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1830b8123daeSJed Brown 
1831b8123daeSJed Brown .keywords: TS, timestep
1832b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1833b8123daeSJed Brown @*/
1834b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1835b8123daeSJed Brown {
1836b8123daeSJed Brown   PetscFunctionBegin;
1837b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1838b8123daeSJed Brown   ts->ops->prestage = func;
1839b8123daeSJed Brown   PetscFunctionReturn(0);
1840b8123daeSJed Brown }
1841b8123daeSJed Brown 
1842b8123daeSJed Brown #undef __FUNCT__
1843b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1844b8123daeSJed Brown /*@
1845b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1846b8123daeSJed Brown 
1847b8123daeSJed Brown   Collective on TS
1848b8123daeSJed Brown 
1849b8123daeSJed Brown   Input Parameters:
1850b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1851b8123daeSJed Brown 
1852b8123daeSJed Brown   Notes:
1853b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1854b8123daeSJed Brown   most users would not generally call this routine themselves.
1855b8123daeSJed Brown 
1856b8123daeSJed Brown   Level: developer
1857b8123daeSJed Brown 
1858b8123daeSJed Brown .keywords: TS, timestep
1859b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1860b8123daeSJed Brown @*/
1861b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1862b8123daeSJed Brown {
1863b8123daeSJed Brown   PetscErrorCode ierr;
1864b8123daeSJed Brown 
1865b8123daeSJed Brown   PetscFunctionBegin;
1866b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1867b8123daeSJed Brown   if (ts->ops->prestage) {
1868b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1869b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1870b8123daeSJed Brown     PetscStackPop;
1871b8123daeSJed Brown   }
1872b8123daeSJed Brown   PetscFunctionReturn(0);
1873b8123daeSJed Brown }
1874b8123daeSJed Brown 
1875b8123daeSJed Brown #undef __FUNCT__
1876e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1877ac226902SBarry Smith /*@C
1878000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
18793f2090d5SJed Brown   called once at the end of each time step.
1880000e7ae3SMatthew Knepley 
18813f9fe445SBarry Smith   Logically Collective on TS
1882000e7ae3SMatthew Knepley 
1883000e7ae3SMatthew Knepley   Input Parameters:
1884000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1885000e7ae3SMatthew Knepley - func - The function
1886000e7ae3SMatthew Knepley 
1887000e7ae3SMatthew Knepley   Calling sequence of func:
1888b8123daeSJed Brown $ func (TS ts);
1889000e7ae3SMatthew Knepley 
1890000e7ae3SMatthew Knepley   Level: intermediate
1891000e7ae3SMatthew Knepley 
1892000e7ae3SMatthew Knepley .keywords: TS, timestep
1893b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1894000e7ae3SMatthew Knepley @*/
18957087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1896000e7ae3SMatthew Knepley {
1897000e7ae3SMatthew Knepley   PetscFunctionBegin;
18980700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1899000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1900000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1901000e7ae3SMatthew Knepley }
1902000e7ae3SMatthew Knepley 
1903e74ef692SMatthew Knepley #undef __FUNCT__
19043f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
190509ee8438SJed Brown /*@
19063f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
19073f2090d5SJed Brown 
19083f2090d5SJed Brown   Collective on TS
19093f2090d5SJed Brown 
19103f2090d5SJed Brown   Input Parameters:
19113f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
19123f2090d5SJed Brown 
19133f2090d5SJed Brown   Notes:
19143f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
19153f2090d5SJed Brown   so most users would not generally call this routine themselves.
19163f2090d5SJed Brown 
19173f2090d5SJed Brown   Level: developer
19183f2090d5SJed Brown 
19193f2090d5SJed Brown .keywords: TS, timestep
19203f2090d5SJed Brown @*/
19217087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
19223f2090d5SJed Brown {
19233f2090d5SJed Brown   PetscErrorCode ierr;
19243f2090d5SJed Brown 
19253f2090d5SJed Brown   PetscFunctionBegin;
19260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
192772ac3e02SJed Brown   if (ts->ops->poststep) {
19283f2090d5SJed Brown     PetscStackPush("TS PostStep function");
19293f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
19303f2090d5SJed Brown     PetscStackPop;
193172ac3e02SJed Brown   }
19323f2090d5SJed Brown   PetscFunctionReturn(0);
19333f2090d5SJed Brown }
19343f2090d5SJed Brown 
1935d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1936d763cef2SBarry Smith 
19374a2ae208SSatish Balay #undef __FUNCT__
1938a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1939d763cef2SBarry Smith /*@C
1940a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1941d763cef2SBarry Smith    timestep to display the iteration's  progress.
1942d763cef2SBarry Smith 
19433f9fe445SBarry Smith    Logically Collective on TS
1944d763cef2SBarry Smith 
1945d763cef2SBarry Smith    Input Parameters:
1946d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1947e213d8f1SJed Brown .  monitor - monitoring routine
1948329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1949b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1950b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1951b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1952d763cef2SBarry Smith 
1953e213d8f1SJed Brown    Calling sequence of monitor:
19540910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
1955d763cef2SBarry Smith 
1956d763cef2SBarry Smith +    ts - the TS context
195788c05cc5SBarry 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
195888c05cc5SBarry Smith                                been interpolated to)
19591f06c33eSBarry Smith .    time - current time
19600910c330SBarry Smith .    u - current iterate
1961d763cef2SBarry Smith -    mctx - [optional] monitoring context
1962d763cef2SBarry Smith 
1963d763cef2SBarry Smith    Notes:
1964d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1965d763cef2SBarry Smith    already has been loaded.
1966d763cef2SBarry Smith 
1967025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1968025f1a04SBarry Smith 
1969d763cef2SBarry Smith    Level: intermediate
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1972d763cef2SBarry Smith 
1973a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1974d763cef2SBarry Smith @*/
1975c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1976d763cef2SBarry Smith {
1977d763cef2SBarry Smith   PetscFunctionBegin;
19780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
197917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1980d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
19818704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]    = mdestroy;
1982d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1983d763cef2SBarry Smith   PetscFunctionReturn(0);
1984d763cef2SBarry Smith }
1985d763cef2SBarry Smith 
19864a2ae208SSatish Balay #undef __FUNCT__
1987a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1988d763cef2SBarry Smith /*@C
1989a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1990d763cef2SBarry Smith 
19913f9fe445SBarry Smith    Logically Collective on TS
1992d763cef2SBarry Smith 
1993d763cef2SBarry Smith    Input Parameters:
1994d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1995d763cef2SBarry Smith 
1996d763cef2SBarry Smith    Notes:
1997d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1998d763cef2SBarry Smith 
1999d763cef2SBarry Smith    Level: intermediate
2000d763cef2SBarry Smith 
2001d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2002d763cef2SBarry Smith 
2003a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2004d763cef2SBarry Smith @*/
20057087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2006d763cef2SBarry Smith {
2007d952e501SBarry Smith   PetscErrorCode ierr;
2008d952e501SBarry Smith   PetscInt       i;
2009d952e501SBarry Smith 
2010d763cef2SBarry Smith   PetscFunctionBegin;
20110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2012d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
20138704b422SBarry Smith     if (ts->monitordestroy[i]) {
20148704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2015d952e501SBarry Smith     }
2016d952e501SBarry Smith   }
2017d763cef2SBarry Smith   ts->numbermonitors = 0;
2018d763cef2SBarry Smith   PetscFunctionReturn(0);
2019d763cef2SBarry Smith }
2020d763cef2SBarry Smith 
20214a2ae208SSatish Balay #undef __FUNCT__
2022a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2023d8e5e3e6SSatish Balay /*@
2024a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
20255516499fSSatish Balay 
20265516499fSSatish Balay    Level: intermediate
202741251cbbSSatish Balay 
20285516499fSSatish Balay .keywords: TS, set, monitor
20295516499fSSatish Balay 
203041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
203141251cbbSSatish Balay @*/
2032649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2033d763cef2SBarry Smith {
2034dfbe8321SBarry Smith   PetscErrorCode ierr;
2035649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
2036d132466eSBarry Smith 
2037d763cef2SBarry Smith   PetscFunctionBegin;
2038649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2039971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2040649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2041d763cef2SBarry Smith   PetscFunctionReturn(0);
2042d763cef2SBarry Smith }
2043d763cef2SBarry Smith 
20444a2ae208SSatish Balay #undef __FUNCT__
2045cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2046cd652676SJed Brown /*@
2047cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2048cd652676SJed Brown 
2049cd652676SJed Brown    Logically Collective on TS
2050cd652676SJed Brown 
2051cd652676SJed Brown    Input Argument:
2052cd652676SJed Brown .  ts - time stepping context
2053cd652676SJed Brown 
2054cd652676SJed Brown    Output Argument:
2055cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2056cd652676SJed Brown 
2057cd652676SJed Brown    Level: intermediate
2058cd652676SJed Brown 
2059cd652676SJed Brown .keywords: TS, set
2060cd652676SJed Brown 
2061cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2062cd652676SJed Brown @*/
2063cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2064cd652676SJed Brown {
2065cd652676SJed Brown   PetscFunctionBegin;
2066cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2067cd652676SJed Brown   ts->retain_stages = flg;
2068cd652676SJed Brown   PetscFunctionReturn(0);
2069cd652676SJed Brown }
2070cd652676SJed Brown 
2071cd652676SJed Brown #undef __FUNCT__
2072cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2073cd652676SJed Brown /*@
2074cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2075cd652676SJed Brown 
2076cd652676SJed Brown    Collective on TS
2077cd652676SJed Brown 
2078cd652676SJed Brown    Input Argument:
2079cd652676SJed Brown +  ts - time stepping context
2080cd652676SJed Brown -  t - time to interpolate to
2081cd652676SJed Brown 
2082cd652676SJed Brown    Output Argument:
20830910c330SBarry Smith .  U - state at given time
2084cd652676SJed Brown 
2085cd652676SJed Brown    Notes:
2086cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2087cd652676SJed Brown 
2088cd652676SJed Brown    Level: intermediate
2089cd652676SJed Brown 
2090cd652676SJed Brown    Developer Notes:
2091cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2092cd652676SJed Brown 
2093cd652676SJed Brown .keywords: TS, set
2094cd652676SJed Brown 
2095cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2096cd652676SJed Brown @*/
20970910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2098cd652676SJed Brown {
2099cd652676SJed Brown   PetscErrorCode ierr;
2100cd652676SJed Brown 
2101cd652676SJed Brown   PetscFunctionBegin;
2102cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2103d8cd7023SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Requested time %G not in last time steps [%G,%G]",t,ts->ptime-ts->time_step_prev,ts->ptime);
2104cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
21050910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2106cd652676SJed Brown   PetscFunctionReturn(0);
2107cd652676SJed Brown }
2108cd652676SJed Brown 
2109cd652676SJed Brown #undef __FUNCT__
21104a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2111d763cef2SBarry Smith /*@
21126d9e5789SSean Farley    TSStep - Steps one time step
2113d763cef2SBarry Smith 
2114d763cef2SBarry Smith    Collective on TS
2115d763cef2SBarry Smith 
2116d763cef2SBarry Smith    Input Parameter:
2117d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2118d763cef2SBarry Smith 
21196d9e5789SSean Farley    Level: intermediate
2120d763cef2SBarry Smith 
2121b8123daeSJed Brown    Notes:
2122b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2123b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2124b8123daeSJed Brown 
212525cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
212625cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
212725cb2221SBarry Smith 
2128d763cef2SBarry Smith .keywords: TS, timestep, solve
2129d763cef2SBarry Smith 
213025cb2221SBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
2131d763cef2SBarry Smith @*/
2132193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2133d763cef2SBarry Smith {
2134362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
2135dfbe8321SBarry Smith   PetscErrorCode ierr;
2136d763cef2SBarry Smith 
2137d763cef2SBarry Smith   PetscFunctionBegin;
21380700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2139d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2140d405a339SMatthew Knepley 
2141362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
2142362cd11cSLisandro Dalcin 
2143362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
2144d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2145193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2146d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2147362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
2148362cd11cSLisandro Dalcin 
2149362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2150cef5090cSJed Brown     if (ts->errorifstepfailed) {
2151cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2152cef5090cSJed Brown         SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
2153cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2154cef5090cSJed Brown     }
2155362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2156362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2157362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2158362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2159362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2160362cd11cSLisandro Dalcin   }
2161362cd11cSLisandro Dalcin 
2162d763cef2SBarry Smith   PetscFunctionReturn(0);
2163d763cef2SBarry Smith }
2164d763cef2SBarry Smith 
21654a2ae208SSatish Balay #undef __FUNCT__
216605175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
216705175c85SJed Brown /*@
216805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
216905175c85SJed Brown 
21701c3436cfSJed Brown    Collective on TS
217105175c85SJed Brown 
217205175c85SJed Brown    Input Arguments:
21731c3436cfSJed Brown +  ts - time stepping context
21741c3436cfSJed Brown .  order - desired order of accuracy
21751c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
217605175c85SJed Brown 
217705175c85SJed Brown    Output Arguments:
21780910c330SBarry Smith .  U - state at the end of the current step
217905175c85SJed Brown 
218005175c85SJed Brown    Level: advanced
218105175c85SJed Brown 
2182108c343cSJed Brown    Notes:
2183108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2184108c343cSJed 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.
2185108c343cSJed Brown 
21861c3436cfSJed Brown .seealso: TSStep(), TSAdapt
218705175c85SJed Brown @*/
21880910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
218905175c85SJed Brown {
219005175c85SJed Brown   PetscErrorCode ierr;
219105175c85SJed Brown 
219205175c85SJed Brown   PetscFunctionBegin;
219305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
219405175c85SJed Brown   PetscValidType(ts,1);
21950910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
21961c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
21970910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
219805175c85SJed Brown   PetscFunctionReturn(0);
219905175c85SJed Brown }
220005175c85SJed Brown 
220105175c85SJed Brown #undef __FUNCT__
22026a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
22036a4d4014SLisandro Dalcin /*@
22046a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
22056a4d4014SLisandro Dalcin 
22066a4d4014SLisandro Dalcin    Collective on TS
22076a4d4014SLisandro Dalcin 
22086a4d4014SLisandro Dalcin    Input Parameter:
22096a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2210*cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
22115a3a76d0SJed Brown 
22126a4d4014SLisandro Dalcin    Level: beginner
22136a4d4014SLisandro Dalcin 
22145a3a76d0SJed Brown    Notes:
22155a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
22165a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
22175a3a76d0SJed Brown    stepped over the final time.
22185a3a76d0SJed Brown 
22196a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
22206a4d4014SLisandro Dalcin 
22216a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
22226a4d4014SLisandro Dalcin @*/
2223*cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
22246a4d4014SLisandro Dalcin {
22254d7d938eSLisandro Dalcin   PetscBool      flg;
22264d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
22274d7d938eSLisandro Dalcin   PetscViewer    viewer;
22286a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2229f22f69f0SBarry Smith 
22306a4d4014SLisandro Dalcin   PetscFunctionBegin;
22310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22320910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22335a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
22340910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
22355a3a76d0SJed Brown       Vec y;
22360910c330SBarry Smith       ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
22375a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
22385a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
22395a3a76d0SJed Brown     }
22400910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
22415a3a76d0SJed Brown   } else {
22420910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
22435a3a76d0SJed Brown   }
2244b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
22456a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2246193ac0bcSJed Brown   ts->steps = 0;
22475ef26d82SJed Brown   ts->ksp_its = 0;
22485ef26d82SJed Brown   ts->snes_its = 0;
2249c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2250c610991cSLisandro Dalcin   ts->reject = 0;
2251193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2252193ac0bcSJed Brown 
2253193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2254193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
22550910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2256*cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2257193ac0bcSJed Brown   } else {
22586a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2259362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2260362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2261362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2262362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2263362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2264e1a7a14fSJed Brown     while (!ts->reason) {
2265193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2266193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2267193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2268193ac0bcSJed Brown     }
2269362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
22700910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2271*cc708dedSBarry Smith       ts->solvetime = ts->max_time;
22720574a7fbSJed Brown     } else {
22730910c330SBarry Smith       ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2274*cc708dedSBarry Smith       ts->solvetime = ts->ptime;
22750574a7fbSJed Brown     }
2276193ac0bcSJed Brown   }
22773923b477SBarry Smith   ierr = TSMonitor(ts,-1,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
22784d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
22794d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
22804d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
22814d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
22824d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2283193ac0bcSJed Brown   }
22846a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
22856a4d4014SLisandro Dalcin }
22866a4d4014SLisandro Dalcin 
22876a4d4014SLisandro Dalcin #undef __FUNCT__
22884a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2289228d79bcSJed Brown /*@
2290228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2291228d79bcSJed Brown 
2292228d79bcSJed Brown    Collective on TS
2293228d79bcSJed Brown 
2294228d79bcSJed Brown    Input Parameters:
2295228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2296228d79bcSJed Brown .  step - step number that has just completed
2297228d79bcSJed Brown .  ptime - model time of the state
22980910c330SBarry Smith -  u - state at the current model time
2299228d79bcSJed Brown 
2300228d79bcSJed Brown    Notes:
2301228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2302228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2303228d79bcSJed Brown 
2304228d79bcSJed Brown    Level: advanced
2305228d79bcSJed Brown 
2306228d79bcSJed Brown .keywords: TS, timestep
2307228d79bcSJed Brown @*/
23080910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2309d763cef2SBarry Smith {
23106849ba73SBarry Smith   PetscErrorCode ierr;
2311a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2312d763cef2SBarry Smith 
2313d763cef2SBarry Smith   PetscFunctionBegin;
2314d763cef2SBarry Smith   for (i=0; i<n; i++) {
23150910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2316d763cef2SBarry Smith   }
2317d763cef2SBarry Smith   PetscFunctionReturn(0);
2318d763cef2SBarry Smith }
2319d763cef2SBarry Smith 
2320d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
23210b039ecaSBarry Smith struct _n_TSMonitorLGCtx {
23220b039ecaSBarry Smith   PetscDrawLG lg;
23230b039ecaSBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
2324201da799SBarry Smith   PetscInt    ksp_its,snes_its;
23250b039ecaSBarry Smith };
23260b039ecaSBarry Smith 
2327d763cef2SBarry Smith 
23284a2ae208SSatish Balay #undef __FUNCT__
2329a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2330d763cef2SBarry Smith /*@C
2331a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2332a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2333d763cef2SBarry Smith 
2334d763cef2SBarry Smith    Collective on TS
2335d763cef2SBarry Smith 
2336d763cef2SBarry Smith    Input Parameters:
2337d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2338d763cef2SBarry Smith .  label - the title to put in the title bar
23397c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2340a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2341a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2342d763cef2SBarry Smith 
2343d763cef2SBarry Smith    Output Parameter:
23440b039ecaSBarry Smith .  ctx - the context
2345d763cef2SBarry Smith 
2346d763cef2SBarry Smith    Options Database Key:
23474f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
23484f09c107SBarry Smith .  -ts_monitor_lg_solution -
234999fdda47SBarry Smith .  -ts_monitor_lg_error -
235099fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
235199fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
235299fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2353d763cef2SBarry Smith 
2354d763cef2SBarry Smith    Notes:
2355a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2356d763cef2SBarry Smith 
2357d763cef2SBarry Smith    Level: intermediate
2358d763cef2SBarry Smith 
23597c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2360d763cef2SBarry Smith 
23614f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
23627c922b88SBarry Smith 
2363d763cef2SBarry Smith @*/
2364a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2365d763cef2SBarry Smith {
2366b0a32e0cSBarry Smith   PetscDraw      win;
2367dfbe8321SBarry Smith   PetscErrorCode ierr;
236899fdda47SBarry Smith   PetscBool      flg = PETSC_TRUE;
2369d763cef2SBarry Smith 
2370d763cef2SBarry Smith   PetscFunctionBegin;
2371201da799SBarry Smith   ierr = PetscNew(struct _n_TSMonitorLGCtx,ctx);CHKERRQ(ierr);
2372a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
23733fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
23740b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
237599fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-lg_indicate_data_points",&flg,PETSC_NULL);CHKERRQ(ierr);
237699fdda47SBarry Smith   if (flg) {
23770b039ecaSBarry Smith     ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg);CHKERRQ(ierr);
237899fdda47SBarry Smith   }
23790b039ecaSBarry Smith   ierr = PetscLogObjectParent((*ctx)->lg,win);CHKERRQ(ierr);
2380a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2381d763cef2SBarry Smith   PetscFunctionReturn(0);
2382d763cef2SBarry Smith }
2383d763cef2SBarry Smith 
23844a2ae208SSatish Balay #undef __FUNCT__
23854f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
23864f09c107SBarry Smith PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2387d763cef2SBarry Smith {
23880b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
2389c32365f1SBarry Smith   PetscReal      x = ptime,y;
2390dfbe8321SBarry Smith   PetscErrorCode ierr;
2391d763cef2SBarry Smith 
2392d763cef2SBarry Smith   PetscFunctionBegin;
2393a9f9c1f6SBarry Smith   if (!n) {
2394a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
2395a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
2396a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
2397a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
2398a9f9c1f6SBarry Smith   }
2399c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
24000b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
240199fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften))) || ((ctx->howoften == -1) && (n == -1))){
24020b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
24033923b477SBarry Smith   }
2404d763cef2SBarry Smith   PetscFunctionReturn(0);
2405d763cef2SBarry Smith }
2406d763cef2SBarry Smith 
24074a2ae208SSatish Balay #undef __FUNCT__
2408a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
2409d763cef2SBarry Smith /*@C
2410a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
2411a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
2412d763cef2SBarry Smith 
24130b039ecaSBarry Smith    Collective on TSMonitorLGCtx
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Input Parameter:
24160b039ecaSBarry Smith .  ctx - the monitor context
2417d763cef2SBarry Smith 
2418d763cef2SBarry Smith    Level: intermediate
2419d763cef2SBarry Smith 
2420d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2421d763cef2SBarry Smith 
24224f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
2423d763cef2SBarry Smith @*/
2424a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
2425d763cef2SBarry Smith {
2426b0a32e0cSBarry Smith   PetscDraw      draw;
2427dfbe8321SBarry Smith   PetscErrorCode ierr;
2428d763cef2SBarry Smith 
2429d763cef2SBarry Smith   PetscFunctionBegin;
24300b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
24316bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
24320b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
24330b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
2434d763cef2SBarry Smith   PetscFunctionReturn(0);
2435d763cef2SBarry Smith }
2436d763cef2SBarry Smith 
24374a2ae208SSatish Balay #undef __FUNCT__
24384a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2439d763cef2SBarry Smith /*@
2440b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2441d763cef2SBarry Smith 
2442d763cef2SBarry Smith    Not Collective
2443d763cef2SBarry Smith 
2444d763cef2SBarry Smith    Input Parameter:
2445d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2446d763cef2SBarry Smith 
2447d763cef2SBarry Smith    Output Parameter:
2448d763cef2SBarry Smith .  t  - the current time
2449d763cef2SBarry Smith 
2450d763cef2SBarry Smith    Level: beginner
2451d763cef2SBarry Smith 
2452b8123daeSJed Brown    Note:
2453b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2454b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2455b8123daeSJed Brown 
2456d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2457d763cef2SBarry Smith 
2458d763cef2SBarry Smith .keywords: TS, get, time
2459d763cef2SBarry Smith @*/
24607087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2461d763cef2SBarry Smith {
2462d763cef2SBarry Smith   PetscFunctionBegin;
24630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2464f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2465d763cef2SBarry Smith   *t = ts->ptime;
2466d763cef2SBarry Smith   PetscFunctionReturn(0);
2467d763cef2SBarry Smith }
2468d763cef2SBarry Smith 
24694a2ae208SSatish Balay #undef __FUNCT__
24706a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
24716a4d4014SLisandro Dalcin /*@
24726a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
24736a4d4014SLisandro Dalcin 
24743f9fe445SBarry Smith    Logically Collective on TS
24756a4d4014SLisandro Dalcin 
24766a4d4014SLisandro Dalcin    Input Parameters:
24776a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
24786a4d4014SLisandro Dalcin -  time - the time
24796a4d4014SLisandro Dalcin 
24806a4d4014SLisandro Dalcin    Level: intermediate
24816a4d4014SLisandro Dalcin 
24826a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
24836a4d4014SLisandro Dalcin 
24846a4d4014SLisandro Dalcin .keywords: TS, set, time
24856a4d4014SLisandro Dalcin @*/
24867087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
24876a4d4014SLisandro Dalcin {
24886a4d4014SLisandro Dalcin   PetscFunctionBegin;
24890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2490c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
24916a4d4014SLisandro Dalcin   ts->ptime = t;
24926a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
24936a4d4014SLisandro Dalcin }
24946a4d4014SLisandro Dalcin 
24956a4d4014SLisandro Dalcin #undef __FUNCT__
24964a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2497d763cef2SBarry Smith /*@C
2498d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2499d763cef2SBarry Smith    TS options in the database.
2500d763cef2SBarry Smith 
25013f9fe445SBarry Smith    Logically Collective on TS
2502d763cef2SBarry Smith 
2503d763cef2SBarry Smith    Input Parameter:
2504d763cef2SBarry Smith +  ts     - The TS context
2505d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2506d763cef2SBarry Smith 
2507d763cef2SBarry Smith    Notes:
2508d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2509d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2510d763cef2SBarry Smith    hyphen.
2511d763cef2SBarry Smith 
2512d763cef2SBarry Smith    Level: advanced
2513d763cef2SBarry Smith 
2514d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2515d763cef2SBarry Smith 
2516d763cef2SBarry Smith .seealso: TSSetFromOptions()
2517d763cef2SBarry Smith 
2518d763cef2SBarry Smith @*/
25197087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2520d763cef2SBarry Smith {
2521dfbe8321SBarry Smith   PetscErrorCode ierr;
2522089b2837SJed Brown   SNES           snes;
2523d763cef2SBarry Smith 
2524d763cef2SBarry Smith   PetscFunctionBegin;
25250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2526d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2527089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2528089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2529d763cef2SBarry Smith   PetscFunctionReturn(0);
2530d763cef2SBarry Smith }
2531d763cef2SBarry Smith 
2532d763cef2SBarry Smith 
25334a2ae208SSatish Balay #undef __FUNCT__
25344a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2535d763cef2SBarry Smith /*@C
2536d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2537d763cef2SBarry Smith    TS options in the database.
2538d763cef2SBarry Smith 
25393f9fe445SBarry Smith    Logically Collective on TS
2540d763cef2SBarry Smith 
2541d763cef2SBarry Smith    Input Parameter:
2542d763cef2SBarry Smith +  ts     - The TS context
2543d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2544d763cef2SBarry Smith 
2545d763cef2SBarry Smith    Notes:
2546d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2547d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2548d763cef2SBarry Smith    hyphen.
2549d763cef2SBarry Smith 
2550d763cef2SBarry Smith    Level: advanced
2551d763cef2SBarry Smith 
2552d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2553d763cef2SBarry Smith 
2554d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2555d763cef2SBarry Smith 
2556d763cef2SBarry Smith @*/
25577087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2558d763cef2SBarry Smith {
2559dfbe8321SBarry Smith   PetscErrorCode ierr;
2560089b2837SJed Brown   SNES           snes;
2561d763cef2SBarry Smith 
2562d763cef2SBarry Smith   PetscFunctionBegin;
25630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2564d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2565089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2566089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2567d763cef2SBarry Smith   PetscFunctionReturn(0);
2568d763cef2SBarry Smith }
2569d763cef2SBarry Smith 
25704a2ae208SSatish Balay #undef __FUNCT__
25714a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2572d763cef2SBarry Smith /*@C
2573d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2574d763cef2SBarry Smith    TS options in the database.
2575d763cef2SBarry Smith 
2576d763cef2SBarry Smith    Not Collective
2577d763cef2SBarry Smith 
2578d763cef2SBarry Smith    Input Parameter:
2579d763cef2SBarry Smith .  ts - The TS context
2580d763cef2SBarry Smith 
2581d763cef2SBarry Smith    Output Parameter:
2582d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2583d763cef2SBarry Smith 
2584d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2585d763cef2SBarry Smith    sufficient length to hold the prefix.
2586d763cef2SBarry Smith 
2587d763cef2SBarry Smith    Level: intermediate
2588d763cef2SBarry Smith 
2589d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2590d763cef2SBarry Smith 
2591d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2592d763cef2SBarry Smith @*/
25937087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2594d763cef2SBarry Smith {
2595dfbe8321SBarry Smith   PetscErrorCode ierr;
2596d763cef2SBarry Smith 
2597d763cef2SBarry Smith   PetscFunctionBegin;
25980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25994482741eSBarry Smith   PetscValidPointer(prefix,2);
2600d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2601d763cef2SBarry Smith   PetscFunctionReturn(0);
2602d763cef2SBarry Smith }
2603d763cef2SBarry Smith 
26044a2ae208SSatish Balay #undef __FUNCT__
26054a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2606d763cef2SBarry Smith /*@C
2607d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2608d763cef2SBarry Smith 
2609d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2610d763cef2SBarry Smith 
2611d763cef2SBarry Smith    Input Parameter:
2612d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2613d763cef2SBarry Smith 
2614d763cef2SBarry Smith    Output Parameters:
2615b5abc632SBarry Smith +  J   - The Jacobian J of F, where U_t = G(U,t)
2616d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2617089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2618d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2619d763cef2SBarry Smith 
2620d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2621d763cef2SBarry Smith 
2622d763cef2SBarry Smith    Level: intermediate
2623d763cef2SBarry Smith 
262426d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2625d763cef2SBarry Smith 
2626d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2627d763cef2SBarry Smith @*/
2628089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2629d763cef2SBarry Smith {
2630089b2837SJed Brown   PetscErrorCode ierr;
2631089b2837SJed Brown   SNES           snes;
263224989b8cSPeter Brune   DM             dm;
2633089b2837SJed Brown 
2634d763cef2SBarry Smith   PetscFunctionBegin;
2635089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2636089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
263724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
263824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2639d763cef2SBarry Smith   PetscFunctionReturn(0);
2640d763cef2SBarry Smith }
2641d763cef2SBarry Smith 
26421713a123SBarry Smith #undef __FUNCT__
26432eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
26442eca1d9cSJed Brown /*@C
26452eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
26462eca1d9cSJed Brown 
26472eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
26482eca1d9cSJed Brown 
26492eca1d9cSJed Brown    Input Parameter:
26502eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
26512eca1d9cSJed Brown 
26522eca1d9cSJed Brown    Output Parameters:
26532eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
26542eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
26552eca1d9cSJed Brown .  f   - The function to compute the matrices
26562eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
26572eca1d9cSJed Brown 
26582eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
26592eca1d9cSJed Brown 
26602eca1d9cSJed Brown    Level: advanced
26612eca1d9cSJed Brown 
26622eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
26632eca1d9cSJed Brown 
26642eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
26652eca1d9cSJed Brown @*/
26667087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
26672eca1d9cSJed Brown {
2668089b2837SJed Brown   PetscErrorCode ierr;
2669089b2837SJed Brown   SNES           snes;
267024989b8cSPeter Brune   DM             dm;
26710910c330SBarry Smith 
26722eca1d9cSJed Brown   PetscFunctionBegin;
2673089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2674089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
267524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
267624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
26772eca1d9cSJed Brown   PetscFunctionReturn(0);
26782eca1d9cSJed Brown }
26792eca1d9cSJed Brown 
268083a4ac43SBarry Smith struct _n_TSMonitorDrawCtx {
26816083293cSBarry Smith   PetscViewer viewer;
26826083293cSBarry Smith   Vec         initialsolution;
26836083293cSBarry Smith   PetscBool   showinitial;
268483a4ac43SBarry Smith   PetscInt    howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
268583a4ac43SBarry Smith };
26866083293cSBarry Smith 
26872eca1d9cSJed Brown #undef __FUNCT__
268883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
26891713a123SBarry Smith /*@C
269083a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
26911713a123SBarry Smith    VecView() for the solution at each timestep
26921713a123SBarry Smith 
26931713a123SBarry Smith    Collective on TS
26941713a123SBarry Smith 
26951713a123SBarry Smith    Input Parameters:
26961713a123SBarry Smith +  ts - the TS context
26971713a123SBarry Smith .  step - current time-step
2698142b95e3SSatish Balay .  ptime - current time
26991713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
27001713a123SBarry Smith 
270199fdda47SBarry Smith    Options Database:
270299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
270399fdda47SBarry Smith 
270499fdda47SBarry 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
270599fdda47SBarry Smith        will look bad
270699fdda47SBarry Smith 
27071713a123SBarry Smith    Level: intermediate
27081713a123SBarry Smith 
27091713a123SBarry Smith .keywords: TS,  vector, monitor, view
27101713a123SBarry Smith 
2711a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
27121713a123SBarry Smith @*/
27130910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
27141713a123SBarry Smith {
2715dfbe8321SBarry Smith   PetscErrorCode   ierr;
271683a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
27171713a123SBarry Smith 
27181713a123SBarry Smith   PetscFunctionBegin;
27196083293cSBarry Smith   if (!step && ictx->showinitial) {
27206083293cSBarry Smith     if (!ictx->initialsolution) {
27210910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
27221713a123SBarry Smith     }
27230910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
27246083293cSBarry Smith   }
27253fbbecb0SBarry Smith   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften)) && (step > -1)) || ((ictx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
27260dcf80beSBarry Smith 
27276083293cSBarry Smith   if (ictx->showinitial) {
27286083293cSBarry Smith     PetscReal pause;
27296083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
27306083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
27316083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
27326083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
27336083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
27346083293cSBarry Smith   }
27350910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
27366083293cSBarry Smith   if (ictx->showinitial) {
27376083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
27386083293cSBarry Smith   }
27391713a123SBarry Smith   PetscFunctionReturn(0);
27401713a123SBarry Smith }
27411713a123SBarry Smith 
27421713a123SBarry Smith 
27436c699258SBarry Smith #undef __FUNCT__
274483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
27456083293cSBarry Smith /*@C
274683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
27476083293cSBarry Smith 
27486083293cSBarry Smith    Collective on TS
27496083293cSBarry Smith 
27506083293cSBarry Smith    Input Parameters:
27516083293cSBarry Smith .    ctx - the monitor context
27526083293cSBarry Smith 
27536083293cSBarry Smith    Level: intermediate
27546083293cSBarry Smith 
27556083293cSBarry Smith .keywords: TS,  vector, monitor, view
27566083293cSBarry Smith 
275783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
27586083293cSBarry Smith @*/
275983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
27606083293cSBarry Smith {
27616083293cSBarry Smith   PetscErrorCode       ierr;
27626083293cSBarry Smith 
27636083293cSBarry Smith   PetscFunctionBegin;
276483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
276583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
276683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
27676083293cSBarry Smith   PetscFunctionReturn(0);
27686083293cSBarry Smith }
27696083293cSBarry Smith 
27706083293cSBarry Smith #undef __FUNCT__
277183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
27726083293cSBarry Smith /*@C
277383a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
27746083293cSBarry Smith 
27756083293cSBarry Smith    Collective on TS
27766083293cSBarry Smith 
27776083293cSBarry Smith    Input Parameter:
27786083293cSBarry Smith .    ts - time-step context
27796083293cSBarry Smith 
27806083293cSBarry Smith    Output Patameter:
27816083293cSBarry Smith .    ctx - the monitor context
27826083293cSBarry Smith 
278399fdda47SBarry Smith    Options Database:
278499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
278599fdda47SBarry Smith 
27866083293cSBarry Smith    Level: intermediate
27876083293cSBarry Smith 
27886083293cSBarry Smith .keywords: TS,  vector, monitor, view
27896083293cSBarry Smith 
279083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
27916083293cSBarry Smith @*/
279283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
27936083293cSBarry Smith {
27946083293cSBarry Smith   PetscErrorCode   ierr;
27956083293cSBarry Smith 
27966083293cSBarry Smith   PetscFunctionBegin;
279783a4ac43SBarry Smith   ierr = PetscNew(struct _n_TSMonitorDrawCtx,ctx);CHKERRQ(ierr);
279883a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
279983a4ac43SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
280083a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
280199fdda47SBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,PETSC_NULL);CHKERRQ(ierr);
28026083293cSBarry Smith   PetscFunctionReturn(0);
28036083293cSBarry Smith }
28046083293cSBarry Smith 
28056083293cSBarry Smith #undef __FUNCT__
280683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
28073a471f94SBarry Smith /*@C
280883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
28093a471f94SBarry Smith    VecView() for the error at each timestep
28103a471f94SBarry Smith 
28113a471f94SBarry Smith    Collective on TS
28123a471f94SBarry Smith 
28133a471f94SBarry Smith    Input Parameters:
28143a471f94SBarry Smith +  ts - the TS context
28153a471f94SBarry Smith .  step - current time-step
28163a471f94SBarry Smith .  ptime - current time
28173a471f94SBarry Smith -  dummy - either a viewer or PETSC_NULL
28183a471f94SBarry Smith 
28193a471f94SBarry Smith    Level: intermediate
28203a471f94SBarry Smith 
28213a471f94SBarry Smith .keywords: TS,  vector, monitor, view
28223a471f94SBarry Smith 
28233a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
28243a471f94SBarry Smith @*/
28250910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
28263a471f94SBarry Smith {
28273a471f94SBarry Smith   PetscErrorCode   ierr;
282883a4ac43SBarry Smith   TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy;
282983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
28303a471f94SBarry Smith   Vec              work;
28313a471f94SBarry Smith 
28323a471f94SBarry Smith   PetscFunctionBegin;
28333fbbecb0SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1)))) PetscFunctionReturn(0);
28340910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
28353a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
28360910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
28373a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
28383a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
28393a471f94SBarry Smith   PetscFunctionReturn(0);
28403a471f94SBarry Smith }
28413a471f94SBarry Smith 
28423a471f94SBarry Smith #undef __FUNCT__
28436c699258SBarry Smith #define __FUNCT__ "TSSetDM"
28446c699258SBarry Smith /*@
28456c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
28466c699258SBarry Smith 
28473f9fe445SBarry Smith    Logically Collective on TS and DM
28486c699258SBarry Smith 
28496c699258SBarry Smith    Input Parameters:
28506c699258SBarry Smith +  ts - the preconditioner context
28516c699258SBarry Smith -  dm - the dm
28526c699258SBarry Smith 
28536c699258SBarry Smith    Level: intermediate
28546c699258SBarry Smith 
28556c699258SBarry Smith 
28566c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
28576c699258SBarry Smith @*/
28587087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
28596c699258SBarry Smith {
28606c699258SBarry Smith   PetscErrorCode ierr;
2861089b2837SJed Brown   SNES           snes;
286224989b8cSPeter Brune   TSDM           tsdm;
28636c699258SBarry Smith 
28646c699258SBarry Smith   PetscFunctionBegin;
28650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
286670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
286724989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
286824989b8cSPeter Brune     PetscContainer oldcontainer,container;
286924989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
287024989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
287124989b8cSPeter Brune     if (oldcontainer && !container) {
287224989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
287324989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
287424989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
287524989b8cSPeter Brune         tsdm->originaldm = dm;
287624989b8cSPeter Brune       }
287724989b8cSPeter Brune     }
28786bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
287924989b8cSPeter Brune   }
28806c699258SBarry Smith   ts->dm = dm;
2881089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2882089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
28836c699258SBarry Smith   PetscFunctionReturn(0);
28846c699258SBarry Smith }
28856c699258SBarry Smith 
28866c699258SBarry Smith #undef __FUNCT__
28876c699258SBarry Smith #define __FUNCT__ "TSGetDM"
28886c699258SBarry Smith /*@
28896c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
28906c699258SBarry Smith 
28913f9fe445SBarry Smith    Not Collective
28926c699258SBarry Smith 
28936c699258SBarry Smith    Input Parameter:
28946c699258SBarry Smith . ts - the preconditioner context
28956c699258SBarry Smith 
28966c699258SBarry Smith    Output Parameter:
28976c699258SBarry Smith .  dm - the dm
28986c699258SBarry Smith 
28996c699258SBarry Smith    Level: intermediate
29006c699258SBarry Smith 
29016c699258SBarry Smith 
29026c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
29036c699258SBarry Smith @*/
29047087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
29056c699258SBarry Smith {
2906496e6a7aSJed Brown   PetscErrorCode ierr;
2907496e6a7aSJed Brown 
29086c699258SBarry Smith   PetscFunctionBegin;
29090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2910496e6a7aSJed Brown   if (!ts->dm) {
2911496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2912496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2913496e6a7aSJed Brown   }
29146c699258SBarry Smith   *dm = ts->dm;
29156c699258SBarry Smith   PetscFunctionReturn(0);
29166c699258SBarry Smith }
29171713a123SBarry Smith 
29180f5c6efeSJed Brown #undef __FUNCT__
29190f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
29200f5c6efeSJed Brown /*@
29210f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
29220f5c6efeSJed Brown 
29233f9fe445SBarry Smith    Logically Collective on SNES
29240f5c6efeSJed Brown 
29250f5c6efeSJed Brown    Input Parameter:
2926d42a1c89SJed Brown + snes - nonlinear solver
29270910c330SBarry Smith . U - the current state at which to evaluate the residual
2928d42a1c89SJed Brown - ctx - user context, must be a TS
29290f5c6efeSJed Brown 
29300f5c6efeSJed Brown    Output Parameter:
29310f5c6efeSJed Brown . F - the nonlinear residual
29320f5c6efeSJed Brown 
29330f5c6efeSJed Brown    Notes:
29340f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
29350f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
29360f5c6efeSJed Brown 
29370f5c6efeSJed Brown    Level: advanced
29380f5c6efeSJed Brown 
29390f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
29400f5c6efeSJed Brown @*/
29410910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
29420f5c6efeSJed Brown {
29430f5c6efeSJed Brown   TS             ts = (TS)ctx;
29440f5c6efeSJed Brown   PetscErrorCode ierr;
29450f5c6efeSJed Brown 
29460f5c6efeSJed Brown   PetscFunctionBegin;
29470f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29480910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
29490f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
29500f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
29510910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
29520f5c6efeSJed Brown   PetscFunctionReturn(0);
29530f5c6efeSJed Brown }
29540f5c6efeSJed Brown 
29550f5c6efeSJed Brown #undef __FUNCT__
29560f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
29570f5c6efeSJed Brown /*@
29580f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
29590f5c6efeSJed Brown 
29600f5c6efeSJed Brown    Collective on SNES
29610f5c6efeSJed Brown 
29620f5c6efeSJed Brown    Input Parameter:
29630f5c6efeSJed Brown + snes - nonlinear solver
29640910c330SBarry Smith . U - the current state at which to evaluate the residual
29650f5c6efeSJed Brown - ctx - user context, must be a TS
29660f5c6efeSJed Brown 
29670f5c6efeSJed Brown    Output Parameter:
29680f5c6efeSJed Brown + A - the Jacobian
29690f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
29700f5c6efeSJed Brown - flag - indicates any structure change in the matrix
29710f5c6efeSJed Brown 
29720f5c6efeSJed Brown    Notes:
29730f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
29740f5c6efeSJed Brown 
29750f5c6efeSJed Brown    Level: developer
29760f5c6efeSJed Brown 
29770f5c6efeSJed Brown .seealso: SNESSetJacobian()
29780f5c6efeSJed Brown @*/
29790910c330SBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat *A,Mat *B,MatStructure *flag,void *ctx)
29800f5c6efeSJed Brown {
29810f5c6efeSJed Brown   TS             ts = (TS)ctx;
29820f5c6efeSJed Brown   PetscErrorCode ierr;
29830f5c6efeSJed Brown 
29840f5c6efeSJed Brown   PetscFunctionBegin;
29850f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29860910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
29870f5c6efeSJed Brown   PetscValidPointer(A,3);
29880f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
29890f5c6efeSJed Brown   PetscValidPointer(B,4);
29900f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
29910f5c6efeSJed Brown   PetscValidPointer(flag,5);
29920f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
29930910c330SBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,flag,ts);CHKERRQ(ierr);
29940f5c6efeSJed Brown   PetscFunctionReturn(0);
29950f5c6efeSJed Brown }
2996325fc9f4SBarry Smith 
29970e4ef248SJed Brown #undef __FUNCT__
29980e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
29990e4ef248SJed Brown /*@C
30000e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
30010e4ef248SJed Brown 
30020e4ef248SJed Brown    Collective on TS
30030e4ef248SJed Brown 
30040e4ef248SJed Brown    Input Arguments:
30050e4ef248SJed Brown +  ts - time stepping context
30060e4ef248SJed Brown .  t - time at which to evaluate
30070910c330SBarry Smith .  U - state at which to evaluate
30080e4ef248SJed Brown -  ctx - context
30090e4ef248SJed Brown 
30100e4ef248SJed Brown    Output Arguments:
30110e4ef248SJed Brown .  F - right hand side
30120e4ef248SJed Brown 
30130e4ef248SJed Brown    Level: intermediate
30140e4ef248SJed Brown 
30150e4ef248SJed Brown    Notes:
30160e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
30170e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
30180e4ef248SJed Brown 
30190e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
30200e4ef248SJed Brown @*/
30210910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
30220e4ef248SJed Brown {
30230e4ef248SJed Brown   PetscErrorCode ierr;
30240e4ef248SJed Brown   Mat            Arhs,Brhs;
30250e4ef248SJed Brown   MatStructure   flg2;
30260e4ef248SJed Brown 
30270e4ef248SJed Brown   PetscFunctionBegin;
30280e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
30290910c330SBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
30300910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
30310e4ef248SJed Brown   PetscFunctionReturn(0);
30320e4ef248SJed Brown }
30330e4ef248SJed Brown 
30340e4ef248SJed Brown #undef __FUNCT__
30350e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
30360e4ef248SJed Brown /*@C
30370e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
30380e4ef248SJed Brown 
30390e4ef248SJed Brown    Collective on TS
30400e4ef248SJed Brown 
30410e4ef248SJed Brown    Input Arguments:
30420e4ef248SJed Brown +  ts - time stepping context
30430e4ef248SJed Brown .  t - time at which to evaluate
30440910c330SBarry Smith .  U - state at which to evaluate
30450e4ef248SJed Brown -  ctx - context
30460e4ef248SJed Brown 
30470e4ef248SJed Brown    Output Arguments:
30480e4ef248SJed Brown +  A - pointer to operator
30490e4ef248SJed Brown .  B - pointer to preconditioning matrix
30500e4ef248SJed Brown -  flg - matrix structure flag
30510e4ef248SJed Brown 
30520e4ef248SJed Brown    Level: intermediate
30530e4ef248SJed Brown 
30540e4ef248SJed Brown    Notes:
30550e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
30560e4ef248SJed Brown 
30570e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
30580e4ef248SJed Brown @*/
30590910c330SBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat *A,Mat *B,MatStructure *flg,void *ctx)
30600e4ef248SJed Brown {
30610e4ef248SJed Brown   PetscFunctionBegin;
30620e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
30630e4ef248SJed Brown   PetscFunctionReturn(0);
30640e4ef248SJed Brown }
30650e4ef248SJed Brown 
30660026cea9SSean Farley #undef __FUNCT__
30670026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
30680026cea9SSean Farley /*@C
30690026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
30700026cea9SSean Farley 
30710026cea9SSean Farley    Collective on TS
30720026cea9SSean Farley 
30730026cea9SSean Farley    Input Arguments:
30740026cea9SSean Farley +  ts - time stepping context
30750026cea9SSean Farley .  t - time at which to evaluate
30760910c330SBarry Smith .  U - state at which to evaluate
30770910c330SBarry Smith .  Udot - time derivative of state vector
30780026cea9SSean Farley -  ctx - context
30790026cea9SSean Farley 
30800026cea9SSean Farley    Output Arguments:
30810026cea9SSean Farley .  F - left hand side
30820026cea9SSean Farley 
30830026cea9SSean Farley    Level: intermediate
30840026cea9SSean Farley 
30850026cea9SSean Farley    Notes:
30860910c330SBarry 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
30870026cea9SSean Farley    user is required to write their own TSComputeIFunction.
30880026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
30890026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
30900026cea9SSean Farley 
30910026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
30920026cea9SSean Farley @*/
30930910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
30940026cea9SSean Farley {
30950026cea9SSean Farley   PetscErrorCode ierr;
30960026cea9SSean Farley   Mat            A,B;
30970026cea9SSean Farley   MatStructure   flg2;
30980026cea9SSean Farley 
30990026cea9SSean Farley   PetscFunctionBegin;
31000026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
31010910c330SBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
31020910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
31030026cea9SSean Farley   PetscFunctionReturn(0);
31040026cea9SSean Farley }
31050026cea9SSean Farley 
31060026cea9SSean Farley #undef __FUNCT__
31070026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
31080026cea9SSean Farley /*@C
310924e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
31100026cea9SSean Farley 
31110026cea9SSean Farley    Collective on TS
31120026cea9SSean Farley 
31130026cea9SSean Farley    Input Arguments:
31140026cea9SSean Farley +  ts - time stepping context
31150026cea9SSean Farley .  t - time at which to evaluate
31160910c330SBarry Smith .  U - state at which to evaluate
31170910c330SBarry Smith .  Udot - time derivative of state vector
31180026cea9SSean Farley .  shift - shift to apply
31190026cea9SSean Farley -  ctx - context
31200026cea9SSean Farley 
31210026cea9SSean Farley    Output Arguments:
31220026cea9SSean Farley +  A - pointer to operator
31230026cea9SSean Farley .  B - pointer to preconditioning matrix
31240026cea9SSean Farley -  flg - matrix structure flag
31250026cea9SSean Farley 
31260026cea9SSean Farley    Level: intermediate
31270026cea9SSean Farley 
31280026cea9SSean Farley    Notes:
31290026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
31300026cea9SSean Farley 
31310026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
31320026cea9SSean Farley @*/
31330910c330SBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
31340026cea9SSean Farley {
31350026cea9SSean Farley   PetscFunctionBegin;
31360026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
31370026cea9SSean Farley   PetscFunctionReturn(0);
31380026cea9SSean Farley }
31390026cea9SSean Farley 
31404af1b03aSJed Brown #undef __FUNCT__
31414af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
31424af1b03aSJed Brown /*@
31434af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
31444af1b03aSJed Brown 
31454af1b03aSJed Brown    Not Collective
31464af1b03aSJed Brown 
31474af1b03aSJed Brown    Input Parameter:
31484af1b03aSJed Brown .  ts - the TS context
31494af1b03aSJed Brown 
31504af1b03aSJed Brown    Output Parameter:
31514af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
31524af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
31534af1b03aSJed Brown 
31544af1b03aSJed Brown    Level: intermediate
31554af1b03aSJed Brown 
3156cd652676SJed Brown    Notes:
3157cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
31584af1b03aSJed Brown 
31594af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
31604af1b03aSJed Brown 
31614af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
31624af1b03aSJed Brown @*/
31634af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
31644af1b03aSJed Brown {
31654af1b03aSJed Brown   PetscFunctionBegin;
31664af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31674af1b03aSJed Brown   PetscValidPointer(reason,2);
31684af1b03aSJed Brown   *reason = ts->reason;
31694af1b03aSJed Brown   PetscFunctionReturn(0);
31704af1b03aSJed Brown }
31714af1b03aSJed Brown 
3172fb1732b5SBarry Smith #undef __FUNCT__
3173*cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3174*cc708dedSBarry Smith /*@
3175*cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3176*cc708dedSBarry Smith 
3177*cc708dedSBarry Smith    Not Collective
3178*cc708dedSBarry Smith 
3179*cc708dedSBarry Smith    Input Parameter:
3180*cc708dedSBarry Smith .  ts - the TS context
3181*cc708dedSBarry Smith 
3182*cc708dedSBarry Smith    Output Parameter:
3183*cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
3184*cc708dedSBarry Smith 
3185*cc708dedSBarry Smith    Level: intermediate
3186*cc708dedSBarry Smith 
3187*cc708dedSBarry Smith    Notes:
3188*cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
3189*cc708dedSBarry Smith 
3190*cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
3191*cc708dedSBarry Smith 
3192*cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
3193*cc708dedSBarry Smith @*/
3194*cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
3195*cc708dedSBarry Smith {
3196*cc708dedSBarry Smith   PetscFunctionBegin;
3197*cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3198*cc708dedSBarry Smith   PetscValidPointer(ftime,2);
3199*cc708dedSBarry Smith   *ftime = ts->solvetime;
3200*cc708dedSBarry Smith   PetscFunctionReturn(0);
3201*cc708dedSBarry Smith }
3202*cc708dedSBarry Smith 
3203*cc708dedSBarry Smith #undef __FUNCT__
32045ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
32059f67acb7SJed Brown /*@
32065ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
32079f67acb7SJed Brown    used by the time integrator.
32089f67acb7SJed Brown 
32099f67acb7SJed Brown    Not Collective
32109f67acb7SJed Brown 
32119f67acb7SJed Brown    Input Parameter:
32129f67acb7SJed Brown .  ts - TS context
32139f67acb7SJed Brown 
32149f67acb7SJed Brown    Output Parameter:
32159f67acb7SJed Brown .  nits - number of nonlinear iterations
32169f67acb7SJed Brown 
32179f67acb7SJed Brown    Notes:
32189f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
32199f67acb7SJed Brown 
32209f67acb7SJed Brown    Level: intermediate
32219f67acb7SJed Brown 
32229f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
32239f67acb7SJed Brown 
32245ef26d82SJed Brown .seealso:  TSGetKSPIterations()
32259f67acb7SJed Brown @*/
32265ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
32279f67acb7SJed Brown {
32289f67acb7SJed Brown   PetscFunctionBegin;
32299f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32309f67acb7SJed Brown   PetscValidIntPointer(nits,2);
32315ef26d82SJed Brown   *nits = ts->snes_its;
32329f67acb7SJed Brown   PetscFunctionReturn(0);
32339f67acb7SJed Brown }
32349f67acb7SJed Brown 
32359f67acb7SJed Brown #undef __FUNCT__
32365ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
32379f67acb7SJed Brown /*@
32385ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
32399f67acb7SJed Brown    used by the time integrator.
32409f67acb7SJed Brown 
32419f67acb7SJed Brown    Not Collective
32429f67acb7SJed Brown 
32439f67acb7SJed Brown    Input Parameter:
32449f67acb7SJed Brown .  ts - TS context
32459f67acb7SJed Brown 
32469f67acb7SJed Brown    Output Parameter:
32479f67acb7SJed Brown .  lits - number of linear iterations
32489f67acb7SJed Brown 
32499f67acb7SJed Brown    Notes:
32509f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
32519f67acb7SJed Brown 
32529f67acb7SJed Brown    Level: intermediate
32539f67acb7SJed Brown 
32549f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
32559f67acb7SJed Brown 
32565ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
32579f67acb7SJed Brown @*/
32585ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
32599f67acb7SJed Brown {
32609f67acb7SJed Brown   PetscFunctionBegin;
32619f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32629f67acb7SJed Brown   PetscValidIntPointer(lits,2);
32635ef26d82SJed Brown   *lits = ts->ksp_its;
32649f67acb7SJed Brown   PetscFunctionReturn(0);
32659f67acb7SJed Brown }
32669f67acb7SJed Brown 
32679f67acb7SJed Brown #undef __FUNCT__
3268cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3269cef5090cSJed Brown /*@
3270cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3271cef5090cSJed Brown 
3272cef5090cSJed Brown    Not Collective
3273cef5090cSJed Brown 
3274cef5090cSJed Brown    Input Parameter:
3275cef5090cSJed Brown .  ts - TS context
3276cef5090cSJed Brown 
3277cef5090cSJed Brown    Output Parameter:
3278cef5090cSJed Brown .  rejects - number of steps rejected
3279cef5090cSJed Brown 
3280cef5090cSJed Brown    Notes:
3281cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3282cef5090cSJed Brown 
3283cef5090cSJed Brown    Level: intermediate
3284cef5090cSJed Brown 
3285cef5090cSJed Brown .keywords: TS, get, number
3286cef5090cSJed Brown 
32875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3288cef5090cSJed Brown @*/
3289cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3290cef5090cSJed Brown {
3291cef5090cSJed Brown   PetscFunctionBegin;
3292cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3293cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3294cef5090cSJed Brown   *rejects = ts->reject;
3295cef5090cSJed Brown   PetscFunctionReturn(0);
3296cef5090cSJed Brown }
3297cef5090cSJed Brown 
3298cef5090cSJed Brown #undef __FUNCT__
3299cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3300cef5090cSJed Brown /*@
3301cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3302cef5090cSJed Brown 
3303cef5090cSJed Brown    Not Collective
3304cef5090cSJed Brown 
3305cef5090cSJed Brown    Input Parameter:
3306cef5090cSJed Brown .  ts - TS context
3307cef5090cSJed Brown 
3308cef5090cSJed Brown    Output Parameter:
3309cef5090cSJed Brown .  fails - number of failed nonlinear solves
3310cef5090cSJed Brown 
3311cef5090cSJed Brown    Notes:
3312cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3313cef5090cSJed Brown 
3314cef5090cSJed Brown    Level: intermediate
3315cef5090cSJed Brown 
3316cef5090cSJed Brown .keywords: TS, get, number
3317cef5090cSJed Brown 
33185ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3319cef5090cSJed Brown @*/
3320cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3321cef5090cSJed Brown {
3322cef5090cSJed Brown   PetscFunctionBegin;
3323cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3324cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3325cef5090cSJed Brown   *fails = ts->num_snes_failures;
3326cef5090cSJed Brown   PetscFunctionReturn(0);
3327cef5090cSJed Brown }
3328cef5090cSJed Brown 
3329cef5090cSJed Brown #undef __FUNCT__
3330cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3331cef5090cSJed Brown /*@
3332cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3333cef5090cSJed Brown 
3334cef5090cSJed Brown    Not Collective
3335cef5090cSJed Brown 
3336cef5090cSJed Brown    Input Parameter:
3337cef5090cSJed Brown +  ts - TS context
3338cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3339cef5090cSJed Brown 
3340cef5090cSJed Brown    Notes:
3341cef5090cSJed Brown    The counter is reset to zero for each step
3342cef5090cSJed Brown 
3343cef5090cSJed Brown    Options Database Key:
3344cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3345cef5090cSJed Brown 
3346cef5090cSJed Brown    Level: intermediate
3347cef5090cSJed Brown 
3348cef5090cSJed Brown .keywords: TS, set, maximum, number
3349cef5090cSJed Brown 
33505ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3351cef5090cSJed Brown @*/
3352cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3353cef5090cSJed Brown {
3354cef5090cSJed Brown   PetscFunctionBegin;
3355cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3356cef5090cSJed Brown   ts->max_reject = rejects;
3357cef5090cSJed Brown   PetscFunctionReturn(0);
3358cef5090cSJed Brown }
3359cef5090cSJed Brown 
3360cef5090cSJed Brown #undef __FUNCT__
3361cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3362cef5090cSJed Brown /*@
3363cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3364cef5090cSJed Brown 
3365cef5090cSJed Brown    Not Collective
3366cef5090cSJed Brown 
3367cef5090cSJed Brown    Input Parameter:
3368cef5090cSJed Brown +  ts - TS context
3369cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3370cef5090cSJed Brown 
3371cef5090cSJed Brown    Notes:
3372cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3373cef5090cSJed Brown 
3374cef5090cSJed Brown    Options Database Key:
3375cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3376cef5090cSJed Brown 
3377cef5090cSJed Brown    Level: intermediate
3378cef5090cSJed Brown 
3379cef5090cSJed Brown .keywords: TS, set, maximum, number
3380cef5090cSJed Brown 
33815ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3382cef5090cSJed Brown @*/
3383cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3384cef5090cSJed Brown {
3385cef5090cSJed Brown   PetscFunctionBegin;
3386cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3387cef5090cSJed Brown   ts->max_snes_failures = fails;
3388cef5090cSJed Brown   PetscFunctionReturn(0);
3389cef5090cSJed Brown }
3390cef5090cSJed Brown 
3391cef5090cSJed Brown #undef __FUNCT__
3392cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3393cef5090cSJed Brown /*@
3394cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3395cef5090cSJed Brown 
3396cef5090cSJed Brown    Not Collective
3397cef5090cSJed Brown 
3398cef5090cSJed Brown    Input Parameter:
3399cef5090cSJed Brown +  ts - TS context
3400cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3401cef5090cSJed Brown 
3402cef5090cSJed Brown    Options Database Key:
3403cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3404cef5090cSJed Brown 
3405cef5090cSJed Brown    Level: intermediate
3406cef5090cSJed Brown 
3407cef5090cSJed Brown .keywords: TS, set, error
3408cef5090cSJed Brown 
34095ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3410cef5090cSJed Brown @*/
3411cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3412cef5090cSJed Brown {
3413cef5090cSJed Brown   PetscFunctionBegin;
3414cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3415cef5090cSJed Brown   ts->errorifstepfailed = err;
3416cef5090cSJed Brown   PetscFunctionReturn(0);
3417cef5090cSJed Brown }
3418cef5090cSJed Brown 
3419cef5090cSJed Brown #undef __FUNCT__
3420fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3421fb1732b5SBarry Smith /*@C
3422fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3423fb1732b5SBarry Smith 
3424fb1732b5SBarry Smith    Collective on TS
3425fb1732b5SBarry Smith 
3426fb1732b5SBarry Smith    Input Parameters:
3427fb1732b5SBarry Smith +  ts - the TS context
3428fb1732b5SBarry Smith .  step - current time-step
3429fb1732b5SBarry Smith .  ptime - current time
34300910c330SBarry Smith .  u - current state
3431fb1732b5SBarry Smith -  viewer - binary viewer
3432fb1732b5SBarry Smith 
3433fb1732b5SBarry Smith    Level: intermediate
3434fb1732b5SBarry Smith 
3435fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3436fb1732b5SBarry Smith 
3437fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3438fb1732b5SBarry Smith @*/
34390910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
3440fb1732b5SBarry Smith {
3441fb1732b5SBarry Smith   PetscErrorCode ierr;
3442ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
3443fb1732b5SBarry Smith 
3444fb1732b5SBarry Smith   PetscFunctionBegin;
34450910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
3446ed81e22dSJed Brown   PetscFunctionReturn(0);
3447ed81e22dSJed Brown }
3448ed81e22dSJed Brown 
3449ed81e22dSJed Brown #undef __FUNCT__
3450ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3451ed81e22dSJed Brown /*@C
3452ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3453ed81e22dSJed Brown 
3454ed81e22dSJed Brown    Collective on TS
3455ed81e22dSJed Brown 
3456ed81e22dSJed Brown    Input Parameters:
3457ed81e22dSJed Brown +  ts - the TS context
3458ed81e22dSJed Brown .  step - current time-step
3459ed81e22dSJed Brown .  ptime - current time
34600910c330SBarry Smith .  u - current state
3461ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3462ed81e22dSJed Brown 
3463ed81e22dSJed Brown    Level: intermediate
3464ed81e22dSJed Brown 
3465ed81e22dSJed Brown    Notes:
3466ed81e22dSJed 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.
3467ed81e22dSJed Brown    These are named according to the file name template.
3468ed81e22dSJed Brown 
3469ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3470ed81e22dSJed Brown 
3471ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3472ed81e22dSJed Brown 
3473ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3474ed81e22dSJed Brown @*/
34750910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
3476ed81e22dSJed Brown {
3477ed81e22dSJed Brown   PetscErrorCode ierr;
3478ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3479ed81e22dSJed Brown   PetscViewer    viewer;
3480ed81e22dSJed Brown 
3481ed81e22dSJed Brown   PetscFunctionBegin;
34828caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3483ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
34840910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
3485ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3486ed81e22dSJed Brown   PetscFunctionReturn(0);
3487ed81e22dSJed Brown }
3488ed81e22dSJed Brown 
3489ed81e22dSJed Brown #undef __FUNCT__
3490ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3491ed81e22dSJed Brown /*@C
3492ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3493ed81e22dSJed Brown 
3494ed81e22dSJed Brown    Collective on TS
3495ed81e22dSJed Brown 
3496ed81e22dSJed Brown    Input Parameters:
3497ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3498ed81e22dSJed Brown 
3499ed81e22dSJed Brown    Level: intermediate
3500ed81e22dSJed Brown 
3501ed81e22dSJed Brown    Note:
3502ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3503ed81e22dSJed Brown 
3504ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3505ed81e22dSJed Brown 
3506ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3507ed81e22dSJed Brown @*/
3508ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3509ed81e22dSJed Brown {
3510ed81e22dSJed Brown   PetscErrorCode ierr;
3511ed81e22dSJed Brown 
3512ed81e22dSJed Brown   PetscFunctionBegin;
3513ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3514fb1732b5SBarry Smith   PetscFunctionReturn(0);
3515fb1732b5SBarry Smith }
3516fb1732b5SBarry Smith 
351784df9cb4SJed Brown #undef __FUNCT__
351884df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
351984df9cb4SJed Brown /*@
352084df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
352184df9cb4SJed Brown 
3522ed81e22dSJed Brown    Collective on TS if controller has not been created yet
352384df9cb4SJed Brown 
352484df9cb4SJed Brown    Input Arguments:
3525ed81e22dSJed Brown .  ts - time stepping context
352684df9cb4SJed Brown 
352784df9cb4SJed Brown    Output Arguments:
3528ed81e22dSJed Brown .  adapt - adaptive controller
352984df9cb4SJed Brown 
353084df9cb4SJed Brown    Level: intermediate
353184df9cb4SJed Brown 
3532ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
353384df9cb4SJed Brown @*/
353484df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
353584df9cb4SJed Brown {
353684df9cb4SJed Brown   PetscErrorCode ierr;
353784df9cb4SJed Brown 
353884df9cb4SJed Brown   PetscFunctionBegin;
353984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
354084df9cb4SJed Brown   PetscValidPointer(adapt,2);
354184df9cb4SJed Brown   if (!ts->adapt) {
354284df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
35431c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
35441c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
354584df9cb4SJed Brown   }
354684df9cb4SJed Brown   *adapt = ts->adapt;
354784df9cb4SJed Brown   PetscFunctionReturn(0);
354884df9cb4SJed Brown }
3549d6ebe24aSShri Abhyankar 
3550d6ebe24aSShri Abhyankar #undef __FUNCT__
35511c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
35521c3436cfSJed Brown /*@
35531c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
35541c3436cfSJed Brown 
35551c3436cfSJed Brown    Logically Collective
35561c3436cfSJed Brown 
35571c3436cfSJed Brown    Input Arguments:
35581c3436cfSJed Brown +  ts - time integration context
35591c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
35601c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
35611c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
35621c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
35631c3436cfSJed Brown 
35641c3436cfSJed Brown    Level: beginner
35651c3436cfSJed Brown 
3566c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
35671c3436cfSJed Brown @*/
35681c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
35691c3436cfSJed Brown {
35701c3436cfSJed Brown   PetscErrorCode ierr;
35711c3436cfSJed Brown 
35721c3436cfSJed Brown   PetscFunctionBegin;
3573c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
35741c3436cfSJed Brown   if (vatol) {
35751c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
35761c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
35771c3436cfSJed Brown     ts->vatol = vatol;
35781c3436cfSJed Brown   }
3579c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
35801c3436cfSJed Brown   if (vrtol) {
35811c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
35821c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
35831c3436cfSJed Brown     ts->vrtol = vrtol;
35841c3436cfSJed Brown   }
35851c3436cfSJed Brown   PetscFunctionReturn(0);
35861c3436cfSJed Brown }
35871c3436cfSJed Brown 
35881c3436cfSJed Brown #undef __FUNCT__
3589c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3590c5033834SJed Brown /*@
3591c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3592c5033834SJed Brown 
3593c5033834SJed Brown    Logically Collective
3594c5033834SJed Brown 
3595c5033834SJed Brown    Input Arguments:
3596c5033834SJed Brown .  ts - time integration context
3597c5033834SJed Brown 
3598c5033834SJed Brown    Output Arguments:
3599c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3600c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3601c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3602c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3603c5033834SJed Brown 
3604c5033834SJed Brown    Level: beginner
3605c5033834SJed Brown 
3606c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3607c5033834SJed Brown @*/
3608c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3609c5033834SJed Brown {
3610c5033834SJed Brown   PetscFunctionBegin;
3611c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3612c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3613c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3614c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3615c5033834SJed Brown   PetscFunctionReturn(0);
3616c5033834SJed Brown }
3617c5033834SJed Brown 
3618c5033834SJed Brown #undef __FUNCT__
36191c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
36201c3436cfSJed Brown /*@
36211c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
36221c3436cfSJed Brown 
36231c3436cfSJed Brown    Collective on TS
36241c3436cfSJed Brown 
36251c3436cfSJed Brown    Input Arguments:
36261c3436cfSJed Brown +  ts - time stepping context
36271c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
36281c3436cfSJed Brown 
36291c3436cfSJed Brown    Output Arguments:
36301c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
36311c3436cfSJed Brown 
36321c3436cfSJed Brown    Level: developer
36331c3436cfSJed Brown 
36341c3436cfSJed Brown .seealso: TSSetTolerances()
36351c3436cfSJed Brown @*/
36361c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
36371c3436cfSJed Brown {
36381c3436cfSJed Brown   PetscErrorCode    ierr;
36391c3436cfSJed Brown   PetscInt          i,n,N;
36400910c330SBarry Smith   const PetscScalar *u,*y;
36410910c330SBarry Smith   Vec               U;
36421c3436cfSJed Brown   PetscReal         sum,gsum;
36431c3436cfSJed Brown 
36441c3436cfSJed Brown   PetscFunctionBegin;
36451c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36461c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
36471c3436cfSJed Brown   PetscValidPointer(norm,3);
36480910c330SBarry Smith   U = ts->vec_sol;
36490910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
36500910c330SBarry Smith   if (U == Y) SETERRQ(((PetscObject)U)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
36511c3436cfSJed Brown 
36520910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
36530910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
36540910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
36551c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
36561c3436cfSJed Brown   sum = 0.;
3657ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3658ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3659ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3660ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3661ceefc113SJed Brown     for (i=0; i<n; i++) {
36620910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
36630910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3664ceefc113SJed Brown     }
3665ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3666ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3667ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3668ceefc113SJed Brown     const PetscScalar *atol;
3669ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3670ceefc113SJed Brown     for (i=0; i<n; i++) {
36710910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
36720910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3673ceefc113SJed Brown     }
3674ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3675ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3676ceefc113SJed Brown     const PetscScalar *rtol;
3677ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3678ceefc113SJed Brown     for (i=0; i<n; i++) {
36790910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
36800910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
3681ceefc113SJed Brown     }
3682ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3683ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
36841c3436cfSJed Brown     for (i=0; i<n; i++) {
36850910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
36860910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
36871c3436cfSJed Brown     }
3688ceefc113SJed Brown   }
36890910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
36901c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
36911c3436cfSJed Brown 
36921c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
36931c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
36941c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
36951c3436cfSJed Brown   PetscFunctionReturn(0);
36961c3436cfSJed Brown }
36971c3436cfSJed Brown 
36981c3436cfSJed Brown #undef __FUNCT__
36998d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
37008d59e960SJed Brown /*@
37018d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
37028d59e960SJed Brown 
37038d59e960SJed Brown    Logically Collective on TS
37048d59e960SJed Brown 
37058d59e960SJed Brown    Input Arguments:
37068d59e960SJed Brown +  ts - time stepping context
37078d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
37088d59e960SJed Brown 
37098d59e960SJed Brown    Note:
37108d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
37118d59e960SJed Brown 
37128d59e960SJed Brown    Level: intermediate
37138d59e960SJed Brown 
37148d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
37158d59e960SJed Brown @*/
37168d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
37178d59e960SJed Brown {
37188d59e960SJed Brown   PetscFunctionBegin;
37198d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37208d59e960SJed Brown   ts->cfltime_local = cfltime;
37218d59e960SJed Brown   ts->cfltime = -1.;
37228d59e960SJed Brown   PetscFunctionReturn(0);
37238d59e960SJed Brown }
37248d59e960SJed Brown 
37258d59e960SJed Brown #undef __FUNCT__
37268d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
37278d59e960SJed Brown /*@
37288d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
37298d59e960SJed Brown 
37308d59e960SJed Brown    Collective on TS
37318d59e960SJed Brown 
37328d59e960SJed Brown    Input Arguments:
37338d59e960SJed Brown .  ts - time stepping context
37348d59e960SJed Brown 
37358d59e960SJed Brown    Output Arguments:
37368d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
37378d59e960SJed Brown 
37388d59e960SJed Brown    Level: advanced
37398d59e960SJed Brown 
37408d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
37418d59e960SJed Brown @*/
37428d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
37438d59e960SJed Brown {
37448d59e960SJed Brown   PetscErrorCode ierr;
37458d59e960SJed Brown 
37468d59e960SJed Brown   PetscFunctionBegin;
37478d59e960SJed Brown   if (ts->cfltime < 0) {
37488d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
37498d59e960SJed Brown   }
37508d59e960SJed Brown   *cfltime = ts->cfltime;
37518d59e960SJed Brown   PetscFunctionReturn(0);
37528d59e960SJed Brown }
37538d59e960SJed Brown 
37548d59e960SJed Brown #undef __FUNCT__
3755d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3756d6ebe24aSShri Abhyankar /*@
3757d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3758d6ebe24aSShri Abhyankar 
3759d6ebe24aSShri Abhyankar    Input Parameters:
3760d6ebe24aSShri Abhyankar .  ts   - the TS context.
3761d6ebe24aSShri Abhyankar .  xl   - lower bound.
3762d6ebe24aSShri Abhyankar .  xu   - upper bound.
3763d6ebe24aSShri Abhyankar 
3764d6ebe24aSShri Abhyankar    Notes:
3765d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
376633c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3767d6ebe24aSShri Abhyankar 
37682bd2b0e6SSatish Balay    Level: advanced
37692bd2b0e6SSatish Balay 
3770d6ebe24aSShri Abhyankar @*/
3771d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3772d6ebe24aSShri Abhyankar {
3773d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3774d6ebe24aSShri Abhyankar   SNES           snes;
3775d6ebe24aSShri Abhyankar 
3776d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3777d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3778d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3779d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3780d6ebe24aSShri Abhyankar }
3781d6ebe24aSShri Abhyankar 
3782325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3783c6db04a5SJed Brown #include <mex.h>
3784325fc9f4SBarry Smith 
3785325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3786325fc9f4SBarry Smith 
3787325fc9f4SBarry Smith #undef __FUNCT__
3788325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3789325fc9f4SBarry Smith /*
3790325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3791325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3792325fc9f4SBarry Smith 
3793325fc9f4SBarry Smith    Collective on TS
3794325fc9f4SBarry Smith 
3795325fc9f4SBarry Smith    Input Parameters:
3796325fc9f4SBarry Smith +  snes - the TS context
37970910c330SBarry Smith -  u - input vector
3798325fc9f4SBarry Smith 
3799325fc9f4SBarry Smith    Output Parameter:
3800325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3801325fc9f4SBarry Smith 
3802325fc9f4SBarry Smith    Notes:
3803325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3804325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3805325fc9f4SBarry Smith    themselves.
3806325fc9f4SBarry Smith 
3807325fc9f4SBarry Smith    Level: developer
3808325fc9f4SBarry Smith 
3809325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3810325fc9f4SBarry Smith 
3811325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3812325fc9f4SBarry Smith */
38130910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
3814325fc9f4SBarry Smith {
3815325fc9f4SBarry Smith   PetscErrorCode   ierr;
3816325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3817325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3818325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3819325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3820325fc9f4SBarry Smith 
3821325fc9f4SBarry Smith   PetscFunctionBegin;
3822325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
38230910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
38240910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
3825325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
38260910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
3827325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3828325fc9f4SBarry Smith 
3829325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
38300910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
38310910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
38320910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
3833325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3834325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3835325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3836325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3837325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3838325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3839325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3840325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3841325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3842325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3843325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3844325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3845325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3846325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3847325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3848325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3849325fc9f4SBarry Smith   PetscFunctionReturn(0);
3850325fc9f4SBarry Smith }
3851325fc9f4SBarry Smith 
3852325fc9f4SBarry Smith 
3853325fc9f4SBarry Smith #undef __FUNCT__
3854325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3855325fc9f4SBarry Smith /*
3856325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3857325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3858e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3859325fc9f4SBarry Smith 
3860325fc9f4SBarry Smith    Logically Collective on TS
3861325fc9f4SBarry Smith 
3862325fc9f4SBarry Smith    Input Parameters:
3863325fc9f4SBarry Smith +  ts - the TS context
3864325fc9f4SBarry Smith -  func - function evaluation routine
3865325fc9f4SBarry Smith 
3866325fc9f4SBarry Smith    Calling sequence of func:
38670910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
3868325fc9f4SBarry Smith 
3869325fc9f4SBarry Smith    Level: beginner
3870325fc9f4SBarry Smith 
3871325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3872325fc9f4SBarry Smith 
3873325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3874325fc9f4SBarry Smith */
3875cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3876325fc9f4SBarry Smith {
3877325fc9f4SBarry Smith   PetscErrorCode  ierr;
3878325fc9f4SBarry Smith   TSMatlabContext *sctx;
3879325fc9f4SBarry Smith 
3880325fc9f4SBarry Smith   PetscFunctionBegin;
3881325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3882325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3883325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3884325fc9f4SBarry Smith   /*
3885325fc9f4SBarry Smith      This should work, but it doesn't
3886325fc9f4SBarry Smith   sctx->ctx = ctx;
3887325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3888325fc9f4SBarry Smith   */
3889325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3890cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3891325fc9f4SBarry Smith   PetscFunctionReturn(0);
3892325fc9f4SBarry Smith }
3893325fc9f4SBarry Smith 
3894325fc9f4SBarry Smith #undef __FUNCT__
3895325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3896325fc9f4SBarry Smith /*
3897325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3898325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3899325fc9f4SBarry Smith 
3900325fc9f4SBarry Smith    Collective on TS
3901325fc9f4SBarry Smith 
3902325fc9f4SBarry Smith    Input Parameters:
3903cdcf91faSSean Farley +  ts - the TS context
39040910c330SBarry Smith .  u - input vector
3905325fc9f4SBarry Smith .  A, B - the matrices
3906325fc9f4SBarry Smith -  ctx - user context
3907325fc9f4SBarry Smith 
3908325fc9f4SBarry Smith    Output Parameter:
3909325fc9f4SBarry Smith .  flag - structure of the matrix
3910325fc9f4SBarry Smith 
3911325fc9f4SBarry Smith    Level: developer
3912325fc9f4SBarry Smith 
3913325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3914325fc9f4SBarry Smith 
3915325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3916325fc9f4SBarry Smith @*/
39170910c330SBarry Smith PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3918325fc9f4SBarry Smith {
3919325fc9f4SBarry Smith   PetscErrorCode  ierr;
3920325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3921325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3922325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3923325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3924325fc9f4SBarry Smith 
3925325fc9f4SBarry Smith   PetscFunctionBegin;
3926cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39270910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
3928325fc9f4SBarry Smith 
39290910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
3930325fc9f4SBarry Smith 
3931cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
39320910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
39330910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
39340910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
39350910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
3936325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3937325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3938325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3939325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3940325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3941325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3942325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3943325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3944325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3945325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3946325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3947325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3948325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3949325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3950325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3951325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3952325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3953325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3954325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3955325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3956325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3957325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3958325fc9f4SBarry Smith   PetscFunctionReturn(0);
3959325fc9f4SBarry Smith }
3960325fc9f4SBarry Smith 
3961325fc9f4SBarry Smith 
3962325fc9f4SBarry Smith #undef __FUNCT__
3963325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3964325fc9f4SBarry Smith /*
3965325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3966e3c5b3baSBarry 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
3967325fc9f4SBarry Smith 
3968325fc9f4SBarry Smith    Logically Collective on TS
3969325fc9f4SBarry Smith 
3970325fc9f4SBarry Smith    Input Parameters:
3971cdcf91faSSean Farley +  ts - the TS context
3972325fc9f4SBarry Smith .  A,B - Jacobian matrices
3973325fc9f4SBarry Smith .  func - function evaluation routine
3974325fc9f4SBarry Smith -  ctx - user context
3975325fc9f4SBarry Smith 
3976325fc9f4SBarry Smith    Calling sequence of func:
39770910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
3978325fc9f4SBarry Smith 
3979325fc9f4SBarry Smith 
3980325fc9f4SBarry Smith    Level: developer
3981325fc9f4SBarry Smith 
3982325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3983325fc9f4SBarry Smith 
3984325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3985325fc9f4SBarry Smith */
3986cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3987325fc9f4SBarry Smith {
3988325fc9f4SBarry Smith   PetscErrorCode    ierr;
3989325fc9f4SBarry Smith   TSMatlabContext *sctx;
3990325fc9f4SBarry Smith 
3991325fc9f4SBarry Smith   PetscFunctionBegin;
3992325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3993325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3994325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3995325fc9f4SBarry Smith   /*
3996325fc9f4SBarry Smith      This should work, but it doesn't
3997325fc9f4SBarry Smith   sctx->ctx = ctx;
3998325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3999325fc9f4SBarry Smith   */
4000325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4001cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4002325fc9f4SBarry Smith   PetscFunctionReturn(0);
4003325fc9f4SBarry Smith }
4004325fc9f4SBarry Smith 
4005b5b1a830SBarry Smith #undef __FUNCT__
4006b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4007b5b1a830SBarry Smith /*
4008b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4009b5b1a830SBarry Smith 
4010b5b1a830SBarry Smith    Collective on TS
4011b5b1a830SBarry Smith 
4012b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4013b5b1a830SBarry Smith @*/
40140910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4015b5b1a830SBarry Smith {
4016b5b1a830SBarry Smith   PetscErrorCode  ierr;
4017b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
4018a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
4019b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4020b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4021b5b1a830SBarry Smith 
4022b5b1a830SBarry Smith   PetscFunctionBegin;
4023cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40240910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4025b5b1a830SBarry Smith 
4026cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
40270910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4028b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4029b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4030b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4031b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4032b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4033b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4034b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4035b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4036b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4037b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4038b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4039b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4040b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4041b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4042b5b1a830SBarry Smith   PetscFunctionReturn(0);
4043b5b1a830SBarry Smith }
4044b5b1a830SBarry Smith 
4045b5b1a830SBarry Smith 
4046b5b1a830SBarry Smith #undef __FUNCT__
4047b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4048b5b1a830SBarry Smith /*
4049b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4050b5b1a830SBarry Smith 
4051b5b1a830SBarry Smith    Level: developer
4052b5b1a830SBarry Smith 
4053b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4054b5b1a830SBarry Smith 
4055b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4056b5b1a830SBarry Smith */
4057cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4058b5b1a830SBarry Smith {
4059b5b1a830SBarry Smith   PetscErrorCode    ierr;
4060b5b1a830SBarry Smith   TSMatlabContext *sctx;
4061b5b1a830SBarry Smith 
4062b5b1a830SBarry Smith   PetscFunctionBegin;
4063b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4064b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4065b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4066b5b1a830SBarry Smith   /*
4067b5b1a830SBarry Smith      This should work, but it doesn't
4068b5b1a830SBarry Smith   sctx->ctx = ctx;
4069b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4070b5b1a830SBarry Smith   */
4071b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4072cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
4073b5b1a830SBarry Smith   PetscFunctionReturn(0);
4074b5b1a830SBarry Smith }
4075325fc9f4SBarry Smith #endif
4076b3603a34SBarry Smith 
40770b039ecaSBarry Smith 
40780b039ecaSBarry Smith 
4079b3603a34SBarry Smith #undef __FUNCT__
40804f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4081b3603a34SBarry Smith /*@C
40824f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4083b3603a34SBarry Smith        in a time based line graph
4084b3603a34SBarry Smith 
4085b3603a34SBarry Smith    Collective on TS
4086b3603a34SBarry Smith 
4087b3603a34SBarry Smith    Input Parameters:
4088b3603a34SBarry Smith +  ts - the TS context
4089b3603a34SBarry Smith .  step - current time-step
4090b3603a34SBarry Smith .  ptime - current time
4091b3603a34SBarry Smith -  lg - a line graph object
4092b3603a34SBarry Smith 
4093b3603a34SBarry Smith    Level: intermediate
4094b3603a34SBarry Smith 
40950b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4096b3603a34SBarry Smith 
4097b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4098b3603a34SBarry Smith 
4099b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4100b3603a34SBarry Smith @*/
41010910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4102b3603a34SBarry Smith {
4103b3603a34SBarry Smith   PetscErrorCode    ierr;
41040b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4105b3603a34SBarry Smith   const PetscScalar *yy;
410658ff32f7SBarry Smith   PetscInt          dim;
4107b3603a34SBarry Smith 
4108b3603a34SBarry Smith   PetscFunctionBegin;
410958ff32f7SBarry Smith   if (!step) {
4110a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4111a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4112a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
41130910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
41140b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
41150b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
411658ff32f7SBarry Smith   }
41170910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4118e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4119e3efe391SJed Brown   {
4120e3efe391SJed Brown     PetscReal *yreal;
4121e3efe391SJed Brown     PetscInt i,n;
41220910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4123e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4124e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
41250b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4126e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4127e3efe391SJed Brown   }
4128e3efe391SJed Brown #else
41290b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4130e3efe391SJed Brown #endif
41310910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
41323fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
41330b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
41343923b477SBarry Smith   }
4135b3603a34SBarry Smith   PetscFunctionReturn(0);
4136b3603a34SBarry Smith }
4137b3603a34SBarry Smith 
4138b3603a34SBarry Smith #undef __FUNCT__
41394f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4140ef20d060SBarry Smith /*@C
41414f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4142ef20d060SBarry Smith        in a time based line graph
4143ef20d060SBarry Smith 
4144ef20d060SBarry Smith    Collective on TS
4145ef20d060SBarry Smith 
4146ef20d060SBarry Smith    Input Parameters:
4147ef20d060SBarry Smith +  ts - the TS context
4148ef20d060SBarry Smith .  step - current time-step
4149ef20d060SBarry Smith .  ptime - current time
4150ef20d060SBarry Smith -  lg - a line graph object
4151ef20d060SBarry Smith 
4152ef20d060SBarry Smith    Level: intermediate
4153ef20d060SBarry Smith 
4154abd5a294SJed Brown    Notes:
4155abd5a294SJed Brown    Only for sequential solves.
4156abd5a294SJed Brown 
4157abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4158abd5a294SJed Brown 
4159abd5a294SJed Brown    Options Database Keys:
41604f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4161ef20d060SBarry Smith 
4162ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4163ef20d060SBarry Smith 
4164abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4165ef20d060SBarry Smith @*/
41660910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4167ef20d060SBarry Smith {
4168ef20d060SBarry Smith   PetscErrorCode    ierr;
41690b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4170ef20d060SBarry Smith   const PetscScalar *yy;
4171ef20d060SBarry Smith   Vec               y;
4172a9f9c1f6SBarry Smith   PetscInt          dim;
4173ef20d060SBarry Smith 
4174ef20d060SBarry Smith   PetscFunctionBegin;
4175a9f9c1f6SBarry Smith   if (!step) {
4176a9f9c1f6SBarry Smith     PetscDrawAxis  axis;
4177a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
41781ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
41790910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
4180a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
4181a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4182a9f9c1f6SBarry Smith   }
41830910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
4184ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
41850910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
4186ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
4187e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4188e3efe391SJed Brown   {
4189e3efe391SJed Brown     PetscReal *yreal;
4190e3efe391SJed Brown     PetscInt  i,n;
4191e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
4192e3efe391SJed Brown     ierr = PetscMalloc(n*sizeof(PetscReal),&yreal);CHKERRQ(ierr);
4193e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
41940b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4195e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4196e3efe391SJed Brown   }
4197e3efe391SJed Brown #else
41980b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4199e3efe391SJed Brown #endif
4200ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
4201ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
42023fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(step % ctx->howoften)) && (step > -1)) || ((ctx->howoften == -1) && (step == -1))){
42030b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
42043923b477SBarry Smith   }
4205ef20d060SBarry Smith   PetscFunctionReturn(0);
4206ef20d060SBarry Smith }
4207ef20d060SBarry Smith 
4208201da799SBarry Smith #undef __FUNCT__
4209201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
4210201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4211201da799SBarry Smith {
4212201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4213201da799SBarry Smith   PetscReal      x = ptime,y;
4214201da799SBarry Smith   PetscErrorCode ierr;
4215201da799SBarry Smith   PetscInt       its;
4216201da799SBarry Smith 
4217201da799SBarry Smith   PetscFunctionBegin;
4218201da799SBarry Smith   if (!n) {
4219201da799SBarry Smith     PetscDrawAxis  axis;
4220201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4221201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
4222201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4223201da799SBarry Smith     ctx->snes_its  = 0;
4224201da799SBarry Smith   }
4225201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
4226201da799SBarry Smith   y    = its - ctx->snes_its;
4227201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
42283fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4229201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4230201da799SBarry Smith   }
4231201da799SBarry Smith   ctx->snes_its = its;
4232201da799SBarry Smith   PetscFunctionReturn(0);
4233201da799SBarry Smith }
4234201da799SBarry Smith 
4235201da799SBarry Smith #undef __FUNCT__
4236201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
4237201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
4238201da799SBarry Smith {
4239201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4240201da799SBarry Smith   PetscReal      x = ptime,y;
4241201da799SBarry Smith   PetscErrorCode ierr;
4242201da799SBarry Smith   PetscInt       its;
4243201da799SBarry Smith 
4244201da799SBarry Smith   PetscFunctionBegin;
4245201da799SBarry Smith   if (!n) {
4246201da799SBarry Smith     PetscDrawAxis  axis;
4247201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4248201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
4249201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4250201da799SBarry Smith     ctx->ksp_its = 0;
4251201da799SBarry Smith   }
4252201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
4253201da799SBarry Smith   y    = its - ctx->ksp_its;
4254201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
425599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))){
4256201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
4257201da799SBarry Smith   }
4258201da799SBarry Smith   ctx->ksp_its = its;
4259201da799SBarry Smith   PetscFunctionReturn(0);
4260201da799SBarry Smith }
4261f9c1d6abSBarry Smith 
4262f9c1d6abSBarry Smith #undef __FUNCT__
4263f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
4264f9c1d6abSBarry Smith /*@
4265f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
4266f9c1d6abSBarry Smith 
4267f9c1d6abSBarry Smith    Collective on TS and Vec
4268f9c1d6abSBarry Smith 
4269f9c1d6abSBarry Smith    Input Parameters:
4270f9c1d6abSBarry Smith +  ts - the TS context
4271f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
4272f9c1d6abSBarry Smith 
4273f9c1d6abSBarry Smith    Output Parameters:
4274f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
4275f9c1d6abSBarry Smith 
4276f9c1d6abSBarry Smith    Level: developer
4277f9c1d6abSBarry Smith 
4278f9c1d6abSBarry Smith .keywords: TS, compute
4279f9c1d6abSBarry Smith 
4280f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
4281f9c1d6abSBarry Smith @*/
4282f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
4283f9c1d6abSBarry Smith {
4284f9c1d6abSBarry Smith   PetscErrorCode ierr;
4285f9c1d6abSBarry Smith 
4286f9c1d6abSBarry Smith   PetscFunctionBegin;
4287f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4288f9c1d6abSBarry Smith   if (!ts->ops->linearstability) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_SUP,"Linearized stability function not provided for this method");
4289f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
4290f9c1d6abSBarry Smith   PetscFunctionReturn(0);
4291f9c1d6abSBarry Smith }
4292