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