xref: /petsc/src/ts/interface/ts.c (revision a80ad3e0b1d13640ca709fadabf46bb33024dbd8)
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;
8031748224SBarry Smith   PetscReal      time_step;
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley   PetscFunctionBegin;
830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
843194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
85a43b19c4SJed Brown     /* Handle TS type options */
86a43b19c4SJed Brown     ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
87bdad233fSMatthew Knepley 
88bdad233fSMatthew Knepley     /* Handle generic TS options */
89bdad233fSMatthew Knepley     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
903bca7d26SBarry Smith     ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
91d8cd7023SBarry Smith     ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,PETSC_NULL);CHKERRQ(ierr);
9231748224SBarry Smith     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
9331748224SBarry Smith     if (flg) {
9431748224SBarry Smith       ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
9531748224SBarry Smith     }
96a43b19c4SJed Brown     opt = ts->exact_final_time == PETSC_DECIDE ? PETSC_FALSE : (PetscBool)ts->exact_final_time;
97a43b19c4SJed Brown     ierr = PetscOptionsBool("-ts_exact_final_time","Interpolate output to stop exactly at the final time","TSSetExactFinalTime",opt,&opt,&flg);CHKERRQ(ierr);
98461e00b3SSean Farley     if (flg) {ierr = TSSetExactFinalTime(ts,opt);CHKERRQ(ierr);}
99cef5090cSJed 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);
100cef5090cSJed 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);
101cef5090cSJed Brown     ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,PETSC_NULL);CHKERRQ(ierr);
1021c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,PETSC_NULL);CHKERRQ(ierr);
1031c3436cfSJed Brown     ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,PETSC_NULL);CHKERRQ(ierr);
104bdad233fSMatthew Knepley 
105bdad233fSMatthew Knepley     /* Monitor options */
106a6570f20SBarry Smith     ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
107eabae89aSBarry Smith     if (flg) {
108649052a6SBarry Smith       ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,monfilename,&monviewer);CHKERRQ(ierr);
109649052a6SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
110bdad233fSMatthew Knepley     }
1115180491cSLisandro Dalcin     ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1125180491cSLisandro Dalcin     if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1135180491cSLisandro Dalcin 
11490d69ab7SBarry Smith     opt  = PETSC_FALSE;
115acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_draw","Monitor timestep size graphically","TSMonitorLG",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
116a7cc72afSBarry Smith     if (opt) {
117*a80ad3e0SBarry Smith       PetscDrawLG lg;
118*a80ad3e0SBarry Smith 
119*a80ad3e0SBarry Smith       ierr = TSMonitorLGCreate(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,&lg);
120*a80ad3e0SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorLG,lg,TSMonitorLGDestroy);CHKERRQ(ierr);
121bdad233fSMatthew Knepley     }
12290d69ab7SBarry Smith     opt  = PETSC_FALSE;
123acfcf0e5SJed Brown     ierr = PetscOptionsBool("-ts_monitor_solution","Monitor solution graphically","TSMonitorSolution",opt,&opt,PETSC_NULL);CHKERRQ(ierr);
124a7cc72afSBarry Smith     if (opt) {
1256083293cSBarry Smith       void        *ctx;
126*a80ad3e0SBarry Smith       PetscViewer viewer;
127*a80ad3e0SBarry Smith 
128*a80ad3e0SBarry Smith       ierr = PetscViewerDrawOpen(((PetscObject)ts)->comm,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,&viewer);
129*a80ad3e0SBarry Smith       ierr = TSMonitorSolutionCreate(ts,viewer,&ctx);CHKERRQ(ierr);
130*a80ad3e0SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1316083293cSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolution,ctx,TSMonitorSolutionDestroy);CHKERRQ(ierr);
132bdad233fSMatthew Knepley     }
133fb1732b5SBarry Smith     opt  = PETSC_FALSE;
134fb1732b5SBarry Smith     ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
135fb1732b5SBarry Smith     if (flg) {
136fb1732b5SBarry Smith       PetscViewer ctx;
137fb1732b5SBarry Smith       if (monfilename[0]) {
138fb1732b5SBarry Smith         ierr = PetscViewerBinaryOpen(((PetscObject)ts)->comm,monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
139fb1732b5SBarry Smith       } else {
140fb1732b5SBarry Smith         ctx = PETSC_VIEWER_BINARY_(((PetscObject)ts)->comm);
141fb1732b5SBarry Smith       }
142fb1732b5SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
143fb1732b5SBarry Smith     }
144ed81e22dSJed Brown     opt  = PETSC_FALSE;
145ed81e22dSJed 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);
146ed81e22dSJed Brown     if (flg) {
147ed81e22dSJed Brown       const char *ptr,*ptr2;
148ed81e22dSJed Brown       char *filetemplate;
149ed81e22dSJed Brown       if (!monfilename[0]) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
150ed81e22dSJed Brown       /* Do some cursory validation of the input. */
151ed81e22dSJed Brown       ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
152ed81e22dSJed Brown       if (!ptr) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
153ed81e22dSJed Brown       for (ptr++ ; ptr && *ptr; ptr++) {
154ed81e22dSJed Brown         ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
155ed81e22dSJed 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");
156ed81e22dSJed Brown         if (ptr2) break;
157ed81e22dSJed Brown       }
158ed81e22dSJed Brown       ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
159ed81e22dSJed Brown       ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
160ed81e22dSJed Brown     }
161bdad233fSMatthew Knepley 
1621c3436cfSJed Brown     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
1631c3436cfSJed Brown     ierr = TSAdaptSetFromOptions(adapt);CHKERRQ(ierr);
1641c3436cfSJed Brown 
165d52bd9f3SBarry Smith     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
166d52bd9f3SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
167d52bd9f3SBarry Smith 
168bdad233fSMatthew Knepley     /* Handle specific TS options */
169abc0a331SBarry Smith     if (ts->ops->setfromoptions) {
170bdad233fSMatthew Knepley       ierr = (*ts->ops->setfromoptions)(ts);CHKERRQ(ierr);
171bdad233fSMatthew Knepley     }
1725d973c19SBarry Smith 
1735d973c19SBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
1745d973c19SBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
175bdad233fSMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
176bdad233fSMatthew Knepley   PetscFunctionReturn(0);
177bdad233fSMatthew Knepley }
178bdad233fSMatthew Knepley 
179bdad233fSMatthew Knepley #undef __FUNCT__
180cdcf91faSSean Farley #undef __FUNCT__
1814a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSJacobian"
182a7a1495cSBarry Smith /*@
1838c385f81SBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
184a7a1495cSBarry Smith       set with TSSetRHSJacobian().
185a7a1495cSBarry Smith 
186a7a1495cSBarry Smith    Collective on TS and Vec
187a7a1495cSBarry Smith 
188a7a1495cSBarry Smith    Input Parameters:
189316643e7SJed Brown +  ts - the TS context
190a7a1495cSBarry Smith .  t - current timestep
191a7a1495cSBarry Smith -  x - input vector
192a7a1495cSBarry Smith 
193a7a1495cSBarry Smith    Output Parameters:
194a7a1495cSBarry Smith +  A - Jacobian matrix
195a7a1495cSBarry Smith .  B - optional preconditioning matrix
196a7a1495cSBarry Smith -  flag - flag indicating matrix structure
197a7a1495cSBarry Smith 
198a7a1495cSBarry Smith    Notes:
199a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
200a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
201a7a1495cSBarry Smith 
20294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
203a7a1495cSBarry Smith    flag parameter.
204a7a1495cSBarry Smith 
205a7a1495cSBarry Smith    Level: developer
206a7a1495cSBarry Smith 
207a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
208a7a1495cSBarry Smith 
20994b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
210a7a1495cSBarry Smith @*/
2117087cfbeSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg)
212a7a1495cSBarry Smith {
213dfbe8321SBarry Smith   PetscErrorCode ierr;
2140e4ef248SJed Brown   PetscInt       Xstate;
21524989b8cSPeter Brune   DM             dm;
21624989b8cSPeter Brune   TSDM           tsdm;
21724989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
21824989b8cSPeter Brune   void           *ctx;
21924989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
220a7a1495cSBarry Smith 
221a7a1495cSBarry Smith   PetscFunctionBegin;
2220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2230700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
224c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,X,3);
22524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22624989b8cSPeter Brune   ierr = DMTSGetContext(dm,&tsdm);CHKERRQ(ierr);
22724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
22824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,PETSC_NULL);CHKERRQ(ierr);
2290e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
2300e4ef248SJed Brown   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == X && ts->rhsjacobian.Xstate == Xstate))) {
2310e4ef248SJed Brown     *flg = ts->rhsjacobian.mstructure;
2320e4ef248SJed Brown     PetscFunctionReturn(0);
233f8ede8e7SJed Brown   }
234d90be118SSean Farley 
23524989b8cSPeter Brune   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
236d90be118SSean Farley 
23724989b8cSPeter Brune   if (rhsjacobianfunc) {
238d5ba7fb7SMatthew Knepley     ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
239a7a1495cSBarry Smith     *flg = DIFFERENT_NONZERO_PATTERN;
240a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
24124989b8cSPeter Brune     ierr = (*rhsjacobianfunc)(ts,t,X,A,B,flg,ctx);CHKERRQ(ierr);
242a7a1495cSBarry Smith     PetscStackPop;
243d5ba7fb7SMatthew Knepley     ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
244a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
2450700a824SBarry Smith     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
2460700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
247ef66eb69SBarry Smith   } else {
248214bc6a2SJed Brown     ierr = MatZeroEntries(*A);CHKERRQ(ierr);
249214bc6a2SJed Brown     if (*A != *B) {ierr = MatZeroEntries(*B);CHKERRQ(ierr);}
250214bc6a2SJed Brown     *flg = SAME_NONZERO_PATTERN;
251ef66eb69SBarry Smith   }
2520e4ef248SJed Brown   ts->rhsjacobian.time = t;
2530e4ef248SJed Brown   ts->rhsjacobian.X = X;
2540e4ef248SJed Brown   ierr = PetscObjectStateQuery((PetscObject)X,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
2550e4ef248SJed Brown   ts->rhsjacobian.mstructure = *flg;
256a7a1495cSBarry Smith   PetscFunctionReturn(0);
257a7a1495cSBarry Smith }
258a7a1495cSBarry Smith 
2594a2ae208SSatish Balay #undef __FUNCT__
2604a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
261316643e7SJed Brown /*@
262d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
263d763cef2SBarry Smith 
264316643e7SJed Brown    Collective on TS and Vec
265316643e7SJed Brown 
266316643e7SJed Brown    Input Parameters:
267316643e7SJed Brown +  ts - the TS context
268316643e7SJed Brown .  t - current time
269316643e7SJed Brown -  x - state vector
270316643e7SJed Brown 
271316643e7SJed Brown    Output Parameter:
272316643e7SJed Brown .  y - right hand side
273316643e7SJed Brown 
274316643e7SJed Brown    Note:
275316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
276316643e7SJed Brown    is used internally within the nonlinear solvers.
277316643e7SJed Brown 
278316643e7SJed Brown    Level: developer
279316643e7SJed Brown 
280316643e7SJed Brown .keywords: TS, compute
281316643e7SJed Brown 
282316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
283316643e7SJed Brown @*/
284dfbe8321SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec x,Vec y)
285d763cef2SBarry Smith {
286dfbe8321SBarry Smith   PetscErrorCode ierr;
287d763cef2SBarry Smith 
28824989b8cSPeter Brune   TSRHSFunction rhsfunction;
28924989b8cSPeter Brune   TSIFunction   ifunction;
29024989b8cSPeter Brune   void          *ctx;
29124989b8cSPeter Brune   DM            dm;
29224989b8cSPeter Brune 
293d763cef2SBarry Smith   PetscFunctionBegin;
2940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2950700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2960700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
29724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
29824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
29924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,PETSC_NULL);CHKERRQ(ierr);
300d763cef2SBarry Smith 
30124989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
302d763cef2SBarry Smith 
303d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
30424989b8cSPeter Brune   if (rhsfunction) {
305d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
30624989b8cSPeter Brune     ierr = (*rhsfunction)(ts,t,x,y,ctx);CHKERRQ(ierr);
307d763cef2SBarry Smith     PetscStackPop;
308214bc6a2SJed Brown   } else {
309214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
310b2cd27e8SJed Brown   }
31144a41b28SSean Farley 
312d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_FunctionEval,ts,x,y,0);CHKERRQ(ierr);
313d763cef2SBarry Smith   PetscFunctionReturn(0);
314d763cef2SBarry Smith }
315d763cef2SBarry Smith 
3164a2ae208SSatish Balay #undef __FUNCT__
317214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
318214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
319214bc6a2SJed Brown {
3202dd45cf8SJed Brown   Vec            F;
321214bc6a2SJed Brown   PetscErrorCode ierr;
322214bc6a2SJed Brown 
323214bc6a2SJed Brown   PetscFunctionBegin;
3240da9a4f0SJed Brown   *Frhs = PETSC_NULL;
3252dd45cf8SJed Brown   ierr = TSGetIFunction(ts,&F,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
326214bc6a2SJed Brown   if (!ts->Frhs) {
3272dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
328214bc6a2SJed Brown   }
329214bc6a2SJed Brown   *Frhs = ts->Frhs;
330214bc6a2SJed Brown   PetscFunctionReturn(0);
331214bc6a2SJed Brown }
332214bc6a2SJed Brown 
333214bc6a2SJed Brown #undef __FUNCT__
334214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
335214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
336214bc6a2SJed Brown {
337214bc6a2SJed Brown   Mat            A,B;
3382dd45cf8SJed Brown   PetscErrorCode ierr;
339214bc6a2SJed Brown 
340214bc6a2SJed Brown   PetscFunctionBegin;
341214bc6a2SJed Brown   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
342214bc6a2SJed Brown   if (Arhs) {
343214bc6a2SJed Brown     if (!ts->Arhs) {
344214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
345214bc6a2SJed Brown     }
346214bc6a2SJed Brown     *Arhs = ts->Arhs;
347214bc6a2SJed Brown   }
348214bc6a2SJed Brown   if (Brhs) {
349214bc6a2SJed Brown     if (!ts->Brhs) {
350214bc6a2SJed Brown       ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
351214bc6a2SJed Brown     }
352214bc6a2SJed Brown     *Brhs = ts->Brhs;
353214bc6a2SJed Brown   }
354214bc6a2SJed Brown   PetscFunctionReturn(0);
355214bc6a2SJed Brown }
356214bc6a2SJed Brown 
357214bc6a2SJed Brown #undef __FUNCT__
358316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
359316643e7SJed Brown /*@
360316643e7SJed Brown    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,X,Xdot)=0
361316643e7SJed Brown 
362316643e7SJed Brown    Collective on TS and Vec
363316643e7SJed Brown 
364316643e7SJed Brown    Input Parameters:
365316643e7SJed Brown +  ts - the TS context
366316643e7SJed Brown .  t - current time
367316643e7SJed Brown .  X - state vector
368214bc6a2SJed Brown .  Xdot - time derivative of state vector
369214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
370316643e7SJed Brown 
371316643e7SJed Brown    Output Parameter:
372316643e7SJed Brown .  Y - right hand side
373316643e7SJed Brown 
374316643e7SJed Brown    Note:
375316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
376316643e7SJed Brown    is used internally within the nonlinear solvers.
377316643e7SJed Brown 
378316643e7SJed Brown    If the user did did not write their equations in implicit form, this
379316643e7SJed Brown    function recasts them in implicit form.
380316643e7SJed Brown 
381316643e7SJed Brown    Level: developer
382316643e7SJed Brown 
383316643e7SJed Brown .keywords: TS, compute
384316643e7SJed Brown 
385316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
386316643e7SJed Brown @*/
387214bc6a2SJed Brown PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec Y,PetscBool imex)
388316643e7SJed Brown {
389316643e7SJed Brown   PetscErrorCode ierr;
39024989b8cSPeter Brune   TSIFunction    ifunction;
39124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
39224989b8cSPeter Brune   void           *ctx;
39324989b8cSPeter Brune   DM             dm;
394316643e7SJed Brown 
395316643e7SJed Brown   PetscFunctionBegin;
3960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3970700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
3980700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
3990700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
400316643e7SJed Brown 
40124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
40224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
40324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,PETSC_NULL);CHKERRQ(ierr);
40424989b8cSPeter Brune 
40524989b8cSPeter Brune   if (!rhsfunction && !ifunction) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
406d90be118SSean Farley 
407316643e7SJed Brown   ierr = PetscLogEventBegin(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
40824989b8cSPeter Brune   if (ifunction) {
409316643e7SJed Brown     PetscStackPush("TS user implicit function");
41024989b8cSPeter Brune     ierr = (*ifunction)(ts,t,X,Xdot,Y,ctx);CHKERRQ(ierr);
411316643e7SJed Brown     PetscStackPop;
412214bc6a2SJed Brown   }
413214bc6a2SJed Brown   if (imex) {
41424989b8cSPeter Brune     if (!ifunction) {
4152dd45cf8SJed Brown       ierr = VecCopy(Xdot,Y);CHKERRQ(ierr);
4162dd45cf8SJed Brown     }
41724989b8cSPeter Brune   } else if (rhsfunction) {
41824989b8cSPeter Brune     if (ifunction) {
419214bc6a2SJed Brown       Vec Frhs;
420214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
421214bc6a2SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Frhs);CHKERRQ(ierr);
422214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
4232dd45cf8SJed Brown     } else {
4242dd45cf8SJed Brown       ierr = TSComputeRHSFunction(ts,t,X,Y);CHKERRQ(ierr);
4252dd45cf8SJed Brown       ierr = VecAYPX(Y,-1,Xdot);CHKERRQ(ierr);
426316643e7SJed Brown     }
4274a6899ffSJed Brown   }
428316643e7SJed Brown   ierr = PetscLogEventEnd(TS_FunctionEval,ts,X,Xdot,Y);CHKERRQ(ierr);
429316643e7SJed Brown   PetscFunctionReturn(0);
430316643e7SJed Brown }
431316643e7SJed Brown 
432316643e7SJed Brown #undef __FUNCT__
433316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
434316643e7SJed Brown /*@
435316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
436316643e7SJed Brown 
437316643e7SJed Brown    Collective on TS and Vec
438316643e7SJed Brown 
439316643e7SJed Brown    Input
440316643e7SJed Brown       Input Parameters:
441316643e7SJed Brown +  ts - the TS context
442316643e7SJed Brown .  t - current timestep
443316643e7SJed Brown .  X - state vector
444316643e7SJed Brown .  Xdot - time derivative of state vector
445214bc6a2SJed Brown .  shift - shift to apply, see note below
446214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
447316643e7SJed Brown 
448316643e7SJed Brown    Output Parameters:
449316643e7SJed Brown +  A - Jacobian matrix
450316643e7SJed Brown .  B - optional preconditioning matrix
451316643e7SJed Brown -  flag - flag indicating matrix structure
452316643e7SJed Brown 
453316643e7SJed Brown    Notes:
454316643e7SJed Brown    If F(t,X,Xdot)=0 is the DAE, the required Jacobian is
455316643e7SJed Brown 
456316643e7SJed Brown    dF/dX + shift*dF/dXdot
457316643e7SJed Brown 
458316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
459316643e7SJed Brown    is used internally within the nonlinear solvers.
460316643e7SJed Brown 
461316643e7SJed Brown    Level: developer
462316643e7SJed Brown 
463316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
464316643e7SJed Brown 
465316643e7SJed Brown .seealso:  TSSetIJacobian()
46663495f91SJed Brown @*/
467214bc6a2SJed Brown PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,PetscBool imex)
468316643e7SJed Brown {
4690026cea9SSean Farley   PetscInt Xstate, Xdotstate;
470316643e7SJed Brown   PetscErrorCode ierr;
47124989b8cSPeter Brune   TSIJacobian    ijacobian;
47224989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
47324989b8cSPeter Brune   DM             dm;
47424989b8cSPeter Brune   void           *ctx;
475316643e7SJed Brown 
476316643e7SJed Brown   PetscFunctionBegin;
47724989b8cSPeter Brune 
4780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4790700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
4800700a824SBarry Smith   PetscValidHeaderSpecific(Xdot,VEC_CLASSID,4);
481316643e7SJed Brown   PetscValidPointer(A,6);
4820700a824SBarry Smith   PetscValidHeaderSpecific(*A,MAT_CLASSID,6);
483316643e7SJed Brown   PetscValidPointer(B,7);
4840700a824SBarry Smith   PetscValidHeaderSpecific(*B,MAT_CLASSID,7);
485316643e7SJed Brown   PetscValidPointer(flg,8);
48624989b8cSPeter Brune 
48724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
48824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
48924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,PETSC_NULL);CHKERRQ(ierr);
49024989b8cSPeter Brune 
4910026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&Xstate);CHKERRQ(ierr);
4920026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&Xdotstate);CHKERRQ(ierr);
4930026cea9SSean 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))) {
4940026cea9SSean Farley     *flg = ts->ijacobian.mstructure;
4950026cea9SSean Farley     ierr = MatScale(*A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4960026cea9SSean Farley     PetscFunctionReturn(0);
4970026cea9SSean Farley   }
498316643e7SJed Brown 
49924989b8cSPeter Brune   if (!rhsjacobian && !ijacobian) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
500316643e7SJed Brown 
5014e684422SJed Brown   *flg = SAME_NONZERO_PATTERN;  /* In case we're solving a linear problem in which case it wouldn't get initialized below. */
502316643e7SJed Brown   ierr = PetscLogEventBegin(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
50324989b8cSPeter Brune   if (ijacobian) {
5042dd45cf8SJed Brown     *flg = DIFFERENT_NONZERO_PATTERN;
505316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
50624989b8cSPeter Brune     ierr = (*ijacobian)(ts,t,X,Xdot,shift,A,B,flg,ctx);CHKERRQ(ierr);
507316643e7SJed Brown     PetscStackPop;
508214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
509214bc6a2SJed Brown     PetscValidHeaderSpecific(*A,MAT_CLASSID,4);
510214bc6a2SJed Brown     PetscValidHeaderSpecific(*B,MAT_CLASSID,5);
5114a6899ffSJed Brown   }
512214bc6a2SJed Brown   if (imex) {
51324989b8cSPeter Brune     if (!ijacobian) {  /* system was written as Xdot = F(t,X) */
514214bc6a2SJed Brown       ierr = MatZeroEntries(*A);CHKERRQ(ierr);
5152dd45cf8SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
516214bc6a2SJed Brown       if (*A != *B) {
517214bc6a2SJed Brown         ierr = MatZeroEntries(*B);CHKERRQ(ierr);
518214bc6a2SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
519214bc6a2SJed Brown       }
520214bc6a2SJed Brown       *flg = SAME_PRECONDITIONER;
521214bc6a2SJed Brown     }
522214bc6a2SJed Brown   } else {
52324989b8cSPeter Brune     if (!ijacobian) {
524214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,A,B,flg);CHKERRQ(ierr);
525214bc6a2SJed Brown       ierr = MatScale(*A,-1);CHKERRQ(ierr);
526214bc6a2SJed Brown       ierr = MatShift(*A,shift);CHKERRQ(ierr);
527316643e7SJed Brown       if (*A != *B) {
528316643e7SJed Brown         ierr = MatScale(*B,-1);CHKERRQ(ierr);
529316643e7SJed Brown         ierr = MatShift(*B,shift);CHKERRQ(ierr);
530316643e7SJed Brown       }
53124989b8cSPeter Brune     } else if (rhsjacobian) {
532214bc6a2SJed Brown       Mat Arhs,Brhs;
533214bc6a2SJed Brown       MatStructure axpy,flg2 = DIFFERENT_NONZERO_PATTERN;
534214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
535214bc6a2SJed Brown       ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
536214bc6a2SJed Brown       axpy = (*flg == flg2) ? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN;
537214bc6a2SJed Brown       ierr = MatAXPY(*A,-1,Arhs,axpy);CHKERRQ(ierr);
538214bc6a2SJed Brown       if (*A != *B) {
539214bc6a2SJed Brown         ierr = MatAXPY(*B,-1,Brhs,axpy);CHKERRQ(ierr);
540214bc6a2SJed Brown       }
541214bc6a2SJed Brown       *flg = PetscMin(*flg,flg2);
542214bc6a2SJed Brown     }
543316643e7SJed Brown   }
5440026cea9SSean Farley 
5450026cea9SSean Farley   ts->ijacobian.time = t;
5460026cea9SSean Farley   ts->ijacobian.X = X;
5470026cea9SSean Farley   ts->ijacobian.Xdot = Xdot;
5480026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)X,&ts->ijacobian.Xstate);CHKERRQ(ierr);
5490026cea9SSean Farley   ierr = PetscObjectStateQuery((PetscObject)Xdot,&ts->ijacobian.Xdotstate);CHKERRQ(ierr);
5500026cea9SSean Farley   ts->ijacobian.shift = shift;
5510026cea9SSean Farley   ts->ijacobian.imex = imex;
5520026cea9SSean Farley   ts->ijacobian.mstructure = *flg;
553316643e7SJed Brown   ierr = PetscLogEventEnd(TS_JacobianEval,ts,X,*A,*B);CHKERRQ(ierr);
554316643e7SJed Brown   PetscFunctionReturn(0);
555316643e7SJed Brown }
556316643e7SJed Brown 
557316643e7SJed Brown #undef __FUNCT__
5584a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
559d763cef2SBarry Smith /*@C
560d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
561d763cef2SBarry Smith     F(t,u), where U_t = F(t,u).
562d763cef2SBarry Smith 
5633f9fe445SBarry Smith     Logically Collective on TS
564d763cef2SBarry Smith 
565d763cef2SBarry Smith     Input Parameters:
566d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
567ca94891dSJed Brown .   r - vector to put the computed right hand side (or PETSC_NULL to have it created)
568d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
569d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
570d763cef2SBarry Smith           function evaluation routine (may be PETSC_NULL)
571d763cef2SBarry Smith 
572d763cef2SBarry Smith     Calling sequence of func:
57387828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
574d763cef2SBarry Smith 
575d763cef2SBarry Smith +   t - current timestep
576d763cef2SBarry Smith .   u - input vector
577d763cef2SBarry Smith .   F - function vector
578d763cef2SBarry Smith -   ctx - [optional] user-defined function context
579d763cef2SBarry Smith 
580d763cef2SBarry Smith     Level: beginner
581d763cef2SBarry Smith 
582d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
583d763cef2SBarry Smith 
584d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian()
585d763cef2SBarry Smith @*/
586089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
587d763cef2SBarry Smith {
588089b2837SJed Brown   PetscErrorCode ierr;
589089b2837SJed Brown   SNES           snes;
590e856ceecSJed Brown   Vec            ralloc = PETSC_NULL;
59124989b8cSPeter Brune   DM             dm;
592d763cef2SBarry Smith 
593089b2837SJed Brown   PetscFunctionBegin;
5940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
595ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
59624989b8cSPeter Brune 
59724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
59824989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
599089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
600e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
601e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
602e856ceecSJed Brown     r = ralloc;
603e856ceecSJed Brown   }
604089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
605e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
606d763cef2SBarry Smith   PetscFunctionReturn(0);
607d763cef2SBarry Smith }
608d763cef2SBarry Smith 
6094a2ae208SSatish Balay #undef __FUNCT__
6104a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
611d763cef2SBarry Smith /*@C
612d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
613d763cef2SBarry Smith    where U_t = F(U,t), as well as the location to store the matrix.
614d763cef2SBarry Smith 
6153f9fe445SBarry Smith    Logically Collective on TS
616d763cef2SBarry Smith 
617d763cef2SBarry Smith    Input Parameters:
618d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
619d763cef2SBarry Smith .  A   - Jacobian matrix
620d763cef2SBarry Smith .  B   - preconditioner matrix (usually same as A)
621d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
622d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
623d763cef2SBarry Smith          Jacobian evaluation routine (may be PETSC_NULL)
624d763cef2SBarry Smith 
625d763cef2SBarry Smith    Calling sequence of func:
62687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat *A,Mat *B,MatStructure *flag,void *ctx);
627d763cef2SBarry Smith 
628d763cef2SBarry Smith +  t - current timestep
629d763cef2SBarry Smith .  u - input vector
630d763cef2SBarry Smith .  A - matrix A, where U_t = A(t)u
631d763cef2SBarry Smith .  B - preconditioner matrix, usually the same as A
632d763cef2SBarry Smith .  flag - flag indicating information about the preconditioner matrix
63394b7f48cSBarry Smith           structure (same as flag in KSPSetOperators())
634d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
635d763cef2SBarry Smith 
636d763cef2SBarry Smith    Notes:
63794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
638d763cef2SBarry Smith    output parameter in the routine func().  Be sure to read this information!
639d763cef2SBarry Smith 
640d763cef2SBarry Smith    The routine func() takes Mat * as the matrix arguments rather than Mat.
641d763cef2SBarry Smith    This allows the matrix evaluation routine to replace A and/or B with a
64256335db2SHong Zhang    completely new matrix structure (not just different matrix elements)
643d763cef2SBarry Smith    when appropriate, for instance, if the nonzero structure is changing
644d763cef2SBarry Smith    throughout the global iterations.
645d763cef2SBarry Smith 
646d763cef2SBarry Smith    Level: beginner
647d763cef2SBarry Smith 
648d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
649d763cef2SBarry Smith 
650ab637aeaSJed Brown .seealso: SNESDefaultComputeJacobianColor(), TSSetRHSFunction()
651d763cef2SBarry Smith 
652d763cef2SBarry Smith @*/
653089b2837SJed Brown PetscErrorCode  TSSetRHSJacobian(TS ts,Mat A,Mat B,TSRHSJacobian f,void *ctx)
654d763cef2SBarry Smith {
655277b19d0SLisandro Dalcin   PetscErrorCode ierr;
656089b2837SJed Brown   SNES           snes;
65724989b8cSPeter Brune   DM             dm;
65824989b8cSPeter Brune   TSIJacobian    ijacobian;
659277b19d0SLisandro Dalcin 
660d763cef2SBarry Smith   PetscFunctionBegin;
6610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
662277b19d0SLisandro Dalcin   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
663277b19d0SLisandro Dalcin   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
664277b19d0SLisandro Dalcin   if (A) PetscCheckSameComm(ts,1,A,2);
665277b19d0SLisandro Dalcin   if (B) PetscCheckSameComm(ts,1,B,3);
666d763cef2SBarry Smith 
66724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
66824989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
66924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,PETSC_NULL);CHKERRQ(ierr);
67024989b8cSPeter Brune 
671089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6725f659677SPeter Brune   if (!ijacobian) {
673089b2837SJed Brown     ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
6740e4ef248SJed Brown   }
6750e4ef248SJed Brown   if (A) {
6760e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
6770e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
6780e4ef248SJed Brown     ts->Arhs = A;
6790e4ef248SJed Brown   }
6800e4ef248SJed Brown   if (B) {
6810e4ef248SJed Brown     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
6820e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
6830e4ef248SJed Brown     ts->Brhs = B;
6840e4ef248SJed Brown   }
685d763cef2SBarry Smith   PetscFunctionReturn(0);
686d763cef2SBarry Smith }
687d763cef2SBarry Smith 
688316643e7SJed Brown 
689316643e7SJed Brown #undef __FUNCT__
690316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
691316643e7SJed Brown /*@C
692316643e7SJed Brown    TSSetIFunction - Set the function to compute F(t,U,U_t) where F = 0 is the DAE to be solved.
693316643e7SJed Brown 
6943f9fe445SBarry Smith    Logically Collective on TS
695316643e7SJed Brown 
696316643e7SJed Brown    Input Parameters:
697316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
698ca94891dSJed Brown .  r   - vector to hold the residual (or PETSC_NULL to have it created internally)
699316643e7SJed Brown .  f   - the function evaluation routine
700316643e7SJed Brown -  ctx - user-defined context for private data for the function evaluation routine (may be PETSC_NULL)
701316643e7SJed Brown 
702316643e7SJed Brown    Calling sequence of f:
703316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
704316643e7SJed Brown 
705316643e7SJed Brown +  t   - time at step/stage being solved
706316643e7SJed Brown .  u   - state vector
707316643e7SJed Brown .  u_t - time derivative of state vector
708316643e7SJed Brown .  F   - function vector
709316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
710316643e7SJed Brown 
711316643e7SJed Brown    Important:
712d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
713316643e7SJed Brown 
714316643e7SJed Brown    Level: beginner
715316643e7SJed Brown 
716316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
717316643e7SJed Brown 
718d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
719316643e7SJed Brown @*/
720089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
721316643e7SJed Brown {
722089b2837SJed Brown   PetscErrorCode ierr;
723089b2837SJed Brown   SNES           snes;
724e856ceecSJed Brown   Vec            resalloc = PETSC_NULL;
72524989b8cSPeter Brune   DM             dm;
726316643e7SJed Brown 
727316643e7SJed Brown   PetscFunctionBegin;
7280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
729ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
73024989b8cSPeter Brune 
73124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
73224989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
73324989b8cSPeter Brune 
734089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
735e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
736e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
737e856ceecSJed Brown     res = resalloc;
738e856ceecSJed Brown   }
739089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
740e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
74124989b8cSPeter Brune 
742089b2837SJed Brown   PetscFunctionReturn(0);
743089b2837SJed Brown }
744089b2837SJed Brown 
745089b2837SJed Brown #undef __FUNCT__
746089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
747089b2837SJed Brown /*@C
748089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
749089b2837SJed Brown 
750089b2837SJed Brown    Not Collective
751089b2837SJed Brown 
752089b2837SJed Brown    Input Parameter:
753089b2837SJed Brown .  ts - the TS context
754089b2837SJed Brown 
755089b2837SJed Brown    Output Parameter:
756089b2837SJed Brown +  r - vector to hold residual (or PETSC_NULL)
757089b2837SJed Brown .  func - the function to compute residual (or PETSC_NULL)
758089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
759089b2837SJed Brown 
760089b2837SJed Brown    Level: advanced
761089b2837SJed Brown 
762089b2837SJed Brown .keywords: TS, nonlinear, get, function
763089b2837SJed Brown 
764089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
765089b2837SJed Brown @*/
766089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
767089b2837SJed Brown {
768089b2837SJed Brown   PetscErrorCode ierr;
769089b2837SJed Brown   SNES snes;
77024989b8cSPeter Brune   DM   dm;
771089b2837SJed Brown 
772089b2837SJed Brown   PetscFunctionBegin;
773089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
774089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
775089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
77624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
77724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
778089b2837SJed Brown   PetscFunctionReturn(0);
779089b2837SJed Brown }
780089b2837SJed Brown 
781089b2837SJed Brown #undef __FUNCT__
782089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
783089b2837SJed Brown /*@C
784089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
785089b2837SJed Brown 
786089b2837SJed Brown    Not Collective
787089b2837SJed Brown 
788089b2837SJed Brown    Input Parameter:
789089b2837SJed Brown .  ts - the TS context
790089b2837SJed Brown 
791089b2837SJed Brown    Output Parameter:
792089b2837SJed Brown +  r - vector to hold computed right hand side (or PETSC_NULL)
793089b2837SJed Brown .  func - the function to compute right hand side (or PETSC_NULL)
794089b2837SJed Brown -  ctx - the function context (or PETSC_NULL)
795089b2837SJed Brown 
796089b2837SJed Brown    Level: advanced
797089b2837SJed Brown 
798089b2837SJed Brown .keywords: TS, nonlinear, get, function
799089b2837SJed Brown 
800089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
801089b2837SJed Brown @*/
802089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
803089b2837SJed Brown {
804089b2837SJed Brown   PetscErrorCode ierr;
805089b2837SJed Brown   SNES snes;
80624989b8cSPeter Brune   DM   dm;
807089b2837SJed Brown 
808089b2837SJed Brown   PetscFunctionBegin;
809089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
810089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
811089b2837SJed Brown   ierr = SNESGetFunction(snes,r,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
81224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
81324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
814316643e7SJed Brown   PetscFunctionReturn(0);
815316643e7SJed Brown }
816316643e7SJed Brown 
817316643e7SJed Brown #undef __FUNCT__
818316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
819316643e7SJed Brown /*@C
820a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
821a4f0a591SBarry Smith         you provided with TSSetIFunction().
822316643e7SJed Brown 
8233f9fe445SBarry Smith    Logically Collective on TS
824316643e7SJed Brown 
825316643e7SJed Brown    Input Parameters:
826316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
827316643e7SJed Brown .  A   - Jacobian matrix
828316643e7SJed Brown .  B   - preconditioning matrix for A (may be same as A)
829316643e7SJed Brown .  f   - the Jacobian evaluation routine
830316643e7SJed Brown -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be PETSC_NULL)
831316643e7SJed Brown 
832316643e7SJed Brown    Calling sequence of f:
8331b4a444bSJed Brown $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat *A,Mat *B,MatStructure *flag,void *ctx);
834316643e7SJed Brown 
835316643e7SJed Brown +  t    - time at step/stage being solved
8361b4a444bSJed Brown .  U    - state vector
8371b4a444bSJed Brown .  U_t  - time derivative of state vector
838316643e7SJed Brown .  a    - shift
8391b4a444bSJed Brown .  A    - Jacobian of G(U) = F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
840316643e7SJed Brown .  B    - preconditioning matrix for A, may be same as A
841316643e7SJed Brown .  flag - flag indicating information about the preconditioner matrix
842316643e7SJed Brown           structure (same as flag in KSPSetOperators())
843316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
844316643e7SJed Brown 
845316643e7SJed Brown    Notes:
846316643e7SJed Brown    The matrices A and B are exactly the matrices that are used by SNES for the nonlinear solve.
847316643e7SJed Brown 
848a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
849a4f0a591SBarry 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.
850a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
851a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
852a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
853a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
854a4f0a591SBarry Smith 
855316643e7SJed Brown    Level: beginner
856316643e7SJed Brown 
857316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
858316643e7SJed Brown 
859ab637aeaSJed Brown .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESDefaultComputeJacobianColor(), SNESDefaultComputeJacobian()
860316643e7SJed Brown 
861316643e7SJed Brown @*/
8627087cfbeSBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat A,Mat B,TSIJacobian f,void *ctx)
863316643e7SJed Brown {
864316643e7SJed Brown   PetscErrorCode ierr;
865089b2837SJed Brown   SNES           snes;
86624989b8cSPeter Brune   DM             dm;
867316643e7SJed Brown 
868316643e7SJed Brown   PetscFunctionBegin;
8690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8700700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
8710700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
872316643e7SJed Brown   if (A) PetscCheckSameComm(ts,1,A,2);
873316643e7SJed Brown   if (B) PetscCheckSameComm(ts,1,B,3);
87424989b8cSPeter Brune 
87524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
87624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
87724989b8cSPeter Brune 
878089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
879089b2837SJed Brown   ierr = SNESSetJacobian(snes,A,B,SNESTSFormJacobian,ts);CHKERRQ(ierr);
880316643e7SJed Brown   PetscFunctionReturn(0);
881316643e7SJed Brown }
882316643e7SJed Brown 
8834a2ae208SSatish Balay #undef __FUNCT__
8844a2ae208SSatish Balay #define __FUNCT__ "TSView"
8857e2c5f70SBarry Smith /*@C
886d763cef2SBarry Smith     TSView - Prints the TS data structure.
887d763cef2SBarry Smith 
8884c49b128SBarry Smith     Collective on TS
889d763cef2SBarry Smith 
890d763cef2SBarry Smith     Input Parameters:
891d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
892d763cef2SBarry Smith -   viewer - visualization context
893d763cef2SBarry Smith 
894d763cef2SBarry Smith     Options Database Key:
895d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
896d763cef2SBarry Smith 
897d763cef2SBarry Smith     Notes:
898d763cef2SBarry Smith     The available visualization contexts include
899b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
900b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
901d763cef2SBarry Smith          output where only the first processor opens
902d763cef2SBarry Smith          the file.  All other processors send their
903d763cef2SBarry Smith          data to the first processor to print.
904d763cef2SBarry Smith 
905d763cef2SBarry Smith     The user can open an alternative visualization context with
906b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
907d763cef2SBarry Smith 
908d763cef2SBarry Smith     Level: beginner
909d763cef2SBarry Smith 
910d763cef2SBarry Smith .keywords: TS, timestep, view
911d763cef2SBarry Smith 
912b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
913d763cef2SBarry Smith @*/
9147087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
915d763cef2SBarry Smith {
916dfbe8321SBarry Smith   PetscErrorCode ierr;
91719fd82e9SBarry Smith   TSType         type;
918089b2837SJed Brown   PetscBool      iascii,isstring,isundials;
919d763cef2SBarry Smith 
920d763cef2SBarry Smith   PetscFunctionBegin;
9210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9223050cee2SBarry Smith   if (!viewer) {
9237adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)ts)->comm,&viewer);CHKERRQ(ierr);
9243050cee2SBarry Smith   }
9250700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
926c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
927fd16b177SBarry Smith 
928251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
929251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
93032077d6dSBarry Smith   if (iascii) {
931317d6ea6SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer,"TS Object");CHKERRQ(ierr);
93277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
933a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%G\n",ts->max_time);CHKERRQ(ierr);
934d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
9355ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
936c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
937d763cef2SBarry Smith     }
9385ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
939193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
940d52bd9f3SBarry Smith     if (ts->ops->view) {
941d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
942d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
943d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
944d52bd9f3SBarry Smith     }
9450f5bd95cSBarry Smith   } else if (isstring) {
946a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
947b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
948d763cef2SBarry Smith   }
949b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
950251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
951b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
952d763cef2SBarry Smith   PetscFunctionReturn(0);
953d763cef2SBarry Smith }
954d763cef2SBarry Smith 
955d763cef2SBarry Smith 
9564a2ae208SSatish Balay #undef __FUNCT__
9574a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
958b07ff414SBarry Smith /*@
959d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
960d763cef2SBarry Smith    the timesteppers.
961d763cef2SBarry Smith 
9623f9fe445SBarry Smith    Logically Collective on TS
963d763cef2SBarry Smith 
964d763cef2SBarry Smith    Input Parameters:
965d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
966d763cef2SBarry Smith -  usrP - optional user context
967d763cef2SBarry Smith 
968d763cef2SBarry Smith    Level: intermediate
969d763cef2SBarry Smith 
970d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
971d763cef2SBarry Smith 
972d763cef2SBarry Smith .seealso: TSGetApplicationContext()
973d763cef2SBarry Smith @*/
9747087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
975d763cef2SBarry Smith {
976d763cef2SBarry Smith   PetscFunctionBegin;
9770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
978d763cef2SBarry Smith   ts->user = usrP;
979d763cef2SBarry Smith   PetscFunctionReturn(0);
980d763cef2SBarry Smith }
981d763cef2SBarry Smith 
9824a2ae208SSatish Balay #undef __FUNCT__
9834a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
984b07ff414SBarry Smith /*@
985d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
986d763cef2SBarry Smith     timestepper.
987d763cef2SBarry Smith 
988d763cef2SBarry Smith     Not Collective
989d763cef2SBarry Smith 
990d763cef2SBarry Smith     Input Parameter:
991d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
992d763cef2SBarry Smith 
993d763cef2SBarry Smith     Output Parameter:
994d763cef2SBarry Smith .   usrP - user context
995d763cef2SBarry Smith 
996d763cef2SBarry Smith     Level: intermediate
997d763cef2SBarry Smith 
998d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
999d763cef2SBarry Smith 
1000d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1001d763cef2SBarry Smith @*/
1002e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1003d763cef2SBarry Smith {
1004d763cef2SBarry Smith   PetscFunctionBegin;
10050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1006e71120c6SJed Brown   *(void**)usrP = ts->user;
1007d763cef2SBarry Smith   PetscFunctionReturn(0);
1008d763cef2SBarry Smith }
1009d763cef2SBarry Smith 
10104a2ae208SSatish Balay #undef __FUNCT__
10114a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1012d763cef2SBarry Smith /*@
1013b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1014d763cef2SBarry Smith 
1015d763cef2SBarry Smith    Not Collective
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith    Input Parameter:
1018d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1019d763cef2SBarry Smith 
1020d763cef2SBarry Smith    Output Parameter:
1021b8123daeSJed Brown .  iter - number of steps completed so far
1022d763cef2SBarry Smith 
1023d763cef2SBarry Smith    Level: intermediate
1024d763cef2SBarry Smith 
1025d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
1026b8123daeSJed Brown .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStep()
1027d763cef2SBarry Smith @*/
10287087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt* iter)
1029d763cef2SBarry Smith {
1030d763cef2SBarry Smith   PetscFunctionBegin;
10310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10324482741eSBarry Smith   PetscValidIntPointer(iter,2);
1033d763cef2SBarry Smith   *iter = ts->steps;
1034d763cef2SBarry Smith   PetscFunctionReturn(0);
1035d763cef2SBarry Smith }
1036d763cef2SBarry Smith 
10374a2ae208SSatish Balay #undef __FUNCT__
10384a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1039d763cef2SBarry Smith /*@
1040d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1041d763cef2SBarry Smith    as well as the initial time.
1042d763cef2SBarry Smith 
10433f9fe445SBarry Smith    Logically Collective on TS
1044d763cef2SBarry Smith 
1045d763cef2SBarry Smith    Input Parameters:
1046d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1047d763cef2SBarry Smith .  initial_time - the initial time
1048d763cef2SBarry Smith -  time_step - the size of the timestep
1049d763cef2SBarry Smith 
1050d763cef2SBarry Smith    Level: intermediate
1051d763cef2SBarry Smith 
1052d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1053d763cef2SBarry Smith 
1054d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1055d763cef2SBarry Smith @*/
10567087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1057d763cef2SBarry Smith {
1058e144a568SJed Brown   PetscErrorCode ierr;
1059e144a568SJed Brown 
1060d763cef2SBarry Smith   PetscFunctionBegin;
10610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1062e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1063d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1064d763cef2SBarry Smith   PetscFunctionReturn(0);
1065d763cef2SBarry Smith }
1066d763cef2SBarry Smith 
10674a2ae208SSatish Balay #undef __FUNCT__
10684a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1069d763cef2SBarry Smith /*@
1070d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1071d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1072d763cef2SBarry Smith 
10733f9fe445SBarry Smith    Logically Collective on TS
1074d763cef2SBarry Smith 
1075d763cef2SBarry Smith    Input Parameters:
1076d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1077d763cef2SBarry Smith -  time_step - the size of the timestep
1078d763cef2SBarry Smith 
1079d763cef2SBarry Smith    Level: intermediate
1080d763cef2SBarry Smith 
1081d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1082d763cef2SBarry Smith 
1083d763cef2SBarry Smith .keywords: TS, set, timestep
1084d763cef2SBarry Smith @*/
10857087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1086d763cef2SBarry Smith {
1087d763cef2SBarry Smith   PetscFunctionBegin;
10880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1089c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1090d763cef2SBarry Smith   ts->time_step = time_step;
109131748224SBarry Smith   ts->time_step_orig = time_step;
1092d763cef2SBarry Smith   PetscFunctionReturn(0);
1093d763cef2SBarry Smith }
1094d763cef2SBarry Smith 
10954a2ae208SSatish Balay #undef __FUNCT__
1096a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1097a43b19c4SJed Brown /*@
1098a43b19c4SJed Brown    TSSetExactFinalTime - Determines whether to interpolate solution to the
1099a43b19c4SJed Brown       exact final time requested by the user or just returns it at the final time
1100a43b19c4SJed Brown       it computed.
1101a43b19c4SJed Brown 
1102a43b19c4SJed Brown   Logically Collective on TS
1103a43b19c4SJed Brown 
1104a43b19c4SJed Brown    Input Parameter:
1105a43b19c4SJed Brown +   ts - the time-step context
1106a43b19c4SJed Brown -   ft - PETSC_TRUE if interpolates, else PETSC_FALSE
1107a43b19c4SJed Brown 
1108a43b19c4SJed Brown    Level: beginner
1109a43b19c4SJed Brown 
1110a43b19c4SJed Brown .seealso: TSSetDuration()
1111a43b19c4SJed Brown @*/
1112a43b19c4SJed Brown PetscErrorCode  TSSetExactFinalTime(TS ts,PetscBool flg)
1113a43b19c4SJed Brown {
1114a43b19c4SJed Brown 
1115a43b19c4SJed Brown   PetscFunctionBegin;
1116a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1117d8cd7023SBarry Smith   PetscValidLogicalCollectiveBool(ts,flg,2);
1118a43b19c4SJed Brown   ts->exact_final_time = flg;
1119a43b19c4SJed Brown   PetscFunctionReturn(0);
1120a43b19c4SJed Brown }
1121a43b19c4SJed Brown 
1122a43b19c4SJed Brown #undef __FUNCT__
11234a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1124d763cef2SBarry Smith /*@
1125d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1126d763cef2SBarry Smith 
1127d763cef2SBarry Smith    Not Collective
1128d763cef2SBarry Smith 
1129d763cef2SBarry Smith    Input Parameter:
1130d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1131d763cef2SBarry Smith 
1132d763cef2SBarry Smith    Output Parameter:
1133d763cef2SBarry Smith .  dt - the current timestep size
1134d763cef2SBarry Smith 
1135d763cef2SBarry Smith    Level: intermediate
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1138d763cef2SBarry Smith 
1139d763cef2SBarry Smith .keywords: TS, get, timestep
1140d763cef2SBarry Smith @*/
11417087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal* dt)
1142d763cef2SBarry Smith {
1143d763cef2SBarry Smith   PetscFunctionBegin;
11440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1145f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1146d763cef2SBarry Smith   *dt = ts->time_step;
1147d763cef2SBarry Smith   PetscFunctionReturn(0);
1148d763cef2SBarry Smith }
1149d763cef2SBarry Smith 
11504a2ae208SSatish Balay #undef __FUNCT__
11514a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1152d8e5e3e6SSatish Balay /*@
1153d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1154d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1155d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1156d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1157d763cef2SBarry Smith 
1158d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1159d763cef2SBarry Smith 
1160d763cef2SBarry Smith    Input Parameter:
1161d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1162d763cef2SBarry Smith 
1163d763cef2SBarry Smith    Output Parameter:
1164d763cef2SBarry Smith .  v - the vector containing the solution
1165d763cef2SBarry Smith 
1166d763cef2SBarry Smith    Level: intermediate
1167d763cef2SBarry Smith 
1168d763cef2SBarry Smith .seealso: TSGetTimeStep()
1169d763cef2SBarry Smith 
1170d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1171d763cef2SBarry Smith @*/
11727087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1173d763cef2SBarry Smith {
1174d763cef2SBarry Smith   PetscFunctionBegin;
11750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11764482741eSBarry Smith   PetscValidPointer(v,2);
11778737fe31SLisandro Dalcin   *v = ts->vec_sol;
1178d763cef2SBarry Smith   PetscFunctionReturn(0);
1179d763cef2SBarry Smith }
1180d763cef2SBarry Smith 
1181bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
11824a2ae208SSatish Balay #undef __FUNCT__
1183bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1184d8e5e3e6SSatish Balay /*@
1185bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1186d763cef2SBarry Smith 
1187bdad233fSMatthew Knepley   Not collective
1188d763cef2SBarry Smith 
1189bdad233fSMatthew Knepley   Input Parameters:
1190bdad233fSMatthew Knepley + ts   - The TS
1191bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1192d763cef2SBarry Smith .vb
1193d763cef2SBarry Smith          U_t = A U
1194d763cef2SBarry Smith          U_t = A(t) U
1195d763cef2SBarry Smith          U_t = F(t,U)
1196d763cef2SBarry Smith .ve
1197d763cef2SBarry Smith 
1198d763cef2SBarry Smith    Level: beginner
1199d763cef2SBarry Smith 
1200bdad233fSMatthew Knepley .keywords: TS, problem type
1201bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1202d763cef2SBarry Smith @*/
12037087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1204a7cc72afSBarry Smith {
12059e2a6581SJed Brown   PetscErrorCode ierr;
12069e2a6581SJed Brown 
1207d763cef2SBarry Smith   PetscFunctionBegin;
12080700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1209bdad233fSMatthew Knepley   ts->problem_type = type;
12109e2a6581SJed Brown   if (type == TS_LINEAR) {
12119e2a6581SJed Brown     SNES snes;
12129e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12139e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
12149e2a6581SJed Brown   }
1215d763cef2SBarry Smith   PetscFunctionReturn(0);
1216d763cef2SBarry Smith }
1217d763cef2SBarry Smith 
1218bdad233fSMatthew Knepley #undef __FUNCT__
1219bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1220bdad233fSMatthew Knepley /*@C
1221bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1222bdad233fSMatthew Knepley 
1223bdad233fSMatthew Knepley   Not collective
1224bdad233fSMatthew Knepley 
1225bdad233fSMatthew Knepley   Input Parameter:
1226bdad233fSMatthew Knepley . ts   - The TS
1227bdad233fSMatthew Knepley 
1228bdad233fSMatthew Knepley   Output Parameter:
1229bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1230bdad233fSMatthew Knepley .vb
1231089b2837SJed Brown          M U_t = A U
1232089b2837SJed Brown          M(t) U_t = A(t) U
1233bdad233fSMatthew Knepley          U_t = F(t,U)
1234bdad233fSMatthew Knepley .ve
1235bdad233fSMatthew Knepley 
1236bdad233fSMatthew Knepley    Level: beginner
1237bdad233fSMatthew Knepley 
1238bdad233fSMatthew Knepley .keywords: TS, problem type
1239bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1240bdad233fSMatthew Knepley @*/
12417087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1242a7cc72afSBarry Smith {
1243bdad233fSMatthew Knepley   PetscFunctionBegin;
12440700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
12454482741eSBarry Smith   PetscValidIntPointer(type,2);
1246bdad233fSMatthew Knepley   *type = ts->problem_type;
1247bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1248bdad233fSMatthew Knepley }
1249d763cef2SBarry Smith 
12504a2ae208SSatish Balay #undef __FUNCT__
12514a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1252d763cef2SBarry Smith /*@
1253d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1254d763cef2SBarry Smith    of a timestepper.
1255d763cef2SBarry Smith 
1256d763cef2SBarry Smith    Collective on TS
1257d763cef2SBarry Smith 
1258d763cef2SBarry Smith    Input Parameter:
1259d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1260d763cef2SBarry Smith 
1261d763cef2SBarry Smith    Notes:
1262d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1263d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1264d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1265d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1266d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1267d763cef2SBarry Smith 
1268d763cef2SBarry Smith    Level: advanced
1269d763cef2SBarry Smith 
1270d763cef2SBarry Smith .keywords: TS, timestep, setup
1271d763cef2SBarry Smith 
1272d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1273d763cef2SBarry Smith @*/
12747087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1275d763cef2SBarry Smith {
1276dfbe8321SBarry Smith   PetscErrorCode ierr;
12776c6b9e74SPeter Brune   DM             dm;
12786c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
12796c6b9e74SPeter Brune   PetscErrorCode (*jac)(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
12806c6b9e74SPeter Brune   TSIJacobian    ijac;
12816c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1282d763cef2SBarry Smith 
1283d763cef2SBarry Smith   PetscFunctionBegin;
12840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1285277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1286277b19d0SLisandro Dalcin 
12877adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
12889596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1289d763cef2SBarry Smith   }
1290a43b19c4SJed Brown   if (ts->exact_final_time == PETSC_DECIDE) ts->exact_final_time = PETSC_FALSE;
1291277b19d0SLisandro Dalcin 
1292277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1293277b19d0SLisandro Dalcin 
12941c3436cfSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
12951c3436cfSJed Brown 
1296277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1297000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1298277b19d0SLisandro Dalcin   }
1299277b19d0SLisandro Dalcin 
13006c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
13016c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
13026c6b9e74SPeter Brune    */
13036c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
13046c6b9e74SPeter Brune   ierr = DMSNESGetFunction(dm,&func,PETSC_NULL);CHKERRQ(ierr);
13056c6b9e74SPeter Brune   if (!func) {
13066c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
13076c6b9e74SPeter Brune   }
13086c6b9e74SPeter Brune   /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
13096c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
13106c6b9e74SPeter Brune    */
13116c6b9e74SPeter Brune   ierr = DMSNESGetJacobian(dm,&jac,PETSC_NULL);CHKERRQ(ierr);
13126c6b9e74SPeter Brune   ierr = DMTSGetIJacobian(dm,&ijac,PETSC_NULL);CHKERRQ(ierr);
13136c6b9e74SPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjac,PETSC_NULL);CHKERRQ(ierr);
13146c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
13156c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
13166c6b9e74SPeter Brune   }
1317277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1318277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1319277b19d0SLisandro Dalcin }
1320277b19d0SLisandro Dalcin 
1321277b19d0SLisandro Dalcin #undef __FUNCT__
1322277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1323277b19d0SLisandro Dalcin /*@
1324277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1325277b19d0SLisandro Dalcin 
1326277b19d0SLisandro Dalcin    Collective on TS
1327277b19d0SLisandro Dalcin 
1328277b19d0SLisandro Dalcin    Input Parameter:
1329277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1330277b19d0SLisandro Dalcin 
1331277b19d0SLisandro Dalcin    Level: beginner
1332277b19d0SLisandro Dalcin 
1333277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1334277b19d0SLisandro Dalcin 
1335277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1336277b19d0SLisandro Dalcin @*/
1337277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1338277b19d0SLisandro Dalcin {
1339277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1340277b19d0SLisandro Dalcin 
1341277b19d0SLisandro Dalcin   PetscFunctionBegin;
1342277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1343277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1344277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1345277b19d0SLisandro Dalcin   }
1346277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
13474e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
13484e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1349214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
13506bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1351e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1352e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
135338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1354277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1355d763cef2SBarry Smith   PetscFunctionReturn(0);
1356d763cef2SBarry Smith }
1357d763cef2SBarry Smith 
13584a2ae208SSatish Balay #undef __FUNCT__
13594a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1360d8e5e3e6SSatish Balay /*@
1361d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1362d763cef2SBarry Smith    with TSCreate().
1363d763cef2SBarry Smith 
1364d763cef2SBarry Smith    Collective on TS
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith    Input Parameter:
1367d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith    Level: beginner
1370d763cef2SBarry Smith 
1371d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1372d763cef2SBarry Smith 
1373d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1374d763cef2SBarry Smith @*/
13756bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1376d763cef2SBarry Smith {
13776849ba73SBarry Smith   PetscErrorCode ierr;
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith   PetscFunctionBegin;
13806bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
13816bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
13826bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1383d763cef2SBarry Smith 
13846bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1385277b19d0SLisandro Dalcin 
1386be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13876bf464f9SBarry Smith   ierr = PetscObjectDepublish((*ts));CHKERRQ(ierr);
13886bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
13896d4c513bSLisandro Dalcin 
139084df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
13916bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
13926bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
13936bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
13946d4c513bSLisandro Dalcin 
1395a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1396d763cef2SBarry Smith   PetscFunctionReturn(0);
1397d763cef2SBarry Smith }
1398d763cef2SBarry Smith 
13994a2ae208SSatish Balay #undef __FUNCT__
14004a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
1401d8e5e3e6SSatish Balay /*@
1402d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
1403d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
1404d763cef2SBarry Smith 
1405d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
1406d763cef2SBarry Smith 
1407d763cef2SBarry Smith    Input Parameter:
1408d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1409d763cef2SBarry Smith 
1410d763cef2SBarry Smith    Output Parameter:
1411d763cef2SBarry Smith .  snes - the nonlinear solver context
1412d763cef2SBarry Smith 
1413d763cef2SBarry Smith    Notes:
1414d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
1415d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
141694b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
1417d763cef2SBarry Smith 
1418d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
1419d763cef2SBarry Smith    this case TSGetSNES() returns PETSC_NULL in snes.
1420d763cef2SBarry Smith 
1421d763cef2SBarry Smith    Level: beginner
1422d763cef2SBarry Smith 
1423d763cef2SBarry Smith .keywords: timestep, get, SNES
1424d763cef2SBarry Smith @*/
14257087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
1426d763cef2SBarry Smith {
1427d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1428d372ba47SLisandro Dalcin 
1429d763cef2SBarry Smith   PetscFunctionBegin;
14300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14314482741eSBarry Smith   PetscValidPointer(snes,2);
1432d372ba47SLisandro Dalcin   if (!ts->snes) {
1433d372ba47SLisandro Dalcin     ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr);
14346c6b9e74SPeter Brune     ierr = SNESSetFunction(ts->snes,PETSC_NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
1435d372ba47SLisandro Dalcin     ierr = PetscLogObjectParent(ts,ts->snes);CHKERRQ(ierr);
1436d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
1437496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
14389e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
14399e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
14409e2a6581SJed Brown     }
1441d372ba47SLisandro Dalcin   }
1442d763cef2SBarry Smith   *snes = ts->snes;
1443d763cef2SBarry Smith   PetscFunctionReturn(0);
1444d763cef2SBarry Smith }
1445d763cef2SBarry Smith 
14464a2ae208SSatish Balay #undef __FUNCT__
144794b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
1448d8e5e3e6SSatish Balay /*@
144994b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
1450d763cef2SBarry Smith    a TS (timestepper) context.
1451d763cef2SBarry Smith 
145294b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
1453d763cef2SBarry Smith 
1454d763cef2SBarry Smith    Input Parameter:
1455d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1456d763cef2SBarry Smith 
1457d763cef2SBarry Smith    Output Parameter:
145894b7f48cSBarry Smith .  ksp - the nonlinear solver context
1459d763cef2SBarry Smith 
1460d763cef2SBarry Smith    Notes:
146194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
1462d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
1463d763cef2SBarry Smith    KSP and PC contexts as well.
1464d763cef2SBarry Smith 
146594b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
146694b7f48cSBarry Smith    in this case TSGetKSP() returns PETSC_NULL in ksp.
1467d763cef2SBarry Smith 
1468d763cef2SBarry Smith    Level: beginner
1469d763cef2SBarry Smith 
147094b7f48cSBarry Smith .keywords: timestep, get, KSP
1471d763cef2SBarry Smith @*/
14727087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
1473d763cef2SBarry Smith {
1474d372ba47SLisandro Dalcin   PetscErrorCode ierr;
1475089b2837SJed Brown   SNES           snes;
1476d372ba47SLisandro Dalcin 
1477d763cef2SBarry Smith   PetscFunctionBegin;
14780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14794482741eSBarry Smith   PetscValidPointer(ksp,2);
148017186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
1481e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
1482089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1483089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
1484d763cef2SBarry Smith   PetscFunctionReturn(0);
1485d763cef2SBarry Smith }
1486d763cef2SBarry Smith 
1487d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
1488d763cef2SBarry Smith 
14894a2ae208SSatish Balay #undef __FUNCT__
1490adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
1491adb62b0dSMatthew Knepley /*@
1492adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
1493adb62b0dSMatthew Knepley    maximum time for iteration.
1494adb62b0dSMatthew Knepley 
14953f9fe445SBarry Smith    Not Collective
1496adb62b0dSMatthew Knepley 
1497adb62b0dSMatthew Knepley    Input Parameters:
1498adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
1499adb62b0dSMatthew Knepley .  maxsteps - maximum number of iterations to use, or PETSC_NULL
1500adb62b0dSMatthew Knepley -  maxtime  - final time to iterate to, or PETSC_NULL
1501adb62b0dSMatthew Knepley 
1502adb62b0dSMatthew Knepley    Level: intermediate
1503adb62b0dSMatthew Knepley 
1504adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
1505adb62b0dSMatthew Knepley @*/
15067087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
1507adb62b0dSMatthew Knepley {
1508adb62b0dSMatthew Knepley   PetscFunctionBegin;
15090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1510abc0a331SBarry Smith   if (maxsteps) {
15114482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
1512adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
1513adb62b0dSMatthew Knepley   }
1514abc0a331SBarry Smith   if (maxtime) {
15154482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
1516adb62b0dSMatthew Knepley     *maxtime  = ts->max_time;
1517adb62b0dSMatthew Knepley   }
1518adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
1519adb62b0dSMatthew Knepley }
1520adb62b0dSMatthew Knepley 
1521adb62b0dSMatthew Knepley #undef __FUNCT__
15224a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
1523d763cef2SBarry Smith /*@
1524d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
1525d763cef2SBarry Smith    maximum time for iteration.
1526d763cef2SBarry Smith 
15273f9fe445SBarry Smith    Logically Collective on TS
1528d763cef2SBarry Smith 
1529d763cef2SBarry Smith    Input Parameters:
1530d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1531d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
1532d763cef2SBarry Smith -  maxtime - final time to iterate to
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith    Options Database Keys:
1535d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
15363bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith    Notes:
1539d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith    Level: intermediate
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
1544a43b19c4SJed Brown 
1545a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
1546d763cef2SBarry Smith @*/
15477087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
1548d763cef2SBarry Smith {
1549d763cef2SBarry Smith   PetscFunctionBegin;
15500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1551c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
1552c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
155339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
155439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time  = maxtime;
1555d763cef2SBarry Smith   PetscFunctionReturn(0);
1556d763cef2SBarry Smith }
1557d763cef2SBarry Smith 
15584a2ae208SSatish Balay #undef __FUNCT__
15594a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
1560d763cef2SBarry Smith /*@
1561d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
1562d763cef2SBarry Smith    for use by the TS routines.
1563d763cef2SBarry Smith 
15643f9fe445SBarry Smith    Logically Collective on TS and Vec
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith    Input Parameters:
1567d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1568d763cef2SBarry Smith -  x - the solution vector
1569d763cef2SBarry Smith 
1570d763cef2SBarry Smith    Level: beginner
1571d763cef2SBarry Smith 
1572d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
1573d763cef2SBarry Smith @*/
15747087cfbeSBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec x)
1575d763cef2SBarry Smith {
15768737fe31SLisandro Dalcin   PetscErrorCode ierr;
1577496e6a7aSJed Brown   DM             dm;
15788737fe31SLisandro Dalcin 
1579d763cef2SBarry Smith   PetscFunctionBegin;
15800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15810700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
15828737fe31SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
15836bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
15848737fe31SLisandro Dalcin   ts->vec_sol = x;
1585496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1586496e6a7aSJed Brown   ierr = DMShellSetGlobalVector(dm,x);CHKERRQ(ierr);
1587d763cef2SBarry Smith   PetscFunctionReturn(0);
1588d763cef2SBarry Smith }
1589d763cef2SBarry Smith 
1590e74ef692SMatthew Knepley #undef __FUNCT__
1591e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
1592ac226902SBarry Smith /*@C
1593000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
15943f2090d5SJed Brown   called once at the beginning of each time step.
1595000e7ae3SMatthew Knepley 
15963f9fe445SBarry Smith   Logically Collective on TS
1597000e7ae3SMatthew Knepley 
1598000e7ae3SMatthew Knepley   Input Parameters:
1599000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1600000e7ae3SMatthew Knepley - func - The function
1601000e7ae3SMatthew Knepley 
1602000e7ae3SMatthew Knepley   Calling sequence of func:
1603000e7ae3SMatthew Knepley . func (TS ts);
1604000e7ae3SMatthew Knepley 
1605000e7ae3SMatthew Knepley   Level: intermediate
1606000e7ae3SMatthew Knepley 
1607b8123daeSJed Brown   Note:
1608b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
1609b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
1610b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
1611b8123daeSJed Brown 
1612000e7ae3SMatthew Knepley .keywords: TS, timestep
1613b8123daeSJed Brown .seealso: TSSetPreStage(), TSSetPostStep(), TSStep()
1614000e7ae3SMatthew Knepley @*/
16157087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
1616000e7ae3SMatthew Knepley {
1617000e7ae3SMatthew Knepley   PetscFunctionBegin;
16180700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1619000e7ae3SMatthew Knepley   ts->ops->prestep = func;
1620000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1621000e7ae3SMatthew Knepley }
1622000e7ae3SMatthew Knepley 
1623e74ef692SMatthew Knepley #undef __FUNCT__
16243f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
162509ee8438SJed Brown /*@
16263f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
16273f2090d5SJed Brown 
16283f2090d5SJed Brown   Collective on TS
16293f2090d5SJed Brown 
16303f2090d5SJed Brown   Input Parameters:
16313f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
16323f2090d5SJed Brown 
16333f2090d5SJed Brown   Notes:
16343f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
16353f2090d5SJed Brown   so most users would not generally call this routine themselves.
16363f2090d5SJed Brown 
16373f2090d5SJed Brown   Level: developer
16383f2090d5SJed Brown 
16393f2090d5SJed Brown .keywords: TS, timestep
1640b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStage(), TSPostStep()
16413f2090d5SJed Brown @*/
16427087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
16433f2090d5SJed Brown {
16443f2090d5SJed Brown   PetscErrorCode ierr;
16453f2090d5SJed Brown 
16463f2090d5SJed Brown   PetscFunctionBegin;
16470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
164872ac3e02SJed Brown   if (ts->ops->prestep) {
16493f2090d5SJed Brown     PetscStackPush("TS PreStep function");
16503f2090d5SJed Brown     ierr = (*ts->ops->prestep)(ts);CHKERRQ(ierr);
16513f2090d5SJed Brown     PetscStackPop;
1652312ce896SJed Brown   }
16533f2090d5SJed Brown   PetscFunctionReturn(0);
16543f2090d5SJed Brown }
16553f2090d5SJed Brown 
16563f2090d5SJed Brown #undef __FUNCT__
1657b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
1658b8123daeSJed Brown /*@C
1659b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
1660b8123daeSJed Brown   called once at the beginning of each stage.
1661b8123daeSJed Brown 
1662b8123daeSJed Brown   Logically Collective on TS
1663b8123daeSJed Brown 
1664b8123daeSJed Brown   Input Parameters:
1665b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
1666b8123daeSJed Brown - func - The function
1667b8123daeSJed Brown 
1668b8123daeSJed Brown   Calling sequence of func:
1669b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
1670b8123daeSJed Brown 
1671b8123daeSJed Brown   Level: intermediate
1672b8123daeSJed Brown 
1673b8123daeSJed Brown   Note:
1674b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
1675b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
1676b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
1677b8123daeSJed Brown 
1678b8123daeSJed Brown .keywords: TS, timestep
1679b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
1680b8123daeSJed Brown @*/
1681b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
1682b8123daeSJed Brown {
1683b8123daeSJed Brown   PetscFunctionBegin;
1684b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1685b8123daeSJed Brown   ts->ops->prestage = func;
1686b8123daeSJed Brown   PetscFunctionReturn(0);
1687b8123daeSJed Brown }
1688b8123daeSJed Brown 
1689b8123daeSJed Brown #undef __FUNCT__
1690b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
1691b8123daeSJed Brown /*@
1692b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
1693b8123daeSJed Brown 
1694b8123daeSJed Brown   Collective on TS
1695b8123daeSJed Brown 
1696b8123daeSJed Brown   Input Parameters:
1697b8123daeSJed Brown . ts   - The TS context obtained from TSCreate()
1698b8123daeSJed Brown 
1699b8123daeSJed Brown   Notes:
1700b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
1701b8123daeSJed Brown   most users would not generally call this routine themselves.
1702b8123daeSJed Brown 
1703b8123daeSJed Brown   Level: developer
1704b8123daeSJed Brown 
1705b8123daeSJed Brown .keywords: TS, timestep
1706b8123daeSJed Brown .seealso: TSSetPreStep(), TSPreStep(), TSPostStep()
1707b8123daeSJed Brown @*/
1708b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
1709b8123daeSJed Brown {
1710b8123daeSJed Brown   PetscErrorCode ierr;
1711b8123daeSJed Brown 
1712b8123daeSJed Brown   PetscFunctionBegin;
1713b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1714b8123daeSJed Brown   if (ts->ops->prestage) {
1715b8123daeSJed Brown     PetscStackPush("TS PreStage function");
1716b8123daeSJed Brown     ierr = (*ts->ops->prestage)(ts,stagetime);CHKERRQ(ierr);
1717b8123daeSJed Brown     PetscStackPop;
1718b8123daeSJed Brown   }
1719b8123daeSJed Brown   PetscFunctionReturn(0);
1720b8123daeSJed Brown }
1721b8123daeSJed Brown 
1722b8123daeSJed Brown #undef __FUNCT__
1723e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
1724ac226902SBarry Smith /*@C
1725000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
17263f2090d5SJed Brown   called once at the end of each time step.
1727000e7ae3SMatthew Knepley 
17283f9fe445SBarry Smith   Logically Collective on TS
1729000e7ae3SMatthew Knepley 
1730000e7ae3SMatthew Knepley   Input Parameters:
1731000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
1732000e7ae3SMatthew Knepley - func - The function
1733000e7ae3SMatthew Knepley 
1734000e7ae3SMatthew Knepley   Calling sequence of func:
1735b8123daeSJed Brown $ func (TS ts);
1736000e7ae3SMatthew Knepley 
1737000e7ae3SMatthew Knepley   Level: intermediate
1738000e7ae3SMatthew Knepley 
1739000e7ae3SMatthew Knepley .keywords: TS, timestep
1740b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
1741000e7ae3SMatthew Knepley @*/
17427087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
1743000e7ae3SMatthew Knepley {
1744000e7ae3SMatthew Knepley   PetscFunctionBegin;
17450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1746000e7ae3SMatthew Knepley   ts->ops->poststep = func;
1747000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
1748000e7ae3SMatthew Knepley }
1749000e7ae3SMatthew Knepley 
1750e74ef692SMatthew Knepley #undef __FUNCT__
17513f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
175209ee8438SJed Brown /*@
17533f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
17543f2090d5SJed Brown 
17553f2090d5SJed Brown   Collective on TS
17563f2090d5SJed Brown 
17573f2090d5SJed Brown   Input Parameters:
17583f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
17593f2090d5SJed Brown 
17603f2090d5SJed Brown   Notes:
17613f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
17623f2090d5SJed Brown   so most users would not generally call this routine themselves.
17633f2090d5SJed Brown 
17643f2090d5SJed Brown   Level: developer
17653f2090d5SJed Brown 
17663f2090d5SJed Brown .keywords: TS, timestep
17673f2090d5SJed Brown @*/
17687087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
17693f2090d5SJed Brown {
17703f2090d5SJed Brown   PetscErrorCode ierr;
17713f2090d5SJed Brown 
17723f2090d5SJed Brown   PetscFunctionBegin;
17730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
177472ac3e02SJed Brown   if (ts->ops->poststep) {
17753f2090d5SJed Brown     PetscStackPush("TS PostStep function");
17763f2090d5SJed Brown     ierr = (*ts->ops->poststep)(ts);CHKERRQ(ierr);
17773f2090d5SJed Brown     PetscStackPop;
177872ac3e02SJed Brown   }
17793f2090d5SJed Brown   PetscFunctionReturn(0);
17803f2090d5SJed Brown }
17813f2090d5SJed Brown 
1782d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
1783d763cef2SBarry Smith 
17844a2ae208SSatish Balay #undef __FUNCT__
1785a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
1786d763cef2SBarry Smith /*@C
1787a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
1788d763cef2SBarry Smith    timestep to display the iteration's  progress.
1789d763cef2SBarry Smith 
17903f9fe445SBarry Smith    Logically Collective on TS
1791d763cef2SBarry Smith 
1792d763cef2SBarry Smith    Input Parameters:
1793d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1794e213d8f1SJed Brown .  monitor - monitoring routine
1795329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
1796b3006f0bSLois Curfman McInnes              monitor routine (use PETSC_NULL if no context is desired)
1797b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1798b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
1799d763cef2SBarry Smith 
1800e213d8f1SJed Brown    Calling sequence of monitor:
1801e213d8f1SJed Brown $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec x,void *mctx)
1802d763cef2SBarry Smith 
1803d763cef2SBarry Smith +    ts - the TS context
1804d763cef2SBarry Smith .    steps - iteration number
18051f06c33eSBarry Smith .    time - current time
1806d763cef2SBarry Smith .    x - current iterate
1807d763cef2SBarry Smith -    mctx - [optional] monitoring context
1808d763cef2SBarry Smith 
1809d763cef2SBarry Smith    Notes:
1810d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
1811d763cef2SBarry Smith    already has been loaded.
1812d763cef2SBarry Smith 
1813025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
1814025f1a04SBarry Smith 
1815d763cef2SBarry Smith    Level: intermediate
1816d763cef2SBarry Smith 
1817d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1818d763cef2SBarry Smith 
1819a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
1820d763cef2SBarry Smith @*/
1821c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
1822d763cef2SBarry Smith {
1823d763cef2SBarry Smith   PetscFunctionBegin;
18240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
182517186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1826d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]           = monitor;
1827329f5518SBarry Smith   ts->mdestroy[ts->numbermonitors]          = mdestroy;
1828d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++]  = (void*)mctx;
1829d763cef2SBarry Smith   PetscFunctionReturn(0);
1830d763cef2SBarry Smith }
1831d763cef2SBarry Smith 
18324a2ae208SSatish Balay #undef __FUNCT__
1833a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
1834d763cef2SBarry Smith /*@C
1835a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
1836d763cef2SBarry Smith 
18373f9fe445SBarry Smith    Logically Collective on TS
1838d763cef2SBarry Smith 
1839d763cef2SBarry Smith    Input Parameters:
1840d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1841d763cef2SBarry Smith 
1842d763cef2SBarry Smith    Notes:
1843d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
1844d763cef2SBarry Smith 
1845d763cef2SBarry Smith    Level: intermediate
1846d763cef2SBarry Smith 
1847d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
1848d763cef2SBarry Smith 
1849a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
1850d763cef2SBarry Smith @*/
18517087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
1852d763cef2SBarry Smith {
1853d952e501SBarry Smith   PetscErrorCode ierr;
1854d952e501SBarry Smith   PetscInt       i;
1855d952e501SBarry Smith 
1856d763cef2SBarry Smith   PetscFunctionBegin;
18570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1858d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
1859d952e501SBarry Smith     if (ts->mdestroy[i]) {
18603c4aec1bSBarry Smith       ierr = (*ts->mdestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
1861d952e501SBarry Smith     }
1862d952e501SBarry Smith   }
1863d763cef2SBarry Smith   ts->numbermonitors = 0;
1864d763cef2SBarry Smith   PetscFunctionReturn(0);
1865d763cef2SBarry Smith }
1866d763cef2SBarry Smith 
18674a2ae208SSatish Balay #undef __FUNCT__
1868a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
1869d8e5e3e6SSatish Balay /*@
1870a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
18715516499fSSatish Balay 
18725516499fSSatish Balay    Level: intermediate
187341251cbbSSatish Balay 
18745516499fSSatish Balay .keywords: TS, set, monitor
18755516499fSSatish Balay 
187641251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
187741251cbbSSatish Balay @*/
1878649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
1879d763cef2SBarry Smith {
1880dfbe8321SBarry Smith   PetscErrorCode ierr;
1881649052a6SBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(((PetscObject)ts)->comm);
1882d132466eSBarry Smith 
1883d763cef2SBarry Smith   PetscFunctionBegin;
1884649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1885971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
1886649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
1887d763cef2SBarry Smith   PetscFunctionReturn(0);
1888d763cef2SBarry Smith }
1889d763cef2SBarry Smith 
18904a2ae208SSatish Balay #undef __FUNCT__
1891cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
1892cd652676SJed Brown /*@
1893cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
1894cd652676SJed Brown 
1895cd652676SJed Brown    Logically Collective on TS
1896cd652676SJed Brown 
1897cd652676SJed Brown    Input Argument:
1898cd652676SJed Brown .  ts - time stepping context
1899cd652676SJed Brown 
1900cd652676SJed Brown    Output Argument:
1901cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
1902cd652676SJed Brown 
1903cd652676SJed Brown    Level: intermediate
1904cd652676SJed Brown 
1905cd652676SJed Brown .keywords: TS, set
1906cd652676SJed Brown 
1907cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
1908cd652676SJed Brown @*/
1909cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
1910cd652676SJed Brown {
1911cd652676SJed Brown 
1912cd652676SJed Brown   PetscFunctionBegin;
1913cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1914cd652676SJed Brown   ts->retain_stages = flg;
1915cd652676SJed Brown   PetscFunctionReturn(0);
1916cd652676SJed Brown }
1917cd652676SJed Brown 
1918cd652676SJed Brown #undef __FUNCT__
1919cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
1920cd652676SJed Brown /*@
1921cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
1922cd652676SJed Brown 
1923cd652676SJed Brown    Collective on TS
1924cd652676SJed Brown 
1925cd652676SJed Brown    Input Argument:
1926cd652676SJed Brown +  ts - time stepping context
1927cd652676SJed Brown -  t - time to interpolate to
1928cd652676SJed Brown 
1929cd652676SJed Brown    Output Argument:
1930cd652676SJed Brown .  X - state at given time
1931cd652676SJed Brown 
1932cd652676SJed Brown    Notes:
1933cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
1934cd652676SJed Brown 
1935cd652676SJed Brown    Level: intermediate
1936cd652676SJed Brown 
1937cd652676SJed Brown    Developer Notes:
1938cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
1939cd652676SJed Brown 
1940cd652676SJed Brown .keywords: TS, set
1941cd652676SJed Brown 
1942cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
1943cd652676SJed Brown @*/
1944cd652676SJed Brown PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec X)
1945cd652676SJed Brown {
1946cd652676SJed Brown   PetscErrorCode ierr;
1947cd652676SJed Brown 
1948cd652676SJed Brown   PetscFunctionBegin;
1949cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1950d8cd7023SBarry 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);
1951cd652676SJed Brown   if (!ts->ops->interpolate) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
1952cd652676SJed Brown   ierr = (*ts->ops->interpolate)(ts,t,X);CHKERRQ(ierr);
1953cd652676SJed Brown   PetscFunctionReturn(0);
1954cd652676SJed Brown }
1955cd652676SJed Brown 
1956cd652676SJed Brown #undef __FUNCT__
19574a2ae208SSatish Balay #define __FUNCT__ "TSStep"
1958d763cef2SBarry Smith /*@
19596d9e5789SSean Farley    TSStep - Steps one time step
1960d763cef2SBarry Smith 
1961d763cef2SBarry Smith    Collective on TS
1962d763cef2SBarry Smith 
1963d763cef2SBarry Smith    Input Parameter:
1964d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1965d763cef2SBarry Smith 
19666d9e5789SSean Farley    Level: intermediate
1967d763cef2SBarry Smith 
1968b8123daeSJed Brown    Notes:
1969b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
1970b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
1971b8123daeSJed Brown 
1972d763cef2SBarry Smith .keywords: TS, timestep, solve
1973d763cef2SBarry Smith 
1974b8123daeSJed Brown .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage()
1975d763cef2SBarry Smith @*/
1976193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
1977d763cef2SBarry Smith {
1978362cd11cSLisandro Dalcin   PetscReal      ptime_prev;
1979dfbe8321SBarry Smith   PetscErrorCode ierr;
1980d763cef2SBarry Smith 
1981d763cef2SBarry Smith   PetscFunctionBegin;
19820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1983d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
1984d405a339SMatthew Knepley 
1985362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
1986362cd11cSLisandro Dalcin 
1987362cd11cSLisandro Dalcin   ptime_prev = ts->ptime;
1988d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1989193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
1990d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
1991362cd11cSLisandro Dalcin   ts->time_step_prev = ts->ptime - ptime_prev;
1992362cd11cSLisandro Dalcin 
1993362cd11cSLisandro Dalcin   if (ts->reason < 0) {
1994cef5090cSJed Brown     if (ts->errorifstepfailed) {
1995cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
1996cef5090cSJed 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]);
1997cef5090cSJed Brown       } else SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
1998cef5090cSJed Brown     }
1999362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2000362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2001362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2002362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2003362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2004362cd11cSLisandro Dalcin   }
2005362cd11cSLisandro Dalcin 
2006d763cef2SBarry Smith   PetscFunctionReturn(0);
2007d763cef2SBarry Smith }
2008d763cef2SBarry Smith 
20094a2ae208SSatish Balay #undef __FUNCT__
201005175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
201105175c85SJed Brown /*@
201205175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
201305175c85SJed Brown 
20141c3436cfSJed Brown    Collective on TS
201505175c85SJed Brown 
201605175c85SJed Brown    Input Arguments:
20171c3436cfSJed Brown +  ts - time stepping context
20181c3436cfSJed Brown .  order - desired order of accuracy
20191c3436cfSJed Brown -  done - whether the step was evaluated at this order (pass PETSC_NULL to generate an error if not available)
202005175c85SJed Brown 
202105175c85SJed Brown    Output Arguments:
20221c3436cfSJed Brown .  X - state at the end of the current step
202305175c85SJed Brown 
202405175c85SJed Brown    Level: advanced
202505175c85SJed Brown 
2026108c343cSJed Brown    Notes:
2027108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2028108c343cSJed 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.
2029108c343cSJed Brown 
20301c3436cfSJed Brown .seealso: TSStep(), TSAdapt
203105175c85SJed Brown @*/
20321c3436cfSJed Brown PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec X,PetscBool *done)
203305175c85SJed Brown {
203405175c85SJed Brown   PetscErrorCode ierr;
203505175c85SJed Brown 
203605175c85SJed Brown   PetscFunctionBegin;
203705175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
203805175c85SJed Brown   PetscValidType(ts,1);
203905175c85SJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
20401c3436cfSJed Brown   if (!ts->ops->evaluatestep) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
20411c3436cfSJed Brown   ierr = (*ts->ops->evaluatestep)(ts,order,X,done);CHKERRQ(ierr);
204205175c85SJed Brown   PetscFunctionReturn(0);
204305175c85SJed Brown }
204405175c85SJed Brown 
204505175c85SJed Brown #undef __FUNCT__
20466a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
20476a4d4014SLisandro Dalcin /*@
20486a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
20496a4d4014SLisandro Dalcin 
20506a4d4014SLisandro Dalcin    Collective on TS
20516a4d4014SLisandro Dalcin 
20526a4d4014SLisandro Dalcin    Input Parameter:
20536a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2054362cd11cSLisandro Dalcin -  x - the solution vector
20556a4d4014SLisandro Dalcin 
20565a3a76d0SJed Brown    Output Parameter:
20575a3a76d0SJed Brown .  ftime - time of the state vector x upon completion
20585a3a76d0SJed Brown 
20596a4d4014SLisandro Dalcin    Level: beginner
20606a4d4014SLisandro Dalcin 
20615a3a76d0SJed Brown    Notes:
20625a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
20635a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
20645a3a76d0SJed Brown    stepped over the final time.
20655a3a76d0SJed Brown 
20666a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
20676a4d4014SLisandro Dalcin 
20686a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
20696a4d4014SLisandro Dalcin @*/
20705a3a76d0SJed Brown PetscErrorCode TSSolve(TS ts,Vec x,PetscReal *ftime)
20716a4d4014SLisandro Dalcin {
20724d7d938eSLisandro Dalcin   PetscBool      flg;
20734d7d938eSLisandro Dalcin   char           filename[PETSC_MAX_PATH_LEN];
20744d7d938eSLisandro Dalcin   PetscViewer    viewer;
20756a4d4014SLisandro Dalcin   PetscErrorCode ierr;
2076f22f69f0SBarry Smith 
20776a4d4014SLisandro Dalcin   PetscFunctionBegin;
20780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2079193ac0bcSJed Brown   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
20805a3a76d0SJed Brown   if (ts->exact_final_time) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
20815a3a76d0SJed Brown     if (!ts->vec_sol || x == ts->vec_sol) {
20825a3a76d0SJed Brown       Vec y;
20835a3a76d0SJed Brown       ierr = VecDuplicate(x,&y);CHKERRQ(ierr);
20845a3a76d0SJed Brown       ierr = TSSetSolution(ts,y);CHKERRQ(ierr);
20855a3a76d0SJed Brown       ierr = VecDestroy(&y);CHKERRQ(ierr); /* grant ownership */
20865a3a76d0SJed Brown     }
2087362cd11cSLisandro Dalcin     ierr = VecCopy(x,ts->vec_sol);CHKERRQ(ierr);
20885a3a76d0SJed Brown   } else {
2089193ac0bcSJed Brown     ierr = TSSetSolution(ts,x);CHKERRQ(ierr);
20905a3a76d0SJed Brown   }
2091b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
20926a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2093193ac0bcSJed Brown   ts->steps = 0;
20945ef26d82SJed Brown   ts->ksp_its = 0;
20955ef26d82SJed Brown   ts->snes_its = 0;
2096c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2097c610991cSLisandro Dalcin   ts->reject = 0;
2098193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
2099193ac0bcSJed Brown 
2100193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2101193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
21025a3a76d0SJed Brown     ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
2103a5b82c69SSean Farley     if (ftime) *ftime = ts->ptime;
2104193ac0bcSJed Brown   } else {
21056a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2106362cd11cSLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2107362cd11cSLisandro Dalcin     if (ts->steps >= ts->max_steps)
2108362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_ITS;
2109362cd11cSLisandro Dalcin     else if (ts->ptime >= ts->max_time)
2110362cd11cSLisandro Dalcin       ts->reason = TS_CONVERGED_TIME;
2111e1a7a14fSJed Brown     while (!ts->reason) {
2112193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2113193ac0bcSJed Brown       ierr = TSPostStep(ts);CHKERRQ(ierr);
2114193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2115193ac0bcSJed Brown     }
2116362cd11cSLisandro Dalcin     if (ts->exact_final_time && ts->ptime > ts->max_time) {
2117a43b19c4SJed Brown       ierr = TSInterpolate(ts,ts->max_time,x);CHKERRQ(ierr);
21185a3a76d0SJed Brown       if (ftime) *ftime = ts->max_time;
21190574a7fbSJed Brown     } else {
21200574a7fbSJed Brown       ierr = VecCopy(ts->vec_sol,x);CHKERRQ(ierr);
21210574a7fbSJed Brown       if (ftime) *ftime = ts->ptime;
21220574a7fbSJed Brown     }
2123193ac0bcSJed Brown   }
21244d7d938eSLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)ts)->prefix,"-ts_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
21254d7d938eSLisandro Dalcin   if (flg && !PetscPreLoadingOn) {
21264d7d938eSLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)ts)->comm,filename,&viewer);CHKERRQ(ierr);
21274d7d938eSLisandro Dalcin     ierr = TSView(ts,viewer);CHKERRQ(ierr);
21284d7d938eSLisandro Dalcin     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
2129193ac0bcSJed Brown   }
21306a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
21316a4d4014SLisandro Dalcin }
21326a4d4014SLisandro Dalcin 
21336a4d4014SLisandro Dalcin #undef __FUNCT__
21344a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2135228d79bcSJed Brown /*@
2136228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2137228d79bcSJed Brown 
2138228d79bcSJed Brown    Collective on TS
2139228d79bcSJed Brown 
2140228d79bcSJed Brown    Input Parameters:
2141228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2142228d79bcSJed Brown .  step - step number that has just completed
2143228d79bcSJed Brown .  ptime - model time of the state
2144228d79bcSJed Brown -  x - state at the current model time
2145228d79bcSJed Brown 
2146228d79bcSJed Brown    Notes:
2147228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2148228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2149228d79bcSJed Brown 
2150228d79bcSJed Brown    Level: advanced
2151228d79bcSJed Brown 
2152228d79bcSJed Brown .keywords: TS, timestep
2153228d79bcSJed Brown @*/
2154a7cc72afSBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec x)
2155d763cef2SBarry Smith {
21566849ba73SBarry Smith   PetscErrorCode ierr;
2157a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2158d763cef2SBarry Smith 
2159d763cef2SBarry Smith   PetscFunctionBegin;
2160d763cef2SBarry Smith   for (i=0; i<n; i++) {
2161142b95e3SSatish Balay     ierr = (*ts->monitor[i])(ts,step,ptime,x,ts->monitorcontext[i]);CHKERRQ(ierr);
2162d763cef2SBarry Smith   }
2163d763cef2SBarry Smith   PetscFunctionReturn(0);
2164d763cef2SBarry Smith }
2165d763cef2SBarry Smith 
2166d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
2167d763cef2SBarry Smith 
21684a2ae208SSatish Balay #undef __FUNCT__
2169a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGCreate"
2170d763cef2SBarry Smith /*@C
2171a6570f20SBarry Smith    TSMonitorLGCreate - Creates a line graph context for use with
2172d763cef2SBarry Smith    TS to monitor convergence of preconditioned residual norms.
2173d763cef2SBarry Smith 
2174d763cef2SBarry Smith    Collective on TS
2175d763cef2SBarry Smith 
2176d763cef2SBarry Smith    Input Parameters:
2177d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2178d763cef2SBarry Smith .  label - the title to put in the title bar
21797c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2180d763cef2SBarry Smith -  m, n - the screen width and height in pixels
2181d763cef2SBarry Smith 
2182d763cef2SBarry Smith    Output Parameter:
2183d763cef2SBarry Smith .  draw - the drawing context
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith    Options Database Key:
2186a6570f20SBarry Smith .  -ts_monitor_draw - automatically sets line graph monitor
2187d763cef2SBarry Smith 
2188d763cef2SBarry Smith    Notes:
2189a6570f20SBarry Smith    Use TSMonitorLGDestroy() to destroy this line graph, not PetscDrawLGDestroy().
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith    Level: intermediate
2192d763cef2SBarry Smith 
21937c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2194d763cef2SBarry Smith 
2195a6570f20SBarry Smith .seealso: TSMonitorLGDestroy(), TSMonitorSet()
21967c922b88SBarry Smith 
2197d763cef2SBarry Smith @*/
2198*a80ad3e0SBarry Smith PetscErrorCode  TSMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
2199d763cef2SBarry Smith {
2200b0a32e0cSBarry Smith   PetscDraw      win;
2201dfbe8321SBarry Smith   PetscErrorCode ierr;
220270ddbef3SBarry Smith   PetscDrawAxis  axis;
2203d763cef2SBarry Smith 
2204d763cef2SBarry Smith   PetscFunctionBegin;
2205*a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
2206b0a32e0cSBarry Smith   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
2207b0a32e0cSBarry Smith   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
2208b0a32e0cSBarry Smith   ierr = PetscDrawLGIndicateDataPoints(*draw);CHKERRQ(ierr);
220970ddbef3SBarry Smith   ierr = PetscDrawLGGetAxis(*draw,&axis);CHKERRQ(ierr);
221070ddbef3SBarry Smith   ierr = PetscDrawAxisSetLabels(axis,"Integration Time","Iteration","Time");CHKERRQ(ierr);
221152e6d16bSBarry Smith   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
2212d763cef2SBarry Smith   PetscFunctionReturn(0);
2213d763cef2SBarry Smith }
2214d763cef2SBarry Smith 
22154a2ae208SSatish Balay #undef __FUNCT__
2216a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLG"
2217a6570f20SBarry Smith PetscErrorCode TSMonitorLG(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
2218d763cef2SBarry Smith {
2219b0a32e0cSBarry Smith   PetscDrawLG    lg = (PetscDrawLG) monctx;
222087828ca2SBarry Smith   PetscReal      x,y = ptime;
2221dfbe8321SBarry Smith   PetscErrorCode ierr;
2222d763cef2SBarry Smith 
2223d763cef2SBarry Smith   PetscFunctionBegin;
22247c922b88SBarry Smith   if (!monctx) {
22257c922b88SBarry Smith     MPI_Comm      comm;
2226b0a32e0cSBarry Smith     PetscViewer   viewer;
222770ddbef3SBarry Smith     PetscDrawAxis axis;
22287c922b88SBarry Smith 
22297c922b88SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
2230b0a32e0cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(comm);
2231b0a32e0cSBarry Smith     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
223270ddbef3SBarry Smith     ierr   = PetscDrawLGGetAxis(lg,&axis);CHKERRQ(ierr);
223370ddbef3SBarry Smith     ierr   = PetscDrawAxisSetLabels(axis,"Integration Time","Iteration","Time");CHKERRQ(ierr);
22347c922b88SBarry Smith   }
22357c922b88SBarry Smith 
2236b0a32e0cSBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
223787828ca2SBarry Smith   x = (PetscReal)n;
2238b0a32e0cSBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
2239d763cef2SBarry Smith   if (n < 20 || (n % 5)) {
2240b0a32e0cSBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
2241d763cef2SBarry Smith   }
2242d763cef2SBarry Smith   PetscFunctionReturn(0);
2243d763cef2SBarry Smith }
2244d763cef2SBarry Smith 
22454a2ae208SSatish Balay #undef __FUNCT__
2246a6570f20SBarry Smith #define __FUNCT__ "TSMonitorLGDestroy"
2247d763cef2SBarry Smith /*@C
2248a6570f20SBarry Smith    TSMonitorLGDestroy - Destroys a line graph context that was created
2249a6570f20SBarry Smith    with TSMonitorLGCreate().
2250d763cef2SBarry Smith 
2251b0a32e0cSBarry Smith    Collective on PetscDrawLG
2252d763cef2SBarry Smith 
2253d763cef2SBarry Smith    Input Parameter:
2254d763cef2SBarry Smith .  draw - the drawing context
2255d763cef2SBarry Smith 
2256d763cef2SBarry Smith    Level: intermediate
2257d763cef2SBarry Smith 
2258d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
2259d763cef2SBarry Smith 
2260a6570f20SBarry Smith .seealso: TSMonitorLGCreate(),  TSMonitorSet(), TSMonitorLG();
2261d763cef2SBarry Smith @*/
22626bf464f9SBarry Smith PetscErrorCode  TSMonitorLGDestroy(PetscDrawLG *drawlg)
2263d763cef2SBarry Smith {
2264b0a32e0cSBarry Smith   PetscDraw      draw;
2265dfbe8321SBarry Smith   PetscErrorCode ierr;
2266d763cef2SBarry Smith 
2267d763cef2SBarry Smith   PetscFunctionBegin;
22686bf464f9SBarry Smith   ierr = PetscDrawLGGetDraw(*drawlg,&draw);CHKERRQ(ierr);
22696bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
2270b0a32e0cSBarry Smith   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
2271d763cef2SBarry Smith   PetscFunctionReturn(0);
2272d763cef2SBarry Smith }
2273d763cef2SBarry Smith 
22744a2ae208SSatish Balay #undef __FUNCT__
22754a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
2276d763cef2SBarry Smith /*@
2277b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
2278d763cef2SBarry Smith 
2279d763cef2SBarry Smith    Not Collective
2280d763cef2SBarry Smith 
2281d763cef2SBarry Smith    Input Parameter:
2282d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2283d763cef2SBarry Smith 
2284d763cef2SBarry Smith    Output Parameter:
2285d763cef2SBarry Smith .  t  - the current time
2286d763cef2SBarry Smith 
2287d763cef2SBarry Smith    Level: beginner
2288d763cef2SBarry Smith 
2289b8123daeSJed Brown    Note:
2290b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
2291b8123daeSJed Brown    TSSetPreStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
2292b8123daeSJed Brown 
2293d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
2294d763cef2SBarry Smith 
2295d763cef2SBarry Smith .keywords: TS, get, time
2296d763cef2SBarry Smith @*/
22977087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal* t)
2298d763cef2SBarry Smith {
2299d763cef2SBarry Smith   PetscFunctionBegin;
23000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2301f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
2302d763cef2SBarry Smith   *t = ts->ptime;
2303d763cef2SBarry Smith   PetscFunctionReturn(0);
2304d763cef2SBarry Smith }
2305d763cef2SBarry Smith 
23064a2ae208SSatish Balay #undef __FUNCT__
23076a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
23086a4d4014SLisandro Dalcin /*@
23096a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
23106a4d4014SLisandro Dalcin 
23113f9fe445SBarry Smith    Logically Collective on TS
23126a4d4014SLisandro Dalcin 
23136a4d4014SLisandro Dalcin    Input Parameters:
23146a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
23156a4d4014SLisandro Dalcin -  time - the time
23166a4d4014SLisandro Dalcin 
23176a4d4014SLisandro Dalcin    Level: intermediate
23186a4d4014SLisandro Dalcin 
23196a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
23206a4d4014SLisandro Dalcin 
23216a4d4014SLisandro Dalcin .keywords: TS, set, time
23226a4d4014SLisandro Dalcin @*/
23237087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
23246a4d4014SLisandro Dalcin {
23256a4d4014SLisandro Dalcin   PetscFunctionBegin;
23260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2327c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
23286a4d4014SLisandro Dalcin   ts->ptime = t;
23296a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
23306a4d4014SLisandro Dalcin }
23316a4d4014SLisandro Dalcin 
23326a4d4014SLisandro Dalcin #undef __FUNCT__
23334a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
2334d763cef2SBarry Smith /*@C
2335d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
2336d763cef2SBarry Smith    TS options in the database.
2337d763cef2SBarry Smith 
23383f9fe445SBarry Smith    Logically Collective on TS
2339d763cef2SBarry Smith 
2340d763cef2SBarry Smith    Input Parameter:
2341d763cef2SBarry Smith +  ts     - The TS context
2342d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2343d763cef2SBarry Smith 
2344d763cef2SBarry Smith    Notes:
2345d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2346d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2347d763cef2SBarry Smith    hyphen.
2348d763cef2SBarry Smith 
2349d763cef2SBarry Smith    Level: advanced
2350d763cef2SBarry Smith 
2351d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
2352d763cef2SBarry Smith 
2353d763cef2SBarry Smith .seealso: TSSetFromOptions()
2354d763cef2SBarry Smith 
2355d763cef2SBarry Smith @*/
23567087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
2357d763cef2SBarry Smith {
2358dfbe8321SBarry Smith   PetscErrorCode ierr;
2359089b2837SJed Brown   SNES           snes;
2360d763cef2SBarry Smith 
2361d763cef2SBarry Smith   PetscFunctionBegin;
23620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2363d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2364089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2365089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2366d763cef2SBarry Smith   PetscFunctionReturn(0);
2367d763cef2SBarry Smith }
2368d763cef2SBarry Smith 
2369d763cef2SBarry Smith 
23704a2ae208SSatish Balay #undef __FUNCT__
23714a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
2372d763cef2SBarry Smith /*@C
2373d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
2374d763cef2SBarry Smith    TS options in the database.
2375d763cef2SBarry Smith 
23763f9fe445SBarry Smith    Logically Collective on TS
2377d763cef2SBarry Smith 
2378d763cef2SBarry Smith    Input Parameter:
2379d763cef2SBarry Smith +  ts     - The TS context
2380d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
2381d763cef2SBarry Smith 
2382d763cef2SBarry Smith    Notes:
2383d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
2384d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
2385d763cef2SBarry Smith    hyphen.
2386d763cef2SBarry Smith 
2387d763cef2SBarry Smith    Level: advanced
2388d763cef2SBarry Smith 
2389d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
2390d763cef2SBarry Smith 
2391d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
2392d763cef2SBarry Smith 
2393d763cef2SBarry Smith @*/
23947087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
2395d763cef2SBarry Smith {
2396dfbe8321SBarry Smith   PetscErrorCode ierr;
2397089b2837SJed Brown   SNES           snes;
2398d763cef2SBarry Smith 
2399d763cef2SBarry Smith   PetscFunctionBegin;
24000700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2401d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2402089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2403089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
2404d763cef2SBarry Smith   PetscFunctionReturn(0);
2405d763cef2SBarry Smith }
2406d763cef2SBarry Smith 
24074a2ae208SSatish Balay #undef __FUNCT__
24084a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
2409d763cef2SBarry Smith /*@C
2410d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
2411d763cef2SBarry Smith    TS options in the database.
2412d763cef2SBarry Smith 
2413d763cef2SBarry Smith    Not Collective
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Input Parameter:
2416d763cef2SBarry Smith .  ts - The TS context
2417d763cef2SBarry Smith 
2418d763cef2SBarry Smith    Output Parameter:
2419d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
2420d763cef2SBarry Smith 
2421d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
2422d763cef2SBarry Smith    sufficient length to hold the prefix.
2423d763cef2SBarry Smith 
2424d763cef2SBarry Smith    Level: intermediate
2425d763cef2SBarry Smith 
2426d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
2427d763cef2SBarry Smith 
2428d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
2429d763cef2SBarry Smith @*/
24307087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
2431d763cef2SBarry Smith {
2432dfbe8321SBarry Smith   PetscErrorCode ierr;
2433d763cef2SBarry Smith 
2434d763cef2SBarry Smith   PetscFunctionBegin;
24350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24364482741eSBarry Smith   PetscValidPointer(prefix,2);
2437d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
2438d763cef2SBarry Smith   PetscFunctionReturn(0);
2439d763cef2SBarry Smith }
2440d763cef2SBarry Smith 
24414a2ae208SSatish Balay #undef __FUNCT__
24424a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
2443d763cef2SBarry Smith /*@C
2444d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
2445d763cef2SBarry Smith 
2446d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
2447d763cef2SBarry Smith 
2448d763cef2SBarry Smith    Input Parameter:
2449d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
2450d763cef2SBarry Smith 
2451d763cef2SBarry Smith    Output Parameters:
2452d763cef2SBarry Smith +  J   - The Jacobian J of F, where U_t = F(U,t)
2453d763cef2SBarry Smith .  M   - The preconditioner matrix, usually the same as J
2454089b2837SJed Brown .  func - Function to compute the Jacobian of the RHS
2455d763cef2SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine
2456d763cef2SBarry Smith 
2457d763cef2SBarry Smith    Notes: You can pass in PETSC_NULL for any return argument you do not need.
2458d763cef2SBarry Smith 
2459d763cef2SBarry Smith    Level: intermediate
2460d763cef2SBarry Smith 
246126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
2462d763cef2SBarry Smith 
2463d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
2464d763cef2SBarry Smith @*/
2465089b2837SJed Brown PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *J,Mat *M,TSRHSJacobian *func,void **ctx)
2466d763cef2SBarry Smith {
2467089b2837SJed Brown   PetscErrorCode ierr;
2468089b2837SJed Brown   SNES           snes;
246924989b8cSPeter Brune   DM             dm;
2470089b2837SJed Brown 
2471d763cef2SBarry Smith   PetscFunctionBegin;
2472089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2473089b2837SJed Brown   ierr = SNESGetJacobian(snes,J,M,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
247424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
247524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
2476d763cef2SBarry Smith   PetscFunctionReturn(0);
2477d763cef2SBarry Smith }
2478d763cef2SBarry Smith 
24791713a123SBarry Smith #undef __FUNCT__
24802eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
24812eca1d9cSJed Brown /*@C
24822eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
24832eca1d9cSJed Brown 
24842eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
24852eca1d9cSJed Brown 
24862eca1d9cSJed Brown    Input Parameter:
24872eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
24882eca1d9cSJed Brown 
24892eca1d9cSJed Brown    Output Parameters:
24902eca1d9cSJed Brown +  A   - The Jacobian of F(t,U,U_t)
24912eca1d9cSJed Brown .  B   - The preconditioner matrix, often the same as A
24922eca1d9cSJed Brown .  f   - The function to compute the matrices
24932eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
24942eca1d9cSJed Brown 
24952eca1d9cSJed Brown    Notes: You can pass in PETSC_NULL for any return argument you do not need.
24962eca1d9cSJed Brown 
24972eca1d9cSJed Brown    Level: advanced
24982eca1d9cSJed Brown 
24992eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
25002eca1d9cSJed Brown 
25012eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
25022eca1d9cSJed Brown @*/
25037087cfbeSBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *A,Mat *B,TSIJacobian *f,void **ctx)
25042eca1d9cSJed Brown {
2505089b2837SJed Brown   PetscErrorCode ierr;
2506089b2837SJed Brown   SNES           snes;
250724989b8cSPeter Brune   DM             dm;
25082eca1d9cSJed Brown   PetscFunctionBegin;
2509089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2510089b2837SJed Brown   ierr = SNESGetJacobian(snes,A,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
251124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
251224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
25132eca1d9cSJed Brown   PetscFunctionReturn(0);
25142eca1d9cSJed Brown }
25152eca1d9cSJed Brown 
25166083293cSBarry Smith typedef struct {
25176083293cSBarry Smith   PetscViewer viewer;
25186083293cSBarry Smith   Vec         initialsolution;
25196083293cSBarry Smith   PetscBool   showinitial;
25206083293cSBarry Smith } TSMonitorSolutionCtx;
25216083293cSBarry Smith 
25222eca1d9cSJed Brown #undef __FUNCT__
2523a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSolution"
25241713a123SBarry Smith /*@C
2525a6570f20SBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by calling
25261713a123SBarry Smith    VecView() for the solution at each timestep
25271713a123SBarry Smith 
25281713a123SBarry Smith    Collective on TS
25291713a123SBarry Smith 
25301713a123SBarry Smith    Input Parameters:
25311713a123SBarry Smith +  ts - the TS context
25321713a123SBarry Smith .  step - current time-step
2533142b95e3SSatish Balay .  ptime - current time
25341713a123SBarry Smith -  dummy - either a viewer or PETSC_NULL
25351713a123SBarry Smith 
25361713a123SBarry Smith    Level: intermediate
25371713a123SBarry Smith 
25381713a123SBarry Smith .keywords: TS,  vector, monitor, view
25391713a123SBarry Smith 
2540a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
25411713a123SBarry Smith @*/
25427087cfbeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec x,void *dummy)
25431713a123SBarry Smith {
2544dfbe8321SBarry Smith   PetscErrorCode       ierr;
25456083293cSBarry Smith   TSMonitorSolutionCtx *ictx = (TSMonitorSolutionCtx*)dummy;
25461713a123SBarry Smith 
25471713a123SBarry Smith   PetscFunctionBegin;
25486083293cSBarry Smith   if (!step && ictx->showinitial) {
25496083293cSBarry Smith     if (!ictx->initialsolution) {
25506083293cSBarry Smith       ierr = VecDuplicate(x,&ictx->initialsolution);CHKERRQ(ierr);
25511713a123SBarry Smith     }
25526083293cSBarry Smith     ierr = VecCopy(x,ictx->initialsolution);CHKERRQ(ierr);
25536083293cSBarry Smith   }
25546083293cSBarry Smith   if (ictx->showinitial) {
25556083293cSBarry Smith     PetscReal pause;
25566083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
25576083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
25586083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
25596083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
25606083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
25616083293cSBarry Smith   }
25626083293cSBarry Smith   ierr = VecView(x,ictx->viewer);CHKERRQ(ierr);
25636083293cSBarry Smith   if (ictx->showinitial) {
25646083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
25656083293cSBarry Smith   }
25661713a123SBarry Smith   PetscFunctionReturn(0);
25671713a123SBarry Smith }
25681713a123SBarry Smith 
25691713a123SBarry Smith 
25706c699258SBarry Smith #undef __FUNCT__
25716083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionDestroy"
25726083293cSBarry Smith /*@C
25736083293cSBarry Smith    TSMonitorSolutionDestroy - Destroys the monitor context for TSMonitorSolution
25746083293cSBarry Smith 
25756083293cSBarry Smith    Collective on TS
25766083293cSBarry Smith 
25776083293cSBarry Smith    Input Parameters:
25786083293cSBarry Smith .    ctx - the monitor context
25796083293cSBarry Smith 
25806083293cSBarry Smith    Level: intermediate
25816083293cSBarry Smith 
25826083293cSBarry Smith .keywords: TS,  vector, monitor, view
25836083293cSBarry Smith 
25846083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
25856083293cSBarry Smith @*/
25866083293cSBarry Smith PetscErrorCode  TSMonitorSolutionDestroy(void **ctx)
25876083293cSBarry Smith {
25886083293cSBarry Smith   PetscErrorCode       ierr;
25896083293cSBarry Smith   TSMonitorSolutionCtx *ictx = *(TSMonitorSolutionCtx**)ctx;
25906083293cSBarry Smith 
25916083293cSBarry Smith   PetscFunctionBegin;
25926083293cSBarry Smith   ierr = PetscViewerDestroy(&ictx->viewer);CHKERRQ(ierr);
25936083293cSBarry Smith   ierr = VecDestroy(&ictx->initialsolution);CHKERRQ(ierr);
25946083293cSBarry Smith   ierr = PetscFree(ictx);CHKERRQ(ierr);
25956083293cSBarry Smith   PetscFunctionReturn(0);
25966083293cSBarry Smith }
25976083293cSBarry Smith 
25986083293cSBarry Smith #undef __FUNCT__
25996083293cSBarry Smith #define __FUNCT__ "TSMonitorSolutionCreate"
26006083293cSBarry Smith /*@C
26016083293cSBarry Smith    TSMonitorSolutionCreate - Creates the monitor context for TSMonitorSolution
26026083293cSBarry Smith 
26036083293cSBarry Smith    Collective on TS
26046083293cSBarry Smith 
26056083293cSBarry Smith    Input Parameter:
26066083293cSBarry Smith .    ts - time-step context
26076083293cSBarry Smith 
26086083293cSBarry Smith    Output Patameter:
26096083293cSBarry Smith .    ctx - the monitor context
26106083293cSBarry Smith 
26116083293cSBarry Smith    Level: intermediate
26126083293cSBarry Smith 
26136083293cSBarry Smith .keywords: TS,  vector, monitor, view
26146083293cSBarry Smith 
26156083293cSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorSolution()
26166083293cSBarry Smith @*/
26176083293cSBarry Smith PetscErrorCode  TSMonitorSolutionCreate(TS ts,PetscViewer viewer,void **ctx)
26186083293cSBarry Smith {
26196083293cSBarry Smith   PetscErrorCode       ierr;
26206083293cSBarry Smith   TSMonitorSolutionCtx *ictx;
26216083293cSBarry Smith 
26226083293cSBarry Smith   PetscFunctionBegin;
26236083293cSBarry Smith   ierr = PetscNew(TSMonitorSolutionCtx,&ictx);CHKERRQ(ierr);
26246083293cSBarry Smith   *ctx = (void*)ictx;
26256083293cSBarry Smith   if (!viewer) {
26266083293cSBarry Smith     viewer = PETSC_VIEWER_DRAW_(((PetscObject)ts)->comm);
26276083293cSBarry Smith   }
26286083293cSBarry Smith   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
26296083293cSBarry Smith   ictx->viewer      = viewer;
26306083293cSBarry Smith   ictx->showinitial = PETSC_FALSE;
26316083293cSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->prefix,"-ts_monitor_solution_initial",&ictx->showinitial,PETSC_NULL);CHKERRQ(ierr);
26326083293cSBarry Smith   PetscFunctionReturn(0);
26336083293cSBarry Smith }
26346083293cSBarry Smith 
26356083293cSBarry Smith #undef __FUNCT__
26366c699258SBarry Smith #define __FUNCT__ "TSSetDM"
26376c699258SBarry Smith /*@
26386c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
26396c699258SBarry Smith 
26403f9fe445SBarry Smith    Logically Collective on TS and DM
26416c699258SBarry Smith 
26426c699258SBarry Smith    Input Parameters:
26436c699258SBarry Smith +  ts - the preconditioner context
26446c699258SBarry Smith -  dm - the dm
26456c699258SBarry Smith 
26466c699258SBarry Smith    Level: intermediate
26476c699258SBarry Smith 
26486c699258SBarry Smith 
26496c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
26506c699258SBarry Smith @*/
26517087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
26526c699258SBarry Smith {
26536c699258SBarry Smith   PetscErrorCode ierr;
2654089b2837SJed Brown   SNES           snes;
265524989b8cSPeter Brune   TSDM           tsdm;
26566c699258SBarry Smith 
26576c699258SBarry Smith   PetscFunctionBegin;
26580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
265970663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
266024989b8cSPeter Brune   if (ts->dm) {               /* Move the TSDM context over to the new DM unless the new DM already has one */
266124989b8cSPeter Brune     PetscContainer oldcontainer,container;
266224989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)ts->dm,"TSDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr);
266324989b8cSPeter Brune     ierr = PetscObjectQuery((PetscObject)dm,"TSDM",(PetscObject*)&container);CHKERRQ(ierr);
266424989b8cSPeter Brune     if (oldcontainer && !container) {
266524989b8cSPeter Brune       ierr = DMTSCopyContext(ts->dm,dm);CHKERRQ(ierr);
266624989b8cSPeter Brune       ierr = DMTSGetContext(ts->dm,&tsdm);CHKERRQ(ierr);
266724989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
266824989b8cSPeter Brune         tsdm->originaldm = dm;
266924989b8cSPeter Brune       }
267024989b8cSPeter Brune     }
26716bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
267224989b8cSPeter Brune   }
26736c699258SBarry Smith   ts->dm = dm;
2674089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2675089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
26766c699258SBarry Smith   PetscFunctionReturn(0);
26776c699258SBarry Smith }
26786c699258SBarry Smith 
26796c699258SBarry Smith #undef __FUNCT__
26806c699258SBarry Smith #define __FUNCT__ "TSGetDM"
26816c699258SBarry Smith /*@
26826c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
26836c699258SBarry Smith 
26843f9fe445SBarry Smith    Not Collective
26856c699258SBarry Smith 
26866c699258SBarry Smith    Input Parameter:
26876c699258SBarry Smith . ts - the preconditioner context
26886c699258SBarry Smith 
26896c699258SBarry Smith    Output Parameter:
26906c699258SBarry Smith .  dm - the dm
26916c699258SBarry Smith 
26926c699258SBarry Smith    Level: intermediate
26936c699258SBarry Smith 
26946c699258SBarry Smith 
26956c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
26966c699258SBarry Smith @*/
26977087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
26986c699258SBarry Smith {
2699496e6a7aSJed Brown   PetscErrorCode ierr;
2700496e6a7aSJed Brown 
27016c699258SBarry Smith   PetscFunctionBegin;
27020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2703496e6a7aSJed Brown   if (!ts->dm) {
2704496e6a7aSJed Brown     ierr = DMShellCreate(((PetscObject)ts)->comm,&ts->dm);CHKERRQ(ierr);
2705496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
2706496e6a7aSJed Brown   }
27076c699258SBarry Smith   *dm = ts->dm;
27086c699258SBarry Smith   PetscFunctionReturn(0);
27096c699258SBarry Smith }
27101713a123SBarry Smith 
27110f5c6efeSJed Brown #undef __FUNCT__
27120f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
27130f5c6efeSJed Brown /*@
27140f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
27150f5c6efeSJed Brown 
27163f9fe445SBarry Smith    Logically Collective on SNES
27170f5c6efeSJed Brown 
27180f5c6efeSJed Brown    Input Parameter:
2719d42a1c89SJed Brown + snes - nonlinear solver
27200f5c6efeSJed Brown . X - the current state at which to evaluate the residual
2721d42a1c89SJed Brown - ctx - user context, must be a TS
27220f5c6efeSJed Brown 
27230f5c6efeSJed Brown    Output Parameter:
27240f5c6efeSJed Brown . F - the nonlinear residual
27250f5c6efeSJed Brown 
27260f5c6efeSJed Brown    Notes:
27270f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
27280f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
27290f5c6efeSJed Brown 
27300f5c6efeSJed Brown    Level: advanced
27310f5c6efeSJed Brown 
27320f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
27330f5c6efeSJed Brown @*/
27347087cfbeSBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec X,Vec F,void *ctx)
27350f5c6efeSJed Brown {
27360f5c6efeSJed Brown   TS ts = (TS)ctx;
27370f5c6efeSJed Brown   PetscErrorCode ierr;
27380f5c6efeSJed Brown 
27390f5c6efeSJed Brown   PetscFunctionBegin;
27400f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27410f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
27420f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
27430f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
27440f5c6efeSJed Brown   ierr = (ts->ops->snesfunction)(snes,X,F,ts);CHKERRQ(ierr);
27450f5c6efeSJed Brown   PetscFunctionReturn(0);
27460f5c6efeSJed Brown }
27470f5c6efeSJed Brown 
27480f5c6efeSJed Brown #undef __FUNCT__
27490f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
27500f5c6efeSJed Brown /*@
27510f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
27520f5c6efeSJed Brown 
27530f5c6efeSJed Brown    Collective on SNES
27540f5c6efeSJed Brown 
27550f5c6efeSJed Brown    Input Parameter:
27560f5c6efeSJed Brown + snes - nonlinear solver
27570f5c6efeSJed Brown . X - the current state at which to evaluate the residual
27580f5c6efeSJed Brown - ctx - user context, must be a TS
27590f5c6efeSJed Brown 
27600f5c6efeSJed Brown    Output Parameter:
27610f5c6efeSJed Brown + A - the Jacobian
27620f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
27630f5c6efeSJed Brown - flag - indicates any structure change in the matrix
27640f5c6efeSJed Brown 
27650f5c6efeSJed Brown    Notes:
27660f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
27670f5c6efeSJed Brown 
27680f5c6efeSJed Brown    Level: developer
27690f5c6efeSJed Brown 
27700f5c6efeSJed Brown .seealso: SNESSetJacobian()
27710f5c6efeSJed Brown @*/
27727087cfbeSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flag,void *ctx)
27730f5c6efeSJed Brown {
27740f5c6efeSJed Brown   TS ts = (TS)ctx;
27750f5c6efeSJed Brown   PetscErrorCode ierr;
27760f5c6efeSJed Brown 
27770f5c6efeSJed Brown   PetscFunctionBegin;
27780f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
27790f5c6efeSJed Brown   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
27800f5c6efeSJed Brown   PetscValidPointer(A,3);
27810f5c6efeSJed Brown   PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
27820f5c6efeSJed Brown   PetscValidPointer(B,4);
27830f5c6efeSJed Brown   PetscValidHeaderSpecific(*B,MAT_CLASSID,4);
27840f5c6efeSJed Brown   PetscValidPointer(flag,5);
27850f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
27860f5c6efeSJed Brown   ierr = (ts->ops->snesjacobian)(snes,X,A,B,flag,ts);CHKERRQ(ierr);
27870f5c6efeSJed Brown   PetscFunctionReturn(0);
27880f5c6efeSJed Brown }
2789325fc9f4SBarry Smith 
27900e4ef248SJed Brown #undef __FUNCT__
27910e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
27920e4ef248SJed Brown /*@C
27930e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
27940e4ef248SJed Brown 
27950e4ef248SJed Brown    Collective on TS
27960e4ef248SJed Brown 
27970e4ef248SJed Brown    Input Arguments:
27980e4ef248SJed Brown +  ts - time stepping context
27990e4ef248SJed Brown .  t - time at which to evaluate
28000e4ef248SJed Brown .  X - state at which to evaluate
28010e4ef248SJed Brown -  ctx - context
28020e4ef248SJed Brown 
28030e4ef248SJed Brown    Output Arguments:
28040e4ef248SJed Brown .  F - right hand side
28050e4ef248SJed Brown 
28060e4ef248SJed Brown    Level: intermediate
28070e4ef248SJed Brown 
28080e4ef248SJed Brown    Notes:
28090e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
28100e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
28110e4ef248SJed Brown 
28120e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
28130e4ef248SJed Brown @*/
28140e4ef248SJed Brown PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec X,Vec F,void *ctx)
28150e4ef248SJed Brown {
28160e4ef248SJed Brown   PetscErrorCode ierr;
28170e4ef248SJed Brown   Mat Arhs,Brhs;
28180e4ef248SJed Brown   MatStructure flg2;
28190e4ef248SJed Brown 
28200e4ef248SJed Brown   PetscFunctionBegin;
28210e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
28220e4ef248SJed Brown   ierr = TSComputeRHSJacobian(ts,t,X,&Arhs,&Brhs,&flg2);CHKERRQ(ierr);
28230e4ef248SJed Brown   ierr = MatMult(Arhs,X,F);CHKERRQ(ierr);
28240e4ef248SJed Brown   PetscFunctionReturn(0);
28250e4ef248SJed Brown }
28260e4ef248SJed Brown 
28270e4ef248SJed Brown #undef __FUNCT__
28280e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
28290e4ef248SJed Brown /*@C
28300e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
28310e4ef248SJed Brown 
28320e4ef248SJed Brown    Collective on TS
28330e4ef248SJed Brown 
28340e4ef248SJed Brown    Input Arguments:
28350e4ef248SJed Brown +  ts - time stepping context
28360e4ef248SJed Brown .  t - time at which to evaluate
28370e4ef248SJed Brown .  X - state at which to evaluate
28380e4ef248SJed Brown -  ctx - context
28390e4ef248SJed Brown 
28400e4ef248SJed Brown    Output Arguments:
28410e4ef248SJed Brown +  A - pointer to operator
28420e4ef248SJed Brown .  B - pointer to preconditioning matrix
28430e4ef248SJed Brown -  flg - matrix structure flag
28440e4ef248SJed Brown 
28450e4ef248SJed Brown    Level: intermediate
28460e4ef248SJed Brown 
28470e4ef248SJed Brown    Notes:
28480e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
28490e4ef248SJed Brown 
28500e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
28510e4ef248SJed Brown @*/
28520e4ef248SJed Brown PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec X,Mat *A,Mat *B,MatStructure *flg,void *ctx)
28530e4ef248SJed Brown {
28540e4ef248SJed Brown 
28550e4ef248SJed Brown   PetscFunctionBegin;
28560e4ef248SJed Brown   *flg = SAME_PRECONDITIONER;
28570e4ef248SJed Brown   PetscFunctionReturn(0);
28580e4ef248SJed Brown }
28590e4ef248SJed Brown 
28600026cea9SSean Farley #undef __FUNCT__
28610026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
28620026cea9SSean Farley /*@C
28630026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
28640026cea9SSean Farley 
28650026cea9SSean Farley    Collective on TS
28660026cea9SSean Farley 
28670026cea9SSean Farley    Input Arguments:
28680026cea9SSean Farley +  ts - time stepping context
28690026cea9SSean Farley .  t - time at which to evaluate
28700026cea9SSean Farley .  X - state at which to evaluate
28710026cea9SSean Farley .  Xdot - time derivative of state vector
28720026cea9SSean Farley -  ctx - context
28730026cea9SSean Farley 
28740026cea9SSean Farley    Output Arguments:
28750026cea9SSean Farley .  F - left hand side
28760026cea9SSean Farley 
28770026cea9SSean Farley    Level: intermediate
28780026cea9SSean Farley 
28790026cea9SSean Farley    Notes:
28800026cea9SSean 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
28810026cea9SSean Farley    user is required to write their own TSComputeIFunction.
28820026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
28830026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
28840026cea9SSean Farley 
28850026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
28860026cea9SSean Farley @*/
28870026cea9SSean Farley PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
28880026cea9SSean Farley {
28890026cea9SSean Farley   PetscErrorCode ierr;
28900026cea9SSean Farley   Mat A,B;
28910026cea9SSean Farley   MatStructure flg2;
28920026cea9SSean Farley 
28930026cea9SSean Farley   PetscFunctionBegin;
28940026cea9SSean Farley   ierr = TSGetIJacobian(ts,&A,&B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
28950026cea9SSean Farley   ierr = TSComputeIJacobian(ts,t,X,Xdot,1.0,&A,&B,&flg2,PETSC_TRUE);CHKERRQ(ierr);
28960026cea9SSean Farley   ierr = MatMult(A,Xdot,F);CHKERRQ(ierr);
28970026cea9SSean Farley   PetscFunctionReturn(0);
28980026cea9SSean Farley }
28990026cea9SSean Farley 
29000026cea9SSean Farley #undef __FUNCT__
29010026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
29020026cea9SSean Farley /*@C
290324e94211SJed Brown    TSComputeIJacobianConstant - Reuses a Jacobian that is time-independent.
29040026cea9SSean Farley 
29050026cea9SSean Farley    Collective on TS
29060026cea9SSean Farley 
29070026cea9SSean Farley    Input Arguments:
29080026cea9SSean Farley +  ts - time stepping context
29090026cea9SSean Farley .  t - time at which to evaluate
29100026cea9SSean Farley .  X - state at which to evaluate
29110026cea9SSean Farley .  Xdot - time derivative of state vector
29120026cea9SSean Farley .  shift - shift to apply
29130026cea9SSean Farley -  ctx - context
29140026cea9SSean Farley 
29150026cea9SSean Farley    Output Arguments:
29160026cea9SSean Farley +  A - pointer to operator
29170026cea9SSean Farley .  B - pointer to preconditioning matrix
29180026cea9SSean Farley -  flg - matrix structure flag
29190026cea9SSean Farley 
29200026cea9SSean Farley    Level: intermediate
29210026cea9SSean Farley 
29220026cea9SSean Farley    Notes:
29230026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
29240026cea9SSean Farley 
29250026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
29260026cea9SSean Farley @*/
29270026cea9SSean Farley PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flg,void *ctx)
29280026cea9SSean Farley {
29290026cea9SSean Farley 
29300026cea9SSean Farley   PetscFunctionBegin;
29310026cea9SSean Farley   *flg = SAME_PRECONDITIONER;
29320026cea9SSean Farley   PetscFunctionReturn(0);
29330026cea9SSean Farley }
29340026cea9SSean Farley 
29354af1b03aSJed Brown 
29364af1b03aSJed Brown #undef __FUNCT__
29374af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
29384af1b03aSJed Brown /*@
29394af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
29404af1b03aSJed Brown 
29414af1b03aSJed Brown    Not Collective
29424af1b03aSJed Brown 
29434af1b03aSJed Brown    Input Parameter:
29444af1b03aSJed Brown .  ts - the TS context
29454af1b03aSJed Brown 
29464af1b03aSJed Brown    Output Parameter:
29474af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
29484af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
29494af1b03aSJed Brown 
29504af1b03aSJed Brown    Level: intermediate
29514af1b03aSJed Brown 
2952cd652676SJed Brown    Notes:
2953cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
29544af1b03aSJed Brown 
29554af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
29564af1b03aSJed Brown 
29574af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
29584af1b03aSJed Brown @*/
29594af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
29604af1b03aSJed Brown {
29614af1b03aSJed Brown   PetscFunctionBegin;
29624af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29634af1b03aSJed Brown   PetscValidPointer(reason,2);
29644af1b03aSJed Brown   *reason = ts->reason;
29654af1b03aSJed Brown   PetscFunctionReturn(0);
29664af1b03aSJed Brown }
29674af1b03aSJed Brown 
2968fb1732b5SBarry Smith #undef __FUNCT__
29695ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
29709f67acb7SJed Brown /*@
29715ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
29729f67acb7SJed Brown    used by the time integrator.
29739f67acb7SJed Brown 
29749f67acb7SJed Brown    Not Collective
29759f67acb7SJed Brown 
29769f67acb7SJed Brown    Input Parameter:
29779f67acb7SJed Brown .  ts - TS context
29789f67acb7SJed Brown 
29799f67acb7SJed Brown    Output Parameter:
29809f67acb7SJed Brown .  nits - number of nonlinear iterations
29819f67acb7SJed Brown 
29829f67acb7SJed Brown    Notes:
29839f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
29849f67acb7SJed Brown 
29859f67acb7SJed Brown    Level: intermediate
29869f67acb7SJed Brown 
29879f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
29889f67acb7SJed Brown 
29895ef26d82SJed Brown .seealso:  TSGetKSPIterations()
29909f67acb7SJed Brown @*/
29915ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
29929f67acb7SJed Brown {
29939f67acb7SJed Brown   PetscFunctionBegin;
29949f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29959f67acb7SJed Brown   PetscValidIntPointer(nits,2);
29965ef26d82SJed Brown   *nits = ts->snes_its;
29979f67acb7SJed Brown   PetscFunctionReturn(0);
29989f67acb7SJed Brown }
29999f67acb7SJed Brown 
30009f67acb7SJed Brown #undef __FUNCT__
30015ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
30029f67acb7SJed Brown /*@
30035ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
30049f67acb7SJed Brown    used by the time integrator.
30059f67acb7SJed Brown 
30069f67acb7SJed Brown    Not Collective
30079f67acb7SJed Brown 
30089f67acb7SJed Brown    Input Parameter:
30099f67acb7SJed Brown .  ts - TS context
30109f67acb7SJed Brown 
30119f67acb7SJed Brown    Output Parameter:
30129f67acb7SJed Brown .  lits - number of linear iterations
30139f67acb7SJed Brown 
30149f67acb7SJed Brown    Notes:
30159f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
30169f67acb7SJed Brown 
30179f67acb7SJed Brown    Level: intermediate
30189f67acb7SJed Brown 
30199f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
30209f67acb7SJed Brown 
30215ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
30229f67acb7SJed Brown @*/
30235ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
30249f67acb7SJed Brown {
30259f67acb7SJed Brown   PetscFunctionBegin;
30269f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30279f67acb7SJed Brown   PetscValidIntPointer(lits,2);
30285ef26d82SJed Brown   *lits = ts->ksp_its;
30299f67acb7SJed Brown   PetscFunctionReturn(0);
30309f67acb7SJed Brown }
30319f67acb7SJed Brown 
30329f67acb7SJed Brown #undef __FUNCT__
3033cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
3034cef5090cSJed Brown /*@
3035cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
3036cef5090cSJed Brown 
3037cef5090cSJed Brown    Not Collective
3038cef5090cSJed Brown 
3039cef5090cSJed Brown    Input Parameter:
3040cef5090cSJed Brown .  ts - TS context
3041cef5090cSJed Brown 
3042cef5090cSJed Brown    Output Parameter:
3043cef5090cSJed Brown .  rejects - number of steps rejected
3044cef5090cSJed Brown 
3045cef5090cSJed Brown    Notes:
3046cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3047cef5090cSJed Brown 
3048cef5090cSJed Brown    Level: intermediate
3049cef5090cSJed Brown 
3050cef5090cSJed Brown .keywords: TS, get, number
3051cef5090cSJed Brown 
30525ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
3053cef5090cSJed Brown @*/
3054cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
3055cef5090cSJed Brown {
3056cef5090cSJed Brown   PetscFunctionBegin;
3057cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3058cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
3059cef5090cSJed Brown   *rejects = ts->reject;
3060cef5090cSJed Brown   PetscFunctionReturn(0);
3061cef5090cSJed Brown }
3062cef5090cSJed Brown 
3063cef5090cSJed Brown #undef __FUNCT__
3064cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
3065cef5090cSJed Brown /*@
3066cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
3067cef5090cSJed Brown 
3068cef5090cSJed Brown    Not Collective
3069cef5090cSJed Brown 
3070cef5090cSJed Brown    Input Parameter:
3071cef5090cSJed Brown .  ts - TS context
3072cef5090cSJed Brown 
3073cef5090cSJed Brown    Output Parameter:
3074cef5090cSJed Brown .  fails - number of failed nonlinear solves
3075cef5090cSJed Brown 
3076cef5090cSJed Brown    Notes:
3077cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
3078cef5090cSJed Brown 
3079cef5090cSJed Brown    Level: intermediate
3080cef5090cSJed Brown 
3081cef5090cSJed Brown .keywords: TS, get, number
3082cef5090cSJed Brown 
30835ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
3084cef5090cSJed Brown @*/
3085cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
3086cef5090cSJed Brown {
3087cef5090cSJed Brown   PetscFunctionBegin;
3088cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3089cef5090cSJed Brown   PetscValidIntPointer(fails,2);
3090cef5090cSJed Brown   *fails = ts->num_snes_failures;
3091cef5090cSJed Brown   PetscFunctionReturn(0);
3092cef5090cSJed Brown }
3093cef5090cSJed Brown 
3094cef5090cSJed Brown #undef __FUNCT__
3095cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
3096cef5090cSJed Brown /*@
3097cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
3098cef5090cSJed Brown 
3099cef5090cSJed Brown    Not Collective
3100cef5090cSJed Brown 
3101cef5090cSJed Brown    Input Parameter:
3102cef5090cSJed Brown +  ts - TS context
3103cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
3104cef5090cSJed Brown 
3105cef5090cSJed Brown    Notes:
3106cef5090cSJed Brown    The counter is reset to zero for each step
3107cef5090cSJed Brown 
3108cef5090cSJed Brown    Options Database Key:
3109cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
3110cef5090cSJed Brown 
3111cef5090cSJed Brown    Level: intermediate
3112cef5090cSJed Brown 
3113cef5090cSJed Brown .keywords: TS, set, maximum, number
3114cef5090cSJed Brown 
31155ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3116cef5090cSJed Brown @*/
3117cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
3118cef5090cSJed Brown {
3119cef5090cSJed Brown   PetscFunctionBegin;
3120cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3121cef5090cSJed Brown   ts->max_reject = rejects;
3122cef5090cSJed Brown   PetscFunctionReturn(0);
3123cef5090cSJed Brown }
3124cef5090cSJed Brown 
3125cef5090cSJed Brown #undef __FUNCT__
3126cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
3127cef5090cSJed Brown /*@
3128cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
3129cef5090cSJed Brown 
3130cef5090cSJed Brown    Not Collective
3131cef5090cSJed Brown 
3132cef5090cSJed Brown    Input Parameter:
3133cef5090cSJed Brown +  ts - TS context
3134cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
3135cef5090cSJed Brown 
3136cef5090cSJed Brown    Notes:
3137cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
3138cef5090cSJed Brown 
3139cef5090cSJed Brown    Options Database Key:
3140cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
3141cef5090cSJed Brown 
3142cef5090cSJed Brown    Level: intermediate
3143cef5090cSJed Brown 
3144cef5090cSJed Brown .keywords: TS, set, maximum, number
3145cef5090cSJed Brown 
31465ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
3147cef5090cSJed Brown @*/
3148cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
3149cef5090cSJed Brown {
3150cef5090cSJed Brown   PetscFunctionBegin;
3151cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3152cef5090cSJed Brown   ts->max_snes_failures = fails;
3153cef5090cSJed Brown   PetscFunctionReturn(0);
3154cef5090cSJed Brown }
3155cef5090cSJed Brown 
3156cef5090cSJed Brown #undef __FUNCT__
3157cef5090cSJed Brown #define __FUNCT__ "TSSetErrorIfStepFails()"
3158cef5090cSJed Brown /*@
3159cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
3160cef5090cSJed Brown 
3161cef5090cSJed Brown    Not Collective
3162cef5090cSJed Brown 
3163cef5090cSJed Brown    Input Parameter:
3164cef5090cSJed Brown +  ts - TS context
3165cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
3166cef5090cSJed Brown 
3167cef5090cSJed Brown    Options Database Key:
3168cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
3169cef5090cSJed Brown 
3170cef5090cSJed Brown    Level: intermediate
3171cef5090cSJed Brown 
3172cef5090cSJed Brown .keywords: TS, set, error
3173cef5090cSJed Brown 
31745ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
3175cef5090cSJed Brown @*/
3176cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
3177cef5090cSJed Brown {
3178cef5090cSJed Brown   PetscFunctionBegin;
3179cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3180cef5090cSJed Brown   ts->errorifstepfailed = err;
3181cef5090cSJed Brown   PetscFunctionReturn(0);
3182cef5090cSJed Brown }
3183cef5090cSJed Brown 
3184cef5090cSJed Brown #undef __FUNCT__
3185fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
3186fb1732b5SBarry Smith /*@C
3187fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
3188fb1732b5SBarry Smith 
3189fb1732b5SBarry Smith    Collective on TS
3190fb1732b5SBarry Smith 
3191fb1732b5SBarry Smith    Input Parameters:
3192fb1732b5SBarry Smith +  ts - the TS context
3193fb1732b5SBarry Smith .  step - current time-step
3194fb1732b5SBarry Smith .  ptime - current time
3195ed81e22dSJed Brown .  x - current state
3196fb1732b5SBarry Smith -  viewer - binary viewer
3197fb1732b5SBarry Smith 
3198fb1732b5SBarry Smith    Level: intermediate
3199fb1732b5SBarry Smith 
3200fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
3201fb1732b5SBarry Smith 
3202fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3203fb1732b5SBarry Smith @*/
3204ed81e22dSJed Brown PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec x,void *viewer)
3205fb1732b5SBarry Smith {
3206fb1732b5SBarry Smith   PetscErrorCode       ierr;
3207ed81e22dSJed Brown   PetscViewer          v = (PetscViewer)viewer;
3208fb1732b5SBarry Smith 
3209fb1732b5SBarry Smith   PetscFunctionBegin;
3210ed81e22dSJed Brown   ierr = VecView(x,v);CHKERRQ(ierr);
3211ed81e22dSJed Brown   PetscFunctionReturn(0);
3212ed81e22dSJed Brown }
3213ed81e22dSJed Brown 
3214ed81e22dSJed Brown #undef __FUNCT__
3215ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
3216ed81e22dSJed Brown /*@C
3217ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
3218ed81e22dSJed Brown 
3219ed81e22dSJed Brown    Collective on TS
3220ed81e22dSJed Brown 
3221ed81e22dSJed Brown    Input Parameters:
3222ed81e22dSJed Brown +  ts - the TS context
3223ed81e22dSJed Brown .  step - current time-step
3224ed81e22dSJed Brown .  ptime - current time
3225ed81e22dSJed Brown .  x - current state
3226ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3227ed81e22dSJed Brown 
3228ed81e22dSJed Brown    Level: intermediate
3229ed81e22dSJed Brown 
3230ed81e22dSJed Brown    Notes:
3231ed81e22dSJed 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.
3232ed81e22dSJed Brown    These are named according to the file name template.
3233ed81e22dSJed Brown 
3234ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
3235ed81e22dSJed Brown 
3236ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3237ed81e22dSJed Brown 
3238ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
3239ed81e22dSJed Brown @*/
3240ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec x,void *filenametemplate)
3241ed81e22dSJed Brown {
3242ed81e22dSJed Brown   PetscErrorCode ierr;
3243ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
3244ed81e22dSJed Brown   PetscViewer    viewer;
3245ed81e22dSJed Brown 
3246ed81e22dSJed Brown   PetscFunctionBegin;
32478caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
3248ed81e22dSJed Brown   ierr = PetscViewerVTKOpen(((PetscObject)ts)->comm,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
3249fb1732b5SBarry Smith   ierr = VecView(x,viewer);CHKERRQ(ierr);
3250ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
3251ed81e22dSJed Brown   PetscFunctionReturn(0);
3252ed81e22dSJed Brown }
3253ed81e22dSJed Brown 
3254ed81e22dSJed Brown #undef __FUNCT__
3255ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
3256ed81e22dSJed Brown /*@C
3257ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
3258ed81e22dSJed Brown 
3259ed81e22dSJed Brown    Collective on TS
3260ed81e22dSJed Brown 
3261ed81e22dSJed Brown    Input Parameters:
3262ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
3263ed81e22dSJed Brown 
3264ed81e22dSJed Brown    Level: intermediate
3265ed81e22dSJed Brown 
3266ed81e22dSJed Brown    Note:
3267ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
3268ed81e22dSJed Brown 
3269ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
3270ed81e22dSJed Brown 
3271ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
3272ed81e22dSJed Brown @*/
3273ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
3274ed81e22dSJed Brown {
3275ed81e22dSJed Brown   PetscErrorCode ierr;
3276ed81e22dSJed Brown 
3277ed81e22dSJed Brown   PetscFunctionBegin;
3278ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
3279fb1732b5SBarry Smith   PetscFunctionReturn(0);
3280fb1732b5SBarry Smith }
3281fb1732b5SBarry Smith 
328284df9cb4SJed Brown #undef __FUNCT__
328384df9cb4SJed Brown #define __FUNCT__ "TSGetAdapt"
328484df9cb4SJed Brown /*@
328584df9cb4SJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
328684df9cb4SJed Brown 
3287ed81e22dSJed Brown    Collective on TS if controller has not been created yet
328884df9cb4SJed Brown 
328984df9cb4SJed Brown    Input Arguments:
3290ed81e22dSJed Brown .  ts - time stepping context
329184df9cb4SJed Brown 
329284df9cb4SJed Brown    Output Arguments:
3293ed81e22dSJed Brown .  adapt - adaptive controller
329484df9cb4SJed Brown 
329584df9cb4SJed Brown    Level: intermediate
329684df9cb4SJed Brown 
3297ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
329884df9cb4SJed Brown @*/
329984df9cb4SJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
330084df9cb4SJed Brown {
330184df9cb4SJed Brown   PetscErrorCode ierr;
330284df9cb4SJed Brown 
330384df9cb4SJed Brown   PetscFunctionBegin;
330484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
330584df9cb4SJed Brown   PetscValidPointer(adapt,2);
330684df9cb4SJed Brown   if (!ts->adapt) {
330784df9cb4SJed Brown     ierr = TSAdaptCreate(((PetscObject)ts)->comm,&ts->adapt);CHKERRQ(ierr);
33081c3436cfSJed Brown     ierr = PetscLogObjectParent(ts,ts->adapt);CHKERRQ(ierr);
33091c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
331084df9cb4SJed Brown   }
331184df9cb4SJed Brown   *adapt = ts->adapt;
331284df9cb4SJed Brown   PetscFunctionReturn(0);
331384df9cb4SJed Brown }
3314d6ebe24aSShri Abhyankar 
3315d6ebe24aSShri Abhyankar #undef __FUNCT__
33161c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
33171c3436cfSJed Brown /*@
33181c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
33191c3436cfSJed Brown 
33201c3436cfSJed Brown    Logically Collective
33211c3436cfSJed Brown 
33221c3436cfSJed Brown    Input Arguments:
33231c3436cfSJed Brown +  ts - time integration context
33241c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
33251c3436cfSJed Brown .  vatol - vector of absolute tolerances or PETSC_NULL, used in preference to atol if present
33261c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
33271c3436cfSJed Brown -  vrtol - vector of relative tolerances or PETSC_NULL, used in preference to atol if present
33281c3436cfSJed Brown 
33291c3436cfSJed Brown    Level: beginner
33301c3436cfSJed Brown 
3331c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
33321c3436cfSJed Brown @*/
33331c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
33341c3436cfSJed Brown {
33351c3436cfSJed Brown   PetscErrorCode ierr;
33361c3436cfSJed Brown 
33371c3436cfSJed Brown   PetscFunctionBegin;
3338c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
33391c3436cfSJed Brown   if (vatol) {
33401c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
33411c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
33421c3436cfSJed Brown     ts->vatol = vatol;
33431c3436cfSJed Brown   }
3344c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
33451c3436cfSJed Brown   if (vrtol) {
33461c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
33471c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
33481c3436cfSJed Brown     ts->vrtol = vrtol;
33491c3436cfSJed Brown   }
33501c3436cfSJed Brown   PetscFunctionReturn(0);
33511c3436cfSJed Brown }
33521c3436cfSJed Brown 
33531c3436cfSJed Brown #undef __FUNCT__
3354c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
3355c5033834SJed Brown /*@
3356c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
3357c5033834SJed Brown 
3358c5033834SJed Brown    Logically Collective
3359c5033834SJed Brown 
3360c5033834SJed Brown    Input Arguments:
3361c5033834SJed Brown .  ts - time integration context
3362c5033834SJed Brown 
3363c5033834SJed Brown    Output Arguments:
3364c5033834SJed Brown +  atol - scalar absolute tolerances, PETSC_NULL to ignore
3365c5033834SJed Brown .  vatol - vector of absolute tolerances, PETSC_NULL to ignore
3366c5033834SJed Brown .  rtol - scalar relative tolerances, PETSC_NULL to ignore
3367c5033834SJed Brown -  vrtol - vector of relative tolerances, PETSC_NULL to ignore
3368c5033834SJed Brown 
3369c5033834SJed Brown    Level: beginner
3370c5033834SJed Brown 
3371c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
3372c5033834SJed Brown @*/
3373c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
3374c5033834SJed Brown {
3375c5033834SJed Brown 
3376c5033834SJed Brown   PetscFunctionBegin;
3377c5033834SJed Brown   if (atol)  *atol  = ts->atol;
3378c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
3379c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
3380c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
3381c5033834SJed Brown   PetscFunctionReturn(0);
3382c5033834SJed Brown }
3383c5033834SJed Brown 
3384c5033834SJed Brown #undef __FUNCT__
33851c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
33861c3436cfSJed Brown /*@
33871c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
33881c3436cfSJed Brown 
33891c3436cfSJed Brown    Collective on TS
33901c3436cfSJed Brown 
33911c3436cfSJed Brown    Input Arguments:
33921c3436cfSJed Brown +  ts - time stepping context
33931c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
33941c3436cfSJed Brown 
33951c3436cfSJed Brown    Output Arguments:
33961c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
33971c3436cfSJed Brown 
33981c3436cfSJed Brown    Level: developer
33991c3436cfSJed Brown 
34001c3436cfSJed Brown .seealso: TSSetTolerances()
34011c3436cfSJed Brown @*/
34021c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
34031c3436cfSJed Brown {
34041c3436cfSJed Brown   PetscErrorCode ierr;
34051c3436cfSJed Brown   PetscInt i,n,N;
34061c3436cfSJed Brown   const PetscScalar *x,*y;
34071c3436cfSJed Brown   Vec X;
34081c3436cfSJed Brown   PetscReal sum,gsum;
34091c3436cfSJed Brown 
34101c3436cfSJed Brown   PetscFunctionBegin;
34111c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34121c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
34131c3436cfSJed Brown   PetscValidPointer(norm,3);
34141c3436cfSJed Brown   X = ts->vec_sol;
34151c3436cfSJed Brown   PetscCheckSameTypeAndComm(X,1,Y,2);
34161c3436cfSJed Brown   if (X == Y) SETERRQ(((PetscObject)X)->comm,PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
34171c3436cfSJed Brown 
34181c3436cfSJed Brown   ierr = VecGetSize(X,&N);CHKERRQ(ierr);
34191c3436cfSJed Brown   ierr = VecGetLocalSize(X,&n);CHKERRQ(ierr);
34201c3436cfSJed Brown   ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
34211c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
34221c3436cfSJed Brown   sum = 0.;
3423ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
3424ceefc113SJed Brown     const PetscScalar *atol,*rtol;
3425ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3426ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3427ceefc113SJed Brown     for (i=0; i<n; i++) {
3428ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3429ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3430ceefc113SJed Brown     }
3431ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3432ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3433ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
3434ceefc113SJed Brown     const PetscScalar *atol;
3435ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3436ceefc113SJed Brown     for (i=0; i<n; i++) {
3437ceefc113SJed Brown       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3438ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3439ceefc113SJed Brown     }
3440ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
3441ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
3442ceefc113SJed Brown     const PetscScalar *rtol;
3443ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3444ceefc113SJed Brown     for (i=0; i<n; i++) {
3445ceefc113SJed Brown       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
3446ceefc113SJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
3447ceefc113SJed Brown     }
3448ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
3449ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
34501c3436cfSJed Brown     for (i=0; i<n; i++) {
34511c3436cfSJed Brown       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(x[i]),PetscAbsScalar(y[i]));
34521c3436cfSJed Brown       sum += PetscSqr(PetscAbsScalar(y[i] - x[i]) / tol);
34531c3436cfSJed Brown     }
3454ceefc113SJed Brown   }
34551c3436cfSJed Brown   ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
34561c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
34571c3436cfSJed Brown 
34581c3436cfSJed Brown   ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,((PetscObject)ts)->comm);CHKERRQ(ierr);
34591c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
34601c3436cfSJed Brown   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(((PetscObject)ts)->comm,PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
34611c3436cfSJed Brown   PetscFunctionReturn(0);
34621c3436cfSJed Brown }
34631c3436cfSJed Brown 
34641c3436cfSJed Brown #undef __FUNCT__
34658d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
34668d59e960SJed Brown /*@
34678d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
34688d59e960SJed Brown 
34698d59e960SJed Brown    Logically Collective on TS
34708d59e960SJed Brown 
34718d59e960SJed Brown    Input Arguments:
34728d59e960SJed Brown +  ts - time stepping context
34738d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
34748d59e960SJed Brown 
34758d59e960SJed Brown    Note:
34768d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
34778d59e960SJed Brown 
34788d59e960SJed Brown    Level: intermediate
34798d59e960SJed Brown 
34808d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
34818d59e960SJed Brown @*/
34828d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
34838d59e960SJed Brown {
34848d59e960SJed Brown 
34858d59e960SJed Brown   PetscFunctionBegin;
34868d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34878d59e960SJed Brown   ts->cfltime_local = cfltime;
34888d59e960SJed Brown   ts->cfltime = -1.;
34898d59e960SJed Brown   PetscFunctionReturn(0);
34908d59e960SJed Brown }
34918d59e960SJed Brown 
34928d59e960SJed Brown #undef __FUNCT__
34938d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
34948d59e960SJed Brown /*@
34958d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
34968d59e960SJed Brown 
34978d59e960SJed Brown    Collective on TS
34988d59e960SJed Brown 
34998d59e960SJed Brown    Input Arguments:
35008d59e960SJed Brown .  ts - time stepping context
35018d59e960SJed Brown 
35028d59e960SJed Brown    Output Arguments:
35038d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
35048d59e960SJed Brown 
35058d59e960SJed Brown    Level: advanced
35068d59e960SJed Brown 
35078d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
35088d59e960SJed Brown @*/
35098d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
35108d59e960SJed Brown {
35118d59e960SJed Brown   PetscErrorCode ierr;
35128d59e960SJed Brown 
35138d59e960SJed Brown   PetscFunctionBegin;
35148d59e960SJed Brown   if (ts->cfltime < 0) {
35158d59e960SJed Brown     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,((PetscObject)ts)->comm);CHKERRQ(ierr);
35168d59e960SJed Brown   }
35178d59e960SJed Brown   *cfltime = ts->cfltime;
35188d59e960SJed Brown   PetscFunctionReturn(0);
35198d59e960SJed Brown }
35208d59e960SJed Brown 
35218d59e960SJed Brown #undef __FUNCT__
3522d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
3523d6ebe24aSShri Abhyankar /*@
3524d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
3525d6ebe24aSShri Abhyankar 
3526d6ebe24aSShri Abhyankar    Input Parameters:
3527d6ebe24aSShri Abhyankar .  ts   - the TS context.
3528d6ebe24aSShri Abhyankar .  xl   - lower bound.
3529d6ebe24aSShri Abhyankar .  xu   - upper bound.
3530d6ebe24aSShri Abhyankar 
3531d6ebe24aSShri Abhyankar    Notes:
3532d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
353333c0c2ddSJed Brown    SNES_VI_NINF and SNES_VI_INF respectively during SNESSetUp().
3534d6ebe24aSShri Abhyankar 
35352bd2b0e6SSatish Balay    Level: advanced
35362bd2b0e6SSatish Balay 
3537d6ebe24aSShri Abhyankar @*/
3538d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
3539d6ebe24aSShri Abhyankar {
3540d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
3541d6ebe24aSShri Abhyankar   SNES           snes;
3542d6ebe24aSShri Abhyankar 
3543d6ebe24aSShri Abhyankar   PetscFunctionBegin;
3544d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3545d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
3546d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
3547d6ebe24aSShri Abhyankar }
3548d6ebe24aSShri Abhyankar 
3549325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3550c6db04a5SJed Brown #include <mex.h>
3551325fc9f4SBarry Smith 
3552325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
3553325fc9f4SBarry Smith 
3554325fc9f4SBarry Smith #undef __FUNCT__
3555325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
3556325fc9f4SBarry Smith /*
3557325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
3558325fc9f4SBarry Smith                          TSSetFunctionMatlab().
3559325fc9f4SBarry Smith 
3560325fc9f4SBarry Smith    Collective on TS
3561325fc9f4SBarry Smith 
3562325fc9f4SBarry Smith    Input Parameters:
3563325fc9f4SBarry Smith +  snes - the TS context
3564325fc9f4SBarry Smith -  x - input vector
3565325fc9f4SBarry Smith 
3566325fc9f4SBarry Smith    Output Parameter:
3567325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
3568325fc9f4SBarry Smith 
3569325fc9f4SBarry Smith    Notes:
3570325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
3571325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
3572325fc9f4SBarry Smith    themselves.
3573325fc9f4SBarry Smith 
3574325fc9f4SBarry Smith    Level: developer
3575325fc9f4SBarry Smith 
3576325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3577325fc9f4SBarry Smith 
3578325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3579325fc9f4SBarry Smith */
35807087cfbeSBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec x,Vec xdot,Vec y, void *ctx)
3581325fc9f4SBarry Smith {
3582325fc9f4SBarry Smith   PetscErrorCode   ierr;
3583325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3584325fc9f4SBarry Smith   int              nlhs = 1,nrhs = 7;
3585325fc9f4SBarry Smith   mxArray          *plhs[1],*prhs[7];
3586325fc9f4SBarry Smith   long long int    lx = 0,lxdot = 0,ly = 0,ls = 0;
3587325fc9f4SBarry Smith 
3588325fc9f4SBarry Smith   PetscFunctionBegin;
3589325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
3590325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3591325fc9f4SBarry Smith   PetscValidHeaderSpecific(xdot,VEC_CLASSID,4);
3592325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
3593325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,x,3);
3594325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
3595325fc9f4SBarry Smith 
3596325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
3597325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3598880f3077SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(xdot));CHKERRQ(ierr);
3599325fc9f4SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr);
3600325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3601325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
3602325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3603325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3604325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
3605325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
3606325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
3607325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
3608325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3609325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3610325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3611325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3612325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3613325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3614325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3615325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3616325fc9f4SBarry Smith   PetscFunctionReturn(0);
3617325fc9f4SBarry Smith }
3618325fc9f4SBarry Smith 
3619325fc9f4SBarry Smith 
3620325fc9f4SBarry Smith #undef __FUNCT__
3621325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
3622325fc9f4SBarry Smith /*
3623325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
3624325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
3625e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
3626325fc9f4SBarry Smith 
3627325fc9f4SBarry Smith    Logically Collective on TS
3628325fc9f4SBarry Smith 
3629325fc9f4SBarry Smith    Input Parameters:
3630325fc9f4SBarry Smith +  ts - the TS context
3631325fc9f4SBarry Smith -  func - function evaluation routine
3632325fc9f4SBarry Smith 
3633325fc9f4SBarry Smith    Calling sequence of func:
3634325fc9f4SBarry Smith $    func (TS ts,PetscReal time,Vec x,Vec xdot,Vec f,void *ctx);
3635325fc9f4SBarry Smith 
3636325fc9f4SBarry Smith    Level: beginner
3637325fc9f4SBarry Smith 
3638325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3639325fc9f4SBarry Smith 
3640325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3641325fc9f4SBarry Smith */
3642cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
3643325fc9f4SBarry Smith {
3644325fc9f4SBarry Smith   PetscErrorCode  ierr;
3645325fc9f4SBarry Smith   TSMatlabContext *sctx;
3646325fc9f4SBarry Smith 
3647325fc9f4SBarry Smith   PetscFunctionBegin;
3648325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3649325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3650325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3651325fc9f4SBarry Smith   /*
3652325fc9f4SBarry Smith      This should work, but it doesn't
3653325fc9f4SBarry Smith   sctx->ctx = ctx;
3654325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3655325fc9f4SBarry Smith   */
3656325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3657cdcf91faSSean Farley   ierr = TSSetIFunction(ts,PETSC_NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
3658325fc9f4SBarry Smith   PetscFunctionReturn(0);
3659325fc9f4SBarry Smith }
3660325fc9f4SBarry Smith 
3661325fc9f4SBarry Smith #undef __FUNCT__
3662325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
3663325fc9f4SBarry Smith /*
3664325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
3665325fc9f4SBarry Smith                          TSSetJacobianMatlab().
3666325fc9f4SBarry Smith 
3667325fc9f4SBarry Smith    Collective on TS
3668325fc9f4SBarry Smith 
3669325fc9f4SBarry Smith    Input Parameters:
3670cdcf91faSSean Farley +  ts - the TS context
3671325fc9f4SBarry Smith .  x - input vector
3672325fc9f4SBarry Smith .  A, B - the matrices
3673325fc9f4SBarry Smith -  ctx - user context
3674325fc9f4SBarry Smith 
3675325fc9f4SBarry Smith    Output Parameter:
3676325fc9f4SBarry Smith .  flag - structure of the matrix
3677325fc9f4SBarry Smith 
3678325fc9f4SBarry Smith    Level: developer
3679325fc9f4SBarry Smith 
3680325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
3681325fc9f4SBarry Smith 
3682325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3683325fc9f4SBarry Smith @*/
3684cdcf91faSSean Farley PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec x,Vec xdot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag, void *ctx)
3685325fc9f4SBarry Smith {
3686325fc9f4SBarry Smith   PetscErrorCode  ierr;
3687325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3688325fc9f4SBarry Smith   int             nlhs = 2,nrhs = 9;
3689325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
3690325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
3691325fc9f4SBarry Smith 
3692325fc9f4SBarry Smith   PetscFunctionBegin;
3693cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3694325fc9f4SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
3695325fc9f4SBarry Smith 
3696325fc9f4SBarry Smith   /* call Matlab function in ctx with arguments x and y */
3697325fc9f4SBarry Smith 
3698cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3699325fc9f4SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3700325fc9f4SBarry Smith   ierr = PetscMemcpy(&lxdot,&xdot,sizeof(x));CHKERRQ(ierr);
3701325fc9f4SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr);
3702325fc9f4SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr);
3703325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3704325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
3705325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
3706325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
3707325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
3708325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
3709325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
3710325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
3711325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
3712325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
3713325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3714325fc9f4SBarry Smith   *flag   =  (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr);
3715325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
3716325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
3717325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
3718325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
3719325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
3720325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
3721325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
3722325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
3723325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
3724325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
3725325fc9f4SBarry Smith   PetscFunctionReturn(0);
3726325fc9f4SBarry Smith }
3727325fc9f4SBarry Smith 
3728325fc9f4SBarry Smith 
3729325fc9f4SBarry Smith #undef __FUNCT__
3730325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
3731325fc9f4SBarry Smith /*
3732325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
3733e3c5b3baSBarry 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
3734325fc9f4SBarry Smith 
3735325fc9f4SBarry Smith    Logically Collective on TS
3736325fc9f4SBarry Smith 
3737325fc9f4SBarry Smith    Input Parameters:
3738cdcf91faSSean Farley +  ts - the TS context
3739325fc9f4SBarry Smith .  A,B - Jacobian matrices
3740325fc9f4SBarry Smith .  func - function evaluation routine
3741325fc9f4SBarry Smith -  ctx - user context
3742325fc9f4SBarry Smith 
3743325fc9f4SBarry Smith    Calling sequence of func:
3744cdcf91faSSean Farley $    flag = func (TS ts,PetscReal time,Vec x,Vec xdot,Mat A,Mat B,void *ctx);
3745325fc9f4SBarry Smith 
3746325fc9f4SBarry Smith 
3747325fc9f4SBarry Smith    Level: developer
3748325fc9f4SBarry Smith 
3749325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
3750325fc9f4SBarry Smith 
3751325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3752325fc9f4SBarry Smith */
3753cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
3754325fc9f4SBarry Smith {
3755325fc9f4SBarry Smith   PetscErrorCode    ierr;
3756325fc9f4SBarry Smith   TSMatlabContext *sctx;
3757325fc9f4SBarry Smith 
3758325fc9f4SBarry Smith   PetscFunctionBegin;
3759325fc9f4SBarry Smith   /* currently sctx is memory bleed */
3760325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3761325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3762325fc9f4SBarry Smith   /*
3763325fc9f4SBarry Smith      This should work, but it doesn't
3764325fc9f4SBarry Smith   sctx->ctx = ctx;
3765325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3766325fc9f4SBarry Smith   */
3767325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3768cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
3769325fc9f4SBarry Smith   PetscFunctionReturn(0);
3770325fc9f4SBarry Smith }
3771325fc9f4SBarry Smith 
3772b5b1a830SBarry Smith #undef __FUNCT__
3773b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
3774b5b1a830SBarry Smith /*
3775b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
3776b5b1a830SBarry Smith 
3777b5b1a830SBarry Smith    Collective on TS
3778b5b1a830SBarry Smith 
3779b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
3780b5b1a830SBarry Smith @*/
3781cdcf91faSSean Farley PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec x, void *ctx)
3782b5b1a830SBarry Smith {
3783b5b1a830SBarry Smith   PetscErrorCode  ierr;
3784b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext *)ctx;
3785a530c242SBarry Smith   int             nlhs = 1,nrhs = 6;
3786b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
3787b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
3788b5b1a830SBarry Smith 
3789b5b1a830SBarry Smith   PetscFunctionBegin;
3790cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3791b5b1a830SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,4);
3792b5b1a830SBarry Smith 
3793cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
3794b5b1a830SBarry Smith   ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr);
3795b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
3796b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
3797b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
3798b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
3799b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
3800b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
3801b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
3802b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
3803b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
3804b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
3805b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
3806b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
3807b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
3808b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
3809b5b1a830SBarry Smith   PetscFunctionReturn(0);
3810b5b1a830SBarry Smith }
3811b5b1a830SBarry Smith 
3812b5b1a830SBarry Smith 
3813b5b1a830SBarry Smith #undef __FUNCT__
3814b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
3815b5b1a830SBarry Smith /*
3816b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
3817b5b1a830SBarry Smith 
3818b5b1a830SBarry Smith    Level: developer
3819b5b1a830SBarry Smith 
3820b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
3821b5b1a830SBarry Smith 
3822b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
3823b5b1a830SBarry Smith */
3824cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
3825b5b1a830SBarry Smith {
3826b5b1a830SBarry Smith   PetscErrorCode    ierr;
3827b5b1a830SBarry Smith   TSMatlabContext *sctx;
3828b5b1a830SBarry Smith 
3829b5b1a830SBarry Smith   PetscFunctionBegin;
3830b5b1a830SBarry Smith   /* currently sctx is memory bleed */
3831b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
3832b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
3833b5b1a830SBarry Smith   /*
3834b5b1a830SBarry Smith      This should work, but it doesn't
3835b5b1a830SBarry Smith   sctx->ctx = ctx;
3836b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
3837b5b1a830SBarry Smith   */
3838b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
3839cdcf91faSSean Farley   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr);
3840b5b1a830SBarry Smith   PetscFunctionReturn(0);
3841b5b1a830SBarry Smith }
3842325fc9f4SBarry Smith #endif
3843