Actual source code: bag.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  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: {

 13:   PetscStrncpy(item->name,name,PETSC_BAG_NAME_LENGTH-1);
 14:   PetscStrncpy(item->help,help,PETSC_BAG_HELP_LENGTH-1);
 15:   if (!bag->bagitems) bag->bagitems = item;
 16:   else {
 17:     PetscBagItem nitem = bag->bagitems;
 18:     while (nitem->next) {
 19:       nitem = nitem->next;
 20:     }
 21:     nitem->next = item;
 22:   }
 23:   bag->count++;
 24:   return(0);
 25: }

 27: /*@C
 28:    PetscBagRegisterEnum - add an enum value to the bag

 30:    Logically Collective on PetscBag

 32:    Input Parameter:
 33: +  bag - the bag of values
 34: .  addr - location of enum in struct
 35: .  mdefault - the initial value
 36: .  list - array of strings containing names of enum values followed by enum name followed by enum prefix
 37: -  help - longer string with more information about the value

 39:    Level: beginner

 41: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
 42:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
 43:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName()

 45: @*/
 46: PetscErrorCode PetscBagRegisterEnum(PetscBag bag,void *addr,const char *const *list,PetscEnum mdefault, const char *name, const char *help)
 47: {
 49:   PetscBagItem   item;
 50:   char           nname[PETSC_BAG_NAME_LENGTH+1];
 51:   PetscBool      printhelp;
 52:   PetscInt       i = 0;

 55:   nname[0] = '-';
 56:   nname[1] = 0;
 57:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
 58:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
 59:   if (printhelp) {
 60:     while (list[i++]) ;
 61:     (*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++) {
 63:       (*PetscHelpPrintf)(bag->bagcomm," %s",list[i]);
 64:     }
 65:     (*PetscHelpPrintf)(bag->bagcomm,"\n");
 66:   }
 67:   PetscOptionsGetEnum(NULL,bag->bagprefix,nname,list,&mdefault,NULL);

 69:   PetscNew(&item);
 70:   item->dtype  = PETSC_ENUM;
 71:   item->offset = ((char*)addr) - ((char*)bag);
 72:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
 73:   item->next        = 0;
 74:   item->msize       = 1;
 75:   PetscStrArrayallocpy(list,(char***)&item->list);
 76:   *(PetscEnum*)addr = mdefault;
 77:   PetscBagRegister_Private(bag,item,name,help);
 78:   return(0);
 79: }

 81: /*@C
 82:    PetscBagRegisterIntArray - add an integer value to the bag

 84:    Logically Collective on PetscBag

 86:    Input Parameter:
 87: +  bag - the bag of values
 88: .  addr - location of integer in struct
 89: .  msize - number of entries in array
 90: .  name - name of the integer array
 91: -  help - longer string with more information about the value

 93:    Level: beginner

 95: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
 96:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
 97:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

 99: @*/
100: PetscErrorCode PetscBagRegisterIntArray(PetscBag bag,void *addr,PetscInt msize, const char *name, const char *help)
101: {
103:   PetscBagItem   item;
104:   char           nname[PETSC_BAG_NAME_LENGTH+1];
105:   PetscBool      printhelp;
106:   PetscInt       i,tmp = msize;

109:   /* PetscMemzero(addr,msize*sizeof(PetscInt));*/
110:   nname[0] = '-';
111:   nname[1] = 0;
112:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
113:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
114:   if (printhelp) {
115:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <",bag->bagprefix ? bag->bagprefix : "",name);
116:     for (i=0; i<msize; i++) {
117:       (*PetscHelpPrintf)(bag->bagcomm,"%D ",*((PetscInt*)addr)+i);
118:     }
119:     (*PetscHelpPrintf)(bag->bagcomm,">: %s \n",help);
120:   }
121:   PetscOptionsGetIntArray(NULL,bag->bagprefix,nname,(PetscInt*)addr,&tmp,NULL);

123:   PetscNew(&item);
124:   item->dtype  = PETSC_INT;
125:   item->offset = ((char*)addr) - ((char*)bag);
126:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
127:   item->next  = 0;
128:   item->msize = msize;
129:   PetscBagRegister_Private(bag,item,name,help);
130:   return(0);
131: }

