Actual source code: gridreg.c

  1: #ifdef PETSC_RCS_HEADER
  2: static char vcid[] = "$Id: gridreg.c,v 1.5 2000/01/10 03:54:25 knepley Exp $";
  3: #endif

 5:  #include src/grid/gridimpl.h

  7: PetscFList GridList                       = PETSC_NULL;
  8: PetscTruth GridRegisterAllCalled          = PETSC_FALSE;
  9: PetscFList GridSerializeList              = PETSC_NULL;
 10: PetscTruth GridSerializeRegisterAllCalled = PETSC_FALSE;

 12: /*@C
 13:   GridSetType - Sets the creation method for the grid.

 15:   Collective on Grid

 17:   Input Parameters:
 18: + grid   - The Grid context
 19: - method - A known method

 21:   Options Database Command:
 22: . -grid_type <method> - Sets the method; use -help for a list
 23:                         of available methods (for instance, tri2d)

 25:   Notes:
 26:   See "petsc/include/grid.h" for available methods (for instance)
 27: . GRID_TRIANGULAR_2D - Triangular 2D grid

 29:   Normally, it is best to use the GridSetFromOptions() command and
 30:   then set the Grid type from the options database rather than by using
 31:   this routine.  Using the options database provides the user with
 32:   maximum flexibility in evaluating the many different solvers.
 33:   The GridSetType() routine is provided for those situations
 34:   where it is necessary to set the application ordering independently of the
 35:   command line or options database.  This might be the case, for example,
 36:   when the choice of solver changes during the execution of the
 37:   program, and the user's application is taking responsibility for
 38:   choosing the appropriate method.  In other words, this routine is
 39:   not for beginners.

 41:   Level: intermediate

 43: .keywords: grid, set, type
 44: .seealso GridSetSerializeType()
 45: @*/
 46: int GridSetType(Grid grid, GridType method)
 47: {
 48:   int      (*r)(Grid);
 49:   PetscTruth match;
 50:   int        ierr;

 54:   PetscTypeCompare((PetscObject) grid, method, &match);
 55:   if (match == PETSC_TRUE) return(0);

 57:   /* Get the function pointers for the method requested */
 58:   if (GridRegisterAllCalled == PETSC_FALSE) {
 59:     GridRegisterAll(PETSC_NULL);
 60:   }
 61:   PetscFListFind(grid->comm, GridList, method, (void (**)(void)) &r);
 62:   if (!r) SETERRQ1(PETSC_ERR_ARG_WRONG, "Unknown grid type: %s", method);

 64:   if (grid->ops->destroy != PETSC_NULL) {
 65:     (*grid->ops->destroy)(grid);
 66:   }
 67:   (*r)(grid);

 69:   PetscObjectChangeTypeName((PetscObject) grid, method);
 70:   return(0);
 71: }

 73: /*@C
 74:   GridGetType - Gets the Grid method type name (as a string).

 76:   Not collective

 78:   Input Parameter:
 79: . grid - The grid

 81:   Output Parameter:
 82: . type - The name of Grid method

 84:   Level: intermediate

 86: .keywords: grid, get, type, name
 87: .seealso GridSetType()
 88: @*/
 89: int GridGetType(Grid grid, GridType *type)
 90: {

 96:   if (GridRegisterAllCalled == PETSC_FALSE) {
 97:     GridRegisterAll(PETSC_NULL);
 98:   }
 99:   *type = grid->type_name;
100:   return(0);
101: }

103: /*@C
104:   GridSetSerializeType - Sets the serialization method for the grid.

106:   Collective on Grid

108:   Input Parameters:
109: + grid   - The Grid context
110: - method - A known method

112:   Options Database Command:
113: . -grid_serialize_type <method> - Sets the method; use -help for a list
114:                                   of available methods (for instance, tri2d_binary)

116:   Notes:
117:   See "petsc/include/grid.h" for available methods (for instance)
118: . GRID_SER_TRIANGULAR_2D_BINARY - Triangular 2D grid to binary file

120:   Normally, it is best to use the GridSetFromOptions() command and
121:   then set the Grid type from the options database rather than by using
122:   this routine.  Using the options database provides the user with
123:   maximum flexibility in evaluating the many different solvers.
124:   The GridSetSerializeType() routine is provided for those situations
125:   where it is necessary to set the application ordering independently of the
126:   command line or options database.  This might be the case, for example,
127:   when the choice of solver changes during the execution of the
128:   program, and the user's application is taking responsibility for
129:   choosing the appropriate method.  In other words, this routine is
130:   not for beginners.

132:   Level: intermediate

134: .keywords: grid, set, type, serialization
135: .seealso GridSetType()
136: @*/
137: int GridSetSerializeType(Grid grid, GridSerializeType method)
138: {
139:   int      (*r)(MPI_Comm, Grid *, PetscViewer, PetscTruth);
140:   PetscTruth match;
141:   int        ierr;

145:   PetscSerializeCompare((PetscObject) grid, method, &match);
146:   if (match == PETSC_TRUE) return(0);

148:   /* Get the function pointers for the method requested but do not call */
149:   if (GridSerializeRegisterAllCalled == PETSC_FALSE) {
150:     GridSerializeRegisterAll(PETSC_NULL);
151:   }
152:   PetscFListFind(grid->comm, GridSerializeList, method, (void (**)(void)) &r);
153:   if (!r) SETERRQ1(PETSC_ERR_ARG_WRONG, "Unknown grid serialization type: %s", method);

155:   PetscObjectChangeSerializeName((PetscObject) grid, method);
156:   return(0);
157: }

