xref: /petsc/src/ts/interface/ts.c (revision e3d84a461ece6f98c2944099877b5aa12ff99127)
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
64a6570f20SBarry Smith -  -ts_monitor_draw - plot information at each timestep
65bdad233fSMatthew Knepley 
66bdad233fSMatthew Knepley    Level: beginner
67bdad233fSMatthew Knepley 
68bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
69bdad233fSMatthew Knepley 
70a313700dSBarry Smith .seealso: TSGetType()
71bdad233fSMatthew Knepley @*/
727087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
73bdad233fSMatthew Knepley {
74ace3abfcSBarry Smith   PetscBool      opt,flg;
75dfbe8321SBarry Smith   PetscErrorCode ierr;
76649052a6SBarry Smith   PetscViewer    monviewer;
77eabae89aSBarry Smith   char           monfilename[PETSC_MAX_PATH_LEN];
78089b2837SJed Brown   SNES           snes;
791c3436cfSJed Brown   TSAdapt        adapt;
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley   PetscFunctionBegin;
820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
833194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
84a43b19c4SJed Brown     /* Handle TS type options */
85a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
86bdad233fSMatthew Knepley 
87bdad233fSMatthew Knepley     /* Handle generic TS options */
88bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
893bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
90d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
91d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&ts->time_step,PETSC_NULL);CHKERRQ(ierr);
92a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
93a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
94461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
95cef5090cSJed 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);
96cef5090cSJed 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);
97cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
981c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
991c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
100bdad233fSMatthew Knepley 
101bdad233fSMatthew Knepley     /* Monitor options */
102a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
103eabae89aSBarry Smith     if (flg) {
104649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
105649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
106bdad233fSMatthew Knepley     }
1075180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1085180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1095180491cSLisandro Dalcin 
11090d69ab7SBarry Smith     opt  = PETSC_FALSE;
111acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
112a7cc72afSBarry Smith     if (opt) {
113a6570f20SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
114bdad233fSMatthew Knepley     }
11590d69ab7SBarry Smith     opt  = PETSC_FALSE;
116acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
117a7cc72afSBarry Smith     if (opt) {
1186083293cSBarry Smith       void *ctx;
1196083293cSBarry Smith       ierr = TSMonitorSolutionCreate(ts,PETSC_NULL,&ctx);CHKERRQ(ierr);
1206083293cSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr);
121bdad233fSMatthew Knepley     }
122fb1732b5SBarry Smith     opt  = PETSC_FALSE;
123fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
124fb1732b5SBarry Smith     if (flg) {
125fb1732b5SBarry Smith       PetscViewer ctx;
126fb1732b5SBarry Smith       if (monfilename[0]) {
127fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
128fb1732b5SBarry Smith       } else {
129fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
130fb1732b5SBarry Smith       }
131fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
132fb1732b5SBarry Smith     }
133ed81e22dSJed Brown     opt  = PETSC_FALSE;
134ed81e22dSJed Brown     ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
135ed81e22dSJed Brown     if (flg) {
136ed81e22dSJed Brown       const char *ptr,*ptr2;
137ed81e22dSJed Brown       char *filetemplate;
138ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
139ed81e22dSJed Brown       /* Do some cursory validation of the input. */
140ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
141ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
142ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
143ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
144ed81e22dSJed Brown         if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
145ed81e22dSJed Brown         if (ptr2) break;
146ed81e22dSJed Brown       }
147ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
148ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
149ed81e22dSJed Brown     }
150bdad233fSMatthew Knepley 
1511c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
1521c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
1531c3436cfSJed Brown 
154d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
155d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
156d52bd9f3SBarry Smith 
157bdad233fSMatthew Knepley     /* Handle specific TS options */
158abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
159bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
160bdad233fSMatthew Knepley     }
1615d973c19SBarry Smith 
1625d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1635d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
164bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
165bdad233fSMatthew Knepley   PetscFunctionReturn(0);
166bdad233fSMatthew Knepley }
167bdad233fSMatthew Knepley 
168bdad233fSMatthew Knepley #undef __FUNCT__
169cdcf91faSSean Farley #undef __FUNCT__
1704a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
171a7a1495cSBarry Smith /*@
1728c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
173a7a1495cSBarry Smith       set with TSSetRHSJacobian().
174a7a1495cSBarry Smith 
175a7a1495cSBarry Smith    Collective on TS and Vec
176a7a1495cSBarry Smith 
177a7a1495cSBarry Smith    Input Parameters:
178316643e7SJed Brown +  ts - the TS context
179a7a1495cSBarry Smith .  t - current timestep
180a7a1495cSBarry Smith -  x - input vector
181a7a1495cSBarry Smith 
182a7a1495cSBarry Smith    Output Parameters:
183a7a1495cSBarry Smith +  A - Jacobian matrix
184a7a1495cSBarry Smith .  B - optional preconditioning matrix
185a7a1495cSBarry Smith -  flag - flag indicating matrix structure
186a7a1495cSBarry Smith 
187a7a1495cSBarry Smith    Notes:
188a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
189a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
190a7a1495cSBarry Smith 
19194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
192a7a1495cSBarry Smith    flag parameter.
193a7a1495cSBarry Smith 
194a7a1495cSBarry Smith    Level: developer
195a7a1495cSBarry Smith 
196a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
197a7a1495cSBarry Smith 
19894b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
199a7a1495cSBarry Smith @*/
2007087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
201a7a1495cSBarry Smith {
202dfbe8321SBarry Smith   PetscErrorCode ierr;
2030e4ef248SJed Brown   PetscInt Xstate;
204a7a1495cSBarry Smith 
205a7a1495cSBarry Smith   PetscFunctionBegin;
2060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2070700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
208c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
2090e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2100e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2110e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2120e4ef248SJed Brown     PetscFunctionReturn(0);
213f8ede8e7SJed Brown   }
214d90be118SSean Farley 
2153b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
216d90be118SSean Farley 
2173b5f76d0SSean Farley   if (ts->userops->rhsjacobian) {
218d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
219a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
220a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
2213b5f76d0SSean Farley     ierr = (*ts->userops->rhsjacobian)(ts,t,X,A,B,flg,ts->jacP);CHKERRQ(ierr);
222a7a1495cSBarry Smith     PetscStackPop;
223d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
224a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2250700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2260700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
227ef66eb69SBarry Smith   } else {
228214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
229214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
230214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
231ef66eb69SBarry Smith   }
2320e4ef248SJed Brown   ts->rhsjacobian.time = t;
2330e4ef248SJed Brown   ts->rhsjacobian.X = X;
2340e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2350e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
236a7a1495cSBarry Smith   PetscFunctionReturn(0);
237a7a1495cSBarry Smith }
238a7a1495cSBarry Smith 
2394a2ae208SSatish Balay #undef __FUNCT__
2404a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
241316643e7SJed Brown /*@
242d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
243d763cef2SBarry Smith 
244316643e7SJed Brown    Collective on TS and Vec
245316643e7SJed Brown 
246316643e7SJed Brown    Input Parameters:
247316643e7SJed Brown +  ts - the TS context
248316643e7SJed Brown .  t - current time
249316643e7SJed Brown -  x - state vector
250316643e7SJed Brown 
251316643e7SJed Brown    Output Parameter:
252316643e7SJed Brown .  y - right hand side
253316643e7SJed Brown 
254316643e7SJed Brown    Note:
255316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
256316643e7SJed Brown    is used internally within the nonlinear solvers.
257316643e7SJed Brown 
258316643e7SJed Brown    Level: developer
259316643e7SJed Brown 
260316643e7SJed Brown .keywords: TS, compute
261316643e7SJed Brown 
262316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
263316643e7SJed Brown @*/
264dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
265d763cef2SBarry Smith {
266dfbe8321SBarry Smith   PetscErrorCode ierr;
267d763cef2SBarry Smith 
268d763cef2SBarry Smith   PetscFunctionBegin;
2690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2700700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2710700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
272d763cef2SBarry Smith 
2733b5f76d0SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
274d763cef2SBarry Smith 
275d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
2763b5f76d0SSean Farley   if (ts->userops->rhsfunction) {
277d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
2783b5f76d0SSean Farley     ierr = (*ts->userops->rhsfunction)(ts,t,x,y,ts->funP);CHKERRQ(ierr);
279d763cef2SBarry Smith     PetscStackPop;
280214bc6a2SJed Brown   } else {
281214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
282b2cd27e8SJed Brown   }
28344a41b28SSean Farley 
284d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
285d763cef2SBarry Smith   PetscFunctionReturn(0);
286d763cef2SBarry Smith }
287d763cef2SBarry Smith 
2884a2ae208SSatish Balay #undef __FUNCT__
289214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
290214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
291214bc6a2SJed Brown {
2922dd45cf8SJed Brown   Vec            F;
293214bc6a2SJed Brown   PetscErrorCode ierr;
294214bc6a2SJed Brown 
295214bc6a2SJed Brown   PetscFunctionBegin;
2960da9a4f0SJed Brown   *Frhs = PETSC_NULL;
2972dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
298214bc6a2SJed Brown   if (!ts->Frhs) {
2992dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
300214bc6a2SJed Brown   }
301214bc6a2SJed Brown   *Frhs = ts->Frhs;
302214bc6a2SJed Brown   PetscFunctionReturn(0);
303214bc6a2SJed Brown }
304214bc6a2SJed Brown 
305214bc6a2SJed Brown #undef __FUNCT__
306214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
307214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
308214bc6a2SJed Brown {
309214bc6a2SJed Brown   Mat            A,B;
3102dd45cf8SJed Brown   PetscErrorCode ierr;
311214bc6a2SJed Brown 
312214bc6a2SJed Brown   PetscFunctionBegin;
313214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
314214bc6a2SJed Brown   if (Arhs) {
315214bc6a2SJed Brown     if (!ts->Arhs) {
316214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
317214bc6a2SJed Brown     }
318214bc6a2SJed Brown     *Arhs = ts->Arhs;
319214bc6a2SJed Brown   }
320214bc6a2SJed Brown   if (Brhs) {
321214bc6a2SJed Brown     if (!ts->Brhs) {
322214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
323214bc6a2SJed Brown     }
324214bc6a2SJed Brown     *Brhs = ts->Brhs;
325214bc6a2SJed Brown   }
326214bc6a2SJed Brown   PetscFunctionReturn(0);
327214bc6a2SJed Brown }
328214bc6a2SJed Brown 
329214bc6a2SJed Brown #undef __FUNCT__
330316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
331316643e7SJed Brown /*@
332316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
333316643e7SJed Brown 
334316643e7SJed Brown    Collective on TS and Vec
335316643e7SJed Brown 
336316643e7SJed Brown    Input Parameters:
337316643e7SJed Brown +  ts - the TS context
338316643e7SJed Brown .  t - current time
339316643e7SJed Brown .  X - state vector
340214bc6a2SJed Brown .  Xdot - time derivative of state vector
341214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
342316643e7SJed Brown 
343316643e7SJed Brown    Output Parameter:
344316643e7SJed Brown .  Y - right hand side
345316643e7SJed Brown 
346316643e7SJed Brown    Note:
347316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
348316643e7SJed Brown    is used internally within the nonlinear solvers.
349316643e7SJed Brown 
350316643e7SJed Brown    If the user did did not write their equations in implicit form, this
351316643e7SJed Brown    function recasts them in implicit form.
352316643e7SJed Brown 
353316643e7SJed Brown    Level: developer
354316643e7SJed Brown 
355316643e7SJed Brown .keywords: TS, compute
356316643e7SJed Brown 
357316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
358316643e7SJed Brown @*/
359214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
360316643e7SJed Brown {
361316643e7SJed Brown   PetscErrorCode ierr;
362316643e7SJed Brown 
363316643e7SJed Brown   PetscFunctionBegin;
3640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3650700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3660700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3670700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
368316643e7SJed Brown 
369d90be118SSean Farley   if (!ts->userops->rhsfunction && !ts->userops->ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
370d90be118SSean Farley 
371316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
3723b5f76d0SSean Farley   if (ts->userops->ifunction) {
373316643e7SJed Brown     PetscStackPush("TS user implicit function");
3743b5f76d0SSean Farley     ierr = (*ts->userops->ifunction)(ts,t,X,Xdot,Y,ts->funP);CHKERRQ(ierr);
375316643e7SJed Brown     PetscStackPop;
376214bc6a2SJed Brown   }
377214bc6a2SJed Brown   if (imex) {
3783b5f76d0SSean Farley     if (!ts->userops->ifunction) {
3792dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
3802dd45cf8SJed Brown     }
3812dd45cf8SJed Brown   } else if (ts->userops->rhsfunction) {
3822dd45cf8SJed Brown     if (ts->userops->ifunction) {
383214bc6a2SJed Brown       Vec Frhs;
384214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
385214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
386214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
3872dd45cf8SJed Brown     } else {
3882dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
3892dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
390316643e7SJed Brown     }
3914a6899ffSJed Brown   }
392316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
393316643e7SJed Brown   PetscFunctionReturn(0);
394316643e7SJed Brown }
395316643e7SJed Brown 
396316643e7SJed Brown #undef __FUNCT__
397316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
398316643e7SJed Brown /*@
399316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
400316643e7SJed Brown 
401316643e7SJed Brown    Collective on TS and Vec
402316643e7SJed Brown 
403316643e7SJed Brown    Input
404316643e7SJed Brown       Input Parameters:
405316643e7SJed Brown +  ts - the TS context
406316643e7SJed Brown .  t - current timestep
407316643e7SJed Brown .  X - state vector
408316643e7SJed Brown .  Xdot - time derivative of state vector
409214bc6a2SJed Brown .  shift - shift to apply, see note below
410214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
411316643e7SJed Brown 
412316643e7SJed Brown    Output Parameters:
413316643e7SJed Brown +  A - Jacobian matrix
414316643e7SJed Brown .  B - optional preconditioning matrix
415316643e7SJed Brown -  flag - flag indicating matrix structure
416316643e7SJed Brown 
417316643e7SJed Brown    Notes:
418316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
419316643e7SJed Brown 
420316643e7SJed Brown    dF/dX + shift*dF/dXdot
421316643e7SJed Brown 
422316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
423316643e7SJed Brown    is used internally within the nonlinear solvers.
424316643e7SJed Brown 
425316643e7SJed Brown    Level: developer
426316643e7SJed Brown 
427316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
428316643e7SJed Brown 
429316643e7SJed Brown .seealso:  TSSetIJacobian()
43063495f91SJed Brown @*/
431214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
432316643e7SJed Brown {
4330026cea9SSean Farley   PetscInt Xstate, Xdotstate;
434316643e7SJed Brown   PetscErrorCode ierr;
435316643e7SJed Brown 
436316643e7SJed Brown   PetscFunctionBegin;
4370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4380700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4390700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
440316643e7SJed Brown   PetscValidPointer(A,6);
4410700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
442316643e7SJed Brown   PetscValidPointer(B,7);
4430700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
444316643e7SJed Brown   PetscValidPointer(flg,8);
4450026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4460026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4470026cea9SSean Farley   if (ts->ijacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->ijacobian.X == X && ts->ijacobian.Xstate == Xstate && ts->ijacobian.Xdot == Xdot && ts->ijacobian.Xdotstate == Xdotstate && ts->ijacobian.imex == imex))) {
4480026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4490026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4500026cea9SSean Farley     PetscFunctionReturn(0);
4510026cea9SSean Farley   }
452316643e7SJed Brown 
4533b5f76d0SSean Farley   if (!ts->userops->rhsjacobian && !ts->userops->ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
454316643e7SJed Brown 
4554e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
456316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
4573b5f76d0SSean Farley   if (ts->userops->ijacobian) {
4582dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
459316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
4603b5f76d0SSean Farley     ierr = (*ts->userops->ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ts->jacP);CHKERRQ(ierr);
461316643e7SJed Brown     PetscStackPop;
462214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
463214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
464214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
4654a6899ffSJed Brown   }
466214bc6a2SJed Brown   if (imex) {
4673b5f76d0SSean Farley     if (!ts->userops->ijacobian) {  /* system was written as Xdot = F(t,X) */
468214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
4692dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
470214bc6a2SJed Brown       if (*A != *B) {
471214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
472214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
473214bc6a2SJed Brown       }
474214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
475214bc6a2SJed Brown     }
476214bc6a2SJed Brown   } else {
4773b5f76d0SSean Farley     if (!ts->userops->ijacobian) {
478214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
479214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
480214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
481316643e7SJed Brown       if (*A != *B) {
482316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
483316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
484316643e7SJed Brown       }
4853b5f76d0SSean Farley     } else if (ts->userops->rhsjacobian) {
486214bc6a2SJed Brown       Mat Arhs,Brhs;
487214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
488214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
489214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
490214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
491214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
492214bc6a2SJed Brown       if (*A != *B) {
493214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
494214bc6a2SJed Brown       }
495214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
496214bc6a2SJed Brown     }
497316643e7SJed Brown   }
4980026cea9SSean Farley 
4990026cea9SSean Farley   ts->ijacobian.time = t;
5000026cea9SSean Farley   ts->ijacobian.X = X;
5010026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
5020026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
5030026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
5040026cea9SSean Farley   ts->ijacobian.shift = shift;
5050026cea9SSean Farley   ts->ijacobian.imex = imex;
5060026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
507316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
508316643e7SJed Brown   PetscFunctionReturn(0);
509316643e7SJed Brown }
510316643e7SJed Brown 
511316643e7SJed Brown #undef __FUNCT__
5124a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
513d763cef2SBarry Smith /*@C
514d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
515d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
516d763cef2SBarry Smith 
5173f9fe445SBarry Smith     Logically Collective on TS
518d763cef2SBarry Smith 
519d763cef2SBarry Smith     Input Parameters:
520d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
521ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
522d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
523d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
524d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
525d763cef2SBarry Smith 
526d763cef2SBarry Smith     Calling sequence of func:
52787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
528d763cef2SBarry Smith 
529d763cef2SBarry Smith +   t - current timestep
530d763cef2SBarry Smith .   u - input vector
531d763cef2SBarry Smith .   F - function vector
532d763cef2SBarry Smith -   ctx - [optional] user-defined function context
533d763cef2SBarry Smith 
534d763cef2SBarry Smith     Level: beginner
535d763cef2SBarry Smith 
536d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
537d763cef2SBarry Smith 
538d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
539d763cef2SBarry Smith @*/
540089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
541d763cef2SBarry Smith {
542089b2837SJed Brown   PetscErrorCode ierr;
543089b2837SJed Brown   SNES           snes;
544e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
545d763cef2SBarry Smith 
546089b2837SJed Brown   PetscFunctionBegin;
5470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
548ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
5493b5f76d0SSean Farley   if (f)   ts->userops->rhsfunction = f;
5504e684422SJed Brown   if (ctx) ts->funP                 = ctx;
551089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
552e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
553e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
554e856ceecSJed Brown     r = ralloc;
555e856ceecSJed Brown   }
556089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
557e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
558d763cef2SBarry Smith   PetscFunctionReturn(0);
559d763cef2SBarry Smith }
560d763cef2SBarry Smith 
5614a2ae208SSatish Balay #undef __FUNCT__
5624a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
563d763cef2SBarry Smith /*@C
564d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
565d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
566d763cef2SBarry Smith 
5673f9fe445SBarry Smith    Logically Collective on TS
568d763cef2SBarry Smith 
569d763cef2SBarry Smith    Input Parameters:
570d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
571d763cef2SBarry Smith .  A   - Jacobian matrix
572d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
573d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
574d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
575d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
576d763cef2SBarry Smith 
577d763cef2SBarry Smith    Calling sequence of func:
57887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
579d763cef2SBarry Smith 
580d763cef2SBarry Smith +  t - current timestep
581d763cef2SBarry Smith .  u - input vector
582d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
583d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
584d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
58594b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
586d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
587d763cef2SBarry Smith 
588d763cef2SBarry Smith    Notes:
58994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
590d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
591d763cef2SBarry Smith 
592d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
593d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
59456335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
595d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
596d763cef2SBarry Smith    throughout the global iterations.
597d763cef2SBarry Smith 
598d763cef2SBarry Smith    Level: beginner
599d763cef2SBarry Smith 
600d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
601d763cef2SBarry Smith 
602ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
603d763cef2SBarry Smith 
604d763cef2SBarry Smith @*/
605089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
606d763cef2SBarry Smith {
607277b19d0SLisandro Dalcin   PetscErrorCode ierr;
608089b2837SJed Brown   SNES           snes;
609277b19d0SLisandro Dalcin 
610d763cef2SBarry Smith   PetscFunctionBegin;
6110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
612277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
613277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
614277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
615277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
616d763cef2SBarry Smith 
6173b5f76d0SSean Farley   if (f)   ts->userops->rhsjacobian = f;
618277b19d0SLisandro Dalcin   if (ctx) ts->jacP                 = ctx;
619089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
620dabe61deSJed Brown   if (!ts->userops->ijacobian) {
621089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
6220e4ef248SJed Brown   }
6230e4ef248SJed Brown   if (A) {
6240e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
6250e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
6260e4ef248SJed Brown     ts->Arhs = A;
6270e4ef248SJed Brown   }
6280e4ef248SJed Brown   if (B) {
6290e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
6300e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
6310e4ef248SJed Brown     ts->Brhs = B;
6320e4ef248SJed Brown   }
633d763cef2SBarry Smith   PetscFunctionReturn(0);
634d763cef2SBarry Smith }
635d763cef2SBarry Smith 
636316643e7SJed Brown 
637316643e7SJed Brown #undef __FUNCT__
638316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
639316643e7SJed Brown /*@C
640316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
641316643e7SJed Brown 
6423f9fe445SBarry Smith    Logically Collective on TS
643316643e7SJed Brown 
644316643e7SJed Brown    Input Parameters:
645316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
646ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
647316643e7SJed Brown .  f   - the function evaluation routine
648316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
649316643e7SJed Brown 
650316643e7SJed Brown    Calling sequence of f:
651316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
652316643e7SJed Brown 
653316643e7SJed Brown +  t   - time at step/stage being solved
654316643e7SJed Brown .  u   - state vector
655316643e7SJed Brown .  u_t - time derivative of state vector
656316643e7SJed Brown .  F   - function vector
657316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
658316643e7SJed Brown 
659316643e7SJed Brown    Important:
660d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
661316643e7SJed Brown 
662316643e7SJed Brown    Level: beginner
663316643e7SJed Brown 
664316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
665316643e7SJed Brown 
666d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
667316643e7SJed Brown @*/
668089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
669316643e7SJed Brown {
670089b2837SJed Brown   PetscErrorCode ierr;
671089b2837SJed Brown   SNES           snes;
672e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
673316643e7SJed Brown 
674316643e7SJed Brown   PetscFunctionBegin;
6750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
676ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
6773b5f76d0SSean Farley   if (f)   ts->userops->ifunction = f;
678089b2837SJed Brown   if (ctx) ts->funP           = ctx;
679089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
680e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
681e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
682e856ceecSJed Brown     res = resalloc;
683e856ceecSJed Brown   }
684089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
685e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
686089b2837SJed Brown   PetscFunctionReturn(0);
687089b2837SJed Brown }
688089b2837SJed Brown 
689089b2837SJed Brown #undef __FUNCT__
690089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
691089b2837SJed Brown /*@C
692089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
693089b2837SJed Brown 
694089b2837SJed Brown    Not Collective
695089b2837SJed Brown 
696089b2837SJed Brown    Input Parameter:
697089b2837SJed Brown .  ts - the TS context
698089b2837SJed Brown 
699089b2837SJed Brown    Output Parameter:
700089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
701089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
702089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
703089b2837SJed Brown 
704089b2837SJed Brown    Level: advanced
705089b2837SJed Brown 
706089b2837SJed Brown .keywords: TS, nonlinear, get, function
707089b2837SJed Brown 
708089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
709089b2837SJed Brown @*/
710089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
711089b2837SJed Brown {
712089b2837SJed Brown   PetscErrorCode ierr;
713089b2837SJed Brown   SNES snes;
714089b2837SJed Brown 
715089b2837SJed Brown   PetscFunctionBegin;
716089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
717089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
718089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7193b5f76d0SSean Farley   if (func) *func = ts->userops->ifunction;
720089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
721089b2837SJed Brown   PetscFunctionReturn(0);
722089b2837SJed Brown }
723089b2837SJed Brown 
724089b2837SJed Brown #undef __FUNCT__
725089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
726089b2837SJed Brown /*@C
727089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
728089b2837SJed Brown 
729089b2837SJed Brown    Not Collective
730089b2837SJed Brown 
731089b2837SJed Brown    Input Parameter:
732089b2837SJed Brown .  ts - the TS context
733089b2837SJed Brown 
734089b2837SJed Brown    Output Parameter:
735089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
736089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
737089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
738089b2837SJed Brown 
739089b2837SJed Brown    Level: advanced
740089b2837SJed Brown 
741089b2837SJed Brown .keywords: TS, nonlinear, get, function
742089b2837SJed Brown 
743089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
744089b2837SJed Brown @*/
745089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
746089b2837SJed Brown {
747089b2837SJed Brown   PetscErrorCode ierr;
748089b2837SJed Brown   SNES snes;
749089b2837SJed Brown 
750089b2837SJed Brown   PetscFunctionBegin;
751089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
752089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
753089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7543b5f76d0SSean Farley   if (func) *func = ts->userops->rhsfunction;
755089b2837SJed Brown   if (ctx)  *ctx  = ts->funP;
756316643e7SJed Brown   PetscFunctionReturn(0);
757316643e7SJed Brown }
758316643e7SJed Brown 
759316643e7SJed Brown #undef __FUNCT__
760316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
761316643e7SJed Brown /*@C
762a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
763a4f0a591SBarry Smith         you provided with TSSetIFunction().
764316643e7SJed Brown 
7653f9fe445SBarry Smith    Logically Collective on TS
766316643e7SJed Brown 
767316643e7SJed Brown    Input Parameters:
768316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
769316643e7SJed Brown .  A   - Jacobian matrix
770316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
771316643e7SJed Brown .  f   - the Jacobian evaluation routine
772316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
773316643e7SJed Brown 
774316643e7SJed Brown    Calling sequence of f:
7751b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
776316643e7SJed Brown 
777316643e7SJed Brown +  t    - time at step/stage being solved
7781b4a444bSJed Brown .  U    - state vector
7791b4a444bSJed Brown .  U_t  - time derivative of state vector
780316643e7SJed Brown .  a    - shift
7811b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
782316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
783316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
784316643e7SJed Brown           structure (same as flag in KSPSetOperators())
785316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
786316643e7SJed Brown 
787316643e7SJed Brown    Notes:
788316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
789316643e7SJed Brown 
790a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
791a4f0a591SBarry Smith    the Jacobian of G(U) = F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
792a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
793a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
794a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
795a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
796a4f0a591SBarry Smith 
797316643e7SJed Brown    Level: beginner
798316643e7SJed Brown 
799316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
800316643e7SJed Brown 
801ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
802316643e7SJed Brown 
803316643e7SJed Brown @*/
8047087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
805316643e7SJed Brown {
806316643e7SJed Brown   PetscErrorCode ierr;
807089b2837SJed Brown   SNES           snes;
808316643e7SJed Brown 
809316643e7SJed Brown   PetscFunctionBegin;
8100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8110700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8120700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
813316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
814316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
8153b5f76d0SSean Farley   if (f)   ts->userops->ijacobian = f;
816316643e7SJed Brown   if (ctx) ts->jacP           = ctx;
817089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
818089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
819316643e7SJed Brown   PetscFunctionReturn(0);
820316643e7SJed Brown }
821316643e7SJed Brown 
8224a2ae208SSatish Balay #undef __FUNCT__
8234a2ae208SSatish Balay #define __FUNCT__ "TSView"
8247e2c5f70SBarry Smith /*@C
825d763cef2SBarry Smith     TSView - Prints the TS data structure.
826d763cef2SBarry Smith 
8274c49b128SBarry Smith     Collective on TS
828d763cef2SBarry Smith 
829d763cef2SBarry Smith     Input Parameters:
830d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
831d763cef2SBarry Smith -   viewer - visualization context
832d763cef2SBarry Smith 
833d763cef2SBarry Smith     Options Database Key:
834d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
835d763cef2SBarry Smith 
836d763cef2SBarry Smith     Notes:
837d763cef2SBarry Smith     The available visualization contexts include
838b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
839b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
840d763cef2SBarry Smith          output where only the first processor opens
841d763cef2SBarry Smith          the file.  All other processors send their
842d763cef2SBarry Smith          data to the first processor to print.
843d763cef2SBarry Smith 
844d763cef2SBarry Smith     The user can open an alternative visualization context with
845b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
846d763cef2SBarry Smith 
847d763cef2SBarry Smith     Level: beginner
848d763cef2SBarry Smith 
849d763cef2SBarry Smith .keywords: TS, timestep, view
850d763cef2SBarry Smith 
851b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
852d763cef2SBarry Smith @*/
8537087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
854d763cef2SBarry Smith {
855dfbe8321SBarry Smith   PetscErrorCode ierr;
856a313700dSBarry Smith   const TSType   type;
857089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
858d763cef2SBarry Smith 
859d763cef2SBarry Smith   PetscFunctionBegin;
8600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8613050cee2SBarry Smith   if (!viewer) {
8627adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
8633050cee2SBarry Smith   }
8640700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
865c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
866fd16b177SBarry Smith 
867251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
868251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
86932077d6dSBarry Smith   if (iascii) {
870317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
87177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
872a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
873d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
8745ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
875c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
876d763cef2SBarry Smith     }
8775ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
878193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
879d52bd9f3SBarry Smith     if (ts->ops->view) {
880d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
881d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
882d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
883d52bd9f3SBarry Smith     }
8840f5bd95cSBarry Smith   } else if (isstring) {
885a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
886b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
887d763cef2SBarry Smith   }
888b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
889251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
890b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
891d763cef2SBarry Smith   PetscFunctionReturn(0);
892d763cef2SBarry Smith }
893d763cef2SBarry Smith 
894d763cef2SBarry Smith 
8954a2ae208SSatish Balay #undef __FUNCT__
8964a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
897b07ff414SBarry Smith /*@
898d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
899d763cef2SBarry Smith    the timesteppers.
900d763cef2SBarry Smith 
9013f9fe445SBarry Smith    Logically Collective on TS
902d763cef2SBarry Smith 
903d763cef2SBarry Smith    Input Parameters:
904d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
905d763cef2SBarry Smith -  usrP - optional user context
906d763cef2SBarry Smith 
907d763cef2SBarry Smith    Level: intermediate
908d763cef2SBarry Smith 
909d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
910d763cef2SBarry Smith 
911d763cef2SBarry Smith .seealso: TSGetApplicationContext()
912d763cef2SBarry Smith @*/
9137087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
914d763cef2SBarry Smith {
915d763cef2SBarry Smith   PetscFunctionBegin;
9160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
917d763cef2SBarry Smith   ts->user = usrP;
918d763cef2SBarry Smith   PetscFunctionReturn(0);
919d763cef2SBarry Smith }
920d763cef2SBarry Smith 
9214a2ae208SSatish Balay #undef __FUNCT__
9224a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
923b07ff414SBarry Smith /*@
924d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
925d763cef2SBarry Smith     timestepper.
926d763cef2SBarry Smith 
927d763cef2SBarry Smith     Not Collective
928d763cef2SBarry Smith 
929d763cef2SBarry Smith     Input Parameter:
930d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
931d763cef2SBarry Smith 
932d763cef2SBarry Smith     Output Parameter:
933d763cef2SBarry Smith .   usrP - user context
934d763cef2SBarry Smith 
935d763cef2SBarry Smith     Level: intermediate
936d763cef2SBarry Smith 
937d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
938d763cef2SBarry Smith 
939d763cef2SBarry Smith .seealso: TSSetApplicationContext()
940d763cef2SBarry Smith @*/
941e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
942d763cef2SBarry Smith {
943d763cef2SBarry Smith   PetscFunctionBegin;
9440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
945e71120c6SJed Brown   *(void**)usrP = ts->user;
946d763cef2SBarry Smith   PetscFunctionReturn(0);
947d763cef2SBarry Smith }
948d763cef2SBarry Smith 
9494a2ae208SSatish Balay #undef __FUNCT__
9504a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
951d763cef2SBarry Smith /*@
952d763cef2SBarry Smith    TSGetTimeStepNumber - Gets the current number of timesteps.
953d763cef2SBarry Smith 
954d763cef2SBarry Smith    Not Collective
955d763cef2SBarry Smith 
956d763cef2SBarry Smith    Input Parameter:
957d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
958d763cef2SBarry Smith 
959d763cef2SBarry Smith    Output Parameter:
960d763cef2SBarry Smith .  iter - number steps so far
961d763cef2SBarry Smith 
962d763cef2SBarry Smith    Level: intermediate
963d763cef2SBarry Smith 
964d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
965d763cef2SBarry Smith @*/
9667087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
967d763cef2SBarry Smith {
968d763cef2SBarry Smith   PetscFunctionBegin;
9690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9704482741eSBarry Smith   PetscValidIntPointer(iter,2);
971d763cef2SBarry Smith   *iter = ts->steps;
972d763cef2SBarry Smith   PetscFunctionReturn(0);
973d763cef2SBarry Smith }
974d763cef2SBarry Smith 
9754a2ae208SSatish Balay #undef __FUNCT__
9764a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
977d763cef2SBarry Smith /*@
978d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
979d763cef2SBarry Smith    as well as the initial time.
980d763cef2SBarry Smith 
9813f9fe445SBarry Smith    Logically Collective on TS
982d763cef2SBarry Smith 
983d763cef2SBarry Smith    Input Parameters:
984d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
985d763cef2SBarry Smith .  initial_time - the initial time
986d763cef2SBarry Smith -  time_step - the size of the timestep
987d763cef2SBarry Smith 
988d763cef2SBarry Smith    Level: intermediate
989d763cef2SBarry Smith 
990d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
991d763cef2SBarry Smith 
992d763cef2SBarry Smith .keywords: TS, set, initial, timestep
993d763cef2SBarry Smith @*/
9947087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
995d763cef2SBarry Smith {
996e144a568SJed Brown   PetscErrorCode ierr;
997e144a568SJed Brown 
998d763cef2SBarry Smith   PetscFunctionBegin;
9990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1000e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1001d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1002d763cef2SBarry Smith   PetscFunctionReturn(0);
1003d763cef2SBarry Smith }
1004d763cef2SBarry Smith 
10054a2ae208SSatish Balay #undef __FUNCT__
10064a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1007d763cef2SBarry Smith /*@
1008d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1009d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1010d763cef2SBarry Smith 
10113f9fe445SBarry Smith    Logically Collective on TS
1012d763cef2SBarry Smith 
1013d763cef2SBarry Smith    Input Parameters:
1014d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1015d763cef2SBarry Smith -  time_step - the size of the timestep
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith    Level: intermediate
1018d763cef2SBarry Smith 
1019d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1020d763cef2SBarry Smith 
1021d763cef2SBarry Smith .keywords: TS, set, timestep
1022d763cef2SBarry Smith @*/
10237087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1024d763cef2SBarry Smith {
1025d763cef2SBarry Smith   PetscFunctionBegin;
10260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1027c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1028d763cef2SBarry Smith   ts->time_step = time_step;
1029d763cef2SBarry Smith   PetscFunctionReturn(0);
1030d763cef2SBarry Smith }
1031d763cef2SBarry Smith 
10324a2ae208SSatish Balay #undef __FUNCT__
1033a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1034a43b19c4SJed Brown /*@
1035a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1036a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1037a43b19c4SJed Brown       it computed.
1038a43b19c4SJed Brown 
1039a43b19c4SJed Brown   Logically Collective on TS
1040a43b19c4SJed Brown 
1041a43b19c4SJed Brown    Input Parameter:
1042a43b19c4SJed Brown +   ts - the time-step context
1043a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1044a43b19c4SJed Brown 
1045a43b19c4SJed Brown    Level: beginner
1046a43b19c4SJed Brown 
1047a43b19c4SJed Brown .seealso: TSSetDuration()
1048a43b19c4SJed Brown @*/
1049a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1050a43b19c4SJed Brown {
1051a43b19c4SJed Brown 
1052a43b19c4SJed Brown   PetscFunctionBegin;
1053a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1054d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1055a43b19c4SJed Brown   ts->exact_final_time = flg;
1056a43b19c4SJed Brown   PetscFunctionReturn(0);
1057a43b19c4SJed Brown }
1058a43b19c4SJed Brown 
1059a43b19c4SJed Brown #undef __FUNCT__
10604a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1061d763cef2SBarry Smith /*@
1062d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith    Not Collective
1065d763cef2SBarry Smith 
1066d763cef2SBarry Smith    Input Parameter:
1067d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1068d763cef2SBarry Smith 
1069d763cef2SBarry Smith    Output Parameter:
1070d763cef2SBarry Smith .  dt - the current timestep size
1071d763cef2SBarry Smith 
1072d763cef2SBarry Smith    Level: intermediate
1073d763cef2SBarry Smith 
1074d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1075d763cef2SBarry Smith 
1076d763cef2SBarry Smith .keywords: TS, get, timestep
1077d763cef2SBarry Smith @*/
10787087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1079d763cef2SBarry Smith {
1080d763cef2SBarry Smith   PetscFunctionBegin;
10810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10824482741eSBarry Smith   PetscValidDoublePointer(dt,2);
1083d763cef2SBarry Smith   *dt = ts->time_step;
1084d763cef2SBarry Smith   PetscFunctionReturn(0);
1085d763cef2SBarry Smith }
1086d763cef2SBarry Smith 
10874a2ae208SSatish Balay #undef __FUNCT__
10884a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1089d8e5e3e6SSatish Balay /*@
1090d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1091d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1092d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1093d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1094d763cef2SBarry Smith 
1095d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1096d763cef2SBarry Smith 
1097d763cef2SBarry Smith    Input Parameter:
1098d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1099d763cef2SBarry Smith 
1100d763cef2SBarry Smith    Output Parameter:
1101d763cef2SBarry Smith .  v - the vector containing the solution
1102d763cef2SBarry Smith 
1103d763cef2SBarry Smith    Level: intermediate
1104d763cef2SBarry Smith 
1105d763cef2SBarry Smith .seealso: TSGetTimeStep()
1106d763cef2SBarry Smith 
1107d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1108d763cef2SBarry Smith @*/
11097087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1110d763cef2SBarry Smith {
1111d763cef2SBarry Smith   PetscFunctionBegin;
11120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11134482741eSBarry Smith   PetscValidPointer(v,2);
11148737fe31SLisandro Dalcin   *v = ts->vec_sol;
1115d763cef2SBarry Smith   PetscFunctionReturn(0);
1116d763cef2SBarry Smith }
1117d763cef2SBarry Smith 
1118bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
11194a2ae208SSatish Balay #undef __FUNCT__
1120bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1121d8e5e3e6SSatish Balay /*@
1122bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1123d763cef2SBarry Smith 
1124bdad233fSMatthew Knepley   Not collective
1125d763cef2SBarry Smith 
1126bdad233fSMatthew Knepley   Input Parameters:
1127bdad233fSMatthew Knepley + ts   - The TS
1128bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1129d763cef2SBarry Smith .vb
1130d763cef2SBarry Smith          U_t = A U
1131d763cef2SBarry Smith          U_t = A(t) U
1132d763cef2SBarry Smith          U_t = F(t,U)
1133d763cef2SBarry Smith .ve
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith    Level: beginner
1136d763cef2SBarry Smith 
1137bdad233fSMatthew Knepley .keywords: TS, problem type
1138bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1139d763cef2SBarry Smith @*/
11407087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1141a7cc72afSBarry Smith {
11429e2a6581SJed Brown   PetscErrorCode ierr;
11439e2a6581SJed Brown 
1144d763cef2SBarry Smith   PetscFunctionBegin;
11450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1146bdad233fSMatthew Knepley   ts->problem_type = type;
11479e2a6581SJed Brown   if (type == TS_LINEAR) {
11489e2a6581SJed Brown     SNES snes;
11499e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11509e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
11519e2a6581SJed Brown   }
1152d763cef2SBarry Smith   PetscFunctionReturn(0);
1153d763cef2SBarry Smith }
1154d763cef2SBarry Smith 
1155bdad233fSMatthew Knepley #undef __FUNCT__
1156bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1157bdad233fSMatthew Knepley /*@C
1158bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1159bdad233fSMatthew Knepley 
1160bdad233fSMatthew Knepley   Not collective
1161bdad233fSMatthew Knepley 
1162bdad233fSMatthew Knepley   Input Parameter:
1163bdad233fSMatthew Knepley . ts   - The TS
1164bdad233fSMatthew Knepley 
1165bdad233fSMatthew Knepley   Output Parameter:
1166bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1167bdad233fSMatthew Knepley .vb
1168089b2837SJed Brown          M U_t = A U
1169089b2837SJed Brown          M(t) U_t = A(t) U
1170bdad233fSMatthew Knepley          U_t = F(t,U)
1171bdad233fSMatthew Knepley .ve
1172bdad233fSMatthew Knepley 
1173bdad233fSMatthew Knepley    Level: beginner
1174bdad233fSMatthew Knepley 
1175bdad233fSMatthew Knepley .keywords: TS, problem type
1176bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1177bdad233fSMatthew Knepley @*/
11787087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1179a7cc72afSBarry Smith {
1180bdad233fSMatthew Knepley   PetscFunctionBegin;
11810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
11824482741eSBarry Smith   PetscValidIntPointer(type,2);
1183bdad233fSMatthew Knepley   *type = ts->problem_type;
1184bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1185bdad233fSMatthew Knepley }
1186d763cef2SBarry Smith 
11874a2ae208SSatish Balay #undef __FUNCT__
11884a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1189d763cef2SBarry Smith /*@
1190d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1191d763cef2SBarry Smith    of a timestepper.
1192d763cef2SBarry Smith 
1193d763cef2SBarry Smith    Collective on TS
1194d763cef2SBarry Smith 
1195d763cef2SBarry Smith    Input Parameter:
1196d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1197d763cef2SBarry Smith 
1198d763cef2SBarry Smith    Notes:
1199d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1200d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1201d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1202d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1203d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1204d763cef2SBarry Smith 
1205d763cef2SBarry Smith    Level: advanced
1206d763cef2SBarry Smith 
1207d763cef2SBarry Smith .keywords: TS, timestep, setup
1208d763cef2SBarry Smith 
1209d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1210d763cef2SBarry Smith @*/
12117087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1212d763cef2SBarry Smith {
1213dfbe8321SBarry Smith   PetscErrorCode ierr;
1214d763cef2SBarry Smith 
1215d763cef2SBarry Smith   PetscFunctionBegin;
12160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1217277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1218277b19d0SLisandro Dalcin 
12197adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
12209596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1221d763cef2SBarry Smith   }
1222a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1223277b19d0SLisandro Dalcin 
1224277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1225277b19d0SLisandro Dalcin 
12261c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
12271c3436cfSJed Brown 
1228277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1229000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1230277b19d0SLisandro Dalcin   }
1231277b19d0SLisandro Dalcin 
1232277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1233277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1234277b19d0SLisandro Dalcin }
1235277b19d0SLisandro Dalcin 
1236277b19d0SLisandro Dalcin #undef __FUNCT__
1237277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1238277b19d0SLisandro Dalcin /*@
1239277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1240277b19d0SLisandro Dalcin 
1241277b19d0SLisandro Dalcin    Collective on TS
1242277b19d0SLisandro Dalcin 
1243277b19d0SLisandro Dalcin    Input Parameter:
1244277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1245277b19d0SLisandro Dalcin 
1246277b19d0SLisandro Dalcin    Level: beginner
1247277b19d0SLisandro Dalcin 
1248277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1249277b19d0SLisandro Dalcin 
1250277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1251277b19d0SLisandro Dalcin @*/
1252277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1253277b19d0SLisandro Dalcin {
1254277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1255277b19d0SLisandro Dalcin 
1256277b19d0SLisandro Dalcin   PetscFunctionBegin;
1257277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1258277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1259277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1260277b19d0SLisandro Dalcin   }
1261277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
12624e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
12634e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1264214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
12656bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1266*e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1267*e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
126838637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1269277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1270d763cef2SBarry Smith   PetscFunctionReturn(0);
1271d763cef2SBarry Smith }
1272d763cef2SBarry Smith 
12734a2ae208SSatish Balay #undef __FUNCT__
12744a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1275d8e5e3e6SSatish Balay /*@
1276d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1277d763cef2SBarry Smith    with TSCreate().
1278d763cef2SBarry Smith 
1279d763cef2SBarry Smith    Collective on TS
1280d763cef2SBarry Smith 
1281d763cef2SBarry Smith    Input Parameter:
1282d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1283d763cef2SBarry Smith 
1284d763cef2SBarry Smith    Level: beginner
1285d763cef2SBarry Smith 
1286d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1287d763cef2SBarry Smith 
1288d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1289d763cef2SBarry Smith @*/
12906bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1291d763cef2SBarry Smith {
12926849ba73SBarry Smith   PetscErrorCode ierr;
1293d763cef2SBarry Smith 
1294d763cef2SBarry Smith   PetscFunctionBegin;
12956bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
12966bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
12976bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1298d763cef2SBarry Smith 
12996bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1300277b19d0SLisandro Dalcin 
1301be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13026bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
13036bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
13046d4c513bSLisandro Dalcin 
130584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
13066bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
13076bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
13086bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
13096d4c513bSLisandro Dalcin 
13103b5f76d0SSean Farley   ierr = PetscFree((*ts)->userops);
13113b5f76d0SSean Farley 
1312a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1313d763cef2SBarry Smith   PetscFunctionReturn(0);
1314d763cef2SBarry Smith }
1315d763cef2SBarry Smith 
13164a2ae208SSatish Balay #undef __FUNCT__
13174a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1318d8e5e3e6SSatish Balay /*@
1319d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1320d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1321d763cef2SBarry Smith 
1322d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1323d763cef2SBarry Smith 
1324d763cef2SBarry Smith    Input Parameter:
1325d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1326d763cef2SBarry Smith 
1327d763cef2SBarry Smith    Output Parameter:
1328d763cef2SBarry Smith .  snes - the nonlinear solver context
1329d763cef2SBarry Smith 
1330d763cef2SBarry Smith    Notes:
1331d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1332d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
133394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1334d763cef2SBarry Smith 
1335d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1336d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1337d763cef2SBarry Smith 
1338d763cef2SBarry Smith    Level: beginner
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith .keywords: timestep, get, SNES
1341d763cef2SBarry Smith @*/
13427087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1343d763cef2SBarry Smith {
1344d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1345d372ba47SLisandro Dalcin 
1346d763cef2SBarry Smith   PetscFunctionBegin;
13470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13484482741eSBarry Smith   PetscValidPointer(snes,2);
1349d372ba47SLisandro Dalcin   if (!ts->snes) {
1350d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
1351d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1352d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1353496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
13549e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
13559e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
13569e2a6581SJed Brown     }
1357d372ba47SLisandro Dalcin   }
1358d763cef2SBarry Smith   *snes = ts->snes;
1359d763cef2SBarry Smith   PetscFunctionReturn(0);
1360d763cef2SBarry Smith }
1361d763cef2SBarry Smith 
13624a2ae208SSatish Balay #undef __FUNCT__
136394b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1364d8e5e3e6SSatish Balay /*@
136594b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1366d763cef2SBarry Smith    a TS (timestepper) context.
1367d763cef2SBarry Smith 
136894b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith    Input Parameter:
1371d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1372d763cef2SBarry Smith 
1373d763cef2SBarry Smith    Output Parameter:
137494b7f48cSBarry Smith .  ksp - the nonlinear solver context
1375d763cef2SBarry Smith 
1376d763cef2SBarry Smith    Notes:
137794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1378d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1379d763cef2SBarry Smith    KSP and PC contexts as well.
1380d763cef2SBarry Smith 
138194b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
138294b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1383d763cef2SBarry Smith 
1384d763cef2SBarry Smith    Level: beginner
1385d763cef2SBarry Smith 
138694b7f48cSBarry Smith .keywords: timestep, get, KSP
1387d763cef2SBarry Smith @*/
13887087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1389d763cef2SBarry Smith {
1390d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1391089b2837SJed Brown   SNES           snes;
1392d372ba47SLisandro Dalcin 
1393d763cef2SBarry Smith   PetscFunctionBegin;
13940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13954482741eSBarry Smith   PetscValidPointer(ksp,2);
139617186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1397e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1398089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1399089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1400d763cef2SBarry Smith   PetscFunctionReturn(0);
1401d763cef2SBarry Smith }
1402d763cef2SBarry Smith 
1403d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1404d763cef2SBarry Smith 
14054a2ae208SSatish Balay #undef __FUNCT__
1406adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1407adb62b0dSMatthew Knepley /*@
1408adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1409adb62b0dSMatthew Knepley    maximum time for iteration.
1410adb62b0dSMatthew Knepley 
14113f9fe445SBarry Smith    Not Collective
1412adb62b0dSMatthew Knepley 
1413adb62b0dSMatthew Knepley    Input Parameters:
1414adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1415adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1416adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1417adb62b0dSMatthew Knepley 
1418adb62b0dSMatthew Knepley    Level: intermediate
1419adb62b0dSMatthew Knepley 
1420adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1421adb62b0dSMatthew Knepley @*/
14227087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1423adb62b0dSMatthew Knepley {
1424adb62b0dSMatthew Knepley   PetscFunctionBegin;
14250700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1426abc0a331SBarry Smith   if (maxsteps) {
14274482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1428adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1429adb62b0dSMatthew Knepley   }
1430abc0a331SBarry Smith   if (maxtime) {
14314482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1432adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1433adb62b0dSMatthew Knepley   }
1434adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1435adb62b0dSMatthew Knepley }
1436adb62b0dSMatthew Knepley 
1437adb62b0dSMatthew Knepley #undef __FUNCT__
14384a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1439d763cef2SBarry Smith /*@
1440d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1441d763cef2SBarry Smith    maximum time for iteration.
1442d763cef2SBarry Smith 
14433f9fe445SBarry Smith    Logically Collective on TS
1444d763cef2SBarry Smith 
1445d763cef2SBarry Smith    Input Parameters:
1446d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1447d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1448d763cef2SBarry Smith -  maxtime - final time to iterate to
1449d763cef2SBarry Smith 
1450d763cef2SBarry Smith    Options Database Keys:
1451d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
14523bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith    Notes:
1455d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1456d763cef2SBarry Smith 
1457d763cef2SBarry Smith    Level: intermediate
1458d763cef2SBarry Smith 
1459d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1460a43b19c4SJed Brown 
1461a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1462d763cef2SBarry Smith @*/
14637087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1464d763cef2SBarry Smith {
1465d763cef2SBarry Smith   PetscFunctionBegin;
14660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1467c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1468c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
146939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
147039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1471d763cef2SBarry Smith   PetscFunctionReturn(0);
1472d763cef2SBarry Smith }
1473d763cef2SBarry Smith 
14744a2ae208SSatish Balay #undef __FUNCT__
14754a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1476d763cef2SBarry Smith /*@
1477d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1478d763cef2SBarry Smith    for use by the TS routines.
1479d763cef2SBarry Smith 
14803f9fe445SBarry Smith    Logically Collective on TS and Vec
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith    Input Parameters:
1483d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1484d763cef2SBarry Smith -  x - the solution vector
1485d763cef2SBarry Smith 
1486d763cef2SBarry Smith    Level: beginner
1487d763cef2SBarry Smith 
1488d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1489d763cef2SBarry Smith @*/
14907087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1491d763cef2SBarry Smith {
14928737fe31SLisandro Dalcin   PetscErrorCode ierr;
1493496e6a7aSJed Brown   DM             dm;
14948737fe31SLisandro Dalcin 
1495d763cef2SBarry Smith   PetscFunctionBegin;
14960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14970700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
14988737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
14996bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
15008737fe31SLisandro Dalcin   ts->vec_sol = x;
1501496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1502496e6a7aSJed Brown   ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr);
1503d763cef2SBarry Smith   PetscFunctionReturn(0);
1504d763cef2SBarry Smith }
1505d763cef2SBarry Smith 
1506e74ef692SMatthew Knepley #undef __FUNCT__
1507e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1508ac226902SBarry Smith /*@C
1509000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
15103f2090d5SJed Brown   called once at the beginning of each time step.
1511000e7ae3SMatthew Knepley 
15123f9fe445SBarry Smith   Logically Collective on TS
1513000e7ae3SMatthew Knepley 
1514000e7ae3SMatthew Knepley   Input Parameters:
1515000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1516000e7ae3SMatthew Knepley - func - The function
1517000e7ae3SMatthew Knepley 
1518000e7ae3SMatthew Knepley   Calling sequence of func:
1519000e7ae3SMatthew Knepley . func (TS ts);
1520000e7ae3SMatthew Knepley 
1521000e7ae3SMatthew Knepley   Level: intermediate
1522000e7ae3SMatthew Knepley 
1523000e7ae3SMatthew Knepley .keywords: TS, timestep
1524000e7ae3SMatthew Knepley @*/
15257087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1526000e7ae3SMatthew Knepley {
1527000e7ae3SMatthew Knepley   PetscFunctionBegin;
15280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1529000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1530000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1531000e7ae3SMatthew Knepley }
1532000e7ae3SMatthew Knepley 
1533e74ef692SMatthew Knepley #undef __FUNCT__
15343f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
153509ee8438SJed Brown /*@
15363f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
15373f2090d5SJed Brown 
15383f2090d5SJed Brown   Collective on TS
15393f2090d5SJed Brown 
15403f2090d5SJed Brown   Input Parameters:
15413f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
15423f2090d5SJed Brown 
15433f2090d5SJed Brown   Notes:
15443f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
15453f2090d5SJed Brown   so most users would not generally call this routine themselves.
15463f2090d5SJed Brown 
15473f2090d5SJed Brown   Level: developer
15483f2090d5SJed Brown 
15493f2090d5SJed Brown .keywords: TS, timestep
15503f2090d5SJed Brown @*/
15517087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
15523f2090d5SJed Brown {
15533f2090d5SJed Brown   PetscErrorCode ierr;
15543f2090d5SJed Brown 
15553f2090d5SJed Brown   PetscFunctionBegin;
15560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
155772ac3e02SJed Brown   if (ts->ops->prestep) {
15583f2090d5SJed Brown     PetscStackPush("TS PreStep function");
15593f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
15603f2090d5SJed Brown     PetscStackPop;
1561312ce896SJed Brown   }
15623f2090d5SJed Brown   PetscFunctionReturn(0);
15633f2090d5SJed Brown }
15643f2090d5SJed Brown 
15653f2090d5SJed Brown #undef __FUNCT__
1566e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1567ac226902SBarry Smith /*@C
1568000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
15693f2090d5SJed Brown   called once at the end of each time step.
1570000e7ae3SMatthew Knepley 
15713f9fe445SBarry Smith   Logically Collective on TS
1572000e7ae3SMatthew Knepley 
1573000e7ae3SMatthew Knepley   Input Parameters:
1574000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1575000e7ae3SMatthew Knepley - func - The function
1576000e7ae3SMatthew Knepley 
1577000e7ae3SMatthew Knepley   Calling sequence of func:
1578000e7ae3SMatthew Knepley . func (TS ts);
1579000e7ae3SMatthew Knepley 
1580000e7ae3SMatthew Knepley   Level: intermediate
1581000e7ae3SMatthew Knepley 
1582000e7ae3SMatthew Knepley .keywords: TS, timestep
1583000e7ae3SMatthew Knepley @*/
15847087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1585000e7ae3SMatthew Knepley {
1586000e7ae3SMatthew Knepley   PetscFunctionBegin;
15870700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1588000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1589000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1590000e7ae3SMatthew Knepley }
1591000e7ae3SMatthew Knepley 
1592e74ef692SMatthew Knepley #undef __FUNCT__
15933f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
159409ee8438SJed Brown /*@
15953f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
15963f2090d5SJed Brown 
15973f2090d5SJed Brown   Collective on TS
15983f2090d5SJed Brown 
15993f2090d5SJed Brown   Input Parameters:
16003f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
16013f2090d5SJed Brown 
16023f2090d5SJed Brown   Notes:
16033f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
16043f2090d5SJed Brown   so most users would not generally call this routine themselves.
16053f2090d5SJed Brown 
16063f2090d5SJed Brown   Level: developer
16073f2090d5SJed Brown 
16083f2090d5SJed Brown .keywords: TS, timestep
16093f2090d5SJed Brown @*/
16107087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
16113f2090d5SJed Brown {
16123f2090d5SJed Brown   PetscErrorCode ierr;
16133f2090d5SJed Brown 
16143f2090d5SJed Brown   PetscFunctionBegin;
16150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
161672ac3e02SJed Brown   if (ts->ops->poststep) {
16173f2090d5SJed Brown     PetscStackPush("TS PostStep function");
16183f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
16193f2090d5SJed Brown     PetscStackPop;
162072ac3e02SJed Brown   }
16213f2090d5SJed Brown   PetscFunctionReturn(0);
16223f2090d5SJed Brown }
16233f2090d5SJed Brown 
1624d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1625d763cef2SBarry Smith 
16264a2ae208SSatish Balay #undef __FUNCT__
1627a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1628d763cef2SBarry Smith /*@C
1629a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1630d763cef2SBarry Smith    timestep to display the iteration's  progress.
1631d763cef2SBarry Smith 
16323f9fe445SBarry Smith    Logically Collective on TS
1633d763cef2SBarry Smith 
1634d763cef2SBarry Smith    Input Parameters:
1635d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1636e213d8f1SJed Brown .  monitor - monitoring routine
1637329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1638b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1639b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1640b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1641d763cef2SBarry Smith 
1642e213d8f1SJed Brown    Calling sequence of monitor:
1643e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith +    ts - the TS context
1646d763cef2SBarry Smith .    steps - iteration number
16471f06c33eSBarry Smith .    time - current time
1648d763cef2SBarry Smith .    x - current iterate
1649d763cef2SBarry Smith -    mctx - [optional] monitoring context
1650d763cef2SBarry Smith 
1651d763cef2SBarry Smith    Notes:
1652d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1653d763cef2SBarry Smith    already has been loaded.
1654d763cef2SBarry Smith 
1655025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1656025f1a04SBarry Smith 
1657d763cef2SBarry Smith    Level: intermediate
1658d763cef2SBarry Smith 
1659d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1660d763cef2SBarry Smith 
1661a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1662d763cef2SBarry Smith @*/
1663c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1664d763cef2SBarry Smith {
1665d763cef2SBarry Smith   PetscFunctionBegin;
16660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1668d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1669329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1670d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1671d763cef2SBarry Smith   PetscFunctionReturn(0);
1672d763cef2SBarry Smith }
1673d763cef2SBarry Smith 
16744a2ae208SSatish Balay #undef __FUNCT__
1675a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1676d763cef2SBarry Smith /*@C
1677a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1678d763cef2SBarry Smith 
16793f9fe445SBarry Smith    Logically Collective on TS
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Input Parameters:
1682d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1683d763cef2SBarry Smith 
1684d763cef2SBarry Smith    Notes:
1685d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Level: intermediate
1688d763cef2SBarry Smith 
1689d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1690d763cef2SBarry Smith 
1691a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1692d763cef2SBarry Smith @*/
16937087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1694d763cef2SBarry Smith {
1695d952e501SBarry Smith   PetscErrorCode ierr;
1696d952e501SBarry Smith   PetscInt       i;
1697d952e501SBarry Smith 
1698d763cef2SBarry Smith   PetscFunctionBegin;
16990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1700d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1701d952e501SBarry Smith     if (ts->mdestroy[i]) {
17023c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1703d952e501SBarry Smith     }
1704d952e501SBarry Smith   }
1705d763cef2SBarry Smith   ts->numbermonitors = 0;
1706d763cef2SBarry Smith   PetscFunctionReturn(0);
1707d763cef2SBarry Smith }
1708d763cef2SBarry Smith 
17094a2ae208SSatish Balay #undef __FUNCT__
1710a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1711d8e5e3e6SSatish Balay /*@
1712a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
17135516499fSSatish Balay 
17145516499fSSatish Balay    Level: intermediate
171541251cbbSSatish Balay 
17165516499fSSatish Balay .keywords: TS, set, monitor
17175516499fSSatish Balay 
171841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
171941251cbbSSatish Balay @*/
1720649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1721d763cef2SBarry Smith {
1722dfbe8321SBarry Smith   PetscErrorCode ierr;
1723649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1724d132466eSBarry Smith 
1725d763cef2SBarry Smith   PetscFunctionBegin;
1726649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1727971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
1728649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1729d763cef2SBarry Smith   PetscFunctionReturn(0);
1730d763cef2SBarry Smith }
1731d763cef2SBarry Smith 
17324a2ae208SSatish Balay #undef __FUNCT__
1733cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1734cd652676SJed Brown /*@
1735cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1736cd652676SJed Brown 
1737cd652676SJed Brown    Logically Collective on TS
1738cd652676SJed Brown 
1739cd652676SJed Brown    Input Argument:
1740cd652676SJed Brown .  ts - time stepping context
1741cd652676SJed Brown 
1742cd652676SJed Brown    Output Argument:
1743cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1744cd652676SJed Brown 
1745cd652676SJed Brown    Level: intermediate
1746cd652676SJed Brown 
1747cd652676SJed Brown .keywords: TS, set
1748cd652676SJed Brown 
1749cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1750cd652676SJed Brown @*/
1751cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1752cd652676SJed Brown {
1753cd652676SJed Brown 
1754cd652676SJed Brown   PetscFunctionBegin;
1755cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1756cd652676SJed Brown   ts->retain_stages = flg;
1757cd652676SJed Brown   PetscFunctionReturn(0);
1758cd652676SJed Brown }
1759cd652676SJed Brown 
1760cd652676SJed Brown #undef __FUNCT__
1761cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1762cd652676SJed Brown /*@
1763cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1764cd652676SJed Brown 
1765cd652676SJed Brown    Collective on TS
1766cd652676SJed Brown 
1767cd652676SJed Brown    Input Argument:
1768cd652676SJed Brown +  ts - time stepping context
1769cd652676SJed Brown -  t - time to interpolate to
1770cd652676SJed Brown 
1771cd652676SJed Brown    Output Argument:
1772cd652676SJed Brown .  X - state at given time
1773cd652676SJed Brown 
1774cd652676SJed Brown    Notes:
1775cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1776cd652676SJed Brown 
1777cd652676SJed Brown    Level: intermediate
1778cd652676SJed Brown 
1779cd652676SJed Brown    Developer Notes:
1780cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1781cd652676SJed Brown 
1782cd652676SJed Brown .keywords: TS, set
1783cd652676SJed Brown 
1784cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1785cd652676SJed Brown @*/
1786cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1787cd652676SJed Brown {
1788cd652676SJed Brown   PetscErrorCode ierr;
1789cd652676SJed Brown 
1790cd652676SJed Brown   PetscFunctionBegin;
1791cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1792d8cd7023SBarry 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);
1793cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1794cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1795cd652676SJed Brown   PetscFunctionReturn(0);
1796cd652676SJed Brown }
1797cd652676SJed Brown 
1798cd652676SJed Brown #undef __FUNCT__
17994a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1800d763cef2SBarry Smith /*@
18016d9e5789SSean Farley    TSStep - Steps one time step
1802d763cef2SBarry Smith 
1803d763cef2SBarry Smith    Collective on TS
1804d763cef2SBarry Smith 
1805d763cef2SBarry Smith    Input Parameter:
1806d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1807d763cef2SBarry Smith 
18086d9e5789SSean Farley    Level: intermediate
1809d763cef2SBarry Smith 
1810d763cef2SBarry Smith .keywords: TS, timestep, solve
1811d763cef2SBarry Smith 
18126d9e5789SSean Farley .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve()
1813d763cef2SBarry Smith @*/
1814193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1815d763cef2SBarry Smith {
1816362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
1817dfbe8321SBarry Smith   PetscErrorCode ierr;
1818d763cef2SBarry Smith 
1819d763cef2SBarry Smith   PetscFunctionBegin;
18200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1821d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1822d405a339SMatthew Knepley 
1823362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
1824362cd11cSLisandro Dalcin 
1825362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
1826d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1827193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1828d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1829362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
1830362cd11cSLisandro Dalcin 
1831362cd11cSLisandro Dalcin   if (ts->reason < 0) {
1832cef5090cSJed Brown     if (ts->errorifstepfailed) {
1833cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
1834cef5090cSJed 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]);
1835cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
1836cef5090cSJed Brown     }
1837362cd11cSLisandro Dalcin   } else if (!ts->reason) {
1838362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
1839362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
1840362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
1841362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
1842362cd11cSLisandro Dalcin   }
1843362cd11cSLisandro Dalcin 
1844d763cef2SBarry Smith   PetscFunctionReturn(0);
1845d763cef2SBarry Smith }
1846d763cef2SBarry Smith 
18474a2ae208SSatish Balay #undef __FUNCT__
184805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
184905175c85SJed Brown /*@
185005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
185105175c85SJed Brown 
18521c3436cfSJed Brown    Collective on TS
185305175c85SJed Brown 
185405175c85SJed Brown    Input Arguments:
18551c3436cfSJed Brown +  ts - time stepping context
18561c3436cfSJed Brown .  order - desired order of accuracy
18571c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
185805175c85SJed Brown 
185905175c85SJed Brown    Output Arguments:
18601c3436cfSJed Brown .  X - state at the end of the current step
186105175c85SJed Brown 
186205175c85SJed Brown    Level: advanced
186305175c85SJed Brown 
1864108c343cSJed Brown    Notes:
1865108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
1866108c343cSJed 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.
1867108c343cSJed Brown 
18681c3436cfSJed Brown .seealso: TSStep(), TSAdapt
186905175c85SJed Brown @*/
18701c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done)
187105175c85SJed Brown {
187205175c85SJed Brown   PetscErrorCode ierr;
187305175c85SJed Brown 
187405175c85SJed Brown   PetscFunctionBegin;
187505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
187605175c85SJed Brown   PetscValidType(ts,1);
187705175c85SJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
18781c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
18791c3436cfSJed Brown   ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr);
188005175c85SJed Brown   PetscFunctionReturn(0);
188105175c85SJed Brown }
188205175c85SJed Brown 
188305175c85SJed Brown #undef __FUNCT__
18846a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
18856a4d4014SLisandro Dalcin /*@
18866a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
18876a4d4014SLisandro Dalcin 
18886a4d4014SLisandro Dalcin    Collective on TS
18896a4d4014SLisandro Dalcin 
18906a4d4014SLisandro Dalcin    Input Parameter:
18916a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1892362cd11cSLisandro Dalcin -  x - the solution vector
18936a4d4014SLisandro Dalcin 
18945a3a76d0SJed Brown    Output Parameter:
18955a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
18965a3a76d0SJed Brown 
18976a4d4014SLisandro Dalcin    Level: beginner
18986a4d4014SLisandro Dalcin 
18995a3a76d0SJed Brown    Notes:
19005a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
19015a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
19025a3a76d0SJed Brown    stepped over the final time.
19035a3a76d0SJed Brown 
19046a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
19056a4d4014SLisandro Dalcin 
19066a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
19076a4d4014SLisandro Dalcin @*/
19085a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
19096a4d4014SLisandro Dalcin {
19104d7d938eSLisandro Dalcin   PetscBool      flg;
19114d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
19124d7d938eSLisandro Dalcin   PetscViewer    viewer;
19136a4d4014SLisandro Dalcin   PetscErrorCode ierr;
1914f22f69f0SBarry Smith 
19156a4d4014SLisandro Dalcin   PetscFunctionBegin;
19160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1917193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
19185a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
19195a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
19205a3a76d0SJed Brown       Vec y;
19215a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
19225a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
19235a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
19245a3a76d0SJed Brown     }
1925362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
19265a3a76d0SJed Brown   } else {
1927193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
19285a3a76d0SJed Brown   }
1929b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
19306a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
1931193ac0bcSJed Brown   ts->steps = 0;
19325ef26d82SJed Brown   ts->ksp_its = 0;
19335ef26d82SJed Brown   ts->snes_its = 0;
1934c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
1935c610991cSLisandro Dalcin   ts->reject = 0;
1936193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
1937193ac0bcSJed Brown 
1938193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
1939193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
19405a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
1941a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
1942193ac0bcSJed Brown   } else {
19436a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
1944362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1945362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
1946362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
1947362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
1948362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
1949e1a7a14fSJed Brown     while (!ts->reason) {
1950193ac0bcSJed Brown       ierr = TSPreStep(ts);CHKERRQ(ierr);
1951193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
1952193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
1953193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
1954193ac0bcSJed Brown     }
1955362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
1956a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
19575a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
19580574a7fbSJed Brown     } else {
19590574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
19600574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
19610574a7fbSJed Brown     }
1962193ac0bcSJed Brown   }
19634d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
19644d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
19654d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
19664d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
19674d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1968193ac0bcSJed Brown   }
19696a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
19706a4d4014SLisandro Dalcin }
19716a4d4014SLisandro Dalcin 
19726a4d4014SLisandro Dalcin #undef __FUNCT__
19734a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
1974228d79bcSJed Brown /*@
1975228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
1976228d79bcSJed Brown 
1977228d79bcSJed Brown    Collective on TS
1978228d79bcSJed Brown 
1979228d79bcSJed Brown    Input Parameters:
1980228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
1981228d79bcSJed Brown .  step - step number that has just completed
1982228d79bcSJed Brown .  ptime - model time of the state
1983228d79bcSJed Brown -  x - state at the current model time
1984228d79bcSJed Brown 
1985228d79bcSJed Brown    Notes:
1986228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
1987228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
1988228d79bcSJed Brown 
1989228d79bcSJed Brown    Level: advanced
1990228d79bcSJed Brown 
1991228d79bcSJed Brown .keywords: TS, timestep
1992228d79bcSJed Brown @*/
1993a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
1994d763cef2SBarry Smith {
19956849ba73SBarry Smith   PetscErrorCode ierr;
1996a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
1997d763cef2SBarry Smith 
1998d763cef2SBarry Smith   PetscFunctionBegin;
1999d763cef2SBarry Smith   for (i=0; i<n; i++) {
2000142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
2001d763cef2SBarry Smith   }
2002d763cef2SBarry Smith   PetscFunctionReturn(0);
2003d763cef2SBarry Smith }
2004d763cef2SBarry Smith 
2005d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
2006d763cef2SBarry Smith 
20074a2ae208SSatish Balay #undef __FUNCT__
2008a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
2009d763cef2SBarry Smith /*@C
2010a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
2011d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
2012d763cef2SBarry Smith 
2013d763cef2SBarry Smith    Collective on TS
2014d763cef2SBarry Smith 
2015d763cef2SBarry Smith    Input Parameters:
2016d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2017d763cef2SBarry Smith .  label - the title to put in the title bar
20187c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2019d763cef2SBarry Smith -  m, n - the screen width and height in pixels
2020d763cef2SBarry Smith 
2021d763cef2SBarry Smith    Output Parameter:
2022d763cef2SBarry Smith .  draw - the drawing context
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith    Options Database Key:
2025a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith    Notes:
2028a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
2029d763cef2SBarry Smith 
2030d763cef2SBarry Smith    Level: intermediate
2031d763cef2SBarry Smith 
20327c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2033d763cef2SBarry Smith 
2034a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
20357c922b88SBarry Smith 
2036d763cef2SBarry Smith @*/
20377087cfbeSBarry Smith PetscErrorCode  TSMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2038d763cef2SBarry Smith {
2039b0a32e0cSBarry Smith   PetscDraw      win;
2040dfbe8321SBarry Smith   PetscErrorCode ierr;
2041d763cef2SBarry Smith 
2042d763cef2SBarry Smith   PetscFunctionBegin;
2043b0a32e0cSBarry Smith   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
2044b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
2045b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
2046b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
2047d763cef2SBarry Smith 
204852e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
2049d763cef2SBarry Smith   PetscFunctionReturn(0);
2050d763cef2SBarry Smith }
2051d763cef2SBarry Smith 
20524a2ae208SSatish Balay #undef __FUNCT__
2053a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
2054a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2055d763cef2SBarry Smith {
2056b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
205787828ca2SBarry Smith   PetscReal      x,y = ptime;
2058dfbe8321SBarry Smith   PetscErrorCode ierr;
2059d763cef2SBarry Smith 
2060d763cef2SBarry Smith   PetscFunctionBegin;
20617c922b88SBarry Smith   if (!monctx) {
20627c922b88SBarry Smith     MPI_Comm    comm;
2063b0a32e0cSBarry Smith     PetscViewer viewer;
20647c922b88SBarry Smith 
20657c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2066b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
2067b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
20687c922b88SBarry Smith   }
20697c922b88SBarry Smith 
2070b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
207187828ca2SBarry Smith   x = (PetscReal)n;
2072b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2073d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
2074b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2075d763cef2SBarry Smith   }
2076d763cef2SBarry Smith   PetscFunctionReturn(0);
2077d763cef2SBarry Smith }
2078d763cef2SBarry Smith 
20794a2ae208SSatish Balay #undef __FUNCT__
2080a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
2081d763cef2SBarry Smith /*@C
2082a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
2083a6570f20SBarry Smith    with TSMonitorLGCreate().
2084d763cef2SBarry Smith 
2085b0a32e0cSBarry Smith    Collective on PetscDrawLG
2086d763cef2SBarry Smith 
2087d763cef2SBarry Smith    Input Parameter:
2088d763cef2SBarry Smith .  draw - the drawing context
2089d763cef2SBarry Smith 
2090d763cef2SBarry Smith    Level: intermediate
2091d763cef2SBarry Smith 
2092d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2093d763cef2SBarry Smith 
2094a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
2095d763cef2SBarry Smith @*/
20966bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
2097d763cef2SBarry Smith {
2098b0a32e0cSBarry Smith   PetscDraw      draw;
2099dfbe8321SBarry Smith   PetscErrorCode ierr;
2100d763cef2SBarry Smith 
2101d763cef2SBarry Smith   PetscFunctionBegin;
21026bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
21036bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
2104b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
2105d763cef2SBarry Smith   PetscFunctionReturn(0);
2106d763cef2SBarry Smith }
2107d763cef2SBarry Smith 
21084a2ae208SSatish Balay #undef __FUNCT__
21094a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2110d763cef2SBarry Smith /*@
2111d763cef2SBarry Smith    TSGetTime - Gets the current time.
2112d763cef2SBarry Smith 
2113d763cef2SBarry Smith    Not Collective
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith    Input Parameter:
2116d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2117d763cef2SBarry Smith 
2118d763cef2SBarry Smith    Output Parameter:
2119d763cef2SBarry Smith .  t  - the current time
2120d763cef2SBarry Smith 
2121d763cef2SBarry Smith    Level: beginner
2122d763cef2SBarry Smith 
2123d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2124d763cef2SBarry Smith 
2125d763cef2SBarry Smith .keywords: TS, get, time
2126d763cef2SBarry Smith @*/
21277087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2128d763cef2SBarry Smith {
2129d763cef2SBarry Smith   PetscFunctionBegin;
21300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21314482741eSBarry Smith   PetscValidDoublePointer(t,2);
2132d763cef2SBarry Smith   *t = ts->ptime;
2133d763cef2SBarry Smith   PetscFunctionReturn(0);
2134d763cef2SBarry Smith }
2135d763cef2SBarry Smith 
21364a2ae208SSatish Balay #undef __FUNCT__
21376a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
21386a4d4014SLisandro Dalcin /*@
21396a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
21406a4d4014SLisandro Dalcin 
21413f9fe445SBarry Smith    Logically Collective on TS
21426a4d4014SLisandro Dalcin 
21436a4d4014SLisandro Dalcin    Input Parameters:
21446a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
21456a4d4014SLisandro Dalcin -  time - the time
21466a4d4014SLisandro Dalcin 
21476a4d4014SLisandro Dalcin    Level: intermediate
21486a4d4014SLisandro Dalcin 
21496a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
21506a4d4014SLisandro Dalcin 
21516a4d4014SLisandro Dalcin .keywords: TS, set, time
21526a4d4014SLisandro Dalcin @*/
21537087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
21546a4d4014SLisandro Dalcin {
21556a4d4014SLisandro Dalcin   PetscFunctionBegin;
21560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2157c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
21586a4d4014SLisandro Dalcin   ts->ptime = t;
21596a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
21606a4d4014SLisandro Dalcin }
21616a4d4014SLisandro Dalcin 
21626a4d4014SLisandro Dalcin #undef __FUNCT__
21634a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2164d763cef2SBarry Smith /*@C
2165d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2166d763cef2SBarry Smith    TS options in the database.
2167d763cef2SBarry Smith 
21683f9fe445SBarry Smith    Logically Collective on TS
2169d763cef2SBarry Smith 
2170d763cef2SBarry Smith    Input Parameter:
2171d763cef2SBarry Smith +  ts     - The TS context
2172d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Notes:
2175d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2176d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2177d763cef2SBarry Smith    hyphen.
2178d763cef2SBarry Smith 
2179d763cef2SBarry Smith    Level: advanced
2180d763cef2SBarry Smith 
2181d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith .seealso: TSSetFromOptions()
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith @*/
21867087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2187d763cef2SBarry Smith {
2188dfbe8321SBarry Smith   PetscErrorCode ierr;
2189089b2837SJed Brown   SNES           snes;
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith   PetscFunctionBegin;
21920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2193d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2194089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2195089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2196d763cef2SBarry Smith   PetscFunctionReturn(0);
2197d763cef2SBarry Smith }
2198d763cef2SBarry Smith 
2199d763cef2SBarry Smith 
22004a2ae208SSatish Balay #undef __FUNCT__
22014a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2202d763cef2SBarry Smith /*@C
2203d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2204d763cef2SBarry Smith    TS options in the database.
2205d763cef2SBarry Smith 
22063f9fe445SBarry Smith    Logically Collective on TS
2207d763cef2SBarry Smith 
2208d763cef2SBarry Smith    Input Parameter:
2209d763cef2SBarry Smith +  ts     - The TS context
2210d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2211d763cef2SBarry Smith 
2212d763cef2SBarry Smith    Notes:
2213d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2214d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2215d763cef2SBarry Smith    hyphen.
2216d763cef2SBarry Smith 
2217d763cef2SBarry Smith    Level: advanced
2218d763cef2SBarry Smith 
2219d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2220d763cef2SBarry Smith 
2221d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2222d763cef2SBarry Smith 
2223d763cef2SBarry Smith @*/
22247087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2225d763cef2SBarry Smith {
2226dfbe8321SBarry Smith   PetscErrorCode ierr;
2227089b2837SJed Brown   SNES           snes;
2228d763cef2SBarry Smith 
2229d763cef2SBarry Smith   PetscFunctionBegin;
22300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2231d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2232089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2233089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2234d763cef2SBarry Smith   PetscFunctionReturn(0);
2235d763cef2SBarry Smith }
2236d763cef2SBarry Smith 
22374a2ae208SSatish Balay #undef __FUNCT__
22384a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2239d763cef2SBarry Smith /*@C
2240d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2241d763cef2SBarry Smith    TS options in the database.
2242d763cef2SBarry Smith 
2243d763cef2SBarry Smith    Not Collective
2244d763cef2SBarry Smith 
2245d763cef2SBarry Smith    Input Parameter:
2246d763cef2SBarry Smith .  ts - The TS context
2247d763cef2SBarry Smith 
2248d763cef2SBarry Smith    Output Parameter:
2249d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2252d763cef2SBarry Smith    sufficient length to hold the prefix.
2253d763cef2SBarry Smith 
2254d763cef2SBarry Smith    Level: intermediate
2255d763cef2SBarry Smith 
2256d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2259d763cef2SBarry Smith @*/
22607087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2261d763cef2SBarry Smith {
2262dfbe8321SBarry Smith   PetscErrorCode ierr;
2263d763cef2SBarry Smith 
2264d763cef2SBarry Smith   PetscFunctionBegin;
22650700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22664482741eSBarry Smith   PetscValidPointer(prefix,2);
2267d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2268d763cef2SBarry Smith   PetscFunctionReturn(0);
2269d763cef2SBarry Smith }
2270d763cef2SBarry Smith 
22714a2ae208SSatish Balay #undef __FUNCT__
22724a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2273d763cef2SBarry Smith /*@C
2274d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2275d763cef2SBarry Smith 
2276d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Input Parameter:
2279d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Output Parameters:
2282d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2283d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2284089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2285d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2288d763cef2SBarry Smith 
2289d763cef2SBarry Smith    Level: intermediate
2290d763cef2SBarry Smith 
229126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2292d763cef2SBarry Smith 
2293d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2294d763cef2SBarry Smith @*/
2295089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2296d763cef2SBarry Smith {
2297089b2837SJed Brown   PetscErrorCode ierr;
2298089b2837SJed Brown   SNES           snes;
2299089b2837SJed Brown 
2300d763cef2SBarry Smith   PetscFunctionBegin;
2301089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2302089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
23033b5f76d0SSean Farley   if (func) *func = ts->userops->rhsjacobian;
230426d46c62SHong Zhang   if (ctx) *ctx = ts->jacP;
2305d763cef2SBarry Smith   PetscFunctionReturn(0);
2306d763cef2SBarry Smith }
2307d763cef2SBarry Smith 
23081713a123SBarry Smith #undef __FUNCT__
23092eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
23102eca1d9cSJed Brown /*@C
23112eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
23122eca1d9cSJed Brown 
23132eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
23142eca1d9cSJed Brown 
23152eca1d9cSJed Brown    Input Parameter:
23162eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
23172eca1d9cSJed Brown 
23182eca1d9cSJed Brown    Output Parameters:
23192eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
23202eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
23212eca1d9cSJed Brown .  f   - The function to compute the matrices
23222eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
23232eca1d9cSJed Brown 
23242eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
23252eca1d9cSJed Brown 
23262eca1d9cSJed Brown    Level: advanced
23272eca1d9cSJed Brown 
23282eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
23292eca1d9cSJed Brown 
23302eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
23312eca1d9cSJed Brown @*/
23327087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
23332eca1d9cSJed Brown {
2334089b2837SJed Brown   PetscErrorCode ierr;
2335089b2837SJed Brown   SNES           snes;
2336089b2837SJed Brown 
23372eca1d9cSJed Brown   PetscFunctionBegin;
2338089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2339089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
23403b5f76d0SSean Farley   if (f) *f = ts->userops->ijacobian;
23412eca1d9cSJed Brown   if (ctx) *ctx = ts->jacP;
23422eca1d9cSJed Brown   PetscFunctionReturn(0);
23432eca1d9cSJed Brown }
23442eca1d9cSJed Brown 
23456083293cSBarry Smith typedef struct {
23466083293cSBarry Smith   PetscViewer viewer;
23476083293cSBarry Smith   Vec         initialsolution;
23486083293cSBarry Smith   PetscBool   showinitial;
23496083293cSBarry Smith } TSMonitorSolutionCtx;
23506083293cSBarry Smith 
23512eca1d9cSJed Brown #undef __FUNCT__
2352a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
23531713a123SBarry Smith /*@C
2354a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
23551713a123SBarry Smith    VecView() for the solution at each timestep
23561713a123SBarry Smith 
23571713a123SBarry Smith    Collective on TS
23581713a123SBarry Smith 
23591713a123SBarry Smith    Input Parameters:
23601713a123SBarry Smith +  ts - the TS context
23611713a123SBarry Smith .  step - current time-step
2362142b95e3SSatish Balay .  ptime - current time
23631713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
23641713a123SBarry Smith 
23651713a123SBarry Smith    Level: intermediate
23661713a123SBarry Smith 
23671713a123SBarry Smith .keywords: TS,  vector, monitor, view
23681713a123SBarry Smith 
2369a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
23701713a123SBarry Smith @*/
23717087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
23721713a123SBarry Smith {
2373dfbe8321SBarry Smith   PetscErrorCode       ierr;
23746083293cSBarry Smith   TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy;
23751713a123SBarry Smith 
23761713a123SBarry Smith   PetscFunctionBegin;
23776083293cSBarry Smith   if (!step && ictx->showinitial) {
23786083293cSBarry Smith     if (!ictx->initialsolution) {
23796083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
23801713a123SBarry Smith     }
23816083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
23826083293cSBarry Smith   }
23836083293cSBarry Smith   if (ictx->showinitial) {
23846083293cSBarry Smith     PetscReal pause;
23856083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
23866083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
23876083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
23886083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
23896083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
23906083293cSBarry Smith   }
23916083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
23926083293cSBarry Smith   if (ictx->showinitial) {
23936083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
23946083293cSBarry Smith   }
23951713a123SBarry Smith   PetscFunctionReturn(0);
23961713a123SBarry Smith }
23971713a123SBarry Smith 
23981713a123SBarry Smith 
23996c699258SBarry Smith #undef __FUNCT__
24006083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy"
24016083293cSBarry Smith /*@C
24026083293cSBarry Smith    TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution
24036083293cSBarry Smith 
24046083293cSBarry Smith    Collective on TS
24056083293cSBarry Smith 
24066083293cSBarry Smith    Input Parameters:
24076083293cSBarry Smith .    ctx - the monitor context
24086083293cSBarry Smith 
24096083293cSBarry Smith    Level: intermediate
24106083293cSBarry Smith 
24116083293cSBarry Smith .keywords: TS,  vector, monitor, view
24126083293cSBarry Smith 
24136083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
24146083293cSBarry Smith @*/
24156083293cSBarry Smith PetscErrorCode  TSMonitorSolutionDestroy(void **ctx)
24166083293cSBarry Smith {
24176083293cSBarry Smith   PetscErrorCode       ierr;
24186083293cSBarry Smith   TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx;
24196083293cSBarry Smith 
24206083293cSBarry Smith   PetscFunctionBegin;
24216083293cSBarry Smith   ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr);
24226083293cSBarry Smith   ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr);
24236083293cSBarry Smith   ierr = PetscFree(ictx);CHKERRQ(ierr);
24246083293cSBarry Smith   PetscFunctionReturn(0);
24256083293cSBarry Smith }
24266083293cSBarry Smith 
24276083293cSBarry Smith #undef __FUNCT__
24286083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate"
24296083293cSBarry Smith /*@C
24306083293cSBarry Smith    TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution
24316083293cSBarry Smith 
24326083293cSBarry Smith    Collective on TS
24336083293cSBarry Smith 
24346083293cSBarry Smith    Input Parameter:
24356083293cSBarry Smith .    ts - time-step context
24366083293cSBarry Smith 
24376083293cSBarry Smith    Output Patameter:
24386083293cSBarry Smith .    ctx - the monitor context
24396083293cSBarry Smith 
24406083293cSBarry Smith    Level: intermediate
24416083293cSBarry Smith 
24426083293cSBarry Smith .keywords: TS,  vector, monitor, view
24436083293cSBarry Smith 
24446083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
24456083293cSBarry Smith @*/
24466083293cSBarry Smith PetscErrorCode  TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx)
24476083293cSBarry Smith {
24486083293cSBarry Smith   PetscErrorCode       ierr;
24496083293cSBarry Smith   TSMonitorSolutionCtx *ictx;
24506083293cSBarry Smith 
24516083293cSBarry Smith   PetscFunctionBegin;
24526083293cSBarry Smith   ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr);
24536083293cSBarry Smith   *ctx = (void*)ictx;
24546083293cSBarry Smith   if (!viewer) {
24556083293cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
24566083293cSBarry Smith   }
24576083293cSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
24586083293cSBarry Smith   ictx->viewer      = viewer;
24596083293cSBarry Smith   ictx->showinitial = PETSC_FALSE;
24606083293cSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr);
24616083293cSBarry Smith   PetscFunctionReturn(0);
24626083293cSBarry Smith }
24636083293cSBarry Smith 
24646083293cSBarry Smith #undef __FUNCT__
24656c699258SBarry Smith #define __FUNCT__ "TSSetDM"
24666c699258SBarry Smith /*@
24676c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
24686c699258SBarry Smith 
24693f9fe445SBarry Smith    Logically Collective on TS and DM
24706c699258SBarry Smith 
24716c699258SBarry Smith    Input Parameters:
24726c699258SBarry Smith +  ts - the preconditioner context
24736c699258SBarry Smith -  dm - the dm
24746c699258SBarry Smith 
24756c699258SBarry Smith    Level: intermediate
24766c699258SBarry Smith 
24776c699258SBarry Smith 
24786c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
24796c699258SBarry Smith @*/
24807087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
24816c699258SBarry Smith {
24826c699258SBarry Smith   PetscErrorCode ierr;
2483089b2837SJed Brown   SNES           snes;
24846c699258SBarry Smith 
24856c699258SBarry Smith   PetscFunctionBegin;
24860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
248770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
24886bf464f9SBarry Smith   ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
24896c699258SBarry Smith   ts->dm = dm;
2490089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2491089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
24926c699258SBarry Smith   PetscFunctionReturn(0);
24936c699258SBarry Smith }
24946c699258SBarry Smith 
24956c699258SBarry Smith #undef __FUNCT__
24966c699258SBarry Smith #define __FUNCT__ "TSGetDM"
24976c699258SBarry Smith /*@
24986c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
24996c699258SBarry Smith 
25003f9fe445SBarry Smith    Not Collective
25016c699258SBarry Smith 
25026c699258SBarry Smith    Input Parameter:
25036c699258SBarry Smith . ts - the preconditioner context
25046c699258SBarry Smith 
25056c699258SBarry Smith    Output Parameter:
25066c699258SBarry Smith .  dm - the dm
25076c699258SBarry Smith 
25086c699258SBarry Smith    Level: intermediate
25096c699258SBarry Smith 
25106c699258SBarry Smith 
25116c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
25126c699258SBarry Smith @*/
25137087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
25146c699258SBarry Smith {
2515496e6a7aSJed Brown   PetscErrorCode ierr;
2516496e6a7aSJed Brown 
25176c699258SBarry Smith   PetscFunctionBegin;
25180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2519496e6a7aSJed Brown   if (!ts->dm) {
2520496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2521496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2522496e6a7aSJed Brown   }
25236c699258SBarry Smith   *dm = ts->dm;
25246c699258SBarry Smith   PetscFunctionReturn(0);
25256c699258SBarry Smith }
25261713a123SBarry Smith 
25270f5c6efeSJed Brown #undef __FUNCT__
25280f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
25290f5c6efeSJed Brown /*@
25300f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
25310f5c6efeSJed Brown 
25323f9fe445SBarry Smith    Logically Collective on SNES
25330f5c6efeSJed Brown 
25340f5c6efeSJed Brown    Input Parameter:
2535d42a1c89SJed Brown + snes - nonlinear solver
25360f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2537d42a1c89SJed Brown - ctx - user context, must be a TS
25380f5c6efeSJed Brown 
25390f5c6efeSJed Brown    Output Parameter:
25400f5c6efeSJed Brown . F - the nonlinear residual
25410f5c6efeSJed Brown 
25420f5c6efeSJed Brown    Notes:
25430f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
25440f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
25450f5c6efeSJed Brown 
25460f5c6efeSJed Brown    Level: advanced
25470f5c6efeSJed Brown 
25480f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
25490f5c6efeSJed Brown @*/
25507087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
25510f5c6efeSJed Brown {
25520f5c6efeSJed Brown   TS ts = (TS)ctx;
25530f5c6efeSJed Brown   PetscErrorCode ierr;
25540f5c6efeSJed Brown 
25550f5c6efeSJed Brown   PetscFunctionBegin;
25560f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25570f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
25580f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
25590f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
25600f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
25610f5c6efeSJed Brown   PetscFunctionReturn(0);
25620f5c6efeSJed Brown }
25630f5c6efeSJed Brown 
25640f5c6efeSJed Brown #undef __FUNCT__
25650f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
25660f5c6efeSJed Brown /*@
25670f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
25680f5c6efeSJed Brown 
25690f5c6efeSJed Brown    Collective on SNES
25700f5c6efeSJed Brown 
25710f5c6efeSJed Brown    Input Parameter:
25720f5c6efeSJed Brown + snes - nonlinear solver
25730f5c6efeSJed Brown . X - the current state at which to evaluate the residual
25740f5c6efeSJed Brown - ctx - user context, must be a TS
25750f5c6efeSJed Brown 
25760f5c6efeSJed Brown    Output Parameter:
25770f5c6efeSJed Brown + A - the Jacobian
25780f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
25790f5c6efeSJed Brown - flag - indicates any structure change in the matrix
25800f5c6efeSJed Brown 
25810f5c6efeSJed Brown    Notes:
25820f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
25830f5c6efeSJed Brown 
25840f5c6efeSJed Brown    Level: developer
25850f5c6efeSJed Brown 
25860f5c6efeSJed Brown .seealso: SNESSetJacobian()
25870f5c6efeSJed Brown @*/
25887087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
25890f5c6efeSJed Brown {
25900f5c6efeSJed Brown   TS ts = (TS)ctx;
25910f5c6efeSJed Brown   PetscErrorCode ierr;
25920f5c6efeSJed Brown 
25930f5c6efeSJed Brown   PetscFunctionBegin;
25940f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
25950f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
25960f5c6efeSJed Brown   PetscValidPointer(A,3);
25970f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
25980f5c6efeSJed Brown   PetscValidPointer(B,4);
25990f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
26000f5c6efeSJed Brown   PetscValidPointer(flag,5);
26010f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
26020f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
26030f5c6efeSJed Brown   PetscFunctionReturn(0);
26040f5c6efeSJed Brown }
2605325fc9f4SBarry Smith 
26060e4ef248SJed Brown #undef __FUNCT__
26070e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
26080e4ef248SJed Brown /*@C
26090e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
26100e4ef248SJed Brown 
26110e4ef248SJed Brown    Collective on TS
26120e4ef248SJed Brown 
26130e4ef248SJed Brown    Input Arguments:
26140e4ef248SJed Brown +  ts - time stepping context
26150e4ef248SJed Brown .  t - time at which to evaluate
26160e4ef248SJed Brown .  X - state at which to evaluate
26170e4ef248SJed Brown -  ctx - context
26180e4ef248SJed Brown 
26190e4ef248SJed Brown    Output Arguments:
26200e4ef248SJed Brown .  F - right hand side
26210e4ef248SJed Brown 
26220e4ef248SJed Brown    Level: intermediate
26230e4ef248SJed Brown 
26240e4ef248SJed Brown    Notes:
26250e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
26260e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
26270e4ef248SJed Brown 
26280e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
26290e4ef248SJed Brown @*/
26300e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
26310e4ef248SJed Brown {
26320e4ef248SJed Brown   PetscErrorCode ierr;
26330e4ef248SJed Brown   Mat Arhs,Brhs;
26340e4ef248SJed Brown   MatStructure flg2;
26350e4ef248SJed Brown 
26360e4ef248SJed Brown   PetscFunctionBegin;
26370e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
26380e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
26390e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
26400e4ef248SJed Brown   PetscFunctionReturn(0);
26410e4ef248SJed Brown }
26420e4ef248SJed Brown 
26430e4ef248SJed Brown #undef __FUNCT__
26440e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
26450e4ef248SJed Brown /*@C
26460e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
26470e4ef248SJed Brown 
26480e4ef248SJed Brown    Collective on TS
26490e4ef248SJed Brown 
26500e4ef248SJed Brown    Input Arguments:
26510e4ef248SJed Brown +  ts - time stepping context
26520e4ef248SJed Brown .  t - time at which to evaluate
26530e4ef248SJed Brown .  X - state at which to evaluate
26540e4ef248SJed Brown -  ctx - context
26550e4ef248SJed Brown 
26560e4ef248SJed Brown    Output Arguments:
26570e4ef248SJed Brown +  A - pointer to operator
26580e4ef248SJed Brown .  B - pointer to preconditioning matrix
26590e4ef248SJed Brown -  flg - matrix structure flag
26600e4ef248SJed Brown 
26610e4ef248SJed Brown    Level: intermediate
26620e4ef248SJed Brown 
26630e4ef248SJed Brown    Notes:
26640e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
26650e4ef248SJed Brown 
26660e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
26670e4ef248SJed Brown @*/
26680e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
26690e4ef248SJed Brown {
26700e4ef248SJed Brown 
26710e4ef248SJed Brown   PetscFunctionBegin;
26720e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
26730e4ef248SJed Brown   PetscFunctionReturn(0);
26740e4ef248SJed Brown }
26750e4ef248SJed Brown 
26760026cea9SSean Farley #undef __FUNCT__
26770026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
26780026cea9SSean Farley /*@C
26790026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
26800026cea9SSean Farley 
26810026cea9SSean Farley    Collective on TS
26820026cea9SSean Farley 
26830026cea9SSean Farley    Input Arguments:
26840026cea9SSean Farley +  ts - time stepping context
26850026cea9SSean Farley .  t - time at which to evaluate
26860026cea9SSean Farley .  X - state at which to evaluate
26870026cea9SSean Farley .  Xdot - time derivative of state vector
26880026cea9SSean Farley -  ctx - context
26890026cea9SSean Farley 
26900026cea9SSean Farley    Output Arguments:
26910026cea9SSean Farley .  F - left hand side
26920026cea9SSean Farley 
26930026cea9SSean Farley    Level: intermediate
26940026cea9SSean Farley 
26950026cea9SSean Farley    Notes:
26960026cea9SSean Farley    The assumption here is that the left hand side is of the form A*Xdot (and not A*Xdot + B*X). For other cases, the
26970026cea9SSean Farley    user is required to write their own TSComputeIFunction.
26980026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
26990026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
27000026cea9SSean Farley 
27010026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
27020026cea9SSean Farley @*/
27030026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
27040026cea9SSean Farley {
27050026cea9SSean Farley   PetscErrorCode ierr;
27060026cea9SSean Farley   Mat A,B;
27070026cea9SSean Farley   MatStructure flg2;
27080026cea9SSean Farley 
27090026cea9SSean Farley   PetscFunctionBegin;
27100026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
27110026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
27120026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
27130026cea9SSean Farley   PetscFunctionReturn(0);
27140026cea9SSean Farley }
27150026cea9SSean Farley 
27160026cea9SSean Farley #undef __FUNCT__
27170026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
27180026cea9SSean Farley /*@C
271924e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
27200026cea9SSean Farley 
27210026cea9SSean Farley    Collective on TS
27220026cea9SSean Farley 
27230026cea9SSean Farley    Input Arguments:
27240026cea9SSean Farley +  ts - time stepping context
27250026cea9SSean Farley .  t - time at which to evaluate
27260026cea9SSean Farley .  X - state at which to evaluate
27270026cea9SSean Farley .  Xdot - time derivative of state vector
27280026cea9SSean Farley .  shift - shift to apply
27290026cea9SSean Farley -  ctx - context
27300026cea9SSean Farley 
27310026cea9SSean Farley    Output Arguments:
27320026cea9SSean Farley +  A - pointer to operator
27330026cea9SSean Farley .  B - pointer to preconditioning matrix
27340026cea9SSean Farley -  flg - matrix structure flag
27350026cea9SSean Farley 
27360026cea9SSean Farley    Level: intermediate
27370026cea9SSean Farley 
27380026cea9SSean Farley    Notes:
27390026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
27400026cea9SSean Farley 
27410026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
27420026cea9SSean Farley @*/
27430026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
27440026cea9SSean Farley {
27450026cea9SSean Farley 
27460026cea9SSean Farley   PetscFunctionBegin;
27470026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
27480026cea9SSean Farley   PetscFunctionReturn(0);
27490026cea9SSean Farley }
27500026cea9SSean Farley 
27514af1b03aSJed Brown 
27524af1b03aSJed Brown #undef __FUNCT__
27534af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
27544af1b03aSJed Brown /*@
27554af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
27564af1b03aSJed Brown 
27574af1b03aSJed Brown    Not Collective
27584af1b03aSJed Brown 
27594af1b03aSJed Brown    Input Parameter:
27604af1b03aSJed Brown .  ts - the TS context
27614af1b03aSJed Brown 
27624af1b03aSJed Brown    Output Parameter:
27634af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
27644af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
27654af1b03aSJed Brown 
27664af1b03aSJed Brown    Level: intermediate
27674af1b03aSJed Brown 
2768cd652676SJed Brown    Notes:
2769cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
27704af1b03aSJed Brown 
27714af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
27724af1b03aSJed Brown 
27734af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
27744af1b03aSJed Brown @*/
27754af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
27764af1b03aSJed Brown {
27774af1b03aSJed Brown   PetscFunctionBegin;
27784af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27794af1b03aSJed Brown   PetscValidPointer(reason,2);
27804af1b03aSJed Brown   *reason = ts->reason;
27814af1b03aSJed Brown   PetscFunctionReturn(0);
27824af1b03aSJed Brown }
27834af1b03aSJed Brown 
2784fb1732b5SBarry Smith #undef __FUNCT__
27855ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
27869f67acb7SJed Brown /*@
27875ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
27889f67acb7SJed Brown    used by the time integrator.
27899f67acb7SJed Brown 
27909f67acb7SJed Brown    Not Collective
27919f67acb7SJed Brown 
27929f67acb7SJed Brown    Input Parameter:
27939f67acb7SJed Brown .  ts - TS context
27949f67acb7SJed Brown 
27959f67acb7SJed Brown    Output Parameter:
27969f67acb7SJed Brown .  nits - number of nonlinear iterations
27979f67acb7SJed Brown 
27989f67acb7SJed Brown    Notes:
27999f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
28009f67acb7SJed Brown 
28019f67acb7SJed Brown    Level: intermediate
28029f67acb7SJed Brown 
28039f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
28049f67acb7SJed Brown 
28055ef26d82SJed Brown .seealso:  TSGetKSPIterations()
28069f67acb7SJed Brown @*/
28075ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
28089f67acb7SJed Brown {
28099f67acb7SJed Brown   PetscFunctionBegin;
28109f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28119f67acb7SJed Brown   PetscValidIntPointer(nits,2);
28125ef26d82SJed Brown   *nits = ts->snes_its;
28139f67acb7SJed Brown   PetscFunctionReturn(0);
28149f67acb7SJed Brown }
28159f67acb7SJed Brown 
28169f67acb7SJed Brown #undef __FUNCT__
28175ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
28189f67acb7SJed Brown /*@
28195ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
28209f67acb7SJed Brown    used by the time integrator.
28219f67acb7SJed Brown 
28229f67acb7SJed Brown    Not Collective
28239f67acb7SJed Brown 
28249f67acb7SJed Brown    Input Parameter:
28259f67acb7SJed Brown .  ts - TS context
28269f67acb7SJed Brown 
28279f67acb7SJed Brown    Output Parameter:
28289f67acb7SJed Brown .  lits - number of linear iterations
28299f67acb7SJed Brown 
28309f67acb7SJed Brown    Notes:
28319f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
28329f67acb7SJed Brown 
28339f67acb7SJed Brown    Level: intermediate
28349f67acb7SJed Brown 
28359f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
28369f67acb7SJed Brown 
28375ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
28389f67acb7SJed Brown @*/
28395ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
28409f67acb7SJed Brown {
28419f67acb7SJed Brown   PetscFunctionBegin;
28429f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28439f67acb7SJed Brown   PetscValidIntPointer(lits,2);
28445ef26d82SJed Brown   *lits = ts->ksp_its;
28459f67acb7SJed Brown   PetscFunctionReturn(0);
28469f67acb7SJed Brown }
28479f67acb7SJed Brown 
28489f67acb7SJed Brown #undef __FUNCT__
2849cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
2850cef5090cSJed Brown /*@
2851cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
2852cef5090cSJed Brown 
2853cef5090cSJed Brown    Not Collective
2854cef5090cSJed Brown 
2855cef5090cSJed Brown    Input Parameter:
2856cef5090cSJed Brown .  ts - TS context
2857cef5090cSJed Brown 
2858cef5090cSJed Brown    Output Parameter:
2859cef5090cSJed Brown .  rejects - number of steps rejected
2860cef5090cSJed Brown 
2861cef5090cSJed Brown    Notes:
2862cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
2863cef5090cSJed Brown 
2864cef5090cSJed Brown    Level: intermediate
2865cef5090cSJed Brown 
2866cef5090cSJed Brown .keywords: TS, get, number
2867cef5090cSJed Brown 
28685ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
2869cef5090cSJed Brown @*/
2870cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
2871cef5090cSJed Brown {
2872cef5090cSJed Brown   PetscFunctionBegin;
2873cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2874cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
2875cef5090cSJed Brown   *rejects = ts->reject;
2876cef5090cSJed Brown   PetscFunctionReturn(0);
2877cef5090cSJed Brown }
2878cef5090cSJed Brown 
2879cef5090cSJed Brown #undef __FUNCT__
2880cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
2881cef5090cSJed Brown /*@
2882cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
2883cef5090cSJed Brown 
2884cef5090cSJed Brown    Not Collective
2885cef5090cSJed Brown 
2886cef5090cSJed Brown    Input Parameter:
2887cef5090cSJed Brown .  ts - TS context
2888cef5090cSJed Brown 
2889cef5090cSJed Brown    Output Parameter:
2890cef5090cSJed Brown .  fails - number of failed nonlinear solves
2891cef5090cSJed Brown 
2892cef5090cSJed Brown    Notes:
2893cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
2894cef5090cSJed Brown 
2895cef5090cSJed Brown    Level: intermediate
2896cef5090cSJed Brown 
2897cef5090cSJed Brown .keywords: TS, get, number
2898cef5090cSJed Brown 
28995ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
2900cef5090cSJed Brown @*/
2901cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
2902cef5090cSJed Brown {
2903cef5090cSJed Brown   PetscFunctionBegin;
2904cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2905cef5090cSJed Brown   PetscValidIntPointer(fails,2);
2906cef5090cSJed Brown   *fails = ts->num_snes_failures;
2907cef5090cSJed Brown   PetscFunctionReturn(0);
2908cef5090cSJed Brown }
2909cef5090cSJed Brown 
2910cef5090cSJed Brown #undef __FUNCT__
2911cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
2912cef5090cSJed Brown /*@
2913cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
2914cef5090cSJed Brown 
2915cef5090cSJed Brown    Not Collective
2916cef5090cSJed Brown 
2917cef5090cSJed Brown    Input Parameter:
2918cef5090cSJed Brown +  ts - TS context
2919cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
2920cef5090cSJed Brown 
2921cef5090cSJed Brown    Notes:
2922cef5090cSJed Brown    The counter is reset to zero for each step
2923cef5090cSJed Brown 
2924cef5090cSJed Brown    Options Database Key:
2925cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
2926cef5090cSJed Brown 
2927cef5090cSJed Brown    Level: intermediate
2928cef5090cSJed Brown 
2929cef5090cSJed Brown .keywords: TS, set, maximum, number
2930cef5090cSJed Brown 
29315ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
2932cef5090cSJed Brown @*/
2933cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
2934cef5090cSJed Brown {
2935cef5090cSJed Brown   PetscFunctionBegin;
2936cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2937cef5090cSJed Brown   ts->max_reject = rejects;
2938cef5090cSJed Brown   PetscFunctionReturn(0);
2939cef5090cSJed Brown }
2940cef5090cSJed Brown 
2941cef5090cSJed Brown #undef __FUNCT__
2942cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
2943cef5090cSJed Brown /*@
2944cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
2945cef5090cSJed Brown 
2946cef5090cSJed Brown    Not Collective
2947cef5090cSJed Brown 
2948cef5090cSJed Brown    Input Parameter:
2949cef5090cSJed Brown +  ts - TS context
2950cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
2951cef5090cSJed Brown 
2952cef5090cSJed Brown    Notes:
2953cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
2954cef5090cSJed Brown 
2955cef5090cSJed Brown    Options Database Key:
2956cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
2957cef5090cSJed Brown 
2958cef5090cSJed Brown    Level: intermediate
2959cef5090cSJed Brown 
2960cef5090cSJed Brown .keywords: TS, set, maximum, number
2961cef5090cSJed Brown 
29625ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
2963cef5090cSJed Brown @*/
2964cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
2965cef5090cSJed Brown {
2966cef5090cSJed Brown   PetscFunctionBegin;
2967cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2968cef5090cSJed Brown   ts->max_snes_failures = fails;
2969cef5090cSJed Brown   PetscFunctionReturn(0);
2970cef5090cSJed Brown }
2971cef5090cSJed Brown 
2972cef5090cSJed Brown #undef __FUNCT__
2973cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
2974cef5090cSJed Brown /*@
2975cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
2976cef5090cSJed Brown 
2977cef5090cSJed Brown    Not Collective
2978cef5090cSJed Brown 
2979cef5090cSJed Brown    Input Parameter:
2980cef5090cSJed Brown +  ts - TS context
2981cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
2982cef5090cSJed Brown 
2983cef5090cSJed Brown    Options Database Key:
2984cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
2985cef5090cSJed Brown 
2986cef5090cSJed Brown    Level: intermediate
2987cef5090cSJed Brown 
2988cef5090cSJed Brown .keywords: TS, set, error
2989cef5090cSJed Brown 
29905ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
2991cef5090cSJed Brown @*/
2992cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
2993cef5090cSJed Brown {
2994cef5090cSJed Brown   PetscFunctionBegin;
2995cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2996cef5090cSJed Brown   ts->errorifstepfailed = err;
2997cef5090cSJed Brown   PetscFunctionReturn(0);
2998cef5090cSJed Brown }
2999cef5090cSJed Brown 
3000cef5090cSJed Brown #undef __FUNCT__
3001fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3002fb1732b5SBarry Smith /*@C
3003fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3004fb1732b5SBarry Smith 
3005fb1732b5SBarry Smith    Collective on TS
3006fb1732b5SBarry Smith 
3007fb1732b5SBarry Smith    Input Parameters:
3008fb1732b5SBarry Smith +  ts - the TS context
3009fb1732b5SBarry Smith .  step - current time-step
3010fb1732b5SBarry Smith .  ptime - current time
3011ed81e22dSJed Brown .  x - current state
3012fb1732b5SBarry Smith -  viewer - binary viewer
3013fb1732b5SBarry Smith 
3014fb1732b5SBarry Smith    Level: intermediate
3015fb1732b5SBarry Smith 
3016fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3017fb1732b5SBarry Smith 
3018fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3019fb1732b5SBarry Smith @*/
3020ed81e22dSJed Brown PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer)
3021fb1732b5SBarry Smith {
3022fb1732b5SBarry Smith   PetscErrorCode       ierr;
3023ed81e22dSJed Brown   PetscViewer          v = (PetscViewer)viewer;
3024fb1732b5SBarry Smith 
3025fb1732b5SBarry Smith   PetscFunctionBegin;
3026ed81e22dSJed Brown   ierr = VecView(x,v);CHKERRQ(ierr);
3027ed81e22dSJed Brown   PetscFunctionReturn(0);
3028ed81e22dSJed Brown }
3029ed81e22dSJed Brown 
3030ed81e22dSJed Brown #undef __FUNCT__
3031ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3032ed81e22dSJed Brown /*@C
3033ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3034ed81e22dSJed Brown 
3035ed81e22dSJed Brown    Collective on TS
3036ed81e22dSJed Brown 
3037ed81e22dSJed Brown    Input Parameters:
3038ed81e22dSJed Brown +  ts - the TS context
3039ed81e22dSJed Brown .  step - current time-step
3040ed81e22dSJed Brown .  ptime - current time
3041ed81e22dSJed Brown .  x - current state
3042ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3043ed81e22dSJed Brown 
3044ed81e22dSJed Brown    Level: intermediate
3045ed81e22dSJed Brown 
3046ed81e22dSJed Brown    Notes:
3047ed81e22dSJed 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.
3048ed81e22dSJed Brown    These are named according to the file name template.
3049ed81e22dSJed Brown 
3050ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3051ed81e22dSJed Brown 
3052ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3053ed81e22dSJed Brown 
3054ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3055ed81e22dSJed Brown @*/
3056ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate)
3057ed81e22dSJed Brown {
3058ed81e22dSJed Brown   PetscErrorCode ierr;
3059ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3060ed81e22dSJed Brown   PetscViewer    viewer;
3061ed81e22dSJed Brown 
3062ed81e22dSJed Brown   PetscFunctionBegin;
3063ed81e22dSJed Brown   ierr = PetscSNPrintf(filename,sizeof filename,(const char*)filenametemplate,step);CHKERRQ(ierr);
3064ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
3065fb1732b5SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
3066ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3067ed81e22dSJed Brown   PetscFunctionReturn(0);
3068ed81e22dSJed Brown }
3069ed81e22dSJed Brown 
3070ed81e22dSJed Brown #undef __FUNCT__
3071ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3072ed81e22dSJed Brown /*@C
3073ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3074ed81e22dSJed Brown 
3075ed81e22dSJed Brown    Collective on TS
3076ed81e22dSJed Brown 
3077ed81e22dSJed Brown    Input Parameters:
3078ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3079ed81e22dSJed Brown 
3080ed81e22dSJed Brown    Level: intermediate
3081ed81e22dSJed Brown 
3082ed81e22dSJed Brown    Note:
3083ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3084ed81e22dSJed Brown 
3085ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3086ed81e22dSJed Brown 
3087ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3088ed81e22dSJed Brown @*/
3089ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3090ed81e22dSJed Brown {
3091ed81e22dSJed Brown   PetscErrorCode ierr;
3092ed81e22dSJed Brown 
3093ed81e22dSJed Brown   PetscFunctionBegin;
3094ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3095fb1732b5SBarry Smith   PetscFunctionReturn(0);
3096fb1732b5SBarry Smith }
3097fb1732b5SBarry Smith 
309884df9cb4SJed Brown #undef __FUNCT__
309984df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
310084df9cb4SJed Brown /*@
310184df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
310284df9cb4SJed Brown 
3103ed81e22dSJed Brown    Collective on TS if controller has not been created yet
310484df9cb4SJed Brown 
310584df9cb4SJed Brown    Input Arguments:
3106ed81e22dSJed Brown .  ts - time stepping context
310784df9cb4SJed Brown 
310884df9cb4SJed Brown    Output Arguments:
3109ed81e22dSJed Brown .  adapt - adaptive controller
311084df9cb4SJed Brown 
311184df9cb4SJed Brown    Level: intermediate
311284df9cb4SJed Brown 
3113ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
311484df9cb4SJed Brown @*/
311584df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
311684df9cb4SJed Brown {
311784df9cb4SJed Brown   PetscErrorCode ierr;
311884df9cb4SJed Brown 
311984df9cb4SJed Brown   PetscFunctionBegin;
312084df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
312184df9cb4SJed Brown   PetscValidPointer(adapt,2);
312284df9cb4SJed Brown   if (!ts->adapt) {
312384df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
31241c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
31251c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
312684df9cb4SJed Brown   }
312784df9cb4SJed Brown   *adapt = ts->adapt;
312884df9cb4SJed Brown   PetscFunctionReturn(0);
312984df9cb4SJed Brown }
3130d6ebe24aSShri Abhyankar 
3131d6ebe24aSShri Abhyankar #undef __FUNCT__
31321c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
31331c3436cfSJed Brown /*@
31341c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
31351c3436cfSJed Brown 
31361c3436cfSJed Brown    Logically Collective
31371c3436cfSJed Brown 
31381c3436cfSJed Brown    Input Arguments:
31391c3436cfSJed Brown +  ts - time integration context
31401c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
31411c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
31421c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
31431c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
31441c3436cfSJed Brown 
31451c3436cfSJed Brown    Level: beginner
31461c3436cfSJed Brown 
3147c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
31481c3436cfSJed Brown @*/
31491c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
31501c3436cfSJed Brown {
31511c3436cfSJed Brown   PetscErrorCode ierr;
31521c3436cfSJed Brown 
31531c3436cfSJed Brown   PetscFunctionBegin;
3154c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
31551c3436cfSJed Brown   if (vatol) {
31561c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
31571c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
31581c3436cfSJed Brown     ts->vatol = vatol;
31591c3436cfSJed Brown   }
3160c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
31611c3436cfSJed Brown   if (vrtol) {
31621c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
31631c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
31641c3436cfSJed Brown     ts->vrtol = vrtol;
31651c3436cfSJed Brown   }
31661c3436cfSJed Brown   PetscFunctionReturn(0);
31671c3436cfSJed Brown }
31681c3436cfSJed Brown 
31691c3436cfSJed Brown #undef __FUNCT__
3170c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3171c5033834SJed Brown /*@
3172c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3173c5033834SJed Brown 
3174c5033834SJed Brown    Logically Collective
3175c5033834SJed Brown 
3176c5033834SJed Brown    Input Arguments:
3177c5033834SJed Brown .  ts - time integration context
3178c5033834SJed Brown 
3179c5033834SJed Brown    Output Arguments:
3180c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3181c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3182c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3183c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3184c5033834SJed Brown 
3185c5033834SJed Brown    Level: beginner
3186c5033834SJed Brown 
3187c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3188c5033834SJed Brown @*/
3189c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3190c5033834SJed Brown {
3191c5033834SJed Brown 
3192c5033834SJed Brown   PetscFunctionBegin;
3193c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3194c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3195c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3196c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3197c5033834SJed Brown   PetscFunctionReturn(0);
3198c5033834SJed Brown }
3199c5033834SJed Brown 
3200c5033834SJed Brown #undef __FUNCT__
32011c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
32021c3436cfSJed Brown /*@
32031c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
32041c3436cfSJed Brown 
32051c3436cfSJed Brown    Collective on TS
32061c3436cfSJed Brown 
32071c3436cfSJed Brown    Input Arguments:
32081c3436cfSJed Brown +  ts - time stepping context
32091c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
32101c3436cfSJed Brown 
32111c3436cfSJed Brown    Output Arguments:
32121c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
32131c3436cfSJed Brown 
32141c3436cfSJed Brown    Level: developer
32151c3436cfSJed Brown 
32161c3436cfSJed Brown .seealso: TSSetTolerances()
32171c3436cfSJed Brown @*/
32181c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
32191c3436cfSJed Brown {
32201c3436cfSJed Brown   PetscErrorCode ierr;
32211c3436cfSJed Brown   PetscInt i,n,N;
32221c3436cfSJed Brown   const PetscScalar *x,*y;
32231c3436cfSJed Brown   Vec X;
32241c3436cfSJed Brown   PetscReal sum,gsum;
32251c3436cfSJed Brown 
32261c3436cfSJed Brown   PetscFunctionBegin;
32271c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32281c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
32291c3436cfSJed Brown   PetscValidPointer(norm,3);
32301c3436cfSJed Brown   X = ts->vec_sol;
32311c3436cfSJed Brown   PetscCheckSameTypeAndComm(X,1,Y,2);
32321c3436cfSJed Brown   if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
32331c3436cfSJed Brown 
32341c3436cfSJed Brown   ierr = VecGetSize(X,&N);CHKERRQ(ierr);
32351c3436cfSJed Brown   ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr);
32361c3436cfSJed Brown   ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
32371c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
32381c3436cfSJed Brown   sum = 0.;
3239ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3240ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3241ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3242ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3243ceefc113SJed Brown     for (i=0; i<n; i++) {
3244ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3245ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3246ceefc113SJed Brown     }
3247ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3248ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3249ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3250ceefc113SJed Brown     const PetscScalar *atol;
3251ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3252ceefc113SJed Brown     for (i=0; i<n; i++) {
3253ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3254ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3255ceefc113SJed Brown     }
3256ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3257ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3258ceefc113SJed Brown     const PetscScalar *rtol;
3259ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3260ceefc113SJed Brown     for (i=0; i<n; i++) {
3261ceefc113SJed Brown       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3262ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3263ceefc113SJed Brown     }
3264ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3265ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
32661c3436cfSJed Brown     for (i=0; i<n; i++) {
32671c3436cfSJed Brown       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
32681c3436cfSJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
32691c3436cfSJed Brown     }
3270ceefc113SJed Brown   }
32711c3436cfSJed Brown   ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
32721c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
32731c3436cfSJed Brown 
32741c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
32751c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
32761c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
32771c3436cfSJed Brown   PetscFunctionReturn(0);
32781c3436cfSJed Brown }
32791c3436cfSJed Brown 
32801c3436cfSJed Brown #undef __FUNCT__
32818d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
32828d59e960SJed Brown /*@
32838d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
32848d59e960SJed Brown 
32858d59e960SJed Brown    Logically Collective on TS
32868d59e960SJed Brown 
32878d59e960SJed Brown    Input Arguments:
32888d59e960SJed Brown +  ts - time stepping context
32898d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
32908d59e960SJed Brown 
32918d59e960SJed Brown    Note:
32928d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
32938d59e960SJed Brown 
32948d59e960SJed Brown    Level: intermediate
32958d59e960SJed Brown 
32968d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
32978d59e960SJed Brown @*/
32988d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
32998d59e960SJed Brown {
33008d59e960SJed Brown 
33018d59e960SJed Brown   PetscFunctionBegin;
33028d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33038d59e960SJed Brown   ts->cfltime_local = cfltime;
33048d59e960SJed Brown   ts->cfltime = -1.;
33058d59e960SJed Brown   PetscFunctionReturn(0);
33068d59e960SJed Brown }
33078d59e960SJed Brown 
33088d59e960SJed Brown #undef __FUNCT__
33098d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
33108d59e960SJed Brown /*@
33118d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
33128d59e960SJed Brown 
33138d59e960SJed Brown    Collective on TS
33148d59e960SJed Brown 
33158d59e960SJed Brown    Input Arguments:
33168d59e960SJed Brown .  ts - time stepping context
33178d59e960SJed Brown 
33188d59e960SJed Brown    Output Arguments:
33198d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
33208d59e960SJed Brown 
33218d59e960SJed Brown    Level: advanced
33228d59e960SJed Brown 
33238d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
33248d59e960SJed Brown @*/
33258d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
33268d59e960SJed Brown {
33278d59e960SJed Brown   PetscErrorCode ierr;
33288d59e960SJed Brown 
33298d59e960SJed Brown   PetscFunctionBegin;
33308d59e960SJed Brown   if (ts->cfltime < 0) {
33318d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
33328d59e960SJed Brown   }
33338d59e960SJed Brown   *cfltime = ts->cfltime;
33348d59e960SJed Brown   PetscFunctionReturn(0);
33358d59e960SJed Brown }
33368d59e960SJed Brown 
33378d59e960SJed Brown #undef __FUNCT__
3338d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3339d6ebe24aSShri Abhyankar /*@
3340d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3341d6ebe24aSShri Abhyankar 
3342d6ebe24aSShri Abhyankar    Input Parameters:
3343d6ebe24aSShri Abhyankar .  ts   - the TS context.
3344d6ebe24aSShri Abhyankar .  xl   - lower bound.
3345d6ebe24aSShri Abhyankar .  xu   - upper bound.
3346d6ebe24aSShri Abhyankar 
3347d6ebe24aSShri Abhyankar    Notes:
3348d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
334933c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3350d6ebe24aSShri Abhyankar 
33512bd2b0e6SSatish Balay    Level: advanced
33522bd2b0e6SSatish Balay 
3353d6ebe24aSShri Abhyankar @*/
3354d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3355d6ebe24aSShri Abhyankar {
3356d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3357d6ebe24aSShri Abhyankar   SNES           snes;
3358d6ebe24aSShri Abhyankar 
3359d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3360d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3361d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3362d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3363d6ebe24aSShri Abhyankar }
3364d6ebe24aSShri Abhyankar 
3365325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3366c6db04a5SJed Brown #include <mex.h>
3367325fc9f4SBarry Smith 
3368325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3369325fc9f4SBarry Smith 
3370325fc9f4SBarry Smith #undef __FUNCT__
3371325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3372325fc9f4SBarry Smith /*
3373325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3374325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3375325fc9f4SBarry Smith 
3376325fc9f4SBarry Smith    Collective on TS
3377325fc9f4SBarry Smith 
3378325fc9f4SBarry Smith    Input Parameters:
3379325fc9f4SBarry Smith +  snes - the TS context
3380325fc9f4SBarry Smith -  x - input vector
3381325fc9f4SBarry Smith 
3382325fc9f4SBarry Smith    Output Parameter:
3383325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3384325fc9f4SBarry Smith 
3385325fc9f4SBarry Smith    Notes:
3386325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3387325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3388325fc9f4SBarry Smith    themselves.
3389325fc9f4SBarry Smith 
3390325fc9f4SBarry Smith    Level: developer
3391325fc9f4SBarry Smith 
3392325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3393325fc9f4SBarry Smith 
3394325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3395325fc9f4SBarry Smith */
33967087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
3397325fc9f4SBarry Smith {
3398325fc9f4SBarry Smith   PetscErrorCode   ierr;
3399325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3400325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3401325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3402325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3403325fc9f4SBarry Smith 
3404325fc9f4SBarry Smith   PetscFunctionBegin;
3405325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
3406325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3407325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
3408325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
3409325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
3410325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3411325fc9f4SBarry Smith 
3412325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3413325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3414880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
3415325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
3416325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3417325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3418325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3419325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3420325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3421325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3422325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3423325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3424325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3425325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3426325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3427325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3428325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3429325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3430325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3431325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3432325fc9f4SBarry Smith   PetscFunctionReturn(0);
3433325fc9f4SBarry Smith }
3434325fc9f4SBarry Smith 
3435325fc9f4SBarry Smith 
3436325fc9f4SBarry Smith #undef __FUNCT__
3437325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3438325fc9f4SBarry Smith /*
3439325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3440325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3441e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3442325fc9f4SBarry Smith 
3443325fc9f4SBarry Smith    Logically Collective on TS
3444325fc9f4SBarry Smith 
3445325fc9f4SBarry Smith    Input Parameters:
3446325fc9f4SBarry Smith +  ts - the TS context
3447325fc9f4SBarry Smith -  func - function evaluation routine
3448325fc9f4SBarry Smith 
3449325fc9f4SBarry Smith    Calling sequence of func:
3450325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
3451325fc9f4SBarry Smith 
3452325fc9f4SBarry Smith    Level: beginner
3453325fc9f4SBarry Smith 
3454325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3455325fc9f4SBarry Smith 
3456325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3457325fc9f4SBarry Smith */
3458cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3459325fc9f4SBarry Smith {
3460325fc9f4SBarry Smith   PetscErrorCode  ierr;
3461325fc9f4SBarry Smith   TSMatlabContext *sctx;
3462325fc9f4SBarry Smith 
3463325fc9f4SBarry Smith   PetscFunctionBegin;
3464325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3465325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3466325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3467325fc9f4SBarry Smith   /*
3468325fc9f4SBarry Smith      This should work, but it doesn't
3469325fc9f4SBarry Smith   sctx->ctx = ctx;
3470325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3471325fc9f4SBarry Smith   */
3472325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3473cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3474325fc9f4SBarry Smith   PetscFunctionReturn(0);
3475325fc9f4SBarry Smith }
3476325fc9f4SBarry Smith 
3477325fc9f4SBarry Smith #undef __FUNCT__
3478325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3479325fc9f4SBarry Smith /*
3480325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3481325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3482325fc9f4SBarry Smith 
3483325fc9f4SBarry Smith    Collective on TS
3484325fc9f4SBarry Smith 
3485325fc9f4SBarry Smith    Input Parameters:
3486cdcf91faSSean Farley +  ts - the TS context
3487325fc9f4SBarry Smith .  x - input vector
3488325fc9f4SBarry Smith .  A, B - the matrices
3489325fc9f4SBarry Smith -  ctx - user context
3490325fc9f4SBarry Smith 
3491325fc9f4SBarry Smith    Output Parameter:
3492325fc9f4SBarry Smith .  flag - structure of the matrix
3493325fc9f4SBarry Smith 
3494325fc9f4SBarry Smith    Level: developer
3495325fc9f4SBarry Smith 
3496325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3497325fc9f4SBarry Smith 
3498325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3499325fc9f4SBarry Smith @*/
3500cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3501325fc9f4SBarry Smith {
3502325fc9f4SBarry Smith   PetscErrorCode  ierr;
3503325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3504325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3505325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3506325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3507325fc9f4SBarry Smith 
3508325fc9f4SBarry Smith   PetscFunctionBegin;
3509cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3510325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3511325fc9f4SBarry Smith 
3512325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
3513325fc9f4SBarry Smith 
3514cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3515325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3516325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
3517325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
3518325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
3519325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3520325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3521325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3522325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3523325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3524325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3525325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3526325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3527325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3528325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3529325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3530325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3531325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3532325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3533325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3534325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3535325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3536325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3537325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3538325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3539325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3540325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3541325fc9f4SBarry Smith   PetscFunctionReturn(0);
3542325fc9f4SBarry Smith }
3543325fc9f4SBarry Smith 
3544325fc9f4SBarry Smith 
3545325fc9f4SBarry Smith #undef __FUNCT__
3546325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3547325fc9f4SBarry Smith /*
3548325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3549e3c5b3baSBarry 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
3550325fc9f4SBarry Smith 
3551325fc9f4SBarry Smith    Logically Collective on TS
3552325fc9f4SBarry Smith 
3553325fc9f4SBarry Smith    Input Parameters:
3554cdcf91faSSean Farley +  ts - the TS context
3555325fc9f4SBarry Smith .  A,B - Jacobian matrices
3556325fc9f4SBarry Smith .  func - function evaluation routine
3557325fc9f4SBarry Smith -  ctx - user context
3558325fc9f4SBarry Smith 
3559325fc9f4SBarry Smith    Calling sequence of func:
3560cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
3561325fc9f4SBarry Smith 
3562325fc9f4SBarry Smith 
3563325fc9f4SBarry Smith    Level: developer
3564325fc9f4SBarry Smith 
3565325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3566325fc9f4SBarry Smith 
3567325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3568325fc9f4SBarry Smith */
3569cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3570325fc9f4SBarry Smith {
3571325fc9f4SBarry Smith   PetscErrorCode    ierr;
3572325fc9f4SBarry Smith   TSMatlabContext *sctx;
3573325fc9f4SBarry Smith 
3574325fc9f4SBarry Smith   PetscFunctionBegin;
3575325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3576325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3577325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3578325fc9f4SBarry Smith   /*
3579325fc9f4SBarry Smith      This should work, but it doesn't
3580325fc9f4SBarry Smith   sctx->ctx = ctx;
3581325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3582325fc9f4SBarry Smith   */
3583325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3584cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3585325fc9f4SBarry Smith   PetscFunctionReturn(0);
3586325fc9f4SBarry Smith }
3587325fc9f4SBarry Smith 
3588b5b1a830SBarry Smith #undef __FUNCT__
3589b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3590b5b1a830SBarry Smith /*
3591b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3592b5b1a830SBarry Smith 
3593b5b1a830SBarry Smith    Collective on TS
3594b5b1a830SBarry Smith 
3595b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3596b5b1a830SBarry Smith @*/
3597cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
3598b5b1a830SBarry Smith {
3599b5b1a830SBarry Smith   PetscErrorCode  ierr;
3600b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3601a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3602b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3603b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3604b5b1a830SBarry Smith 
3605b5b1a830SBarry Smith   PetscFunctionBegin;
3606cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3607b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
3608b5b1a830SBarry Smith 
3609cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3610b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3611b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3612b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3613b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3614b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3615b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3616b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3617b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3618b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3619b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3620b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3621b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
3622b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
3623b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
3624b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
3625b5b1a830SBarry Smith   PetscFunctionReturn(0);
3626b5b1a830SBarry Smith }
3627b5b1a830SBarry Smith 
3628b5b1a830SBarry Smith 
3629b5b1a830SBarry Smith #undef __FUNCT__
3630b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
3631b5b1a830SBarry Smith /*
3632b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
3633b5b1a830SBarry Smith 
3634b5b1a830SBarry Smith    Level: developer
3635b5b1a830SBarry Smith 
3636b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
3637b5b1a830SBarry Smith 
3638b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3639b5b1a830SBarry Smith */
3640cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
3641b5b1a830SBarry Smith {
3642b5b1a830SBarry Smith   PetscErrorCode    ierr;
3643b5b1a830SBarry Smith   TSMatlabContext *sctx;
3644b5b1a830SBarry Smith 
3645b5b1a830SBarry Smith   PetscFunctionBegin;
3646b5b1a830SBarry Smith   /* currently sctx is memory bleed */
3647b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3648b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3649b5b1a830SBarry Smith   /*
3650b5b1a830SBarry Smith      This should work, but it doesn't
3651b5b1a830SBarry Smith   sctx->ctx = ctx;
3652b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3653b5b1a830SBarry Smith   */
3654b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3655cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3656b5b1a830SBarry Smith   PetscFunctionReturn(0);
3657b5b1a830SBarry Smith }
3658325fc9f4SBarry Smith #endif
3659