Actual source code: bag.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: #include <petsc/private/bagimpl.h>     /*I  "petscbag.h"   I*/
  3: #include <petscviewer.h>

  7: /*
  8:       Adds item to the linked list in a bag
  9: */
 10: static PetscErrorCode PetscBagRegister_Private(PetscBag bag,PetscBagItem item,const char *name,const char *help)
 11: {

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

 31: /*@C
 32:    PetscBagRegisterEnum - add an enum value to the bag

 34:    Logically Collective on PetscBag

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

 43:    Level: beginner

 45: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
 46:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
 47:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName()

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

 59:   nname[0] = '-';
 60:   nname[1] = 0;
 61:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
 62:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
 63:   if (printhelp) {
 64:     while (list[i++]) ;
 65:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%s>: (%s) %s (choose one of) ",bag->bagprefix ? bag->bagprefix : "",name,list[mdefault],list[i-3],help);
 66:     for (i=0; list[i+2]; i++) {
 67:       (*PetscHelpPrintf)(bag->bagcomm," %s",list[i]);
 68:     }
 69:     (*PetscHelpPrintf)(bag->bagcomm,"\n");
 70:   }
 71:   PetscOptionsGetEnum(NULL,bag->bagprefix,nname,list,&mdefault,NULL);

 73:   PetscNew(&item);
 74:   item->dtype  = PETSC_ENUM;
 75:   item->offset = ((char*)addr) - ((char*)bag);
 76:   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);
 77:   item->next        = 0;
 78:   item->msize       = 1;
 79:   PetscStrArrayallocpy(list,(char***)&item->list);
 80:   *(PetscEnum*)addr = mdefault;
 81:   PetscBagRegister_Private(bag,item,name,help);
 82:   return(0);
 83: }

 87: /*@C
 88:    PetscBagRegisterIntArray - add an integer value to the bag

 90:    Logically Collective on PetscBag

 92:    Input Parameter:
 93: +  bag - the bag of values
 94: .  addr - location of integer in struct
 95: .  msize - number of entries in array
 96: .  name - name of the integer array
 97: -  help - longer string with more information about the value

 99:    Level: beginner

101: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
102:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
103:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

105: @*/
106: PetscErrorCode PetscBagRegisterIntArray(PetscBag bag,void *addr,PetscInt msize, const char *name, const char *help)
107: {
109:   PetscBagItem   item;
110:   char           nname[PETSC_BAG_NAME_LENGTH+1];
111:   PetscBool      printhelp;
112:   PetscInt       i,tmp = msize;

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

129:   PetscNew(&item);
130:   item->dtype  = PETSC_INT;
131:   item->offset = ((char*)addr) - ((char*)bag);
132:   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);
133:   item->next  = 0;
134:   item->msize = msize;
135:   PetscBagRegister_Private(bag,item,name,help);
136:   return(0);
137: }

141: /*@C
142:    PetscBagRegisterRealArray - add an real array to the bag

144:    Logically Collective on PetscBag

146:    Input Parameter:
147: +  bag - the bag of values
148: .  addr - location of real array in struct
149: .  msize - number of entries in array
150: .  name - name of the integer array
151: -  help - longer string with more information about the value

153:    Level: beginner

155: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
156:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
157:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

159: @*/
160: PetscErrorCode PetscBagRegisterRealArray(PetscBag bag,void *addr,PetscInt msize, const char *name, const char *help)
161: {
163:   PetscBagItem   item;
164:   char           nname[PETSC_BAG_NAME_LENGTH+1];
165:   PetscBool      printhelp;
166:   PetscInt       i,tmp = msize;

169:   /* PetscMemzero(addr,msize*sizeof(PetscInt));*/
170:   nname[0] = '-';
171:   nname[1] = 0;
172:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
173:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
174:   if (printhelp) {
175:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <",bag->bagprefix ? bag->bagprefix : "",name);
176:     for (i=0; i<msize; i++) {
177:       (*PetscHelpPrintf)(bag->bagcomm,"%g ",(double)*((PetscReal*)addr)+i);
178:     }
179:     (*PetscHelpPrintf)(bag->bagcomm,">: %s \n",help);
180:   }
181:   PetscOptionsGetRealArray(NULL,bag->bagprefix,nname,(PetscReal*)addr,&tmp,NULL);

183:   PetscNew(&item);
184:   item->dtype  = PETSC_REAL;
185:   item->offset = ((char*)addr) - ((char*)bag);
186:   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);
187:   item->next  = 0;
188:   item->msize = msize;
189:   PetscBagRegister_Private(bag,item,name,help);
190:   return(0);
191: }

