xref: /petsc/src/ts/trajectory/interface/traj.c (revision af0996ce37bc06907c37d8d91773840993d61e62)
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 PETSC_EXTERN PetscErrorCode TSTrajectoryCreate_Singlefile(TSTrajectory);
200 
201 #undef __FUNCT__
202 #define __FUNCT__ "TSTrajectoryRegisterAll"
203 /*@C
204   TSTrajectoryRegisterAll - Registers all of the trajectory storage schecmes in the TS package.
205 
206   Not Collective
207 
208   Level: advanced
209 
210 .keywords: TS, timestepper, register, all
211 .seealso: TSCreate(), TSRegister(), TSRegisterDestroy()
212 @*/
213 PetscErrorCode  TSTrajectoryRegisterAll(void)
214 {
215   PetscErrorCode ierr;
216 
217   PetscFunctionBegin;
218   TSTrajectoryRegisterAllCalled = PETSC_TRUE;
219 
220   ierr = TSTrajectoryRegister(TSTRAJECTORYBASIC,TSTrajectoryCreate_Basic);CHKERRQ(ierr);
221   ierr = TSTrajectoryRegister(TSTRAJECTORYSINGLEFILE,TSTrajectoryCreate_Singlefile);CHKERRQ(ierr);
222   PetscFunctionReturn(0);
223 }
224 
225 #undef __FUNCT__
226 #define __FUNCT__ "TSTrajectoryDestroy"
227 /*@
228    TSTrajectoryDestroy - Destroys a trajectory context
229 
230    Collective on TSTrajectory
231 
232    Input Parameter:
233 .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()
234 
235    Level: advanced
236 
237 .keywords: TS, timestepper, destroy
238 
239 .seealso: TSCreate(), TSSetUp(), TSSolve()
240 @*/
241 PetscErrorCode  TSTrajectoryDestroy(TSTrajectory *ts)
242 {
243   PetscErrorCode ierr;
244 
245   PetscFunctionBegin;
246   if (!*ts) PetscFunctionReturn(0);
247   PetscValidHeaderSpecific((*ts),TSTRAJECTORY_CLASSID,1);
248   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
249 
250   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
251   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
252   PetscFunctionReturn(0);
253 }
254 
255 #undef __FUNCT__
256 #define __FUNCT__ "TSTrajectorySetTypeFromOptions_Private"
257 /*
258   TSTrajectorySetTypeFromOptions_Private - Sets the type of ts from user options.
259 
260   Collective on TSTrajectory
261 
262   Input Parameter:
263 . ts - The ts
264 
265   Level: intermediate
266 
267 .keywords: TS, set, options, database, type
268 .seealso: TSSetFromOptions(), TSSetType()
269 */
270 static PetscErrorCode TSTrajectorySetTypeFromOptions_Private(PetscOptions *PetscOptionsObject,TSTrajectory ts)
271 {
272   PetscBool      opt;
273   const char     *defaultType;
274   char           typeName[256];
275   PetscErrorCode ierr;
276 
277   PetscFunctionBegin;
278   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
279   else defaultType = TSTRAJECTORYBASIC;
280 
281   if (!TSRegisterAllCalled) {ierr = TSTrajectoryRegisterAll();CHKERRQ(ierr);}
282   ierr = PetscOptionsFList("-tstrajectory_type", "TSTrajectory method"," TSTrajectorySetType", TSTrajectoryList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
283   if (opt) {
284     ierr = TSTrajectorySetType(ts, typeName);CHKERRQ(ierr);
285   } else {
286     ierr = TSTrajectorySetType(ts, defaultType);CHKERRQ(ierr);
287   }
288   PetscFunctionReturn(0);
289 }
290 
291 #undef __FUNCT__
292 #define __FUNCT__ "TSTrajectorySetFromOptions"
293 /*@
294    TSTrajectorySetFromOptions - Sets various TSTrajectory parameters from user options.
295 
296    Collective on TSTrajectory
297 
298    Input Parameter:
299 .  ts - the TSTrajectory context obtained from TSTrajectoryCreate()
300 
301    Options Database Keys:
302 .  -tstrajectory_type <type> - TSTRAJECTORYBASIC
303 
304    Level: advanced
305 
306    Notes: This is not normally called directly by users, instead it is called by TSSetFromOptions() after a call to
307    TSSetSaveTrajectory()
308 
309 .keywords: TS, timestep, set, options, database
310 
311 .seealso: TSGetType(), TSSetSaveTrajectory(), TSGetTrajectory()
312 @*/
313 PetscErrorCode  TSTrajectorySetFromOptions(TSTrajectory ts)
314 {
315   PetscErrorCode ierr;
316 
317   PetscFunctionBegin;
318   PetscValidHeaderSpecific(ts, TSTRAJECTORY_CLASSID,1);
319   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
320   ierr = TSTrajectorySetTypeFromOptions_Private(PetscOptionsObject,ts);CHKERRQ(ierr);
321   ierr = PetscOptionsEnd();CHKERRQ(ierr);
322   PetscFunctionReturn(0);
323 }
324