133: /*@C
134:    PetscBagRegisterRealArray - add an real array to the bag

136:    Logically Collective on PetscBag

138:    Input Parameter:
139: +  bag - the bag of values
140: .  addr - location of real array in struct
141: .  msize - number of entries in array
142: .  name - name of the integer array
143: -  help - longer string with more information about the value

145:    Level: beginner

147: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
148:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
149:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

151: @*/
152: PetscErrorCode PetscBagRegisterRealArray(PetscBag bag,void *addr,PetscInt msize, const char *name, const char *help)
153: {
155:   PetscBagItem   item;
156:   char           nname[PETSC_BAG_NAME_LENGTH+1];
157:   PetscBool      printhelp;
158:   PetscInt       i,tmp = msize;

161:   /* PetscMemzero(addr,msize*sizeof(PetscInt));*/
162:   nname[0] = '-';
163:   nname[1] = 0;
164:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
165:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
166:   if (printhelp) {
167:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <",bag->bagprefix ? bag->bagprefix : "",name);
168:     for (i=0; i<msize; i++) {
169:       (*PetscHelpPrintf)(bag->bagcomm,"%g ",(double)*((PetscReal*)addr)+i);
170:     }
171:     (*PetscHelpPrintf)(bag->bagcomm,">: %s \n",help);
172:   }
173:   PetscOptionsGetRealArray(NULL,bag->bagprefix,nname,(PetscReal*)addr,&tmp,NULL);

175:   PetscNew(&item);
176:   item->dtype  = PETSC_REAL;
177:   item->offset = ((char*)addr) - ((char*)bag);
178:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
179:   item->next  = 0;
180:   item->msize = msize;
181:   PetscBagRegister_Private(bag,item,name,help);
182:   return(0);
183: }

185: /*@C
186:    PetscBagRegisterInt - add an integer value to the bag

188:    Logically Collective on PetscBag

190:    Input Parameter:
191: +  bag - the bag of values
192: .  addr - location of integer in struct
193: .  mdefault - the initial value
194: .  name - name of the integer
195: -  help - longer string with more information about the value

197:    Level: beginner

199: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
200:            PetscBagRegisterInt64(), PetscBagRegisterBool(), PetscBagRegisterScalar()
201:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

203: @*/
204: PetscErrorCode PetscBagRegisterInt(PetscBag bag,void *addr,PetscInt mdefault,const char *name,const char *help)
205: {
207:   PetscBagItem   item;
208:   char           nname[PETSC_BAG_NAME_LENGTH+1];
209:   PetscBool      printhelp;

212:   nname[0] = '-';
213:   nname[1] = 0;
214:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
215:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
216:   if (printhelp) {
217:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%d>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,mdefault,help);
218:   }
219:   PetscOptionsGetInt(NULL,bag->bagprefix,nname,&mdefault,NULL);

221:   PetscNew(&item);
222:   item->dtype  = PETSC_INT;
223:   item->offset = ((char*)addr) - ((char*)bag);
224:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
225:   item->next       = 0;
226:   item->msize      = 1;
227:   *(PetscInt*)addr = mdefault;
228:   PetscBagRegister_Private(bag,item,name,help);
229:   return(0);
230: }

232: /*@C
233:    PetscBagRegisterInt64 - add an integer value to the bag

235:    Logically Collective on PetscBag

237:    Input Parameter:
238: +  bag - the bag of values
239: .  addr - location of integer in struct
240: .  mdefault - the initial value
241: .  name - name of the integer
242: -  help - longer string with more information about the value

244:    Level: beginner

246: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
247:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
248:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

250: @*/
251: PetscErrorCode PetscBagRegisterInt64(PetscBag bag,void *addr,PetscInt64 mdefault,const char *name,const char *help)
252: {
254:   PetscBagItem   item;
255:   char           nname[PETSC_BAG_NAME_LENGTH+1];
256:   PetscBool      printhelp;
257:   PetscInt       odefault = (PetscInt)mdefault;
258:   PetscBool      flg;
259: 
261:   nname[0] = '-';
262:   nname[1] = 0;
263:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
264:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
265:   if (printhelp) {
266:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%d>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,odefault,help);
267:   }
268:   PetscOptionsGetInt(NULL,bag->bagprefix,nname,&odefault,&flg);
269:   if (flg) mdefault = (PetscInt64)odefault;

271:   PetscNew(&item);
272:   item->dtype  = PETSC_INT;
273:   item->offset = ((char*)addr) - ((char*)bag);
274:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
275:   item->next       = 0;
276:   item->msize      = 1;
277:   *(PetscInt64*)addr = mdefault;
278:   PetscBagRegister_Private(bag,item,name,help);
279:   return(0);
280: }