195: /*@C
196:    PetscBagRegisterInt - add an integer value to the bag

198:    Logically Collective on PetscBag

200:    Input Parameter:
201: +  bag - the bag of values
202: .  addr - location of integer in struct
203: .  mdefault - the initial value
204: .  name - name of the integer
205: -  help - longer string with more information about the value

207:    Level: beginner

209: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
210:            PetscBagRegister64bitInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
211:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

213: @*/
214: PetscErrorCode PetscBagRegisterInt(PetscBag bag,void *addr,PetscInt mdefault,const char *name,const char *help)
215: {
217:   PetscBagItem   item;
218:   char           nname[PETSC_BAG_NAME_LENGTH+1];
219:   PetscBool      printhelp;

222:   nname[0] = '-';
223:   nname[1] = 0;
224:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
225:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
226:   if (printhelp) {
227:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%d>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,mdefault,help);
228:   }
229:   PetscOptionsGetInt(NULL,bag->bagprefix,nname,&mdefault,NULL);

231:   PetscNew(&item);
232:   item->dtype  = PETSC_INT;
233:   item->offset = ((char*)addr) - ((char*)bag);
234:   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);
235:   item->next       = 0;
236:   item->msize      = 1;
237:   *(PetscInt*)addr = mdefault;
238:   PetscBagRegister_Private(bag,item,name,help);
239:   return(0);
240: }

244: /*@C
245:    PetscBagRegister64bitInt - add an integer value to the bag

247:    Logically Collective on PetscBag

249:    Input Parameter:
250: +  bag - the bag of values
251: .  addr - location of integer in struct
252: .  mdefault - the initial value
253: .  name - name of the integer
254: -  help - longer string with more information about the value

256:    Level: beginner

258: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
259:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
260:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

262: @*/
263: PetscErrorCode PetscBagRegister64bitInt(PetscBag bag,void *addr,Petsc64bitInt mdefault,const char *name,const char *help)
264: {
266:   PetscBagItem   item;
267:   char           nname[PETSC_BAG_NAME_LENGTH+1];
268:   PetscBool      printhelp;
269:   PetscInt       odefault = (PetscInt)mdefault;
270:   PetscBool      flg;
271: 
273:   nname[0] = '-';
274:   nname[1] = 0;
275:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
276:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
277:   if (printhelp) {
278:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%d>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,odefault,help);
279:   }
280:   PetscOptionsGetInt(NULL,bag->bagprefix,nname,&odefault,&flg);
281:   if (flg) mdefault = (Petsc64bitInt)odefault;

283:   PetscNew(&item);
284:   item->dtype  = PETSC_INT;
285:   item->offset = ((char*)addr) - ((char*)bag);
286:   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);
287:   item->next       = 0;
288:   item->msize      = 1;
289:   *(Petsc64bitInt*)addr = mdefault;
290:   PetscBagRegister_Private(bag,item,name,help);
291:   return(0);
292: }

