xref: /petsc/src/ts/trajectory/interface/traj.c (revision bc952696eb61be435668d699a04c8412c951724e)
1*bc952696SBarry Smith 
2*bc952696SBarry Smith #include <petsc-private/tsimpl.h>        /*I "petscts.h"  I*/
3*bc952696SBarry Smith 
4*bc952696SBarry Smith PetscFunctionList TSTrajectoryList              = NULL;
5*bc952696SBarry Smith PetscBool         TSTrajectoryRegisterAllCalled = PETSC_FALSE;
6*bc952696SBarry Smith PetscClassId      TSTRAJECTORY_CLASSID;
7*bc952696SBarry Smith 
8*bc952696SBarry Smith #undef __FUNCT__
9*bc952696SBarry Smith #define __FUNCT__ "TSTrajectoryRegister"
10*bc952696SBarry Smith /*@C
11*bc952696SBarry Smith   TSTrajectoryRegister - Adds a way of storing trajectories to the TS package
12*bc952696SBarry Smith 
13*bc952696SBarry Smith   Not Collective
14*bc952696SBarry Smith 
15*bc952696SBarry Smith   Input Parameters:
16*bc952696SBarry Smith + name        - The name of a new user-defined creation routine
17*bc952696SBarry Smith - create_func - The creation routine itself
18*bc952696SBarry Smith 
19*bc952696SBarry Smith   Notes:
20*bc952696SBarry Smith   TSTrajectoryRegister() may be called multiple times to add several user-defined tses.
21*bc952696SBarry Smith 
22*bc952696SBarry Smith   Level: advanced
23*bc952696SBarry Smith 
24*bc952696SBarry Smith .keywords: TS, register
25*bc952696SBarry Smith 
26*bc952696SBarry Smith .seealso: TSTrajectoryRegisterAll(), TSTrajectoryRegisterDestroy()
27*bc952696SBarry Smith @*/
28*bc952696SBarry Smith PetscErrorCode  TSTrajectoryRegister(const char sname[], PetscErrorCode (*function)(TSTrajectory))
29*bc952696SBarry Smith {
30*bc952696SBarry Smith   PetscErrorCode ierr;
31*bc952696SBarry Smith 
32*bc952696SBarry Smith   PetscFunctionBegin;
33*bc952696SBarry Smith   ierr = PetscFunctionListAdd(&TSTrajectoryList,sname,function);CHKERRQ(ierr);
34*bc952696SBarry Smith   PetscFunctionReturn(0);
35*bc952696SBarry Smith }
36*bc952696SBarry Smith 
37*bc952696SBarry Smith #undef __FUNCT__
38*bc952696SBarry Smith #define __FUNCT__ "TSTrajectorySet"
39*bc952696SBarry Smith PetscErrorCode TSTrajectorySet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time,Vec X)
40*bc952696SBarry Smith {
41*bc952696SBarry Smith   PetscErrorCode ierr;
42*bc952696SBarry Smith 
43*bc952696SBarry Smith   PetscFunctionBegin;
44*bc952696SBarry Smith   if (!tj) PetscFunctionReturn(0);
45*bc952696SBarry Smith   ierr = (*tj->ops->set)(tj,ts,stepnum,time,X);CHKERRQ(ierr);
46*bc952696SBarry Smith   PetscFunctionReturn(0);
47*bc952696SBarry Smith }
48*bc952696SBarry Smith 
49*bc952696SBarry Smith #undef __FUNCT__
50*bc952696SBarry Smith #define __FUNCT__ "TSTrajectoryGet"
51*bc952696SBarry Smith PetscErrorCode TSTrajectoryGet(TSTrajectory tj,TS ts,PetscInt stepnum,PetscReal time)
52*bc952696SBarry Smith {
53*bc952696SBarry Smith   PetscErrorCode ierr;
54*bc952696SBarry Smith 
55*bc952696SBarry Smith   PetscFunctionBegin;
56*bc952696SBarry Smith   if (!tj) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TS solver did not save trajectory");
57*bc952696SBarry Smith   ierr = (*tj->ops->get)(tj,ts,stepnum,time);CHKERRQ(ierr);
58*bc952696SBarry Smith   PetscFunctionReturn(0);
59*bc952696SBarry Smith }
60*bc952696SBarry Smith 
61*bc952696SBarry Smith #undef __FUNCT__
62*bc952696SBarry Smith #define __FUNCT__ "TSTrajectoryView"
63*bc952696SBarry Smith /*@C
64*bc952696SBarry Smith     TSTrajectoryView - Prints information about the trajectory object
65*bc952696SBarry Smith 
66*bc952696SBarry Smith     Collective on TSTrajectory
67*bc952696SBarry Smith 
68*bc952696SBarry Smith     Input Parameters:
69*bc952696SBarry Smith +   ts - the TSTrajectory context obtained from TSTrajectoryCreate()
70*bc952696SBarry Smith -   viewer - visualization context
71*bc952696SBarry Smith 
72*bc952696SBarry Smith     Options Database Key:
73*bc952696SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
74*bc952696SBarry Smith 
75*bc952696SBarry Smith     Notes:
76*bc952696SBarry Smith     The available visualization contexts include
77*bc952696SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
78*bc952696SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
79*bc952696SBarry Smith          output where only the first processor opens
80*bc952696SBarry Smith          the file.  All other processors send their
81*bc952696SBarry Smith          data to the first processor to print.
82*bc952696SBarry Smith 
83*bc952696SBarry Smith     The user can open an alternative visualization context with
84*bc952696SBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
85*bc952696SBarry Smith 
86*bc952696SBarry Smith     Level: beginner
87*bc952696SBarry Smith 
88*bc952696SBarry Smith .keywords: TS, timestep, view
89*bc952696SBarry Smith 
90*bc952696SBarry Smith .seealso: PetscViewerASCIIOpen()
91*bc952696SBarry Smith @*/
92*bc952696SBarry Smith PetscErrorCode  TSTrajectoryView(TSTrajectory ts,PetscViewer viewer)
93*bc952696SBarry Smith {
94*bc952696SBarry Smith   PetscErrorCode ierr;
95*bc952696SBarry Smith   PetscBool      iascii;
96*bc952696SBarry Smith 
97*bc952696SBarry Smith   PetscFunctionBegin;
98*bc952696SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
99*bc952696SBarry Smith   if (!viewer) {
100*bc952696SBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
101*bc952696SBarry Smith   }
102*bc952696SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
103*bc952696SBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
104*bc952696SBarry Smith 
105*bc952696SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
106*bc952696SBarry Smith   if (iascii) {
107*bc952696SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
108*bc952696SBarry Smith     if (ts->ops->view) {
109*bc952696SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
110*bc952696SBarry Smith     }
111*bc952696SBarry Smith   }
112*bc952696SBarry Smith   PetscFunctionReturn(0);
113*bc952696SBarry Smith }
114*bc952696SBarry Smith 
115*bc952696SBarry Smith #undef  __FUNCT__
116*bc952696SBarry Smith #define __FUNCT__ "TSTrajectoryCreate"
117*bc952696SBarry Smith /*@C
118*bc952696SBarry Smith   TSTrajectoryCreate - This function creates an empty trajectory object used to store the time dependent solution of an ODE/DAE
119*bc952696SBarry Smith 
120*bc952696SBarry Smith   Collective on MPI_Comm
121*bc952696SBarry Smith 
122*bc952696SBarry Smith   Input Parameter:
123*bc952696SBarry Smith . comm - The communicator
124*bc952696SBarry Smith 
125*bc952696SBarry Smith   Output Parameter:
126*bc952696SBarry Smith . tstra   - The trajectory object
127*bc952696SBarry Smith 
128*bc952696SBarry Smith   Level: advanced
129*bc952696SBarry Smith 
130*bc952696SBarry Smith   Notes: Usually one does not call this routine, it is called automatically when one calls TSSetSaveTrajectory(). One can call
131*bc952696SBarry Smith    TSGetTrajectory() to access the created trajectory.
132*bc952696SBarry Smith 
133*bc952696SBarry Smith .keywords: TS, create
134*bc952696SBarry Smith .seealso: TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType(), TSGetTrajectory()
135*bc952696SBarry Smith @*/
136*bc952696SBarry Smith PetscErrorCode  TSTrajectoryCreate(MPI_Comm comm, TSTrajectory *tstra)
137*bc952696SBarry Smith {
138*bc952696SBarry Smith   TSTrajectory   t;
139*bc952696SBarry Smith   PetscErrorCode ierr;
140*bc952696SBarry Smith 
141*bc952696SBarry Smith   PetscFunctionBegin;
142*bc952696SBarry Smith   PetscValidPointer(tstra,1);
143*bc952696SBarry Smith   *tstra = NULL;
144*bc952696SBarry Smith   ierr = TSInitializePackage();CHKERRQ(ierr);
145*bc952696SBarry Smith 
146*bc952696SBarry Smith   ierr = PetscHeaderCreate(t, _p_TSTrajectory, struct _TSTrajectoryOps, TSTRAJECTORY_CLASSID, "TSTrajectory", "Time stepping", "TS", comm, TSTrajectoryDestroy, TSTrajectoryView);CHKERRQ(ierr);
147*bc952696SBarry Smith   ierr = PetscMemzero(t->ops, sizeof(struct _TSTrajectoryOps));CHKERRQ(ierr);
148*bc952696SBarry Smith   *tstra = t;
149*bc952696SBarry Smith   PetscFunctionReturn(0);
150*bc952696SBarry Smith }
151*bc952696SBarry Smith 
152*bc952696SBarry Smith #undef __FUNCT__
153*bc952696SBarry Smith #define __FUNCT__ "TSTrajectorySetType"
154*bc952696SBarry Smith /*@C
155*bc952696SBarry Smith   TSTrajectorySetType - Sets the storage method to be used as in a trajectory
156*bc952696SBarry Smith 
157*bc952696SBarry Smith   Collective on TS
158*bc952696SBarry Smith 
159*bc952696SBarry Smith   Input Parameters:
160*bc952696SBarry Smith + ts   - The TS context
161*bc952696SBarry Smith - type - A known method
162*bc952696SBarry Smith 
163*bc952696SBarry Smith   Options Database Command:
164*bc952696SBarry Smith . -tstrajectory_type <type> - Sets the method; use -help for a list of available methods (for instance, basic)
165*bc952696SBarry Smith 
166*bc952696SBarry Smith    Level: intermediate
167*bc952696SBarry Smith 
168*bc952696SBarry Smith .keywords: TS, set, type
169*bc952696SBarry Smith 
170*bc952696SBarry Smith .seealso: TS, TSSolve(), TSCreate(), TSSetFromOptions(), TSDestroy(), TSType
171*bc952696SBarry Smith 
172*bc952696SBarry Smith @*/
173*bc952696SBarry Smith PetscErrorCode  TSTrajectorySetType(TSTrajectory ts,TSTrajectoryType type)
174*bc952696SBarry Smith {
175*bc952696SBarry Smith   PetscErrorCode (*r)(TSTrajectory);
176*bc952696SBarry Smith   PetscBool      match;
177*bc952696SBarry Smith   PetscErrorCode ierr;
178*bc952696SBarry Smith 
179*bc952696SBarry Smith   PetscFunctionBegin;
180*bc952696SBarry Smith   PetscValidHeaderSpecific(ts, TSTRAJECTORY_CLASSID,1);
181*bc952696SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject) ts, type, &match);CHKERRQ(ierr);
182*bc952696SBarry Smith   if (match) PetscFunctionReturn(0);
183*bc952696SBarry Smith 
184*bc952696SBarry Smith   ierr = PetscFunctionListFind(TSTrajectoryList,type,&r);CHKERRQ(ierr);
185*bc952696SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TSTrajectory type: %s", type);
186*bc952696SBarry Smith   if (ts->ops->destroy) {
187*bc952696SBarry Smith     ierr = (*(ts)->ops->destroy)(ts);CHKERRQ(ierr);
188*bc952696SBarry Smith 
189*bc952696SBarry Smith     ts->ops->destroy = NULL;
190*bc952696SBarry Smith   }
191*bc952696SBarry Smith   ierr = PetscMemzero(ts->ops,sizeof(*ts->ops));CHKERRQ(ierr);
192*bc952696SBarry Smith 
193*bc952696SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ts, type);CHKERRQ(ierr);
194*bc952696SBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
195*bc952696SBarry Smith   PetscFunctionReturn(0);
196*bc952696SBarry Smith }
197*bc952696SBarry Smith 
198*bc952696SBarry Smith PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Basic(TSTrajectory);
199*bc952696SBarry Smith 
200*bc952696SBarry Smith #undef __FUNCT__
201*bc952696SBarry Smith #define __FUNCT__ "TSTrajectoryRegisterAll"
202*bc952696SBarry Smith /*@C
203*bc952696SBarry Smith   TSTrajectoryRegisterAll - Registers all of the trajectory storage schecmes in the TS package.
204*bc952696SBarry Smith 
205*bc952696SBarry Smith   Not Collective
206*bc952696SBarry Smith 
207*bc952696SBarry Smith   Level: advanced
208*bc952696SBarry Smith 
209*bc952696SBarry Smith .keywords: TS, timestepper, register, all
210*bc952696SBarry Smith .seealso: TSCreate(), TSRegister(), TSRegisterDestroy()
211*bc952696SBarry Smith @*/
212*bc952696SBarry Smith PetscErrorCode  TSTrajectoryRegisterAll(void)
213*bc952696SBarry Smith {
214*bc952696SBarry Smith   PetscErrorCode ierr;
215*bc952696SBarry Smith 
216*bc952696SBarry Smith   PetscFunctionBegin;
217*bc952696SBarry Smith   TSTrajectoryRegisterAllCalled = PETSC_TRUE;
218*bc952696SBarry Smith 
219*bc952696SBarry Smith   ierr = TSTrajectoryRegister(TSTRAJECTORYBASIC,TSTrajectoryCreate_Basic);CHKERRQ(ierr);
220*bc952696SBarry Smith   PetscFunctionReturn(0);
221*bc952696SBarry Smith }
222*bc952696SBarry Smith 
223*bc952696SBarry Smith #undef __FUNCT__
224*bc952696SBarry Smith #define __FUNCT__ "TSTrajectoryDestroy"
225*bc952696SBarry Smith /*@
226*bc952696SBarry Smith    TSTrajectoryDestroy - Destroys a trajectory context
227*bc952696SBarry Smith 
228*bc952696SBarry Smith    Collective on TSTrajectory
229*bc952696SBarry Smith 
230*bc952696SBarry Smith    Input Parameter:
231*bc952696SBarry Smith .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()
232*bc952696SBarry Smith 
233*bc952696SBarry Smith    Level: advanced
234*bc952696SBarry Smith 
235*bc952696SBarry Smith .keywords: TS, timestepper, destroy
236*bc952696SBarry Smith 
237*bc952696SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
238*bc952696SBarry Smith @*/
239*bc952696SBarry Smith PetscErrorCode  TSTrajectoryDestroy(TSTrajectory *ts)
240*bc952696SBarry Smith {
241*bc952696SBarry Smith   PetscErrorCode ierr;
242*bc952696SBarry Smith 
243*bc952696SBarry Smith   PetscFunctionBegin;
244*bc952696SBarry Smith   if (!*ts) PetscFunctionReturn(0);
245*bc952696SBarry Smith   PetscValidHeaderSpecific((*ts),TSTRAJECTORY_CLASSID,1);
246*bc952696SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
247*bc952696SBarry Smith 
248*bc952696SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
249*bc952696SBarry Smith   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
250*bc952696SBarry Smith   PetscFunctionReturn(0);
251*bc952696SBarry Smith }
252*bc952696SBarry Smith 
253*bc952696SBarry Smith #undef __FUNCT__
254*bc952696SBarry Smith #define __FUNCT__ "TSTrajectorySetTypeFromOptions"
255*bc952696SBarry Smith /*
256*bc952696SBarry Smith   TSTrajectorySetTypeFromOptions - Sets the type of ts from user options.
257*bc952696SBarry Smith 
258*bc952696SBarry Smith   Collective on TSTrajectory
259*bc952696SBarry Smith 
260*bc952696SBarry Smith   Input Parameter:
261*bc952696SBarry Smith . ts - The ts
262*bc952696SBarry Smith 
263*bc952696SBarry Smith   Level: intermediate
264*bc952696SBarry Smith 
265*bc952696SBarry Smith .keywords: TS, set, options, database, type
266*bc952696SBarry Smith .seealso: TSSetFromOptions(), TSSetType()
267*bc952696SBarry Smith */
268*bc952696SBarry Smith static PetscErrorCode TSTrajectorySetTypeFromOptions(TSTrajectory ts)
269*bc952696SBarry Smith {
270*bc952696SBarry Smith   PetscBool      opt;
271*bc952696SBarry Smith   const char     *defaultType;
272*bc952696SBarry Smith   char           typeName[256];
273*bc952696SBarry Smith   PetscErrorCode ierr;
274*bc952696SBarry Smith 
275*bc952696SBarry Smith   PetscFunctionBegin;
276*bc952696SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
277*bc952696SBarry Smith   else defaultType = TSTRAJECTORYBASIC;
278*bc952696SBarry Smith 
279*bc952696SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSTrajectoryRegisterAll();CHKERRQ(ierr);}
280*bc952696SBarry Smith   ierr = PetscOptionsFList("-tstrajectory_type", "TSTrajectory method"," TSTrajectorySetType", TSTrajectoryList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
281*bc952696SBarry Smith   if (opt) {
282*bc952696SBarry Smith     ierr = TSTrajectorySetType(ts, typeName);CHKERRQ(ierr);
283*bc952696SBarry Smith   } else {
284*bc952696SBarry Smith     ierr = TSTrajectorySetType(ts, defaultType);CHKERRQ(ierr);
285*bc952696SBarry Smith   }
286*bc952696SBarry Smith   PetscFunctionReturn(0);
287*bc952696SBarry Smith }
288*bc952696SBarry Smith 
289*bc952696SBarry Smith #undef __FUNCT__
290*bc952696SBarry Smith #define __FUNCT__ "TSTrajectorySetFromOptions"
291*bc952696SBarry Smith /*@
292*bc952696SBarry Smith    TSTrajectorySetFromOptions - Sets various TSTrajectory parameters from user options.
293*bc952696SBarry Smith 
294*bc952696SBarry Smith    Collective on TSTrajectory
295*bc952696SBarry Smith 
296*bc952696SBarry Smith    Input Parameter:
297*bc952696SBarry Smith .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()
298*bc952696SBarry Smith 
299*bc952696SBarry Smith    Options Database Keys:
300*bc952696SBarry Smith .  -tstrajectory_type <type> - TSTRAJECTORYBASIC
301*bc952696SBarry Smith 
302*bc952696SBarry Smith    Level: advanced
303*bc952696SBarry Smith 
304*bc952696SBarry Smith    Notes: This is not normally called directly by users, instead it is called by TSSetFromOptions() after a call to
305*bc952696SBarry Smith    TSSetSaveTrajectory()
306*bc952696SBarry Smith 
307*bc952696SBarry Smith .keywords: TS, timestep, set, options, database
308*bc952696SBarry Smith 
309*bc952696SBarry Smith .seealso: TSGetType(), TSSetSaveTrajectory(), TSGetTrajectory()
310*bc952696SBarry Smith @*/
311*bc952696SBarry Smith PetscErrorCode  TSTrajectorySetFromOptions(TSTrajectory ts)
312*bc952696SBarry Smith {
313*bc952696SBarry Smith   PetscErrorCode ierr;
314*bc952696SBarry Smith 
315*bc952696SBarry Smith   PetscFunctionBegin;
316*bc952696SBarry Smith   PetscValidHeaderSpecific(ts, TSTRAJECTORY_CLASSID,1);
317*bc952696SBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
318*bc952696SBarry Smith   ierr = TSTrajectorySetTypeFromOptions(ts);CHKERRQ(ierr);
319*bc952696SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
320*bc952696SBarry Smith   PetscFunctionReturn(0);
321*bc952696SBarry Smith }
322