282: /*@C
283:    PetscBagRegisterBoolArray - add a n logical values to the bag

285:    Logically Collective on PetscBag

287:    Input Parameter:
288: +  bag - the bag of values
289: .  addr - location of boolean array in struct
290: .  msize - number of entries in array
291: .  name - name of the boolean array
292: -  help - longer string with more information about the value

294:    Level: beginner

296: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
297:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
298:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

300: @*/
301: PetscErrorCode PetscBagRegisterBoolArray(PetscBag bag,void *addr,PetscInt msize, const char* name, const char* help)
302: {
304:   PetscBagItem   item;
305:   char           nname[PETSC_BAG_NAME_LENGTH+1];
306:   PetscBool      printhelp;
307:   PetscInt       i,tmp = msize;

310:   /* PetscMemzero(addr,msize*sizeof(PetscInt));*/
311:   nname[0] = '-';
312:   nname[1] = 0;
313:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
314:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
315:   if (printhelp) {
316:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <",bag->bagprefix?bag->bagprefix:"",name);
317:     for (i=0; i<msize; i++) {
318:       (*PetscHelpPrintf)(bag->bagcomm,"%D ",*((PetscInt*)addr)+i);
319:     }
320:     (*PetscHelpPrintf)(bag->bagcomm,">: %s \n",help);
321:   }
322:   PetscOptionsGetBoolArray(NULL,bag->bagprefix,nname,(PetscBool*)addr,&tmp,NULL);

324:   PetscNew(&item);
325:   item->dtype  = PETSC_BOOL;
326:   item->offset = ((char*)addr) - ((char*)bag);
327:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
328:   item->next   = 0;
329:   item->msize  = msize;
330:   PetscBagRegister_Private(bag,item,name,help);
331:   return(0);
332: }

334: /*@C
335:    PetscBagRegisterString - add a string value to the bag

337:    Logically Collective on PetscBag

339:    Input Parameter:
340: +  bag - the bag of values
341: .  addr - location of start of string in struct
342: .  msize - length of the string space in the struct
343: .  mdefault - the initial value
344: .  name - name of the string
345: -  help - longer string with more information about the value

347:    Level: beginner

349:    Note: The struct should have the field char mystring[msize]; not char *mystring

351: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
352:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
353:            PetscBagSetFromOptions(),PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

355: @*/
356: PetscErrorCode PetscBagRegisterString(PetscBag bag,void *addr,PetscInt msize,const char* mdefault,const char* name,const char* help)
357: {
359:   PetscBagItem   item;
360:   char           nname[PETSC_BAG_NAME_LENGTH+1];
361:   PetscBool      printhelp;

364:   nname[0] = '-';
365:   nname[1] = 0;
366:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
367:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
368:   if (printhelp) {
369:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%s>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,mdefault,help);
370:   }

372:   PetscNew(&item);
373:   item->dtype  = PETSC_CHAR;
374:   item->offset = ((char*)addr) - ((char*)bag);
375:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
376:   item->next  = 0;
377:   item->msize = msize;
378:   if (mdefault != (char*)addr) {
379:     PetscStrncpy((char*)addr,mdefault,msize-1);
380:   }
381:   PetscOptionsGetString(NULL,bag->bagprefix,nname,(char*)addr,msize,NULL);
382:   PetscBagRegister_Private(bag,item,name,help);
383:   return(0);
384: }

386: /*@C
387:    PetscBagRegisterReal - add a real value to the bag

389:    Logically Collective on PetscBag

391:    Input Parameter:
392: +  bag - the bag of values
393: .  addr - location of double in struct
394: .  mdefault - the initial value
395: .  name - name of the variable
396: -  help - longer string with more information about the value

398:    Level: beginner

400: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
401:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
402:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

404: @*/
405: PetscErrorCode PetscBagRegisterReal(PetscBag bag,void *addr,PetscReal mdefault, const char *name, const char *help)
406: {
408:   PetscBagItem   item;
409:   char           nname[PETSC_BAG_NAME_LENGTH+1];
410:   PetscBool      printhelp;

413:   nname[0] = '-';
414:   nname[1] = 0;
415:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
416:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
417:   if (printhelp) {
418:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%g>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,(double)mdefault,help);
419:   }
420:   PetscOptionsGetReal(NULL,bag->bagprefix,nname,&mdefault,NULL);

422:   PetscNew(&item);
423:   item->dtype  = PETSC_REAL;
424:   item->offset = ((char*)addr) - ((char*)bag);
425:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
426:   item->next        = 0;
427:   item->msize       = 1;
428:   *(PetscReal*)addr = mdefault;
429:   PetscBagRegister_Private(bag,item,name,help);
430:   return(0);
431: }