296: /*@C
297:    PetscBagRegisterBoolArray - add a n logical values to the bag

299:    Logically Collective on PetscBag

301:    Input Parameter:
302: +  bag - the bag of values
303: .  addr - location of boolean array in struct
304: .  msize - number of entries in array
305: .  name - name of the boolean array
306: -  help - longer string with more information about the value

308:    Level: beginner

310: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
311:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
312:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

314: @*/
315: PetscErrorCode PetscBagRegisterBoolArray(PetscBag bag,void *addr,PetscInt msize, const char* name, const char* help)
316: {
318:   PetscBagItem   item;
319:   char           nname[PETSC_BAG_NAME_LENGTH+1];
320:   PetscBool      printhelp;
321:   PetscInt       i,tmp = msize;

324:   /* PetscMemzero(addr,msize*sizeof(PetscInt));*/
325:   nname[0] = '-';
326:   nname[1] = 0;
327:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
328:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
329:   if (printhelp) {
330:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <",bag->bagprefix?bag->bagprefix:"",name);
331:     for (i=0; i<msize; i++) {
332:       (*PetscHelpPrintf)(bag->bagcomm,"%D ",*((PetscInt*)addr)+i);
333:     }
334:     (*PetscHelpPrintf)(bag->bagcomm,">: %s \n",help);
335:   }
336:   PetscOptionsGetBoolArray(NULL,bag->bagprefix,nname,(PetscBool*)addr,&tmp,NULL);

338:   PetscNew(&item);
339:   item->dtype  = PETSC_BOOL;
340:   item->offset = ((char*)addr) - ((char*)bag);
341:   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);
342:   item->next   = 0;
343:   item->msize  = msize;
344:   PetscBagRegister_Private(bag,item,name,help);
345:   return(0);
346: }

350: /*@C
351:    PetscBagRegisterString - add a string value to the bag

353:    Logically Collective on PetscBag

355:    Input Parameter:
356: +  bag - the bag of values
357: .  addr - location of start of string in struct
358: .  msize - length of the string space in the struct
359: .  mdefault - the initial value
360: .  name - name of the string
361: -  help - longer string with more information about the value

363:    Level: beginner

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

367: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
368:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
369:            PetscBagSetFromOptions(),PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

371: @*/
372: PetscErrorCode PetscBagRegisterString(PetscBag bag,void *addr,PetscInt msize,const char* mdefault,const char* name,const char* help)
373: {
375:   PetscBagItem   item;
376:   char           nname[PETSC_BAG_NAME_LENGTH+1];
377:   PetscBool      printhelp;

380:   nname[0] = '-';
381:   nname[1] = 0;
382:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
383:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
384:   if (printhelp) {
385:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%s>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,mdefault,help);
386:   }

388:   PetscNew(&item);
389:   item->dtype  = PETSC_CHAR;
390:   item->offset = ((char*)addr) - ((char*)bag);
391:   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);
392:   item->next  = 0;
393:   item->msize = msize;
394:   if (mdefault != (char*)addr) {
395:     PetscStrncpy((char*)addr,mdefault,msize-1);
396:   }
397:   PetscOptionsGetString(NULL,bag->bagprefix,nname,(char*)addr,msize,NULL);
398:   PetscBagRegister_Private(bag,item,name,help);
399:   return(0);
400: }

404: /*@C
405:    PetscBagRegisterReal - add a real value to the bag

407:    Logically Collective on PetscBag

409:    Input Parameter:
410: +  bag - the bag of values
411: .  addr - location of double in struct
412: .  mdefault - the initial value
413: .  name - name of the variable
414: -  help - longer string with more information about the value

416:    Level: beginner

418: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
419:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
420:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

422: @*/
423: PetscErrorCode PetscBagRegisterReal(PetscBag bag,void *addr,PetscReal mdefault, const char *name, const char *help)
424: {
426:   PetscBagItem   item;
427:   char           nname[PETSC_BAG_NAME_LENGTH+1];
428:   PetscBool      printhelp;

431:   nname[0] = '-';
432:   nname[1] = 0;
433:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
434:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
435:   if (printhelp) {
436:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%g>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,(double)mdefault,help);
437:   }
438:   PetscOptionsGetReal(NULL,bag->bagprefix,nname,&mdefault,NULL);

440:   PetscNew(&item);
441:   item->dtype  = PETSC_REAL;
442:   item->offset = ((char*)addr) - ((char*)bag);
443:   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);
444:   item->next        = 0;
445:   item->msize       = 1;
446:   *(PetscReal*)addr = mdefault;
447:   PetscBagRegister_Private(bag,item,name,help);
448:   return(0);
449: }

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

456:    Logically Collective on PetscBag

458:    Input Parameter:
459: +  bag - the bag of values
460: .  addr - location of scalar in struct
461: .  mdefault - the initial value
462: .  name - name of the variable
463: -  help - longer string with more information about the value


466:    Level: beginner

468: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
469:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
470:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

