Actual source code: space.c

petsc-3.12.0 2019-09-29
Report Typos and Errors
  1:  #include <petsc/private/petscfeimpl.h>
  2:  #include <petscdmshell.h>

  4: PetscClassId PETSCSPACE_CLASSID = 0;

  6: PetscFunctionList PetscSpaceList              = NULL;
  7: PetscBool         PetscSpaceRegisterAllCalled = PETSC_FALSE;

  9: /*@C
 10:   PetscSpaceRegister - Adds a new PetscSpace implementation

 12:   Not Collective

 14:   Input Parameters:
 15: + name        - The name of a new user-defined creation routine
 16: - create_func - The creation routine for the implementation type

 18:   Notes:
 19:   PetscSpaceRegister() may be called multiple times to add several user-defined types of PetscSpaces.  The creation function is called
 20:   when the type is set to 'name'.

 22:   Sample usage:
 23: .vb
 24:     PetscSpaceRegister("my_space", MyPetscSpaceCreate);
 25: .ve

 27:   Then, your PetscSpace type can be chosen with the procedural interface via
 28: .vb
 29:     PetscSpaceCreate(MPI_Comm, PetscSpace *);
 30:     PetscSpaceSetType(PetscSpace, "my_space");
 31: .ve
 32:    or at runtime via the option
 33: .vb
 34:     -petscspace_type my_space
 35: .ve

 37:   Level: advanced

 39: .seealso: PetscSpaceRegisterAll(), PetscSpaceRegisterDestroy()

 41: @*/
 42: PetscErrorCode PetscSpaceRegister(const char sname[], PetscErrorCode (*function)(PetscSpace))
 43: {

 47:   PetscFunctionListAdd(&PetscSpaceList, sname, function);
 48:   return(0);
 49: }

 51: /*@C
 52:   PetscSpaceSetType - Builds a particular PetscSpace

 54:   Collective on sp

 56:   Input Parameters:
 57: + sp   - The PetscSpace object
 58: - name - The kind of space

 60:   Options Database Key:
 61: . -petscspace_type <type> - Sets the PetscSpace type; use -help for a list of available types

 63:   Level: intermediate

 65: .seealso: PetscSpaceGetType(), PetscSpaceCreate()
 66: @*/
 67: PetscErrorCode PetscSpaceSetType(PetscSpace sp, PetscSpaceType name)
 68: {
 69:   PetscErrorCode (*r)(PetscSpace);
 70:   PetscBool      match;

 75:   PetscObjectTypeCompare((PetscObject) sp, name, &match);
 76:   if (match) return(0);

 78:   PetscSpaceRegisterAll();
 79:   PetscFunctionListFind(PetscSpaceList, name, &r);
 80:   if (!r) SETERRQ1(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscSpace type: %s", name);

 82:   if (sp->ops->destroy) {
 83:     (*sp->ops->destroy)(sp);
 84:     sp->ops->destroy = NULL;
 85:   }
 86:   sp->dim = PETSC_DETERMINE;
 87:   (*r)(sp);
 88:   PetscObjectChangeTypeName((PetscObject) sp, name);
 89:   return(0);
 90: }

 92: /*@C
 93:   PetscSpaceGetType - Gets the PetscSpace type name (as a string) from the object.

 95:   Not Collective

 97:   Input Parameter:
 98: . sp  - The PetscSpace

100:   Output Parameter:
101: . name - The PetscSpace type name

103:   Level: intermediate

105: .seealso: PetscSpaceSetType(), PetscSpaceCreate()
106: @*/
107: PetscErrorCode PetscSpaceGetType(PetscSpace sp, PetscSpaceType *name)
108: {

114:   if (!PetscSpaceRegisterAllCalled) {
115:     PetscSpaceRegisterAll();
116:   }
117:   *name = ((PetscObject) sp)->type_name;
118:   return(0);
119: }

121: /*@C
122:   PetscSpaceView - Views a PetscSpace

124:   Collective on sp

126:   Input Parameter:
127: + sp - the PetscSpace object to view
128: - v  - the viewer

130:   Level: beginner

132: .seealso PetscSpaceDestroy()
133: @*/
134: PetscErrorCode PetscSpaceView(PetscSpace sp, PetscViewer v)
135: {
136:   PetscInt       pdim;
137:   PetscBool      iascii;

143:   if (!v) {PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) sp), &v);}
144:   PetscSpaceGetDimension(sp, &pdim);
145:   PetscObjectPrintClassNamePrefixType((PetscObject)sp,v);
146:   PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &iascii);
147:   PetscViewerASCIIPushTab(v);
148:   if (iascii) {PetscViewerASCIIPrintf(v, "Space in %D variables with %D components, size %D\n", sp->Nv, sp->Nc, pdim);}
149:   if (sp->ops->view) {(*sp->ops->view)(sp, v);}
150:   PetscViewerASCIIPopTab(v);
151:   return(0);
152: }