433: /*@C
434:    PetscBagRegisterScalar - add a real or complex number value to the bag

436:    Logically Collective on PetscBag

438:    Input Parameter:
439: +  bag - the bag of values
440: .  addr - location of scalar in struct
441: .  mdefault - the initial value
442: .  name - name of the variable
443: -  help - longer string with more information about the value


446:    Level: beginner

448: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
449:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
450:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

452: @*/
453: PetscErrorCode PetscBagRegisterScalar(PetscBag bag,void *addr,PetscScalar mdefault,const char *name,const char *help)
454: {
456:   PetscBagItem   item;
457:   char           nname[PETSC_BAG_NAME_LENGTH+1];
458:   PetscBool      printhelp;

461:   nname[0] = '-';
462:   nname[1] = 0;
463:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
464:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
465:   if (printhelp) {
466:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%g + %gi>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,(double)PetscRealPart(mdefault),(double)PetscImaginaryPart(mdefault),help);
467:   }
468:   PetscOptionsGetScalar(NULL,bag->bagprefix,nname,&mdefault,NULL);

470:   PetscNew(&item);
471:   item->dtype  = PETSC_SCALAR;
472:   item->offset = ((char*)addr) - ((char*)bag);
473:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
474:   item->next          = 0;
475:   item->msize         = 1;
476:   *(PetscScalar*)addr = mdefault;
477:   PetscBagRegister_Private(bag,item,name,help);
478:   return(0);
479: }

481: /*@C
482:    PetscBagRegisterBool - add a logical value to the bag

484:    Logically Collective on PetscBag

486:    Input Parameter:
487: +  bag - the bag of values
488: .  addr - location of logical in struct
489: .  mdefault - the initial value
490: .  name - name of the variable
491: -  help - longer string with more information about the value


494:    Level: beginner

496: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
497:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
498:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

500: @*/
501: PetscErrorCode PetscBagRegisterBool(PetscBag bag,void *addr,PetscBool mdefault,const char *name,const char *help)
502: {
504:   PetscBagItem   item;
505:   char           nname[PETSC_BAG_NAME_LENGTH+1];
506:   PetscBool      printhelp;

509:   /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
510:   if (mdefault != PETSC_FALSE && mdefault != PETSC_TRUE) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Boolean %s %s must be boolean; integer value %d",name,help,(int)mdefault);
511:   nname[0] = '-';
512:   nname[1] = 0;
513:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
514:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
515:   if (printhelp) {
516:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%s>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,PetscBools[mdefault],help);
517:   }
518:   PetscOptionsGetBool(NULL,bag->bagprefix,nname,&mdefault,NULL);

520:   PetscNew(&item);
521:   item->dtype  = PETSC_BOOL;
522:   item->offset = ((char*)addr) - ((char*)bag);
523:   if (item->offset > bag->bagsize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Registered item %s %s is not in bag memory space",name,help);
524:   item->next        = 0;
525:   item->msize       = 1;
526:   *(PetscBool*)addr = mdefault;
527:   PetscBagRegister_Private(bag,item,name,help);
528:   return(0);
529: }

531: /*@C
532:    PetscBagDestroy - Destroys a bag values

534:    Collective on PetscBag

536:    Input Parameter:
537: .  bag - the bag of values

539:    Level: beginner

541: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
542:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
543:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

545: @*/
546: PetscErrorCode  PetscBagDestroy(PetscBag *bag)
547: {
549:   PetscBagItem   nitem = (*bag)->bagitems,item;

552:   while (nitem) {
553:     item = nitem->next;
554:     if (nitem->list) {
555:       PetscStrArrayDestroy(&nitem->list);
556:     }
557:     PetscFree(nitem);
558:     nitem = item;
559:   }
560:   if ((*bag)->bagprefix) { PetscFree((*bag)->bagprefix); }
561:   PetscFree(*bag);
562:   return(0);
563: }