472: @*/
473: PetscErrorCode PetscBagRegisterScalar(PetscBag bag,void *addr,PetscScalar mdefault,const char *name,const char *help)
474: {
476:   PetscBagItem   item;
477:   char           nname[PETSC_BAG_NAME_LENGTH+1];
478:   PetscBool      printhelp;

481:   nname[0] = '-';
482:   nname[1] = 0;
483:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
484:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
485:   if (printhelp) {
486:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%g + %gi>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,(double)PetscRealPart(mdefault),(double)PetscImaginaryPart(mdefault),help);
487:   }
488:   PetscOptionsGetScalar(NULL,bag->bagprefix,nname,&mdefault,NULL);

490:   PetscNew(&item);
491:   item->dtype  = PETSC_SCALAR;
492:   item->offset = ((char*)addr) - ((char*)bag);
493:   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);
494:   item->next          = 0;
495:   item->msize         = 1;
496:   *(PetscScalar*)addr = mdefault;
497:   PetscBagRegister_Private(bag,item,name,help);
498:   return(0);
499: }

503: /*@C
504:    PetscBagRegisterBool - add a logical value to the bag

506:    Logically Collective on PetscBag

508:    Input Parameter:
509: +  bag - the bag of values
510: .  addr - location of logical in struct
511: .  mdefault - the initial value
512: .  name - name of the variable
513: -  help - longer string with more information about the value


516:    Level: beginner

518: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
519:            PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
520:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

522: @*/
523: PetscErrorCode PetscBagRegisterBool(PetscBag bag,void *addr,PetscBool mdefault,const char *name,const char *help)
524: {
526:   PetscBagItem   item;
527:   char           nname[PETSC_BAG_NAME_LENGTH+1];
528:   PetscBool      printhelp;

531:   /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
532:   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);
533:   nname[0] = '-';
534:   nname[1] = 0;
535:   PetscStrncat(nname,name,PETSC_BAG_NAME_LENGTH-1);
536:   PetscOptionsHasName(NULL,NULL,"-help",&printhelp);
537:   if (printhelp) {
538:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%s>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,PetscBools[mdefault],help);
539:   }
540:   PetscOptionsGetBool(NULL,bag->bagprefix,nname,&mdefault,NULL);

542:   PetscNew(&item);
543:   item->dtype  = PETSC_BOOL;
544:   item->offset = ((char*)addr) - ((char*)bag);
545:   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);
546:   item->next        = 0;
547:   item->msize       = 1;
548:   *(PetscBool*)addr = mdefault;
549:   PetscBagRegister_Private(bag,item,name,help);
550:   return(0);
551: }

555: /*@C
556:    PetscBagDestroy - Destroys a bag values

558:    Collective on PetscBag

560:    Input Parameter:
561: .  bag - the bag of values

563:    Level: beginner

565: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
566:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
567:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

569: @*/
570: PetscErrorCode  PetscBagDestroy(PetscBag *bag)
571: {
573:   PetscBagItem   nitem = (*bag)->bagitems,item;

576:   while (nitem) {
577:     item = nitem->next;
578:     if (nitem->list) {
579:       PetscStrArrayDestroy(&nitem->list);
580:     }
581:     PetscFree(nitem);
582:     nitem = item;
583:   }
584:   if ((*bag)->bagprefix) { PetscFree((*bag)->bagprefix); }
585:   PetscFree(*bag);
586:   return(0);
587: }

