xref: /petsc/include/petscbag.h (revision 18be62a5feccf172f7bc80c15c4be8f6d6443e8b)
1 
2 #if !defined(__PETSCBAG_H)
3 #define __PETSCBAG_H
4 #include "petsc.h"
5 PETSC_EXTERN_CXX_BEGIN
6 
7 #define PETSC_BAG_NAME_LENGTH 64
8 #define PETSC_BAG_HELP_LENGTH 128
9 #define PETSC_BAG_FILE_COOKIE 1211219
10 
11 typedef struct _p_PetscBagItem *PetscBagItem;
12 struct _p_PetscBagItem {
13   PetscDataType dtype;
14   PetscInt      offset;
15   PetscInt      msize;
16   char          name[PETSC_BAG_NAME_LENGTH],help[PETSC_BAG_HELP_LENGTH];
17   const char    **list;
18   PetscTruth    freelist;
19   PetscBagItem  next;};
20 
21 /*S
22      PetscBag - PETSc object that manages a collection of user data including parameters.
23            A bag is essentially a C struct with serialization (you can save it and load it from files).
24 
25    Level: beginner
26 
27     Sample Usage:
28 $      typedef struct {
29 $         PetscBag     bag;
30 $         PetscInt     height;
31 $         PetscScalar  root;
32 $         PetscReal    byebye;
33 $      } MyParameters;
34 $
35 $      MyParameters *params;
36 $
37 $      ierr = PetscBagCreate(MyParameters,&params);
38 $      ierr = PetscBagSetName(params,"MyParameters");
39 $      ierr = PetscBagRegisterInt(params,&params.height,22,"height","Height of the water tower");
40 $
41 $
42 $
43 $
44 $
45 
46 .seealso:  PetscBagSetName(), PetscBagGetName(), PetscBagView(), PetscBagLoad()
47            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterTruth(), PetscBagRegisterScalar()
48            PetscBagSetFromOptions(), PetscBagRegisterVec(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
49 S*/
50 typedef struct {
51   MPI_Comm     bagcomm;
52   PetscInt     bagsize;
53   PetscInt     count;
54   char         bagname[PETSC_BAG_NAME_LENGTH];
55   char         baghelp[PETSC_BAG_HELP_LENGTH];
56   PetscBagItem bagitems;
57 } PetscBag;
58 
59 /*MC
60     PetscBagCreate - Create a bag of values
61 
62   Collective on MPI_Comm
63 
64   Level: Intermediate
65 
66   Synopsis:
67      PetscErrorCode PetscBagCreate(MPI_Comm comm,C struct name,PetscBag **bag);
68 
69   Input Parameters:
70 +  comm - communicator to share bag
71 -  C struct name - name of the C structure holding the values
72 
73   Output Parameter:
74 .   bag - the bag of values
75 
76    Notes:
77       The size of the A struct must be small enough to fit in a PetscInt; by default
78       PetscInt is 4 bytes. The warning about casting to a shorter length can be ignored
79       below unless your A struct is too large
80 
81 .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad()
82            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterTruth(), PetscBagRegisterScalar()
83            PetscBagSetFromOptions(), PetscBagRegisterVec(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
84 M*/
85 #define PetscBagCreate(C,A,B)  PetscNew(A,B) || ((*(B))->bagsize = (PetscInt)sizeof(A),(*(B))->bagcomm = C,0)
86 
87 extern PetscErrorCode PetscBagDestroy(PetscBag*);
88 
89 /*MC
90     PetscBagSetName - Sets the name of a bag of values
91 
92   Not Collective
93 
94   Level: Intermediate
95 
96   Synopsis:
97      PetscErrorCode PetscBagSetName(PetscBag *bag,const char *name, const char *help);
98 
99   Input Parameters:
100 +   bag - the bag of values
101 .   name - the name assigned to the bag
102 -   help - help message for bag
103 
104 .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad()
105            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterTruth(), PetscBagRegisterScalar()
106            PetscBagSetFromOptions(), PetscBagRegisterVec(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
107 M*/
108 #define PetscBagSetName(A,B,C) (PetscStrncpy((A)->bagname,B,PETSC_BAG_NAME_LENGTH-1) || PetscStrncpy((A)->baghelp,C,PETSC_BAG_HELP_LENGTH-1))
109 
110 /*MC
111     PetscBagGetName - Gets the name of a bag of values
112 
113   Not Collective
114 
115   Level: Intermediate
116 
117   Synopsis:
118      PetscErrorCode PetscBagGetName(PetscBag *bag,char **name);
119 
120   Input Parameter:
121 .   bag - the bag of values
122 
123   Output Parameter:
124 .   name - the name assigned to the bag
125 
126 .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad()
127            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterTruth(), PetscBagRegisterScalar()
128            PetscBagSetFromOptions(), PetscBagRegisterVec(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
129 M*/
130 #define PetscBagGetName(A,B) (*(B) = A->bagname,0)
131 
132 extern PetscErrorCode PetscBagRegisterReal(PetscBag*,void*,PetscReal, const char*, const char*);
133 extern PetscErrorCode PetscBagRegisterString(PetscBag*,void*,PetscInt,const char*, const char*, const char*);
134 extern PetscErrorCode PetscBagRegisterScalar(PetscBag*,void*,PetscScalar,const  char*,const  char*);
135 extern PetscErrorCode PetscBagRegisterInt(PetscBag*,void*,PetscInt,const  char*,const  char*);
136 extern PetscErrorCode PetscBagRegisterEnum(PetscBag*,void*,const  char*[],PetscEnum,const char*,const  char*);
137 extern PetscErrorCode PetscBagRegisterTruth(PetscBag*,void*,PetscTruth,const  char*,const  char*);
138 extern PetscErrorCode PetscBagRegisterVec(PetscBag*,void*,const char*,const  char*);
139 
140 extern PetscErrorCode PetscBagSetFromOptions(PetscBag*);
141 
142 extern PetscErrorCode PetscBagView(PetscBag*,PetscViewer);
143 extern PetscErrorCode PetscBagLoad(PetscViewer,PetscBag**);
144 
145 extern PetscErrorCode PetscBagSetViewer(PetscBag*,PetscErrorCode (*)(PetscBag*,PetscViewer));
146 extern PetscErrorCode PetscBagSetLoader(PetscBag*,PetscErrorCode (*)(PetscBag*,PetscViewer));
147 extern PetscErrorCode PetscBagSetDestroy(PetscBag*,PetscErrorCode (*)(PetscBag*));
148 
149 #endif
150