565: /*@
566:    PetscBagSetFromOptions - Allows setting options from a bag

568:    Collective on PetscBag

570:    Input Parameter:
571: .  bag - the bag of values

573:    Level: beginner

575: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagLoad(), PetscBagGetData()
576:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
577:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagView(), PetscBagRegisterEnum()

579: @*/
580: PetscErrorCode  PetscBagSetFromOptions(PetscBag bag)
581: {
583:   PetscBagItem   nitem = bag->bagitems;
584:   char           name[PETSC_BAG_NAME_LENGTH+1],helpname[PETSC_BAG_NAME_LENGTH+PETSC_BAG_HELP_LENGTH+3];
585:   PetscInt       n;

588:   PetscStrcpy(helpname,bag->bagname);
589:   PetscStrcat(helpname," ");
590:   PetscStrcat(helpname,bag->baghelp);
591:   PetscOptionsBegin(bag->bagcomm,bag->bagprefix,helpname,0);
592:   while (nitem) {
593:     name[0] = '-';
594:     name[1] = 0;
595:     PetscStrcat(name,nitem->name);
596:     if (nitem->dtype == PETSC_CHAR) {   /* special handling for fortran required? [due to space padding vs null termination] */
597:       char *value = (char*)(((char*)bag) + nitem->offset);
598:       PetscOptionsString(name,nitem->help,"",value,value,nitem->msize,NULL);
599:     } else if (nitem->dtype == PETSC_REAL) {
600:       PetscReal *value = (PetscReal*)(((char*)bag) + nitem->offset);
601:       if (nitem->msize == 1) {
602:         PetscOptionsReal(name,nitem->help,"",*value,value,NULL);
603:       } else {
604:         n    = nitem->msize;
605:         PetscOptionsRealArray(name,nitem->help,"",value,&n,NULL);
606:       }
607:     } else if (nitem->dtype == PETSC_SCALAR) {
608:       PetscScalar *value = (PetscScalar*)(((char*)bag) + nitem->offset);
609:       PetscOptionsScalar(name,nitem->help,"",*value,value,NULL);
610:     } else if (nitem->dtype == PETSC_INT) {
611:       PetscInt *value = (PetscInt*)(((char*)bag) + nitem->offset);
612:       if (nitem->msize == 1) {
613:         PetscOptionsInt(name,nitem->help,"",*value,value,NULL);
614:       } else {
615:         n    = nitem->msize;
616:         PetscOptionsIntArray(name,nitem->help,"",value,&n,NULL);
617:       }
618:     } else if (nitem->dtype == PETSC_ENUM) {
619:       PetscEnum *value = (PetscEnum*)(((char*)bag) + nitem->offset);
620:       PetscInt  i      = 0;
621:       while (nitem->list[i++]) ;
622:       PetscOptionsEnum(name,nitem->help,nitem->list[i-3],(const char*const*)nitem->list,*value,value,NULL);
623:     } else if (nitem->dtype == PETSC_BOOL) {
624:       PetscBool *value = (PetscBool*)(((char*)bag) + nitem->offset);
625:       if (nitem->msize == 1) {
626:         PetscOptionsBool(name,nitem->help,"",*value,value,NULL);
627:       } else {
628:         n = nitem->msize;
629:         PetscOptionsBoolArray(name,nitem->help,"",value,&n,NULL);
630:       }
631:     }
632:     nitem = nitem->next;
633:   }
634:   PetscOptionsEnd();
635:   return(0);
636: }