591: /*@
592:    PetscBagSetFromOptions - Allows setting options from a bag

594:    Collective on PetscBag

596:    Input Parameter:
597: .  bag - the bag of values

599:    Level: beginner

601: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagLoad(), PetscBagGetData()
602:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
603:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagView(), PetscBagRegisterEnum()

605: @*/
606: PetscErrorCode  PetscBagSetFromOptions(PetscBag bag)
607: {
609:   PetscBagItem   nitem = bag->bagitems;
610:   char           name[PETSC_BAG_NAME_LENGTH+1],helpname[PETSC_BAG_NAME_LENGTH+PETSC_BAG_HELP_LENGTH+3];
611:   PetscInt       n;

614:   PetscStrcpy(helpname,bag->bagname);
615:   PetscStrcat(helpname," ");
616:   PetscStrcat(helpname,bag->baghelp);
617:   PetscOptionsBegin(bag->bagcomm,bag->bagprefix,helpname,0);
618:   while (nitem) {
619:     name[0] = '-';
620:     name[1] = 0;
621:     PetscStrcat(name,nitem->name);
622:     if (nitem->dtype == PETSC_CHAR) {   /* special handling for fortran required? [due to space padding vs null termination] */
623:       char *value = (char*)(((char*)bag) + nitem->offset);
624:       PetscOptionsString(name,nitem->help,"",value,value,nitem->msize,NULL);
625:     } else if (nitem->dtype == PETSC_REAL) {
626:       PetscReal *value = (PetscReal*)(((char*)bag) + nitem->offset);
627:       if (nitem->msize == 1) {
628:         PetscOptionsReal(name,nitem->help,"",*value,value,NULL);
629:       } else {
630:         n    = nitem->msize;
631:         PetscOptionsRealArray(name,nitem->help,"",value,&n,NULL);
632:       }
633:     } else if (nitem->dtype == PETSC_SCALAR) {
634:       PetscScalar *value = (PetscScalar*)(((char*)bag) + nitem->offset);
635:       PetscOptionsScalar(name,nitem->help,"",*value,value,NULL);
636:     } else if (nitem->dtype == PETSC_INT) {
637:       PetscInt *value = (PetscInt*)(((char*)bag) + nitem->offset);
638:       if (nitem->msize == 1) {
639:         PetscOptionsInt(name,nitem->help,"",*value,value,NULL);
640:       } else {
641:         n    = nitem->msize;
642:         PetscOptionsIntArray(name,nitem->help,"",value,&n,NULL);
643:       }
644:     } else if (nitem->dtype == PETSC_ENUM) {
645:       PetscEnum *value = (PetscEnum*)(((char*)bag) + nitem->offset);
646:       PetscInt  i      = 0;
647:       while (nitem->list[i++]) ;
648:       PetscOptionsEnum(name,nitem->help,nitem->list[i-3],(const char*const*)nitem->list,*value,value,NULL);
649:     } else if (nitem->dtype == PETSC_BOOL) {
650:       PetscBool *value = (PetscBool*)(((char*)bag) + nitem->offset);
651:       if (nitem->msize == 1) {
652:         PetscOptionsBool(name,nitem->help,"",*value,value,NULL);
653:       } else {
654:         n = nitem->msize;
655:         PetscOptionsBoolArray(name,nitem->help,"",value,&n,NULL);
656:       }
657:     }
658:     nitem = nitem->next;
659:   }
660:   PetscOptionsEnd();
661:   return(0);
662: }

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

669:    Collective on PetscBag

671:    Input Parameter:
672: +  bag - the bag of values
673: -  viewer - location to view the values

675:    Level: beginner

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

681: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagLoad(), PetscBagGetData()
682:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar(), PetscBagRegisterEnum()
683:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName()

