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