638: /*@C
639:    PetscBagView - Views a bag of values as either ASCII text or a binary file

641:    Collective on PetscBag

643:    Input Parameter:
644: +  bag - the bag of values
645: -  viewer - location to view the values

647:    Level: beginner

649:    Warning: Currently PETSc bags saved in a binary file can only be read back
650:      in on a machine of the same architecture. Let us know when this is a problem
651:      and we'll fix it.

653: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagLoad(), PetscBagGetData()
654:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar(), PetscBagRegisterEnum()
655:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName()

657: @*/
658: PetscErrorCode  PetscBagView(PetscBag bag,PetscViewer view)
659: {
660:   PetscBool      isascii,isbinary;
662:   PetscBagItem   nitem = bag->bagitems;

665:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&isascii);
666:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERBINARY,&isbinary);
667:   if (isascii) {
668:     if (bag->bagprefix) {
669:       PetscViewerASCIIPrintf(view,"PetscBag Object:  %s (%s) %s\n",bag->bagname,bag->bagprefix,bag->baghelp);
670:     } else {
671:       PetscViewerASCIIPrintf(view,"PetscBag Object:  %s %s\n",bag->bagname,bag->baghelp);
672:     }
673:     while (nitem) {
674:       if (nitem->dtype == PETSC_CHAR) {
675:         char *value = (char*)(((char*)bag) + nitem->offset);
676:         char tmp    = value[nitem->msize-1]; /* special handling for fortran chars wihout null terminator */
677:         value[nitem->msize-1] =0;
678:         PetscViewerASCIIPrintf(view,"  %s = %s; %s\n",nitem->name,value,nitem->help);
679:         value[nitem->msize-1] = tmp;
680:       } else if (nitem->dtype == PETSC_REAL) {
681:         PetscReal *value = (PetscReal*)(((char*)bag) + nitem->offset);
682:         PetscInt  i;
683:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
684:         for (i=0; i<nitem->msize; i++) {
685:           PetscViewerASCIIPrintf(view,"%g ",(double)value[i]);
686:         }
687:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
688:       } else if (nitem->dtype == PETSC_SCALAR) {
689:         PetscScalar value = *(PetscScalar*)(((char*)bag) + nitem->offset);
690: #if defined(PETSC_USE_COMPLEX)
691:         if ((double)PetscImaginaryPart(value)) {
692:           PetscViewerASCIIPrintf(view,"  %s = %g + %gi; %s\n",nitem->name,(double)PetscRealPart(value),(double)PetscImaginaryPart(value),nitem->help);
693:         } else {
694:           PetscViewerASCIIPrintf(view,"  %s = %g; %s\n",nitem->name,(double)PetscRealPart(value),nitem->help);
695:         }
696: #else
697:         PetscViewerASCIIPrintf(view,"  %s = %g; %s\n",nitem->name,(double)value,nitem->help);
698: #endif
699:       } else if (nitem->dtype == PETSC_INT) {
700:         PetscInt i,*value = (PetscInt*)(((char*)bag) + nitem->offset);
701:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
702:         for (i=0; i<nitem->msize; i++) {
703:           PetscViewerASCIIPrintf(view,"%D ",value[i]);
704:         }
705:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
706:       } else if (nitem->dtype == PETSC_BOOL) {
707:         PetscBool  *value = (PetscBool*)(((char*)bag) + nitem->offset);
708:         PetscInt  i;
709:          /* some Fortran compilers use -1 as boolean */
710:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
711:         for (i=0; i<nitem->msize; i++) {
712:           if (((int) value[i]) == -1) value[i] = PETSC_TRUE;
713:           /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
714:           if (value[i] != PETSC_FALSE && value[i] != PETSC_TRUE) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Boolean value for %s %s is corrupt; integer value %d",nitem->name,nitem->help,value);
715:           PetscViewerASCIIPrintf(view," %s",PetscBools[value[i]]);
716:         }
717:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
718:       } else if (nitem->dtype == PETSC_ENUM) {
719:         PetscEnum value = *(PetscEnum*)(((char*)bag) + nitem->offset);
720:         PetscInt  i     = 0;
721:         while (nitem->list[i++]) ;
722:         PetscViewerASCIIPrintf(view,"  %s = %s; (%s) %s\n",nitem->name,nitem->list[value],nitem->list[i-3],nitem->help);
723:       }
724:       nitem = nitem->next;
725:     }
726:   } else if (isbinary) {
727:     PetscInt          classid           = PETSC_BAG_FILE_CLASSID, dtype;
728:     PetscInt          deprecatedbagsize = 0;
729:     PetscViewerFormat format;
730:     PetscViewerBinaryWrite(view,&classid,1,PETSC_INT,PETSC_TRUE);
731:     PetscViewerBinaryWrite(view,&deprecatedbagsize,1,PETSC_INT,PETSC_FALSE);
732:     PetscViewerBinaryWrite(view,&bag->count,1,PETSC_INT,PETSC_FALSE);
733:     PetscViewerBinaryWrite(view,bag->bagname,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);
734:     PetscViewerBinaryWrite(view,bag->baghelp,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);
735:     while (nitem) {
736:       PetscViewerBinaryWrite(view,&nitem->offset,1,PETSC_INT,PETSC_FALSE);
737:       dtype = (PetscInt)nitem->dtype;
738:       PetscViewerBinaryWrite(view,&dtype,1,PETSC_INT,PETSC_FALSE);
739:       PetscViewerBinaryWrite(view,nitem->name,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);
740:       PetscViewerBinaryWrite(view,nitem->help,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);
741:       PetscViewerBinaryWrite(view,&nitem->msize,1,PETSC_INT,PETSC_FALSE);
742:       /* some Fortran compilers use -1 as boolean */
743:       if (dtype == PETSC_BOOL && ((*(int*) (((char*)bag) + nitem->offset) == -1))) *(int*) (((char*)bag) + nitem->offset) = PETSC_TRUE;

745:       PetscViewerBinaryWrite(view,(((char*)bag) + nitem->offset),nitem->msize,nitem->dtype,PETSC_FALSE);
746:       if (dtype == PETSC_ENUM) {
747:         PetscViewerBinaryWriteStringArray(view,(const char* const*)nitem->list);
748:       }
749:       nitem = nitem->next;
750:     }
751:     PetscViewerGetFormat(view,&format);
752:     if (format == PETSC_VIEWER_BINARY_MATLAB) {
753:       MPI_Comm comm;
754:       FILE     *info;
755:       PetscObjectGetComm((PetscObject)view,&comm);
756:       PetscViewerBinaryGetInfoPointer(view,&info);
757:       PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
758:       PetscFPrintf(comm,info,"#$$ Set.%s = PetscBinaryRead(fd);\n",bag->bagname);
759:       PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
760:     }
761:   }
762:   return(0);
763: }