685: @*/
686: PetscErrorCode  PetscBagView(PetscBag bag,PetscViewer view)
687: {
688:   PetscBool      isascii,isbinary;
690:   PetscBagItem   nitem = bag->bagitems;

693:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERASCII,&isascii);
694:   PetscObjectTypeCompare((PetscObject)view,PETSCVIEWERBINARY,&isbinary);
695:   if (isascii) {
696:     if (bag->bagprefix) {
697:       PetscViewerASCIIPrintf(view,"PetscBag Object:  %s (%s) %s\n",bag->bagname,bag->bagprefix,bag->baghelp);
698:     } else {
699:       PetscViewerASCIIPrintf(view,"PetscBag Object:  %s %s\n",bag->bagname,bag->baghelp);
700:     }
701:     while (nitem) {
702:       if (nitem->dtype == PETSC_CHAR) {
703:         char *value = (char*)(((char*)bag) + nitem->offset);
704:         char tmp    = value[nitem->msize-1]; /* special handling for fortran chars wihout null terminator */
705:         value[nitem->msize-1] =0;
706:         PetscViewerASCIIPrintf(view,"  %s = %s; %s\n",nitem->name,value,nitem->help);
707:         value[nitem->msize-1] = tmp;
708:       } else if (nitem->dtype == PETSC_REAL) {
709:         PetscReal *value = (PetscReal*)(((char*)bag) + nitem->offset);
710:         PetscInt  i;
711:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
712:         for (i=0; i<nitem->msize; i++) {
713:           PetscViewerASCIIPrintf(view,"%g ",(double)value[i]);
714:         }
715:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
716:       } else if (nitem->dtype == PETSC_SCALAR) {
717:         PetscScalar value = *(PetscScalar*)(((char*)bag) + nitem->offset);
718: #if defined(PETSC_USE_COMPLEX)
719:         if ((double)PetscImaginaryPart(value)) {
720:           PetscViewerASCIIPrintf(view,"  %s = %g + %gi; %s\n",nitem->name,(double)PetscRealPart(value),(double)PetscImaginaryPart(value),nitem->help);
721:         } else {
722:           PetscViewerASCIIPrintf(view,"  %s = %g; %s\n",nitem->name,(double)PetscRealPart(value),nitem->help);
723:         }
724: #else
725:         PetscViewerASCIIPrintf(view,"  %s = %g; %s\n",nitem->name,(double)value,nitem->help);
726: #endif
727:       } else if (nitem->dtype == PETSC_INT) {
728:         PetscInt i,*value = (PetscInt*)(((char*)bag) + nitem->offset);
729:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
730:         for (i=0; i<nitem->msize; i++) {
731:           PetscViewerASCIIPrintf(view,"%D ",value[i]);
732:         }
733:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
734:       } else if (nitem->dtype == PETSC_BOOL) {
735:         PetscBool  *value = (PetscBool*)(((char*)bag) + nitem->offset);
736:         PetscInt  i;
737:          /* some Fortran compilers use -1 as boolean */
738:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
739:         for (i=0; i<nitem->msize; i++) {
740:           if (((int) value[i]) == -1) value[i] = PETSC_TRUE;
741:           /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
742:           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);
743:           PetscViewerASCIIPrintf(view," %s",PetscBools[value[i]]);
744:         }
745:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
746:       } else if (nitem->dtype == PETSC_ENUM) {
747:         PetscEnum value = *(PetscEnum*)(((char*)bag) + nitem->offset);
748:         PetscInt  i     = 0;
749:         while (nitem->list[i++]) ;
750:         PetscViewerASCIIPrintf(view,"  %s = %s; (%s) %s\n",nitem->name,nitem->list[value],nitem->list[i-3],nitem->help);
751:       }
752:       nitem = nitem->next;
753:     }
754:   } else if (isbinary) {
755:     PetscInt          classid           = PETSC_BAG_FILE_CLASSID, dtype;
756:     PetscInt          deprecatedbagsize = 0;
757:     PetscViewerFormat format;
758:     PetscViewerBinaryWrite(view,&classid,1,PETSC_INT,PETSC_TRUE);
759:     PetscViewerBinaryWrite(view,&deprecatedbagsize,1,PETSC_INT,PETSC_FALSE);
760:     PetscViewerBinaryWrite(view,&bag->count,1,PETSC_INT,PETSC_FALSE);
761:     PetscViewerBinaryWrite(view,bag->bagname,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);
762:     PetscViewerBinaryWrite(view,bag->baghelp,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);
763:     while (nitem) {
764:       PetscViewerBinaryWrite(view,&nitem->offset,1,PETSC_INT,PETSC_FALSE);
765:       dtype = (PetscInt)nitem->dtype;
766:       PetscViewerBinaryWrite(view,&dtype,1,PETSC_INT,PETSC_FALSE);
767:       PetscViewerBinaryWrite(view,nitem->name,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);
768:       PetscViewerBinaryWrite(view,nitem->help,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);
769:       PetscViewerBinaryWrite(view,&nitem->msize,1,PETSC_INT,PETSC_FALSE);
770:       /* some Fortran compilers use -1 as boolean */
771:       if (dtype == PETSC_BOOL && ((*(int*) (((char*)bag) + nitem->offset) == -1))) *(int*) (((char*)bag) + nitem->offset) = PETSC_TRUE;

773:       PetscViewerBinaryWrite(view,(((char*)bag) + nitem->offset),nitem->msize,nitem->dtype,PETSC_FALSE);
774:       if (dtype == PETSC_ENUM) {
775:         PetscViewerBinaryWriteStringArray(view,(char**)nitem->list);
776:       }
777:       nitem = nitem->next;
778:     }
779:     PetscViewerGetFormat(view,&format);
780:     if (format == PETSC_VIEWER_BINARY_MATLAB) {
781:       MPI_Comm comm;
782:       FILE     *info;
783:       PetscObjectGetComm((PetscObject)view,&comm);
784:       PetscViewerBinaryGetInfoPointer(view,&info);
785:       PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
786:       PetscFPrintf(comm,info,"#$$ Set.%s = PetscBinaryRead(fd);\n",bag->bagname);
787:       PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
788:     }
789:   }
790:   return(0);
791: }

