Actual source code: bag.c

petsc-3.6.4 2016-04-12
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,"-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(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,"-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(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,"-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(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,"-help",&printhelp);
226:   if (printhelp) {
227:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%d>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,mdefault,help);
228:   }
229:   PetscOptionsGetInt(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,"-help",&printhelp);
277:   if (printhelp) {
278:     (*PetscHelpPrintf)(bag->bagcomm,"  -%s%s <%d>: %s \n",bag->bagprefix ? bag->bagprefix : "",name,odefault,help);
279:   }
280:   PetscOptionsGetInt(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,"-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(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,"-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(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,"-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(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,"-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(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,"-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(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:         PetscViewerASCIIPrintf(view,"  %s = %g + %gi; %s\n",nitem->name,(double)PetscRealPart(value),(double)PetscImaginaryPart(value),nitem->help);
720: #else
721:         PetscViewerASCIIPrintf(view,"  %s = %g; %s\n",nitem->name,(double)value,nitem->help);
722: #endif
723:       } else if (nitem->dtype == PETSC_INT) {
724:         PetscInt i,*value = (PetscInt*)(((char*)bag) + nitem->offset);
725:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
726:         for (i=0; i<nitem->msize; i++) {
727:           PetscViewerASCIIPrintf(view,"%D ",value[i]);
728:         }
729:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
730:       } else if (nitem->dtype == PETSC_BOOL) {
731:         PetscBool  *value = (PetscBool*)(((char*)bag) + nitem->offset);
732:         PetscInt  i;
733:          /* some Fortran compilers use -1 as boolean */
734:         PetscViewerASCIIPrintf(view,"  %s = ",nitem->name);
735:         for (i=0; i<nitem->msize; i++) {
736:           if (((int) value[i]) == -1) value[i] = PETSC_TRUE;
737:           /* the checks here with != PETSC_FALSE and PETSC_TRUE is a special case; here we truly demand that the value be 0 or 1 */
738:           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);
739:           PetscViewerASCIIPrintf(view," %s",PetscBools[value[i]]);
740:         }
741:         PetscViewerASCIIPrintf(view,"; %s\n",nitem->help);
742:       } else if (nitem->dtype == PETSC_ENUM) {
743:         PetscEnum value = *(PetscEnum*)(((char*)bag) + nitem->offset);
744:         PetscInt  i     = 0;
745:         while (nitem->list[i++]) ;
746:         PetscViewerASCIIPrintf(view,"  %s = %s; (%s) %s\n",nitem->name,nitem->list[value],nitem->list[i-3],nitem->help);
747:       }
748:       nitem = nitem->next;
749:     }
750:   } else if (isbinary) {
751:     PetscInt          classid           = PETSC_BAG_FILE_CLASSID, dtype;
752:     PetscInt          deprecatedbagsize = 0;
753:     PetscViewerFormat format;
754:     PetscViewerBinaryWrite(view,&classid,1,PETSC_INT,PETSC_TRUE);
755:     PetscViewerBinaryWrite(view,&deprecatedbagsize,1,PETSC_INT,PETSC_FALSE);
756:     PetscViewerBinaryWrite(view,&bag->count,1,PETSC_INT,PETSC_FALSE);
757:     PetscViewerBinaryWrite(view,bag->bagname,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);
758:     PetscViewerBinaryWrite(view,bag->baghelp,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);
759:     while (nitem) {
760:       PetscViewerBinaryWrite(view,&nitem->offset,1,PETSC_INT,PETSC_FALSE);
761:       dtype = (PetscInt)nitem->dtype;
762:       PetscViewerBinaryWrite(view,&dtype,1,PETSC_INT,PETSC_FALSE);
763:       PetscViewerBinaryWrite(view,nitem->name,PETSC_BAG_NAME_LENGTH,PETSC_CHAR,PETSC_FALSE);
764:       PetscViewerBinaryWrite(view,nitem->help,PETSC_BAG_HELP_LENGTH,PETSC_CHAR,PETSC_FALSE);
765:       PetscViewerBinaryWrite(view,&nitem->msize,1,PETSC_INT,PETSC_FALSE);
766:       /* some Fortran compilers use -1 as boolean */
767:       if (dtype == PETSC_BOOL && ((*(int*) (((char*)bag) + nitem->offset) == -1))) *(int*) (((char*)bag) + nitem->offset) = PETSC_TRUE;

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

791: /*@C
792:    PetscBagLoad - Loads a bag of values from a binary file

794:    Collective on PetscViewer

796:    Input Parameter:
797: +  viewer - file to load values from
798: -  bag - the bag of values

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

802:    Notes:
803:    Level: beginner

805: .seealso: PetscBag, PetscBagSetName(), PetscBagDestroy(), PetscBagView(), PetscBagGetData()
806:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
807:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagGetName(), PetscBagRegisterEnum()

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

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

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

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

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

867: /*@
868:     PetscBagCreate - Create a bag of values

870:   Collective on MPI_Comm

872:   Level: Intermediate

874:   Input Parameters:
875: +  comm - communicator to share bag
876: -  bagsize - size of the C structure holding the values

878:   Output Parameter:
879: .   bag - the bag of values

881:    Notes:
882:       The size of the A 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 A struct is too large

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

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

900:   (*bag)->bagsize        = bagsize+sizeof(struct _n_PetscBag)+sizeof(PetscScalar);
901:   (*bag)->bagcomm        = comm;
902:   (*bag)->bagprefix      = NULL;
903:   (*bag)->structlocation = (void*)(((char*)(*bag)) + sizeof(PetscScalar)*(sizeof(struct _n_PetscBag)/sizeof(PetscScalar)) + sizeof(PetscScalar));
904:   return(0);
905: }

909: /*@C
910:     PetscBagSetName - Sets the name of a bag of values

912:   Not Collective

914:   Level: Intermediate

916:   Input Parameters:
917: +   bag - the bag of values
918: .   name - the name assigned to the bag
919: -   help - help message for bag

921: .seealso: PetscBag, PetscBagGetName(), PetscBagView(), PetscBagLoad(), PetscBagGetData()
922:            PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
923:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
924: @*/

926: PetscErrorCode PetscBagSetName(PetscBag bag, const char *name, const char *help)
927: {

931:   PetscStrncpy(bag->bagname,name,PETSC_BAG_NAME_LENGTH-1);
932:   PetscStrncpy(bag->baghelp,help,PETSC_BAG_HELP_LENGTH-1);
933:   return(0);
934: }


939: /*@C
940:     PetscBagGetName - Gets the name of a bag of values

942:   Not Collective

944:   Level: Intermediate

946:   Input Parameter:
947: .   bag - the bag of values

949:   Output Parameter:
950: .   name - the name assigned to the bag

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

965: /*@C
966:     PetscBagGetData - Gives back the user - access to memory that
967:     should be used for storing user-data-structure

969:   Not Collective

971:   Level: Intermediate

973:   Input Parameter:
974: .   bag - the bag of values

976:   Output Parameter:
977: .   data - pointer to memory that will have user-data-structure

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

992: /*@C
993:   PetscBagSetOptionsPrefix - Sets the prefix used for searching for all
994:   PetscBag items in the options database.

996:   Logically collective on Bag.

998:   Level: Intermediate

1000:   Input Parameters:
1001: +   bag - the bag of values
1002: -   prefix - the prefix to prepend all Bag item names with.

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

1006: .seealso: PetscBag, PetscBagRegisterReal(), PetscBagRegisterInt(), PetscBagRegisterBool(), PetscBagRegisterScalar()
1007:            PetscBagSetFromOptions(), PetscBagCreate(), PetscBagDestroy(), PetscBagRegisterEnum()
1008: @*/

1010: PetscErrorCode PetscBagSetOptionsPrefix(PetscBag bag, const char pre[])
1011: {

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

1027: /*@C
1028:   PetscBagGetNames - Get the names of all entries in the bag

1030:   Not collective

1032:   Input Parameters:
1033: + bag   - the bag of values
1034: - names - array of the correct size to hold names

1036:   Output Parameter:
1037: . names - array of char pointers for names

1039:   Level: intermediate

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

1050:   for (n = 0; nitem; ++n, nitem = nitem->next) {names[n] = nitem->name;}
1051:   return(0);
1052: }