765: /*@C
766:    PetscBagLoad - Loads a bag of values from a binary file

768:    Collective on PetscViewer

770:    Input Parameter:
771: +  viewer - file to load values from
772: -  bag - the bag of values

774:    Notes: You must have created and registered all the fields in the bag before loading into it.

776:    Notes:
777:    Level: beginner

779: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagView(), PetscBagGetData()
780:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
781:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

783: @*/
784: PetscErrorCode  PetscBagLoad(PetscViewer view,PetscBag bag)
785: {
787:   PetscBool      isbinary;
788:   PetscInt       classid,bagcount,i,dtype,msize,offset,deprecatedbagsize;
789:   char           name[PETSC_BAG_NAME_LENGTH],help[PETSC_BAG_HELP_LENGTH],**list;
790:   PetscBagItem   nitem;
791:   MPI_Comm       comm;
792:   PetscMPIInt    flag;

795:   PetscObjectGetComm((PetscObject)view,&comm);
796:   MPI_Comm_compare(comm,bag->bagcomm,&flag);
797:   if (flag != MPI_CONGRUENT && flag != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"Different communicators in the viewer and bag"); \
798:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERBINARY,&isbinary);
799:   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for this viewer type");

801:   PetscViewerBinaryRead(view,&classid,1,NULL,PETSC_INT);
802:   if (classid != PETSC_BAG_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not PetscBag next in binary file");
803:   PetscViewerBinaryRead(view,&deprecatedbagsize,1,NULL,PETSC_INT);
804:   PetscViewerBinaryRead(view,&bagcount,1,NULL,PETSC_INT);
805:   if (bagcount != bag->count) SETERRQ2(comm,PETSC_ERR_ARG_INCOMP,"Bag in file has different number of entries %d then passed in bag %d\n",(int)bagcount,(int)bag->count);
806:   PetscViewerBinaryRead(view,bag->bagname,PETSC_BAG_NAME_LENGTH,NULL,PETSC_CHAR);
807:   PetscViewerBinaryRead(view,bag->baghelp,PETSC_BAG_HELP_LENGTH,NULL,PETSC_CHAR);

809:   nitem = bag->bagitems;
810:   for (i=0; i<bagcount; i++) {
811:     PetscViewerBinaryRead(view,&offset,1,NULL,PETSC_INT);
812:     /* ignore the offset in the file */
813:     PetscViewerBinaryRead(view,&dtype,1,NULL,PETSC_INT);
814:     PetscViewerBinaryRead(view,name,PETSC_BAG_NAME_LENGTH,NULL,PETSC_CHAR);
815:     PetscViewerBinaryRead(view,help,PETSC_BAG_HELP_LENGTH,NULL,PETSC_CHAR);
816:     PetscViewerBinaryRead(view,&msize,1,NULL,PETSC_INT);

818:     if (dtype == (PetscInt) PETSC_CHAR) {
819:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_CHAR);
820:     } else if (dtype == (PetscInt) PETSC_REAL) {
821:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_REAL);
822:     } else if (dtype == (PetscInt) PETSC_SCALAR) {
823:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,1,NULL,PETSC_SCALAR);
824:     } else if (dtype == (PetscInt) PETSC_INT) {
825:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_INT);
826:     } else if (dtype == (PetscInt) PETSC_BOOL) {
827:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_BOOL);
828:     } else if (dtype == (PetscInt) PETSC_ENUM) {
829:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,1,NULL,PETSC_ENUM);
830:       PetscViewerBinaryReadStringArray(view,&list);
831:       /* don't need to save list because it is already registered in the bag */
832:       PetscFree(list);
833:     }
834:     nitem = nitem->next;
835:   }
836:   return(0);
837: }