795: /*@C
796:    PetscBagLoad - Loads a bag of values from a binary file

798:    Collective on PetscViewer

800:    Input Parameter:
801: +  viewer - file to load values from
802: -  bag - the bag of values

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

806:    Notes:
807:    Level: beginner

809: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagView(), PetscBagGetData()
810:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
811:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

813: @*/
814: PetscErrorCode  PetscBagLoad(PetscViewer view,PetscBag bag)
815: {
817:   PetscBool      isbinary;
818:   PetscInt       classid,bagcount,i,dtype,msize,offset,deprecatedbagsize;
819:   char           name[PETSC_BAG_NAME_LENGTH],help[PETSC_BAG_HELP_LENGTH],**list;
820:   PetscBagItem   nitem;
821:   MPI_Comm       comm;
822:   PetscMPIInt    flag;

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

831:   PetscViewerBinaryRead(view,&classid,1,NULL,PETSC_INT);
832:   if (classid != PETSC_BAG_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not PetscBag next in binary file");
833:   PetscViewerBinaryRead(view,&deprecatedbagsize,1,NULL,PETSC_INT);
834:   PetscViewerBinaryRead(view,&bagcount,1,NULL,PETSC_INT);
835:   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);
836:   PetscViewerBinaryRead(view,bag->bagname,PETSC_BAG_NAME_LENGTH,NULL,PETSC_CHAR);
837:   PetscViewerBinaryRead(view,bag->baghelp,PETSC_BAG_HELP_LENGTH,NULL,PETSC_CHAR);

839:   nitem = bag->bagitems;
840:   for (i=0; i<bagcount; i++) {
841:     PetscViewerBinaryRead(view,&offset,1,NULL,PETSC_INT);
842:     /* ignore the offset in the file */
843:     PetscViewerBinaryRead(view,&dtype,1,NULL,PETSC_INT);
844:     PetscViewerBinaryRead(view,name,PETSC_BAG_NAME_LENGTH,NULL,PETSC_CHAR);
845:     PetscViewerBinaryRead(view,help,PETSC_BAG_HELP_LENGTH,NULL,PETSC_CHAR);
846:     PetscViewerBinaryRead(view,&msize,1,NULL,PETSC_INT);

848:     if (dtype == (PetscInt) PETSC_CHAR) {
849:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_CHAR);
850:     } else if (dtype == (PetscInt) PETSC_REAL) {
851:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_REAL);
852:     } else if (dtype == (PetscInt) PETSC_SCALAR) {
853:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,1,NULL,PETSC_SCALAR);
854:     } else if (dtype == (PetscInt) PETSC_INT) {
855:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_INT);
856:     } else if (dtype == (PetscInt) PETSC_BOOL) {
857:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,msize,NULL,PETSC_BOOL);
858:     } else if (dtype == (PetscInt) PETSC_ENUM) {
859:       PetscViewerBinaryRead(view,((char*)bag)+nitem->offset,1,NULL,PETSC_ENUM);
860:       PetscViewerBinaryReadStringArray(view,&list);
861:       /* don't need to save list because it is already registered in the bag */
862:       PetscFree(list);
863:     }
864:     nitem = nitem->next;
865:   }
866:   return(0);
867: }