154: /*@
155:   PetscSpaceSetFromOptions - sets parameters in a PetscSpace from the options database

157:   Collective on sp

159:   Input Parameter:
160: . sp - the PetscSpace object to set options for

162:   Options Database:
163: . -petscspace_degree the approximation order of the space

165:   Level: intermediate

167: .seealso PetscSpaceView()
168: @*/
169: PetscErrorCode PetscSpaceSetFromOptions(PetscSpace sp)
170: {
171:   const char    *defaultType;
172:   char           name[256];
173:   PetscBool      flg;

178:   if (!((PetscObject) sp)->type_name) {
179:     defaultType = PETSCSPACEPOLYNOMIAL;
180:   } else {
181:     defaultType = ((PetscObject) sp)->type_name;
182:   }
183:   if (!PetscSpaceRegisterAllCalled) {PetscSpaceRegisterAll();}

185:   PetscObjectOptionsBegin((PetscObject) sp);
186:   PetscOptionsFList("-petscspace_type", "Linear space", "PetscSpaceSetType", PetscSpaceList, defaultType, name, 256, &flg);
187:   if (flg) {
188:     PetscSpaceSetType(sp, name);
189:   } else if (!((PetscObject) sp)->type_name) {
190:     PetscSpaceSetType(sp, defaultType);
191:   }
192:   {
193:     PetscOptionsDeprecated("-petscspace_order","-petscspace_degree","3.11",NULL);
194:     PetscOptionsBoundedInt("-petscspace_order", "DEPRECATED: The approximation order", "PetscSpaceSetDegree", sp->degree, &sp->degree, NULL,0);
195:   }
196:   PetscOptionsBoundedInt("-petscspace_degree", "The (maximally included) polynomial degree", "PetscSpaceSetDegree", sp->degree, &sp->degree, NULL,0);
197:   PetscOptionsBoundedInt("-petscspace_variables", "The number of different variables, e.g. x and y", "PetscSpaceSetNumVariables", sp->Nv, &sp->Nv, NULL,0);
198:   PetscOptionsBoundedInt("-petscspace_components", "The number of components", "PetscSpaceSetNumComponents", sp->Nc, &sp->Nc, NULL,0);
199:   if (sp->ops->setfromoptions) {
200:     (*sp->ops->setfromoptions)(PetscOptionsObject,sp);
201:   }
202:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
203:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) sp);
204:   PetscOptionsEnd();
205:   PetscSpaceViewFromOptions(sp, NULL, "-petscspace_view");
206:   return(0);
207: }

209: /*@C
210:   PetscSpaceSetUp - Construct data structures for the PetscSpace

212:   Collective on sp

214:   Input Parameter:
215: . sp - the PetscSpace object to setup

217:   Level: intermediate

219: .seealso PetscSpaceView(), PetscSpaceDestroy()
220: @*/
221: PetscErrorCode PetscSpaceSetUp(PetscSpace sp)
222: {

227:   if (sp->ops->setup) {(*sp->ops->setup)(sp);}
228:   return(0);
229: }

231: /*@
232:   PetscSpaceDestroy - Destroys a PetscSpace object

234:   Collective on sp

236:   Input Parameter:
237: . sp - the PetscSpace object to destroy

239:   Level: beginner

241: .seealso PetscSpaceView()
242: @*/
243: PetscErrorCode PetscSpaceDestroy(PetscSpace *sp)
244: {

248:   if (!*sp) return(0);

251:   if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; return(0);}
252:   ((PetscObject) (*sp))->refct = 0;
253:   DMDestroy(&(*sp)->dm);