839: /*@C
840:     PetscBagCreate - Create a bag of values

842:   Collective on MPI_Comm

844:   Level: Intermediate

846:   Input Parameters:
847: +  comm - communicator to share bag
848: -  bagsize - size of the C structure holding the values

850:   Output Parameter:
851: .   bag - the bag of values

853:    Notes:
854:       The size of the A struct must be small enough to fit in a PetscInt; by default
855:       PetscInt is 4 bytes; this means a bag cannot be larger than 2 gigabytes in length.
856:       The warning about casting to a shorter length can be ignored below unless your A struct is too large

858: .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
859:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
860:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
861: @*/
862: PetscErrorCode PetscBagCreate(MPI_Comm comm, size_t bagsize, PetscBag *bag)
863: {
865:   size_t         totalsize = bagsize+sizeof(struct _n_PetscBag)+sizeof(PetscScalar);

868:   PetscInfo1(NULL,"Creating Bag with total size %d\n",(int)totalsize);
869:   PetscMalloc(totalsize,bag);
870:   PetscMemzero(*bag,bagsize+sizeof(struct _n_PetscBag)+sizeof(PetscScalar));

872:   (*bag)->bagsize        = bagsize+sizeof(struct _n_PetscBag)+sizeof(PetscScalar);
873:   (*bag)->bagcomm        = comm;
874:   (*bag)->bagprefix      = NULL;
875:   (*bag)->structlocation = (void*)(((char*)(*bag)) + sizeof(PetscScalar)*(sizeof(struct _n_PetscBag)/sizeof(PetscScalar)) + sizeof(PetscScalar));
876:   return(0);
877: }

879: /*@C
880:     PetscBagSetName - Sets the name of a bag of values

882:   Not Collective

884:   Level: Intermediate

886:   Input Parameters:
887: +   bag - the bag of values
888: .   name - the name assigned to the bag
889: -   help - help message for bag

891: .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
892:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
893:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
894: @*/

896: PetscErrorCode PetscBagSetName(PetscBag bag, const char *name, const char *help)
897: {

901:   PetscStrncpy(bag->bagname,name,PETSC_BAG_NAME_LENGTH-1);
902:   PetscStrncpy(bag->baghelp,help,PETSC_BAG_HELP_LENGTH-1);
903:   return(0);
904: }


907: /*@C
908:     PetscBagGetName - Gets the name of a bag of values

910:   Not Collective

912:   Level: Intermediate

914:   Input Parameter:
915: .   bag - the bag of values

917:   Output Parameter:
918: .   name - the name assigned to the bag

920: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
921:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
922:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
923: @*/
924: PetscErrorCode PetscBagGetName(PetscBag bag, char **name)
925: {
927:   *name = bag->bagname;
928:   return(0);
929: }

931: /*@C
932:     PetscBagGetData - Gives back the user - access to memory that
933:     should be used for storing user-data-structure

935:   Not Collective

937:   Level: Intermediate

939:   Input Parameter:
940: .   bag - the bag of values

942:   Output Parameter:
943: .   data - pointer to memory that will have user-data-structure

945: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad()
946:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
947:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
948: @*/
949: PetscErrorCode PetscBagGetData(PetscBag bag, void **data)
950: {
952:   *data = bag->structlocation;
953:   return(0);
954: }

956: /*@C
957:   PetscBagSetOptionsPrefix - Sets the prefix used for searching for all
958:   PetscBag items in the options database.

960:   Logically collective on Bag.

962:   Level: Intermediate

964:   Input Parameters:
965: +   bag - the bag of values
966: -   prefix - the prefix to prepend all Bag item names with.

968:   NOTES: Must be called prior to registering any of the bag items.

970: .seealso: PetscBag, PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
971:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
972: @*/

974: PetscErrorCode PetscBagSetOptionsPrefix(PetscBag bag, const char pre[])
975: {

979:   if (!pre) {
980:     PetscFree(bag->bagprefix);
981:   } else {
982:     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
983:     PetscFree(bag->bagprefix);
984:     PetscStrallocpy(pre,&(bag->bagprefix));
985:   }
986:   return(0);
987: }

989: /*@C
990:   PetscBagGetNames - Get the names of all entries in the bag

992:   Not collective

994:   Input Parameters:
995: + bag   - the bag of values
996: - names - array of the correct size to hold names

998:   Output Parameter:
999: . names - array of char pointers for names

1001:   Level: intermediate

1003: .seealso: PetscBag, PetscBagGetName(), PetscBagSetName(), PetscBagCreate(), PetscBagGetData()
1004:           PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar(), PetscBagRegisterEnum()
1005: @*/
1006: PetscErrorCode PetscBagGetNames(PetscBag bag, const char *names[])
1007: {
1008:   PetscBagItem nitem = bag->bagitems;
1009:   PetscInt     n;

1012:   for (n = 0; nitem; ++n, nitem = nitem->next) {names[n] = nitem->name;}
1013:   return(0);
1014: }