871: /*@
872:     PetscBagCreate - Create a bag of values

874:   Collective on MPI_Comm

876:   Level: Intermediate

878:   Input Parameters:
879: +  comm - communicator to share bag
880: -  bagsize - size of the C structure holding the values

882:   Output Parameter:
883: .   bag - the bag of values

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

890: .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
891:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
892:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
893: @*/
894: PetscErrorCode PetscBagCreate(MPI_Comm comm, size_t bagsize, PetscBag *bag)
895: {
897:   size_t         totalsize = bagsize+sizeof(struct _n_PetscBag)+sizeof(PetscScalar);

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

904:   (*bag)->bagsize        = bagsize+sizeof(struct _n_PetscBag)+sizeof(PetscScalar);
905:   (*bag)->bagcomm        = comm;
906:   (*bag)->bagprefix      = NULL;
907:   (*bag)->structlocation = (void*)(((char*)(*bag)) + sizeof(PetscScalar)*(sizeof(struct _n_PetscBag)/sizeof(PetscScalar)) + sizeof(PetscScalar));
908:   return(0);
909: }

913: /*@C
914:     PetscBagSetName - Sets the name of a bag of values

916:   Not Collective

918:   Level: Intermediate

920:   Input Parameters:
921: +   bag - the bag of values
922: .   name - the name assigned to the bag
923: -   help - help message for bag

925: .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
926:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
927:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
928: @*/

930: PetscErrorCode PetscBagSetName(PetscBag bag, const char *name, const char *help)
931: {

935:   PetscStrncpy(bag->bagname,name,PETSC_BAG_NAME_LENGTH-1);
936:   PetscStrncpy(bag->baghelp,help,PETSC_BAG_HELP_LENGTH-1);
937:   return(0);
938: }


943: /*@C
944:     PetscBagGetName - Gets the name of a bag of values

946:   Not Collective

948:   Level: Intermediate

950:   Input Parameter:
951: .   bag - the bag of values

953:   Output Parameter:
954: .   name - the name assigned to the bag

956: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
957:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
958:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
959: @*/
960: PetscErrorCode PetscBagGetName(PetscBag bag, char **name)
961: {
963:   *name = bag->bagname;
964:   return(0);
965: }

969: /*@C
970:     PetscBagGetData - Gives back the user - access to memory that
971:     should be used for storing user-data-structure

973:   Not Collective

975:   Level: Intermediate

977:   Input Parameter:
978: .   bag - the bag of values

980:   Output Parameter:
981: .   data - pointer to memory that will have user-data-structure

983: .seealso: PetscBag, PetscBagSetName(), PetscBagView(), PetscBagLoad()
984:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
985:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
986: @*/
987: PetscErrorCode PetscBagGetData(PetscBag bag, void **data)
988: {
990:   *data = bag->structlocation;
991:   return(0);
992: }

996: /*@C
997:   PetscBagSetOptionsPrefix - Sets the prefix used for searching for all
998:   PetscBag items in the options database.

1000:   Logically collective on Bag.

1002:   Level: Intermediate

1004:   Input Parameters:
1005: +   bag - the bag of values
1006: -   prefix - the prefix to prepend all Bag item names with.

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

1010: .seealso: PetscBag, PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
1011:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
1012: @*/

1014: PetscErrorCode PetscBagSetOptionsPrefix(PetscBag bag, const char pre[])
1015: {

1019:   if (!pre) {
1020:     PetscFree(bag->bagprefix);
1021:   } else {
1022:     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Options prefix should not begin with a hypen");
1023:     PetscFree(bag->bagprefix);
1024:     PetscStrallocpy(pre,&(bag->bagprefix));
1025:   }
1026:   return(0);
1027: }

1031: /*@C
1032:   PetscBagGetNames - Get the names of all entries in the bag

1034:   Not collective

1036:   Input Parameters:
1037: + bag   - the bag of values
1038: - names - array of the correct size to hold names

1040:   Output Parameter:
1041: . names - array of char pointers for names

1043:   Level: intermediate

1045: .seealso: PetscBag, PetscBagGetName(), PetscBagSetName(), PetscBagCreate(), PetscBagGetData()
1046:           PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar(), PetscBagRegisterEnum()
1047: @*/
1048: PetscErrorCode PetscBagGetNames(PetscBag bag, const char *names[])
1049: {
1050:   PetscBagItem nitem = bag->bagitems;
1051:   PetscInt     n;

1054:   for (n = 0; nitem; ++n, nitem = nitem->next) {names[n] = nitem->name;}
1055:   return(0);
1056: }