159: /*@C
160:   GridGetSerializeType - Gets the Grid serialization method (as a string).

162:   Not collective

164:   Input Parameter:
165: . grid - The grid

167:   Output Parameter:
168: . type - The name of Grid serialization method

170:   Level: intermediate

172: .keywords: grid, get, serialize, type, name
173: .seealso GridSetType()
174: @*/
175: int GridGetSerializeType(Grid grid, GridSerializeType *type)
176: {

182:   if (GridSerializeRegisterAllCalled == PETSC_FALSE) {
183:     GridSerializeRegisterAll(PETSC_NULL);
184:   }
185:   *type = grid->serialize_name;
186:   return(0);
187: }

189: /*--------------------------------------------------------------------------------------------------------------------*/
190: /*@C
191:   GridRegister - Adds a creation method to the grid package.

193:   Synopsis:

195:   GridRegister(char *name, char *path, char *func_name, int (*create_func)(Grid))

197:   Not Collective

199:   Input Parameters:
200: + name        - The name of a new user-defined creation routine
201: . path        - The path (either absolute or relative) of the library containing this routine
202: . func_name   - The name of the creation routine
203: - create_func - The creation routine itself

205:   Notes:
206:   GridRegister() may be called multiple times to add several user-defined grides.

208:   If dynamic libraries are used, then the fourth input argument (create_func) is ignored.

210:   Sample usage:
211: .vb
212:   GridRegisterDynamic("my_grid", "/home/username/my_lib/lib/libO/solaris/libmy.a", "MyGridCreate", MyGridCreate);
213: .ve

215:   Then, your grid type can be chosen with the procedural interface via
216: .vb
217:     GridCreate(MPI_Comm, Grid *);
218:     GridSetType(vec, "my_grid")
219: .ve
220:   or at runtime via the option
221: .vb
222:     -grid_type my_grid
223: .ve

225:   Note: $PETSC_ARCH and $BOPT occuring in pathname will be replaced with appropriate values.

227:   Level: advanced

229: .keywords: Grid, register
230: .seealso: GridRegisterAll(), GridRegisterDestroy()
231: @*/
232: int GridRegister(const char sname[], const char path[], const char name[], int (*function)(Grid))
233: {
234:   char fullname[256];
235:   int  ierr;

238:   PetscStrcpy(fullname, path);
239:   PetscStrcat(fullname, ":");
240:   PetscStrcat(fullname, name);
241:   PetscFListAdd(&GridList, sname, fullname, (void (*)(void)) function);
242:   return(0);
243: }

245: /*@C
246:   GridSerializeRegister - Adds a serialization method to the grid package.

248:   Synopsis:

250:   GridSerializeRegister(char *name, char *path, char *func_name,
251:                         int (*serialize_func)(MPI_Comm, Grid *, PetscViewer, PetscTruth))

253:   Not Collective

255:   Input Parameters:
256: + name           - The name of a new user-defined serialization routine
257: . path           - The path (either absolute or relative) of the library containing this routine
258: . func_name      - The name of the serialization routine
259: - serialize_func - The serialization routine itself

261:   Notes:
262:   GridSerializeRegister() may be called multiple times to add several user-defined serializers.

264:   If dynamic libraries are used, then the fourth input argument (serialize_func) is ignored.

266:   Sample usage:
267: .vb
268:   GridSerializeRegisterDynamic("my_store", "/home/username/my_lib/lib/libO/solaris/libmy.a", "MyStoreFunc", MyStoreFunc);
269: .ve

271:   Then, your serialization can be chosen with the procedural interface via
272: .vb
273:     GridSetSerializeType(grid, "my_store")
274: .ve
275:   or at runtime via the option
276: .vb
277:     -grid_serialize_type my_store
278: .ve

280:   Note: $PETSC_ARCH and $BOPT occuring in pathname will be replaced with appropriate values.

282:   Level: advanced

284: .keywords: grid, register
285: .seealso: GridSerializeRegisterAll(), GridSerializeRegisterDestroy()
286: M*/
287: int GridSerializeRegister(const char sname[], const char path[], const char name[],
288:                           int (*function)(MPI_Comm, Grid *, PetscViewer, PetscTruth))
289: {
290:   char fullname[256];
291:   int  ierr;

294:   PetscStrcpy(fullname, path);
295:   PetscStrcat(fullname, ":");
296:   PetscStrcat(fullname, name);
297:   PetscFListAdd(&GridSerializeList, sname, fullname, (void (*)(void)) function);
298:   return(0);
299: }

301: /*-------------------------------------------------------------------------------------------------------------------*/
302: /*@C
303:   GridRegisterDestroy - Frees the list of creation routines for grids that were registered by GridRegister().

305:   Not collective

307:   Level: advanced

309: .keywords: grid, register, destroy
310: .seealso: GridRegister(), GridRegisterAll(), GridSerializeRegisterDestroy()
311: @*/
312: int GridRegisterDestroy(void)
313: {

317:   if (GridList != PETSC_NULL) {
318:     PetscFListDestroy(&GridList);
319:     GridList = PETSC_NULL;
320:   }
321:   GridRegisterAllCalled = PETSC_FALSE;
322:   return(0);
323: }

325: /*@C
326:   GridSerializeRegisterDestroy - Frees the list of serialization routines for
327:   grids that were registered by GridSerializeRegister().

329:   Not collective

331:   Level: advanced

333: .keywords: grid, serialization, register, destroy
334: .seealso: GridSerializeRegisterAll(), GridRegisterDestroy()
335: @*/
336: int GridSerializeRegisterDestroy()
337: {

341:   if (GridSerializeList != PETSC_NULL) {
342:     PetscFListDestroy(&GridSerializeList);
343:     GridSerializeList = PETSC_NULL;
344:   }
345:   GridSerializeRegisterAllCalled = PETSC_FALSE;
346:   return(0);
347: }