255:   (*(*sp)->ops->destroy)(*sp);
256:   PetscHeaderDestroy(sp);
257:   return(0);
258: }

260: /*@
261:   PetscSpaceCreate - Creates an empty PetscSpace object. The type can then be set with PetscSpaceSetType().

263:   Collective

265:   Input Parameter:
266: . comm - The communicator for the PetscSpace object

268:   Output Parameter:
269: . sp - The PetscSpace object

271:   Level: beginner

273: .seealso: PetscSpaceSetType(), PETSCSPACEPOLYNOMIAL
274: @*/
275: PetscErrorCode PetscSpaceCreate(MPI_Comm comm, PetscSpace *sp)
276: {
277:   PetscSpace     s;

282:   PetscCitationsRegister(FECitation,&FEcite);
283:   *sp  = NULL;
284:   PetscFEInitializePackage();

286:   PetscHeaderCreate(s, PETSCSPACE_CLASSID, "PetscSpace", "Linear Space", "PetscSpace", comm, PetscSpaceDestroy, PetscSpaceView);

288:   s->degree    = 0;
289:   s->maxDegree = PETSC_DETERMINE;
290:   s->Nc        = 1;
291:   s->Nv        = 0;
292:   s->dim       = PETSC_DETERMINE;
293:   DMShellCreate(comm, &s->dm);
294:   PetscSpaceSetType(s, PETSCSPACEPOLYNOMIAL);

296:   *sp = s;
297:   return(0);
298: }

300: /*@
301:   PetscSpaceGetDimension - Return the dimension of this space, i.e. the number of basis vectors

303:   Input Parameter:
304: . sp - The PetscSpace

306:   Output Parameter:
307: . dim - The dimension

309:   Level: intermediate

311: .seealso: PetscSpaceGetDegree(), PetscSpaceCreate(), PetscSpace
312: @*/
313: PetscErrorCode PetscSpaceGetDimension(PetscSpace sp, PetscInt *dim)
314: {

320:   if (sp->dim == PETSC_DETERMINE) {
321:     if (sp->ops->getdimension) {(*sp->ops->getdimension)(sp, &sp->dim);}
322:   }
323:   *dim = sp->dim;
324:   return(0);
325: }

327: /*@
328:   PetscSpaceGetDegree - Return the polynomial degrees that characterize this space

330:   Input Parameter:
331: . sp - The PetscSpace

333:   Output Parameter:
334: + minDegree - The degree of the largest polynomial space contained in the space
335: - maxDegree - The degree of the smallest polynomial space containing the space


338:   Level: intermediate

340: .seealso: PetscSpaceSetDegree(), PetscSpaceGetDimension(), PetscSpaceCreate(), PetscSpace
341: @*/
342: PetscErrorCode PetscSpaceGetDegree(PetscSpace sp, PetscInt *minDegree, PetscInt *maxDegree)
343: {
348:   if (minDegree) *minDegree = sp->degree;
349:   if (maxDegree) *maxDegree = sp->maxDegree;
350:   return(0);
351: }

353: /*@
354:   PetscSpaceSetDegree - Set the degree of approximation for this space.

356:   Input Parameters:
357: + sp - The PetscSpace
358: . degree - The degree of the largest polynomial space contained in the space
359: - maxDegree - The degree of the largest polynomial space containing the space.  One of degree and maxDegree can be PETSC_DETERMINE.

361:   Level: intermediate

363: .seealso: PetscSpaceGetDegree(), PetscSpaceCreate(), PetscSpace
364: @*/
365: PetscErrorCode PetscSpaceSetDegree(PetscSpace sp, PetscInt degree, PetscInt maxDegree)
366: {
369:   sp->degree = degree;
370:   sp->maxDegree = maxDegree;
371:   return(0);
372: }

374: /*@
375:   PetscSpaceGetNumComponents - Return the number of components for this space

377:   Input Parameter:
378: . sp - The PetscSpace

380:   Output Parameter:
381: . Nc - The number of components

383:   Note: A vector space, for example, will have d components, where d is the spatial dimension

385:   Level: intermediate

387: .seealso: PetscSpaceSetNumComponents(), PetscSpaceGetNumVariables(), PetscSpaceGetDimension(), PetscSpaceCreate(), PetscSpace
388: @*/
389: PetscErrorCode PetscSpaceGetNumComponents(PetscSpace sp, PetscInt *Nc)
390: {
394:   *Nc = sp->Nc;
395:   return(0);
396: }

