xref: /petsc/src/ts/interface/tscreate.c (revision bdad233f16b1da2fc2320f4673f853efec1db8b6) !
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: gridcreate.c,v 1.7 2000/01/10 03:54:25 knepley Exp $";
3 #endif
4 
5 #include "src/grid/gridimpl.h"      /*I "grid.h"  I*/
6 
7 #undef __FUNCT__
8 #define __FUNCT__ "TSPublish_Petsc"
9 static int TSPublish_Petsc(PetscObject obj)
10 {
11 #if defined(PETSC_HAVE_AMS)
12   TS   v = (TS) obj;
13   int  ierr;
14 #endif
15 
16   PetscFunctionBegin;
17 
18 #if defined(PETSC_HAVE_AMS)
19   /* if it is already published then return */
20   if (v->amem >=0) PetscFunctionReturn(0);
21 
22   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
23   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Step",&v->steps,1,AMS_INT,AMS_READ,
24                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
25   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Time",&v->ptime,1,AMS_DOUBLE,AMS_READ,
26                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
27   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"CurrentTimeStep",&v->time_step,1,
28                                AMS_DOUBLE,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
29   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
30 #endif
31   PetscFunctionReturn(0);
32 }
33 
34 #undef  __FUNCT__
35 #define __FUNCT__ "TSCreate"
36 /*@
37   TSCreate - This function creates an empty timestepper. The type can then be set with TSSetType().
38 
39   Collective on MPI_Comm
40 
41   Input Parameter:
42 . comm - The communicator
43 
44   Output Parameter:
45 . ts   - The TS
46 
47   Level: beginner
48 
49 .keywords: TS, create
50 .seealso: TSSetType(), TSSetUp(), TSDestroy(), MeshCreate()
51 @*/
52 int TSCreate(MPI_Comm comm, TS *ts) {
53   TS  t;
54   int ierr;
55 
56   PetscFunctionBegin;
57   PetscValidPointer(ts);
58   *ts = PETSC_NULL;
59 #ifndef PETSC_USE_DYNAMIC_LIBRARIES
60   ierr = TSInitializePackage(PETSC_NULL);                                                                 CHKERRQ(ierr);
61 #endif
62 
63   PetscHeaderCreate(t, _TS, struct _TSOps, TS_COOKIE, -1, "TS", comm, TSDestroy, TSView);
64   PetscLogObjectCreate(t);
65   PetscLogObjectMemory(t, sizeof(struct _TS));
66   ierr = PetscMemzero(t->ops, sizeof(struct _TSOps));                                                     CHKERRQ(ierr);
67   t->bops->publish    = TSPublish_Petsc;
68   t->type_name        = PETSC_NULL;
69   t->serialize_name   = PETSC_NULL;
70 
71   t->ops->applymatrixbc = TSDefaultSystemMatrixBC;
72   t->ops->applyrhsbc    = TSDefaultRhsBC;
73   t->ops->applysolbc    = TSDefaultSolutionBC;
74   t->ops->prestep       = TSDefaultPreStep;
75   t->ops->update        = TSDefaultUpdate;
76   t->ops->poststep      = TSDefaultPostStep;
77 
78   /* General TS description */
79   t->problem_type       = TS_LINEAR;
80   t->vec_sol            = PETSC_NULL;
81   t->vec_sol_always     = PETSC_NULL;
82   t->numbermonitors     = 0;
83   t->isGTS              = PETSC_FALSE;
84   t->isExplicit         = PETSC_NULL;
85   t->Iindex             = PETSC_NULL;
86   t->sles               = PETSC_NULL;
87   t->A                  = PETSC_NULL;
88   t->B                  = PETSC_NULL;
89   t->snes               = PETSC_NULL;
90   t->funP               = PETSC_NULL;
91   t->jacP               = PETSC_NULL;
92   t->setupcalled        = 0;
93   t->data               = PETSC_NULL;
94   t->user               = PETSC_NULL;
95   t->max_steps          = 5000;
96   t->max_time           = 5.0;
97   t->time_step          = .1;
98   t->time_step_old      = t->time_step;
99   t->initial_time_step  = t->time_step;
100   t->steps              = 0;
101   t->ptime              = 0.0;
102   t->linear_its         = 0;
103   t->nonlinear_its      = 0;
104   t->work               = PETSC_NULL;
105   t->nwork              = 0;
106 
107   *ts = t;
108   PetscFunctionReturn(0);
109 }
110 
111 #undef  __FUNCT__
112 #define __FUNCT__ "TSSerialize"
113 /*@
114   TSSerialize - This function stores or recreates a timestepper using a viewer for a binary file.
115 
116   Collective on MPI_Comm
117 
118   Input Parameters:
119 + comm   - The communicator for the ts object
120 . viewer - The viewer context
121 - store  - This flag is PETSC_TRUE is data is being written, otherwise it will be read
122 
123   Output Parameter:
124 . ts     - The ts
125 
126   Level: beginner
127 
128 .keywords: TS, serialize
129 .seealso: PartitionSerialize(), TSSerialize()
130 @*/
131 int TSSerialize(MPI_Comm comm, TS *ts, PetscViewer viewer, PetscTruth store)
132 {
133   int      (*serialize)(MPI_Comm, TS *, PetscViewer, PetscTruth);
134   int        fd, cookie, len;
135   char      *name;
136   PetscTruth match;
137   int        ierr;
138 
139   PetscFunctionBegin;
140   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_COOKIE);
141   PetscValidPointer(ts);
142 
143   ierr = PetscTypeCompare((PetscObject) viewer, PETSC_VIEWER_BINARY, &match);                             CHKERRQ(ierr);
144   if (match == PETSC_FALSE) SETERRQ(PETSC_ERR_ARG_WRONG, "Must be binary viewer");
145   ierr = PetscViewerBinaryGetDescriptor(viewer, &fd);                                                     CHKERRQ(ierr);
146 
147   if (!TSSerializeRegisterAllCalled) {
148     ierr = TSSerializeRegisterAll(PETSC_NULL);                                                            CHKERRQ(ierr);
149   }
150   if (!TSSerializeList) SETERRQ(PETSC_ERR_ARG_CORRUPT, "Could not find table of methods");
151 
152   if (store) {
153     PetscValidHeaderSpecific(*ts, TS_COOKIE);
154     ierr = PetscBinaryWrite(fd, &(*ts)->cookie,         1,   PETSC_INT,  0);                              CHKERRQ(ierr);
155     ierr = PetscStrlen((*ts)->serialize_name, &len);                                                      CHKERRQ(ierr);
156     ierr = PetscBinaryWrite(fd, &len,                     1,   PETSC_INT,  0);                            CHKERRQ(ierr);
157     ierr = PetscBinaryWrite(fd,  (*ts)->serialize_name, len, PETSC_CHAR, 0);                              CHKERRQ(ierr);
158     ierr = PetscFListFind(comm, TSSerializeList, (*ts)->serialize_name, (void (**)(void)) &serialize);    CHKERRQ(ierr);
159     if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
160     ierr = (*serialize)(comm, ts, viewer, store);                                                         CHKERRQ(ierr);
161   } else {
162     ierr = PetscBinaryRead(fd, &cookie, 1,   PETSC_INT);                                                  CHKERRQ(ierr);
163     if (cookie != TS_COOKIE) SETERRQ(PETSC_ERR_ARG_WRONG, "Non-ts object");
164     /* Dispatch to the correct routine */
165     ierr = PetscBinaryRead(fd, &len,    1,   PETSC_INT);                                                  CHKERRQ(ierr);
166     ierr = PetscMalloc((len+1) * sizeof(char), &name);                                                    CHKERRQ(ierr);
167     name[len] = 0;
168     ierr = PetscBinaryRead(fd,  name,   len, PETSC_CHAR);                                                 CHKERRQ(ierr);
169     ierr = PetscFListFind(comm, TSSerializeList, name, (void (**)(void)) &serialize);                     CHKERRQ(ierr);
170     if (!serialize) SETERRQ(PETSC_ERR_ARG_WRONG, "Type cannot be serialized");
171     ierr = (*serialize)(comm, ts, viewer, store);                                                         CHKERRQ(ierr);
172     ierr = PetscStrfree((*ts)->serialize_name);                                                           CHKERRQ(ierr);
173     (*ts)->serialize_name = name;
174   }
175 
176   PetscFunctionReturn(0);
177 }
178