Actual source code: bag.c
1: #include <petsc/private/petscimpl.h>
2: #include <petsc/private/bagimpl.h>
3: #include <petscviewer.h>
5: /*
6: Adds item to the linked list in a bag
7: */
8: static PetscErrorCode PetscBagRegister_Private(PetscBag bag, PetscBagItem item, const char *name, const char *help)
9: {
10: PetscFunctionBegin;
11: PetscCall(PetscStrncpy(item->name, name, PETSC_BAG_NAME_LENGTH - 1));
12: PetscCall(PetscStrncpy(item->help, help, PETSC_BAG_HELP_LENGTH - 1));
13: if (bag->bagitems) {
14: PetscBagItem nitem = bag->bagitems;
16: while (nitem->next) nitem = nitem->next;
17: nitem->next = item;
18: } else bag->bagitems = item;
19: bag->count++;
20: PetscFunctionReturn(PETSC_SUCCESS);
21: }
23: /*@C
24: PetscBagRegisterEnum - add an enum value to a `PetscBag`
26: Logically Collective
28: Input Parameters:
29: + bag - the bag of values
30: . addr - location of enum in struct, for example `¶ms->dt`
31: . list - array of strings containing names of enum values followed by enum name followed by enum prefix
32: . mdefault - the initial value, cast with (`PetscEnum`)
33: . name - the name of the item
34: - help - longer string with more information about the value
36: Level: beginner
38: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
39: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
40: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`
41: @*/
42: PetscErrorCode PetscBagRegisterEnum(PetscBag bag, void *addr, const char *const *list, PetscEnum mdefault, const char *name, const char *help)
43: {
44: PetscBagItem item;
45: char nname[PETSC_BAG_NAME_LENGTH + 1];
46: PetscBool printhelp;
47: PetscInt i = 0;
49: PetscFunctionBegin;
50: PetscAssertPointer(bag, 1);
51: PetscAssertPointer(addr, 2);
52: PetscAssertPointer(list, 3);
53: PetscAssertPointer(name, 5);
54: PetscAssertPointer(help, 6);
55: nname[0] = '-';
56: nname[1] = 0;
57: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
58: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
59: if (printhelp) {
60: while (list[i++]);
61: PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%s>: (%s) %s (choose one of) ", bag->bagprefix ? bag->bagprefix : "", name, list[mdefault], list[i - 3], help));
62: for (i = 0; list[i + 2]; i++) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " %s", list[i]));
63: PetscCall((*PetscHelpPrintf)(bag->bagcomm, "\n"));
64: }
65: PetscCall(PetscOptionsGetEnum(NULL, bag->bagprefix, nname, list, &mdefault, NULL));
67: PetscCall(PetscNew(&item));
68: item->dtype = PETSC_ENUM;
69: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
70: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
71: item->next = NULL;
72: item->msize = 1;
73: PetscCall(PetscStrArrayallocpy(list, (char ***)&item->list));
74: *(PetscEnum *)addr = mdefault;
75: PetscCall(PetscBagRegister_Private(bag, item, name, help));
76: PetscFunctionReturn(PETSC_SUCCESS);
77: }
79: /*@C
80: PetscBagRegisterIntArray - add a `PetscInt` array to a `PetscBag`
82: Logically Collective
84: Input Parameters:
85: + bag - the bag of values
86: . addr - location of integer in struct, for example `¶ms->i`
87: . msize - number of entries in array
88: . name - name of the array
89: - help - longer string with more information about the value
91: Level: beginner
93: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
94: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
95: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
96: @*/
97: PetscErrorCode PetscBagRegisterIntArray(PetscBag bag, void *addr, PetscInt msize, const char *name, const char *help)
98: {
99: PetscBagItem item;
100: char nname[PETSC_BAG_NAME_LENGTH + 1];
101: PetscBool printhelp;
102: PetscInt i, tmp = msize;
104: PetscFunctionBegin;
105: PetscAssertPointer(bag, 1);
106: PetscAssertPointer(addr, 2);
107: PetscAssertPointer(name, 4);
108: PetscAssertPointer(help, 5);
109: nname[0] = '-';
110: nname[1] = 0;
111: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
112: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
113: if (printhelp) {
114: PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <", bag->bagprefix ? bag->bagprefix : "", name));
115: for (i = 0; i < msize; i++) PetscCall((*PetscHelpPrintf)(bag->bagcomm, "%" PetscInt_FMT " ", *((PetscInt *)addr) + i));
116: PetscCall((*PetscHelpPrintf)(bag->bagcomm, ">: %s \n", help));
117: }
118: PetscCall(PetscOptionsGetIntArray(NULL, bag->bagprefix, nname, (PetscInt *)addr, &tmp, NULL));
120: PetscCall(PetscNew(&item));
121: item->dtype = PETSC_INT;
122: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
123: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
124: item->next = NULL;
125: item->msize = msize;
126: PetscCall(PetscBagRegister_Private(bag, item, name, help));
127: PetscFunctionReturn(PETSC_SUCCESS);
128: }
130: /*@C
131: PetscBagRegisterRealArray - add a `PetscReal` array to a `PetscBag`
133: Logically Collective
135: Input Parameters:
136: + bag - the bag of values
137: . addr - location of real array in struct, for example `¶ms->d`
138: . msize - number of entries in the array
139: . name - name of the array
140: - help - longer string with more information about the value
142: Level: beginner
144: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
145: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
146: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
147: @*/
148: PetscErrorCode PetscBagRegisterRealArray(PetscBag bag, void *addr, PetscInt msize, const char *name, const char *help)
149: {
150: PetscBagItem item;
151: char nname[PETSC_BAG_NAME_LENGTH + 1];
152: PetscBool printhelp;
153: PetscInt i, tmp = msize;
155: PetscFunctionBegin;
156: PetscAssertPointer(bag, 1);
157: PetscAssertPointer(addr, 2);
158: PetscAssertPointer(name, 4);
159: PetscAssertPointer(help, 5);
160: nname[0] = '-';
161: nname[1] = 0;
162: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
163: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
164: if (printhelp) {
165: PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <", bag->bagprefix ? bag->bagprefix : "", name));
166: for (i = 0; i < msize; i++) PetscCall((*PetscHelpPrintf)(bag->bagcomm, "%g ", (double)(*((PetscReal *)addr) + i)));
167: PetscCall((*PetscHelpPrintf)(bag->bagcomm, ">: %s \n", help));
168: }
169: PetscCall(PetscOptionsGetRealArray(NULL, bag->bagprefix, nname, (PetscReal *)addr, &tmp, NULL));
171: PetscCall(PetscNew(&item));
172: item->dtype = PETSC_REAL;
173: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
174: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
175: item->next = NULL;
176: item->msize = msize;
177: PetscCall(PetscBagRegister_Private(bag, item, name, help));
178: PetscFunctionReturn(PETSC_SUCCESS);
179: }
181: /*@C
182: PetscBagRegisterInt - add a `PetscInt` value to a `PetscBag`
184: Logically Collective
186: Input Parameters:
187: + bag - the bag of values
188: . addr - location of integer in struct, for example `¶ms->i`
189: . mdefault - the initial value
190: . name - name of the integer
191: - help - longer string with more information about the value
193: Level: beginner
195: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
196: `PetscBagRegisterInt64()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
197: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
198: @*/
199: PetscErrorCode PetscBagRegisterInt(PetscBag bag, void *addr, PetscInt mdefault, const char *name, const char *help)
200: {
201: PetscBagItem item;
202: char nname[PETSC_BAG_NAME_LENGTH + 1];
203: PetscBool printhelp;
205: PetscFunctionBegin;
206: PetscAssertPointer(bag, 1);
207: PetscAssertPointer(addr, 2);
208: PetscAssertPointer(name, 4);
209: PetscAssertPointer(help, 5);
210: nname[0] = '-';
211: nname[1] = 0;
212: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
213: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
214: if (printhelp) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%" PetscInt_FMT ">: %s \n", bag->bagprefix ? bag->bagprefix : "", name, mdefault, help));
215: PetscCall(PetscOptionsGetInt(NULL, bag->bagprefix, nname, &mdefault, NULL));
217: PetscCall(PetscNew(&item));
218: item->dtype = PETSC_INT;
219: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
220: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
221: item->next = NULL;
222: item->msize = 1;
223: *(PetscInt *)addr = mdefault;
224: PetscCall(PetscBagRegister_Private(bag, item, name, help));
225: PetscFunctionReturn(PETSC_SUCCESS);
226: }
228: /*@C
229: PetscBagRegisterInt64 - add a `PetscInt64` value to a `PetscBag`
231: Logically Collective
233: Input Parameters:
234: + bag - the bag of values
235: . addr - location of integer in struct, for example `¶ms->i`
236: . mdefault - the initial value
237: . name - name of the integer
238: - help - longer string with more information about the value
240: Level: beginner
242: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
243: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
244: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
245: @*/
246: PetscErrorCode PetscBagRegisterInt64(PetscBag bag, void *addr, PetscInt64 mdefault, const char *name, const char *help)
247: {
248: PetscBagItem item;
249: char nname[PETSC_BAG_NAME_LENGTH + 1];
250: PetscBool printhelp;
251: PetscInt odefault = (PetscInt)mdefault;
252: PetscBool flg;
254: PetscFunctionBegin;
255: nname[0] = '-';
256: nname[1] = 0;
257: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
258: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
259: if (printhelp) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%" PetscInt_FMT ">: %s \n", bag->bagprefix ? bag->bagprefix : "", name, odefault, help));
260: PetscCall(PetscOptionsGetInt(NULL, bag->bagprefix, nname, &odefault, &flg));
261: if (flg) mdefault = (PetscInt64)odefault;
263: PetscCall(PetscNew(&item));
264: item->dtype = PETSC_INT;
265: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
266: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
267: item->next = NULL;
268: item->msize = 1;
269: *(PetscInt64 *)addr = mdefault;
270: PetscCall(PetscBagRegister_Private(bag, item, name, help));
271: PetscFunctionReturn(PETSC_SUCCESS);
272: }
274: /*@C
275: PetscBagRegisterBoolArray - add a n `PetscBool` values to a `PetscBag`
277: Logically Collective
279: Input Parameters:
280: + bag - the bag of values
281: . addr - location of boolean array in struct, for example `¶ms->b`
282: . msize - number of entries in array
283: . name - name of the boolean array
284: - help - longer string with more information about the value
286: Level: beginner
288: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
289: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
290: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
291: @*/
292: PetscErrorCode PetscBagRegisterBoolArray(PetscBag bag, void *addr, PetscInt msize, const char *name, const char *help)
293: {
294: PetscBagItem item;
295: char nname[PETSC_BAG_NAME_LENGTH + 1];
296: PetscBool printhelp;
297: PetscInt i, tmp = msize;
299: PetscFunctionBegin;
300: PetscAssertPointer(bag, 1);
301: PetscAssertPointer(addr, 2);
302: PetscAssertPointer(name, 4);
303: PetscAssertPointer(help, 5);
304: nname[0] = '-';
305: nname[1] = 0;
306: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
307: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
308: if (printhelp) {
309: PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <", bag->bagprefix ? bag->bagprefix : "", name));
310: for (i = 0; i < msize; i++) PetscCall((*PetscHelpPrintf)(bag->bagcomm, "%" PetscInt_FMT " ", *((PetscInt *)addr) + i));
311: PetscCall((*PetscHelpPrintf)(bag->bagcomm, ">: %s \n", help));
312: }
313: PetscCall(PetscOptionsGetBoolArray(NULL, bag->bagprefix, nname, (PetscBool *)addr, &tmp, NULL));
315: PetscCall(PetscNew(&item));
316: item->dtype = PETSC_BOOL;
317: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
318: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
319: item->next = NULL;
320: item->msize = msize;
321: PetscCall(PetscBagRegister_Private(bag, item, name, help));
322: PetscFunctionReturn(PETSC_SUCCESS);
323: }
325: /*@C
326: PetscBagRegisterString - add a string value to a `PetscBag`
328: Logically Collective
330: Input Parameters:
331: + bag - the bag of values
332: . addr - location of start of string in struct, for example `¶ms->mystring`
333: . msize - length of the string space in the struct
334: . mdefault - the initial value
335: . name - name of the string
336: - help - longer string with more information about the value
338: Level: beginner
340: Note:
341: The struct must have the field char mystring[`msize`]; not char *mystring
343: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
344: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
345: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
346: @*/
347: PetscErrorCode PetscBagRegisterString(PetscBag bag, void *addr, PetscInt msize, const char *mdefault, const char *name, const char *help)
348: {
349: PetscBagItem item;
350: char nname[PETSC_BAG_NAME_LENGTH + 1];
351: PetscBool printhelp;
353: PetscFunctionBegin;
354: PetscAssertPointer(bag, 1);
355: PetscAssertPointer(addr, 2);
356: PetscAssertPointer(mdefault, 4);
357: PetscAssertPointer(name, 5);
358: PetscAssertPointer(help, 6);
359: nname[0] = '-';
360: nname[1] = 0;
361: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
362: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
363: if (printhelp) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%s>: %s \n", bag->bagprefix ? bag->bagprefix : "", name, mdefault, help));
365: PetscCall(PetscNew(&item));
366: item->dtype = PETSC_CHAR;
367: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
368: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
369: item->next = NULL;
370: item->msize = msize;
371: if (mdefault != (char *)addr) PetscCall(PetscStrncpy((char *)addr, mdefault, msize - 1));
372: PetscCall(PetscOptionsGetString(NULL, bag->bagprefix, nname, (char *)addr, msize, NULL));
373: PetscCall(PetscBagRegister_Private(bag, item, name, help));
374: PetscFunctionReturn(PETSC_SUCCESS);
375: }
377: /*@C
378: PetscBagRegisterReal - add a `PetscReal` value to a `PetscBag`
380: Logically Collective
382: Input Parameters:
383: + bag - the bag of values
384: . addr - location of `PetscReal` in struct, for example `¶ms->r`
385: . mdefault - the initial value
386: . name - name of the variable
387: - help - longer string with more information about the value
389: Level: beginner
391: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
392: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
393: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
394: @*/
395: PetscErrorCode PetscBagRegisterReal(PetscBag bag, void *addr, PetscReal mdefault, const char *name, const char *help)
396: {
397: PetscBagItem item;
398: char nname[PETSC_BAG_NAME_LENGTH + 1];
399: PetscBool printhelp;
401: PetscFunctionBegin;
402: PetscAssertPointer(bag, 1);
403: PetscAssertPointer(addr, 2);
404: PetscAssertPointer(name, 4);
405: PetscAssertPointer(help, 5);
406: nname[0] = '-';
407: nname[1] = 0;
408: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
409: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
410: if (printhelp) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%g>: %s \n", bag->bagprefix ? bag->bagprefix : "", name, (double)mdefault, help));
411: PetscCall(PetscOptionsGetReal(NULL, bag->bagprefix, nname, &mdefault, NULL));
413: PetscCall(PetscNew(&item));
414: item->dtype = PETSC_REAL;
415: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
416: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
417: item->next = NULL;
418: item->msize = 1;
419: *(PetscReal *)addr = mdefault;
420: PetscCall(PetscBagRegister_Private(bag, item, name, help));
421: PetscFunctionReturn(PETSC_SUCCESS);
422: }
424: /*@C
425: PetscBagRegisterScalar - add a `PetscScalar` value to a `PetscBag`
427: Logically Collective
429: Input Parameters:
430: + bag - the bag of values
431: . addr - location of `PetscScalar` in struct, for example `¶ms->c`
432: . mdefault - the initial value
433: . name - name of the variable
434: - help - longer string with more information about the value
436: Level: beginner
438: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
439: `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagSetFromOptions()`,
440: `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
441: @*/
442: PetscErrorCode PetscBagRegisterScalar(PetscBag bag, void *addr, PetscScalar mdefault, const char *name, const char *help)
443: {
444: PetscBagItem item;
445: char nname[PETSC_BAG_NAME_LENGTH + 1];
446: PetscBool printhelp;
448: PetscFunctionBegin;
449: PetscAssertPointer(bag, 1);
450: PetscAssertPointer(addr, 2);
451: PetscAssertPointer(name, 4);
452: PetscAssertPointer(help, 5);
453: nname[0] = '-';
454: nname[1] = 0;
455: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
456: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
457: if (printhelp) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%g + %gi>: %s \n", bag->bagprefix ? bag->bagprefix : "", name, (double)PetscRealPart(mdefault), (double)PetscImaginaryPart(mdefault), help));
458: PetscCall(PetscOptionsGetScalar(NULL, bag->bagprefix, nname, &mdefault, NULL));
460: PetscCall(PetscNew(&item));
461: item->dtype = PETSC_SCALAR;
462: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
463: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
464: item->next = NULL;
465: item->msize = 1;
466: *(PetscScalar *)addr = mdefault;
467: PetscCall(PetscBagRegister_Private(bag, item, name, help));
468: PetscFunctionReturn(PETSC_SUCCESS);
469: }
471: /*@C
472: PetscBagRegisterBool - add a `PetscBool` to a `PetscBag`
474: Logically Collective
476: Input Parameters:
477: + bag - the bag of values
478: . addr - location of `PetscBool` in struct, for example `¶ms->b`
479: . mdefault - the initial value, either `PETSC_FALSE` or `PETSC_TRUE`
480: . name - name of the variable
481: - help - longer string with more information about the value
483: Level: beginner
485: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
486: `PetscBagRegisterInt()`, `PetscBagRegisterScalar()`
487: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
488: @*/
489: PetscErrorCode PetscBagRegisterBool(PetscBag bag, void *addr, PetscBool mdefault, const char *name, const char *help)
490: {
491: PetscBagItem item;
492: char nname[PETSC_BAG_NAME_LENGTH + 1];
493: PetscBool printhelp;
495: PetscFunctionBegin;
496: PetscAssertPointer(bag, 1);
497: PetscAssertPointer(addr, 2);
498: PetscAssertPointer(name, 4);
499: PetscAssertPointer(help, 5);
500: /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
501: PetscCheck(mdefault == PETSC_FALSE || mdefault == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Boolean %s %s must be boolean; integer value %d", name, help, (int)mdefault);
502: nname[0] = '-';
503: nname[1] = 0;
504: PetscCall(PetscStrlcat(nname, name, PETSC_BAG_NAME_LENGTH));
505: PetscCall(PetscOptionsHasHelp(NULL, &printhelp));
506: if (printhelp) PetscCall((*PetscHelpPrintf)(bag->bagcomm, " -%s%s <%s>: %s \n", bag->bagprefix ? bag->bagprefix : "", name, PetscBools[mdefault], help));
507: PetscCall(PetscOptionsGetBool(NULL, bag->bagprefix, nname, &mdefault, NULL));
509: PetscCall(PetscNew(&item));
510: item->dtype = PETSC_BOOL;
511: item->offset = (PetscInt)(((size_t)addr) - ((size_t)bag));
512: PetscCheck(item->offset <= bag->bagsize, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Registered item %s %s is not in bag memory space", name, help);
513: item->next = NULL;
514: item->msize = 1;
515: *(PetscBool *)addr = mdefault;
516: PetscCall(PetscBagRegister_Private(bag, item, name, help));
517: PetscFunctionReturn(PETSC_SUCCESS);
518: }
520: /*@
521: PetscBagDestroy - Destroys a `PetscBag`
523: Collective
525: Input Parameter:
526: . bag - the bag of values
528: Level: beginner
530: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
531: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
532: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
533: @*/
534: PetscErrorCode PetscBagDestroy(PetscBag *bag)
535: {
536: PetscBagItem nitem;
538: PetscFunctionBegin;
539: if (!*bag) PetscFunctionReturn(PETSC_SUCCESS);
540: PetscAssertPointer(*bag, 1);
541: nitem = (*bag)->bagitems;
542: while (nitem) {
543: PetscBagItem item = nitem->next;
545: if (nitem->list) PetscCall(PetscStrArrayDestroy(&nitem->list));
546: PetscCall(PetscFree(nitem));
547: nitem = item;
548: }
549: if ((*bag)->bagprefix) PetscCall(PetscFree((*bag)->bagprefix));
550: PetscCall(PetscFree(*bag));
551: PetscFunctionReturn(PETSC_SUCCESS);
552: }
554: /*@
555: PetscBagSetFromOptions - Allows setting entries to a `PetscBag` using the options database
557: Collective
559: Input Parameter:
560: . bag - the bag of values
562: Level: beginner
564: Note:
565: The options database keys for the entries are of the form `-[bagprefix]_name value`
567: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagDestroy()`, `PetscBagLoad()`, `PetscBagGetData()`
568: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
569: `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagView()`, `PetscBagRegisterEnum()`
570: @*/
571: PetscErrorCode PetscBagSetFromOptions(PetscBag bag)
572: {
573: PetscBagItem nitem = bag->bagitems;
574: char name[PETSC_BAG_NAME_LENGTH + 1], helpname[PETSC_BAG_NAME_LENGTH + PETSC_BAG_HELP_LENGTH + 3];
575: PetscInt n;
577: PetscFunctionBegin;
578: PetscAssertPointer(bag, 1);
579: PetscCall(PetscStrncpy(helpname, bag->bagname, sizeof(helpname)));
580: PetscCall(PetscStrlcat(helpname, " ", sizeof(helpname)));
581: PetscCall(PetscStrlcat(helpname, bag->baghelp, sizeof(helpname)));
582: PetscOptionsBegin(bag->bagcomm, bag->bagprefix, helpname, NULL);
583: while (nitem) {
584: name[0] = '-';
585: name[1] = 0;
586: PetscCall(PetscStrlcat(name, nitem->name, sizeof(name)));
587: if (nitem->dtype == PETSC_CHAR) { /* special handling for fortran required? [due to space padding vs null termination] */
588: char *value = (char *)(((char *)bag) + nitem->offset);
589: PetscCall(PetscOptionsString(name, nitem->help, "", value, value, nitem->msize, NULL));
590: } else if (nitem->dtype == PETSC_REAL) {
591: PetscReal *value = (PetscReal *)(((char *)bag) + nitem->offset);
592: if (nitem->msize == 1) {
593: PetscCall(PetscOptionsReal(name, nitem->help, "", *value, value, NULL));
594: } else {
595: n = nitem->msize;
596: PetscCall(PetscOptionsRealArray(name, nitem->help, "", value, &n, NULL));
597: }
598: } else if (nitem->dtype == PETSC_SCALAR) {
599: PetscScalar *value = (PetscScalar *)(((char *)bag) + nitem->offset);
600: PetscCall(PetscOptionsScalar(name, nitem->help, "", *value, value, NULL));
601: } else if (nitem->dtype == PETSC_INT) {
602: PetscInt *value = (PetscInt *)(((char *)bag) + nitem->offset);
603: if (nitem->msize == 1) {
604: PetscCall(PetscOptionsInt(name, nitem->help, "", *value, value, NULL));
605: } else {
606: n = nitem->msize;
607: PetscCall(PetscOptionsIntArray(name, nitem->help, "", value, &n, NULL));
608: }
609: } else if (nitem->dtype == PETSC_ENUM) {
610: PetscEnum *value = (PetscEnum *)(((char *)bag) + nitem->offset);
611: PetscInt i = 0;
612: while (nitem->list[i++]);
613: PetscCall(PetscOptionsEnum(name, nitem->help, nitem->list[i - 3], (const char *const *)nitem->list, *value, value, NULL));
614: } else if (nitem->dtype == PETSC_BOOL) {
615: PetscBool *value = (PetscBool *)(((char *)bag) + nitem->offset);
616: if (nitem->msize == 1) {
617: PetscCall(PetscOptionsBool(name, nitem->help, "", *value, value, NULL));
618: } else {
619: n = nitem->msize;
620: PetscCall(PetscOptionsBoolArray(name, nitem->help, "", value, &n, NULL));
621: }
622: }
623: nitem = nitem->next;
624: }
625: PetscOptionsEnd();
626: PetscFunctionReturn(PETSC_SUCCESS);
627: }
629: /*@
630: PetscBagView - Views a bag of values as either ASCII text or a binary file
632: Collective
634: Input Parameters:
635: + bag - the bag of values
636: - view - location to view the values
638: Level: beginner
640: Note:
641: Currently PETSc bags saved in a binary file can only be read back
642: in on a machine with the same binary format.
644: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagDestroy()`, `PetscBagLoad()`, `PetscBagGetData()`
645: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`, `PetscBagRegisterEnum()`
646: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`
647: @*/
648: PetscErrorCode PetscBagView(PetscBag bag, PetscViewer view)
649: {
650: PetscBool isascii, isbinary;
651: PetscBagItem nitem = bag->bagitems;
653: PetscFunctionBegin;
654: PetscAssertPointer(bag, 1);
656: PetscCall(PetscObjectTypeCompare((PetscObject)view, PETSCVIEWERASCII, &isascii));
657: PetscCall(PetscObjectTypeCompare((PetscObject)view, PETSCVIEWERBINARY, &isbinary));
658: if (isascii) {
659: if (bag->bagprefix) {
660: PetscCall(PetscViewerASCIIPrintf(view, "PetscBag Object: %s (%s) %s\n", bag->bagname, bag->bagprefix, bag->baghelp));
661: } else {
662: PetscCall(PetscViewerASCIIPrintf(view, "PetscBag Object: %s %s\n", bag->bagname, bag->baghelp));
663: }
664: while (nitem) {
665: if (nitem->dtype == PETSC_CHAR) {
666: char *value = (char *)(((char *)bag) + nitem->offset);
667: char tmp = value[nitem->msize - 1]; /* special handling for fortran chars without null terminator */
668: value[nitem->msize - 1] = 0;
669: PetscCall(PetscViewerASCIIPrintf(view, " %s = %s; %s\n", nitem->name, value, nitem->help));
670: value[nitem->msize - 1] = tmp;
671: } else if (nitem->dtype == PETSC_REAL) {
672: PetscReal *value = (PetscReal *)(((char *)bag) + nitem->offset);
673: PetscInt i;
674: PetscCall(PetscViewerASCIIPrintf(view, " %s = ", nitem->name));
675: for (i = 0; i < nitem->msize; i++) PetscCall(PetscViewerASCIIPrintf(view, "%g ", (double)value[i]));
676: PetscCall(PetscViewerASCIIPrintf(view, "; %s\n", nitem->help));
677: } else if (nitem->dtype == PETSC_SCALAR) {
678: PetscScalar value = *(PetscScalar *)(((char *)bag) + nitem->offset);
679: #if defined(PETSC_USE_COMPLEX)
680: if ((double)PetscImaginaryPart(value)) {
681: PetscCall(PetscViewerASCIIPrintf(view, " %s = %g + %gi; %s\n", nitem->name, (double)PetscRealPart(value), (double)PetscImaginaryPart(value), nitem->help));
682: } else {
683: PetscCall(PetscViewerASCIIPrintf(view, " %s = %g; %s\n", nitem->name, (double)PetscRealPart(value), nitem->help));
684: }
685: #else
686: PetscCall(PetscViewerASCIIPrintf(view, " %s = %g; %s\n", nitem->name, (double)value, nitem->help));
687: #endif
688: } else if (nitem->dtype == PETSC_INT) {
689: PetscInt i, *value = (PetscInt *)(((char *)bag) + nitem->offset);
690: PetscCall(PetscViewerASCIIPrintf(view, " %s = ", nitem->name));
691: for (i = 0; i < nitem->msize; i++) PetscCall(PetscViewerASCIIPrintf(view, "%" PetscInt_FMT " ", value[i]));
692: PetscCall(PetscViewerASCIIPrintf(view, "; %s\n", nitem->help));
693: } else if (nitem->dtype == PETSC_BOOL) {
694: PetscBool *value = (PetscBool *)(((char *)bag) + nitem->offset);
695: PetscInt i;
696: /* some Fortran compilers use -1 as boolean */
697: PetscCall(PetscViewerASCIIPrintf(view, " %s = ", nitem->name));
698: for (i = 0; i < nitem->msize; i++) {
699: if (((int)value[i]) == -1) value[i] = PETSC_TRUE;
700: /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
701: PetscCheck(value[i] == PETSC_FALSE || value[i] == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Boolean value for %s %s is corrupt; integer value %" PetscInt_FMT, nitem->name, nitem->help, (PetscInt)value[i]);
702: PetscCall(PetscViewerASCIIPrintf(view, " %s", PetscBools[value[i]]));
703: }
704: PetscCall(PetscViewerASCIIPrintf(view, "; %s\n", nitem->help));
705: } else if (nitem->dtype == PETSC_ENUM) {
706: PetscEnum value = *(PetscEnum *)(((char *)bag) + nitem->offset);
707: PetscInt i = 0;
708: while (nitem->list[i++]);
709: PetscCall(PetscViewerASCIIPrintf(view, " %s = %s; (%s) %s\n", nitem->name, nitem->list[value], nitem->list[i - 3], nitem->help));
710: }
711: nitem = nitem->next;
712: }
713: } else if (isbinary) {
714: PetscInt classid = PETSC_BAG_FILE_CLASSID, dtype;
715: PetscInt deprecatedbagsize = 0;
716: PetscViewerFormat format;
717: PetscCall(PetscViewerBinaryWrite(view, &classid, 1, PETSC_INT));
718: PetscCall(PetscViewerBinaryWrite(view, &deprecatedbagsize, 1, PETSC_INT));
719: PetscCall(PetscViewerBinaryWrite(view, &bag->count, 1, PETSC_INT));
720: PetscCall(PetscViewerBinaryWrite(view, bag->bagname, PETSC_BAG_NAME_LENGTH, PETSC_CHAR));
721: PetscCall(PetscViewerBinaryWrite(view, bag->baghelp, PETSC_BAG_HELP_LENGTH, PETSC_CHAR));
722: while (nitem) {
723: PetscCall(PetscViewerBinaryWrite(view, &nitem->offset, 1, PETSC_INT));
724: dtype = (PetscInt)nitem->dtype;
725: PetscCall(PetscViewerBinaryWrite(view, &dtype, 1, PETSC_INT));
726: PetscCall(PetscViewerBinaryWrite(view, nitem->name, PETSC_BAG_NAME_LENGTH, PETSC_CHAR));
727: PetscCall(PetscViewerBinaryWrite(view, nitem->help, PETSC_BAG_HELP_LENGTH, PETSC_CHAR));
728: PetscCall(PetscViewerBinaryWrite(view, &nitem->msize, 1, PETSC_INT));
729: /* some Fortran compilers use -1 as boolean */
730: if (dtype == PETSC_BOOL && (*(int *)(((char *)bag) + nitem->offset) == -1)) *(int *)(((char *)bag) + nitem->offset) = PETSC_TRUE;
732: PetscCall(PetscViewerBinaryWrite(view, (char *)bag + nitem->offset, nitem->msize, nitem->dtype));
733: if (dtype == PETSC_ENUM) PetscCall(PetscViewerBinaryWriteStringArray(view, (const char *const *)nitem->list));
734: nitem = nitem->next;
735: }
736: PetscCall(PetscViewerGetFormat(view, &format));
737: if (format == PETSC_VIEWER_BINARY_MATLAB) {
738: MPI_Comm comm;
739: FILE *info;
740: PetscCall(PetscObjectGetComm((PetscObject)view, &comm));
741: PetscCall(PetscViewerBinaryGetInfoPointer(view, &info));
742: PetscCall(PetscFPrintf(comm, info, "#--- begin code written by PetscViewerBinary for MATLAB format ---#\n"));
743: PetscCall(PetscFPrintf(comm, info, "#$$ Set.%s = PetscBinaryRead(fd);\n", bag->bagname));
744: PetscCall(PetscFPrintf(comm, info, "#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n"));
745: }
746: }
747: PetscFunctionReturn(PETSC_SUCCESS);
748: }
750: /*@
751: PetscBagViewFromOptions - Processes command line options to determine if/how a `PetscBag` is to be viewed.
753: Collective
755: Input Parameters:
756: + bag - the object
757: . bobj - optional other object that provides prefix (if `NULL` then the prefix in obj is used)
758: - optionname - option to activate viewing
760: Level: intermediate
762: .seealso: `PetscBagCreate()`, `PetscBag`, `PetscViewer`
763: @*/
764: PetscErrorCode PetscBagViewFromOptions(PetscBag bag, PetscObject bobj, const char optionname[])
765: {
766: static PetscBool incall = PETSC_FALSE;
767: PetscViewer viewer;
768: PetscViewerFormat format;
769: const char *prefix, *bprefix = NULL;
770: PetscBool flg;
772: PetscFunctionBegin;
773: if (incall) PetscFunctionReturn(PETSC_SUCCESS);
774: incall = PETSC_TRUE;
775: PetscAssertPointer(bag, 1);
776: if (bobj) PetscCall(PetscObjectGetOptionsPrefix(bobj, &bprefix));
777: prefix = bobj ? bprefix : bag->bagprefix;
778: PetscCall(PetscOptionsCreateViewer(bag->bagcomm, NULL, prefix, optionname, &viewer, &format, &flg));
779: if (flg) {
780: PetscCall(PetscViewerPushFormat(viewer, format));
781: PetscCall(PetscBagView(bag, viewer));
782: PetscCall(PetscViewerFlush(viewer));
783: PetscCall(PetscViewerPopFormat(viewer));
784: PetscCall(PetscViewerDestroy(&viewer));
785: }
786: incall = PETSC_FALSE;
787: PetscFunctionReturn(PETSC_SUCCESS);
788: }
790: /*@
791: PetscBagLoad - Loads a bag of values from a binary file
793: Collective
795: Input Parameters:
796: + view - file to load values from
797: - bag - the bag of values
799: Level: beginner
801: Note:
802: You must have created and registered all the fields in the bag before loading into it. This only loads values.
804: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagDestroy()`, `PetscBagView()`, `PetscBagGetData()`
805: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
806: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagGetName()`, `PetscBagRegisterEnum()`
807: @*/
808: PetscErrorCode PetscBagLoad(PetscViewer view, PetscBag bag)
809: {
810: PetscBool isbinary;
811: PetscInt classid, bagcount, dtype, msize, offset, deprecatedbagsize;
812: char name[PETSC_BAG_NAME_LENGTH], help[PETSC_BAG_HELP_LENGTH], **list;
813: PetscBagItem nitem;
814: MPI_Comm comm;
815: PetscMPIInt flag;
817: PetscFunctionBegin;
819: PetscAssertPointer(bag, 2);
820: PetscCall(PetscObjectGetComm((PetscObject)view, &comm));
821: PetscCallMPI(MPI_Comm_compare(comm, bag->bagcomm, &flag));
822: PetscCheck(flag == MPI_CONGRUENT || flag == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "Different communicators in the viewer and bag");
823: PetscCall(PetscObjectTypeCompare((PetscObject)view, PETSCVIEWERBINARY, &isbinary));
824: PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for this viewer type");
826: PetscCall(PetscViewerBinaryRead(view, &classid, 1, NULL, PETSC_INT));
827: PetscCheck(classid == PETSC_BAG_FILE_CLASSID, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not PetscBag next in binary file");
828: PetscCall(PetscViewerBinaryRead(view, &deprecatedbagsize, 1, NULL, PETSC_INT));
829: PetscCall(PetscViewerBinaryRead(view, &bagcount, 1, NULL, PETSC_INT));
830: PetscCheck(bagcount == bag->count, comm, PETSC_ERR_ARG_INCOMP, "Bag in file has different number of entries %d then passed in bag %d", (int)bagcount, (int)bag->count);
831: PetscCall(PetscViewerBinaryRead(view, bag->bagname, PETSC_BAG_NAME_LENGTH, NULL, PETSC_CHAR));
832: PetscCall(PetscViewerBinaryRead(view, bag->baghelp, PETSC_BAG_HELP_LENGTH, NULL, PETSC_CHAR));
834: nitem = bag->bagitems;
835: for (PetscInt i = 0; i < bagcount; i++) {
836: PetscCall(PetscViewerBinaryRead(view, &offset, 1, NULL, PETSC_INT));
837: /* ignore the offset in the file */
838: PetscCall(PetscViewerBinaryRead(view, &dtype, 1, NULL, PETSC_INT));
839: PetscCall(PetscViewerBinaryRead(view, name, PETSC_BAG_NAME_LENGTH, NULL, PETSC_CHAR));
840: PetscCall(PetscViewerBinaryRead(view, help, PETSC_BAG_HELP_LENGTH, NULL, PETSC_CHAR));
841: PetscCall(PetscViewerBinaryRead(view, &msize, 1, NULL, PETSC_INT));
843: if (dtype == (PetscInt)PETSC_CHAR) {
844: PetscCall(PetscViewerBinaryRead(view, ((char *)bag) + nitem->offset, msize, NULL, PETSC_CHAR));
845: } else if (dtype == (PetscInt)PETSC_REAL) {
846: PetscCall(PetscViewerBinaryRead(view, ((char *)bag) + nitem->offset, msize, NULL, PETSC_REAL));
847: } else if (dtype == (PetscInt)PETSC_SCALAR) {
848: PetscCall(PetscViewerBinaryRead(view, ((char *)bag) + nitem->offset, 1, NULL, PETSC_SCALAR));
849: } else if (dtype == (PetscInt)PETSC_INT) {
850: PetscCall(PetscViewerBinaryRead(view, ((char *)bag) + nitem->offset, msize, NULL, PETSC_INT));
851: } else if (dtype == (PetscInt)PETSC_BOOL) {
852: PetscCall(PetscViewerBinaryRead(view, ((char *)bag) + nitem->offset, msize, NULL, PETSC_BOOL));
853: } else if (dtype == (PetscInt)PETSC_ENUM) {
854: PetscCall(PetscViewerBinaryRead(view, ((char *)bag) + nitem->offset, 1, NULL, PETSC_ENUM));
855: PetscCall(PetscViewerBinaryReadStringArray(view, &list));
856: /* don't need to save list because it is already registered in the bag */
857: PetscCall(PetscFree(list));
858: }
859: nitem = nitem->next;
860: }
861: PetscFunctionReturn(PETSC_SUCCESS);
862: }
864: /*@C
865: PetscBagCreate - Create a bag of values. A `PetscBag` is a representation of a C struct that can be saved to and read from files,
866: can have values set from the options database
868: Collective
870: Input Parameters:
871: + comm - communicator to share bag
872: - bagsize - size of the C structure holding the values, for example sizeof(mystruct)
874: Output Parameter:
875: . bag - the bag of values
877: Level: intermediate
879: Notes:
880: After creating the bag, for each entry in the C struct call the appropriate `PetscBagRegisterInt()` etc to define the C structs layout
882: The size of the struct must be small enough to fit in a `PetscInt`; by default
883: `PetscInt` is 4 bytes; this means a bag cannot be larger than 2 gigabytes in length.
884: The warning about casting to a shorter length can be ignored below unless your struct is too large
886: .seealso: `PetscBag`, `PetscBagGetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
887: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
888: `PetscBagSetFromOptions()`, `PetscBagDestroy()`, `PetscBagRegisterEnum()`
889: @*/
890: PetscErrorCode PetscBagCreate(MPI_Comm comm, size_t bagsize, PetscBag *bag)
891: {
892: const size_t totalsize = bagsize + sizeof(struct _n_PetscBag) + sizeof(PetscScalar);
894: PetscFunctionBegin;
895: PetscAssertPointer(bag, 3);
897: PetscCall(PetscInfo(NULL, "Creating Bag with total size %d\n", (int)totalsize));
898: PetscCall(PetscCalloc(totalsize, bag));
899: PetscCall(PetscIntCast(totalsize, &(*bag)->bagsize));
900: (*bag)->bagcomm = comm;
901: (*bag)->bagprefix = NULL;
902: (*bag)->structlocation = (void *)(((char *)(*bag)) + sizeof(PetscScalar) * (sizeof(struct _n_PetscBag) / sizeof(PetscScalar)) + sizeof(PetscScalar));
903: PetscFunctionReturn(PETSC_SUCCESS);
904: }
906: /*@
907: PetscBagSetName - Sets the name of a bag of values
909: Not Collective
911: Level: intermediate
913: Input Parameters:
914: + bag - the bag of values
915: . name - the name assigned to the bag
916: - help - help message for bag
918: .seealso: `PetscBag`, `PetscBagGetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
919: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
920: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagDestroy()`, `PetscBagRegisterEnum()`
921: @*/
922: PetscErrorCode PetscBagSetName(PetscBag bag, const char *name, const char *help)
923: {
924: PetscFunctionBegin;
925: PetscAssertPointer(bag, 1);
926: PetscAssertPointer(name, 2);
927: PetscAssertPointer(help, 3);
928: PetscCall(PetscStrncpy(bag->bagname, name, PETSC_BAG_NAME_LENGTH - 1));
929: PetscCall(PetscStrncpy(bag->baghelp, help, PETSC_BAG_HELP_LENGTH - 1));
930: PetscFunctionReturn(PETSC_SUCCESS);
931: }
933: /*@C
934: PetscBagGetName - Gets the name of a bag of values
936: Not Collective
938: Level: intermediate
940: Input Parameter:
941: . bag - the bag of values
943: Output Parameter:
944: . name - the name assigned to the bag
946: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`, `PetscBagGetData()`
947: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
948: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagDestroy()`, `PetscBagRegisterEnum()`
949: @*/
950: PetscErrorCode PetscBagGetName(PetscBag bag, const char **name)
951: {
952: PetscFunctionBegin;
953: PetscAssertPointer(bag, 1);
954: PetscAssertPointer(name, 2);
955: *name = bag->bagname;
956: PetscFunctionReturn(PETSC_SUCCESS);
957: }
959: /*@C
960: PetscBagGetData - Gives back the user - access to memory that
961: can be used for storing user-data-structure
963: Not Collective
965: Input Parameter:
966: . bag - the bag of values
968: Output Parameter:
969: . data - pointer to memory that will have user-data-structure, this can be cast to a pointer of the type the C struct used in
970: defining the bag
972: Level: intermediate
974: .seealso: `PetscBag`, `PetscBagSetName()`, `PetscBagView()`, `PetscBagLoad()`
975: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
976: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagDestroy()`, `PetscBagRegisterEnum()`
977: @*/
978: PetscErrorCode PetscBagGetData(PetscBag bag, void **data)
979: {
980: PetscFunctionBegin;
981: PetscAssertPointer(bag, 1);
982: PetscAssertPointer(data, 2);
983: *data = bag->structlocation;
984: PetscFunctionReturn(PETSC_SUCCESS);
985: }
987: /*@
988: PetscBagSetOptionsPrefix - Sets the prefix used for searching for all
989: `PetscBag` items in the options database.
991: Logically Collective
993: Level: intermediate
995: Input Parameters:
996: + bag - the bag of values
997: - pre - the prefix to prepend all Bag item names with.
999: Note:
1000: Must be called prior to registering any of the bag items.
1002: .seealso: `PetscBag`, `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`
1003: `PetscBagSetFromOptions()`, `PetscBagCreate()`, `PetscBagDestroy()`, `PetscBagRegisterEnum()`
1004: @*/
1005: PetscErrorCode PetscBagSetOptionsPrefix(PetscBag bag, const char pre[])
1006: {
1007: PetscFunctionBegin;
1008: PetscAssertPointer(bag, 1);
1009: if (pre) {
1010: PetscAssertPointer(pre, 2);
1011: PetscCheck(pre[0] != '-', PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Options prefix should not begin with a hyphen");
1012: PetscCall(PetscFree(bag->bagprefix));
1013: PetscCall(PetscStrallocpy(pre, &bag->bagprefix));
1014: } else PetscCall(PetscFree(bag->bagprefix));
1015: PetscFunctionReturn(PETSC_SUCCESS);
1016: }
1018: /*@C
1019: PetscBagGetNames - Get the names of all entries in the bag
1021: Not Collective
1023: Input Parameter:
1024: . bag - the bag of values
1026: Output Parameter:
1027: . names - array of char pointers for names
1029: Level: intermediate
1031: .seealso: `PetscBag`, `PetscBagGetName()`, `PetscBagSetName()`, `PetscBagCreate()`, `PetscBagGetData()`
1032: `PetscBagRegisterReal()`, `PetscBagRegisterInt()`, `PetscBagRegisterBool()`, `PetscBagRegisterScalar()`, `PetscBagRegisterEnum()`
1033: @*/
1034: PetscErrorCode PetscBagGetNames(PetscBag bag, const char *names[])
1035: {
1036: PetscBagItem nitem = bag->bagitems;
1038: PetscFunctionBegin;
1039: PetscAssertPointer(bag, 1);
1040: PetscAssertPointer(names, 2);
1041: for (PetscInt n = 0; nitem; ++n, nitem = nitem->next) names[n] = nitem->name;
1042: PetscFunctionReturn(PETSC_SUCCESS);
1043: }