398: /*@
399:   PetscSpaceSetNumComponents - Set the number of components for this space

401:   Input Parameters:
402: + sp - The PetscSpace
403: - order - The number of components

405:   Level: intermediate

407: .seealso: PetscSpaceGetNumComponents(), PetscSpaceSetNumVariables(), PetscSpaceCreate(), PetscSpace
408: @*/
409: PetscErrorCode PetscSpaceSetNumComponents(PetscSpace sp, PetscInt Nc)
410: {
413:   sp->Nc = Nc;
414:   return(0);
415: }

417: /*@
418:   PetscSpaceSetNumVariables - Set the number of variables for this space

420:   Input Parameters:
421: + sp - The PetscSpace
422: - n - The number of variables, e.g. x, y, z...

424:   Level: intermediate

426: .seealso: PetscSpaceGetNumVariables(), PetscSpaceSetNumComponents(), PetscSpaceCreate(), PetscSpace
427: @*/
428: PetscErrorCode PetscSpaceSetNumVariables(PetscSpace sp, PetscInt n)
429: {
432:   sp->Nv = n;
433:   return(0);
434: }

436: /*@
437:   PetscSpaceGetNumVariables - Return the number of variables for this space

439:   Input Parameter:
440: . sp - The PetscSpace

442:   Output Parameter:
443: . Nc - The number of variables, e.g. x, y, z...

445:   Level: intermediate

447: .seealso: PetscSpaceSetNumVariables(), PetscSpaceGetNumComponents(), PetscSpaceGetDimension(), PetscSpaceCreate(), PetscSpace
448: @*/
449: PetscErrorCode PetscSpaceGetNumVariables(PetscSpace sp, PetscInt *n)
450: {
454:   *n = sp->Nv;
455:   return(0);
456: }

458: /*@C
459:   PetscSpaceEvaluate - Evaluate the basis functions and their derivatives (jet) at each point

461:   Input Parameters:
462: + sp      - The PetscSpace
463: . npoints - The number of evaluation points, in reference coordinates
464: - points  - The point coordinates

466:   Output Parameters:
467: + B - The function evaluations in a npoints x nfuncs array
468: . D - The derivative evaluations in a npoints x nfuncs x dim array
469: - H - The second derivative evaluations in a npoints x nfuncs x dim x dim array

471:   Note: Above nfuncs is the dimension of the space, and dim is the spatial dimension. The coordinates are given
472:   on the reference cell, not in real space.

474:   Level: beginner

476: .seealso: PetscFEGetTabulation(), PetscFEGetDefaultTabulation(), PetscSpaceCreate()
477: @*/
478: PetscErrorCode PetscSpaceEvaluate(PetscSpace sp, PetscInt npoints, const PetscReal points[], PetscReal B[], PetscReal D[], PetscReal H[])
479: {

483:   if (!npoints) return(0);
489:   if (sp->ops->evaluate) {(*sp->ops->evaluate)(sp, npoints, points, B, D, H);}
490:   return(0);
491: }

493: /*@
494:   PetscSpaceGetHeightSubspace - Get the subset of the primal space basis that is supported on a mesh point of a given height.

496:   If the space is not defined on mesh points of the given height (e.g. if the space is discontinuous and
497:   pointwise values are not defined on the element boundaries), or if the implementation of PetscSpace does not
498:   support extracting subspaces, then NULL is returned.

500:   This does not increment the reference count on the returned space, and the user should not destroy it.

502:   Not collective

504:   Input Parameters:
505: + sp - the PetscSpace object
506: - height - the height of the mesh point for which the subspace is desired

508:   Output Parameter:
509: . subsp - the subspace

511:   Level: advanced

513: .seealso: PetscDualSpaceGetHeightSubspace(), PetscSpace
514: @*/
515: PetscErrorCode PetscSpaceGetHeightSubspace(PetscSpace sp, PetscInt height, PetscSpace *subsp)
516: {

522:   *subsp = NULL;
523:   if (sp->ops->getheightsubspace) {
524:     (*sp->ops->getheightsubspace)(sp, height, subsp);
525:   }
526:   return(0);
527: }