Actual source code: ex1.c
1: /*$Id: ex1.c,v 1.20 2001/04/10 19:37:19 bsmith Exp bsmith $*/
3: static char help[] = "Reads an AODatabase and displays the key and segment names. Runtime options include:n
4: -f input_file : Specifies input filen
5: -d : Dumps the entire databasen
6: -e : Allows addition of character string values to the databasen
7: -r : Allows removal of items from the databasenn";
9: /*T
10: Concepts: AOData^using an AOData database for grid information;
11: Processors: n
12: T*/
14: /*
15: Include "petscao.h" so that we can use the various AO and AOData routines for
16: manipulating simple parallel databases of grid (and related) information.
17: Note that this file automatically includes:
18: petsc.h - base PETSc routines
19: petscsys.h - system routines
20: petscis.h - index sets
21: */
23: #include petscao.h
25: int main(int argc,char **argv)
26: {
27: int ierr,bs,zero = 0,edited = 0;
28: char filename[256],string[256],*segname,*value,keyname[256],*ikeyname;
29: PetscViewer binary;
30: AOData aodata;
31: PetscTruth keyexists,flag;
33: /* ---------------------------------------------------------------------
34: Initialize PETSc
35: --------------------------------------------------------------------- */
37: PetscInitialize(&argc,&argv,(char *)0,help);
39: /*
40: Load the grid database and initialize graphics
41: */
42: /*
43: Load in the grid database
44: */
45: PetscOptionsGetString(PETSC_NULL,"-f",filename,256,&flag);
46: if (!flag) SETERRQ(1,"Unable to open database, must run with: ex1 -f filename");
47: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,PETSC_BINARY_RDONLY,&binary);
48: AODataLoadBasic(binary,&aodata);
49: PetscViewerDestroy(binary);
51: PetscOptionsHasName(PETSC_NULL,"-d",&flag);
52: if (!flag) {
53: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr)
54: }
55: AODataView(aodata,PETSC_VIEWER_STDOUT_WORLD);
58: /*
59: Allow user to add text keys to database
60: */
61: PetscOptionsHasName(PETSC_NULL,"-e",&flag);
62: if (flag) {
63: edited = 1;
64: printf("Enter keyname: (or return to end) ");
65: gets(string);
66: while (string[0] != 0) {
67: AODataKeyExists(aodata,string,&keyexists);
68: if (!keyexists) {
69: AODataKeyAdd(aodata,string,1,1);
70: }
71: PetscStrcpy(keyname,string);
72: printf("Enter segmentname: value (or return to end) ");
73: gets(string);
74: while (string[0] != 0) {
75: ierr = PetscStrtok(string," ",&segname);
76: ierr = PetscStrtok(0," ",&value);
77: ierr = PetscStrlen(value,&bs);
78: AODataSegmentAdd(aodata,keyname,segname,bs,1,&zero,value,PETSC_CHAR);
79: printf("Enter segmentname: value (or return to end) ");
80: gets(string);
81: }
82: printf("Enter keyname: (or return to end) ");
83: gets(string);
84: }
85: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr)
86: AODataView(aodata,PETSC_VIEWER_STDOUT_WORLD);
87: }
89: /*
90: Allow user to remove keys and segements from database
91: */
92: PetscOptionsHasName(PETSC_NULL,"-r",&flag);
93: if (flag) {
94: edited = 1;
95: printf("Enter keyname to remove: (or return to end) ");
96: gets(string);
97: while (string[0] != 0) {
98: AODataKeyRemove(aodata,string);
99: printf("Enter keyname to remove: (or return to end) ");
100: gets(string);
101: }
102: printf("Enter keyname segment name to remove: (or return to end) ");
103: gets(string);
104: while (string[0] != 0) {
105: PetscStrtok(string," ",&ikeyname);
106: PetscStrtok(0," ",&segname);
107: AODataSegmentRemove(aodata,ikeyname,segname);
108: printf("Enter keyname segment name to remove: (or return to end) ");
109: gets(string);
110: }
111: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr)
112: AODataView(aodata,PETSC_VIEWER_STDOUT_WORLD);
113: }
115: if (edited) {
116: PetscStrcat(filename,".new");
117: PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,PETSC_BINARY_CREATE,&binary);
118: AODataView(aodata,binary);
119: PetscViewerDestroy(binary);
120: }
122: AODataDestroy(aodata);
125: PetscFinalize();
127: return(0);
128: }