Actual source code: space.c

petsc-master 2019-12-13
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:    PetscSpaceViewFromOptions - View from Options

124:    Collective on PetscSpace

126:    Input Parameters:
127: +  A - the PetscSpace object
128: .  obj - Optional object
129: -  name - command line option

131:    Level: intermediate
132: .seealso:  PetscSpace, PetscSpaceView, PetscObjectViewFromOptions(), PetscSpaceCreate()
133: @*/
134: PetscErrorCode  PetscSpaceViewFromOptions(PetscSpace A,PetscObject obj,const char name[])
135: {

140:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
141:   return(0);
142: }

144: /*@C
145:   PetscSpaceView - Views a PetscSpace

147:   Collective on sp

149:   Input Parameter:
150: + sp - the PetscSpace object to view
151: - v  - the viewer

153:   Level: beginner

155: .seealso PetscSpaceDestroy()
156: @*/
157: PetscErrorCode PetscSpaceView(PetscSpace sp, PetscViewer v)
158: {
159:   PetscInt       pdim;
160:   PetscBool      iascii;

166:   if (!v) {PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) sp), &v);}
167:   PetscSpaceGetDimension(sp, &pdim);
168:   PetscObjectPrintClassNamePrefixType((PetscObject)sp,v);
169:   PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &iascii);
170:   PetscViewerASCIIPushTab(v);
171:   if (iascii) {PetscViewerASCIIPrintf(v, "Space in %D variables with %D components, size %D\n", sp->Nv, sp->Nc, pdim);}
172:   if (sp->ops->view) {(*sp->ops->view)(sp, v);}
173:   PetscViewerASCIIPopTab(v);
174:   return(0);
175: }

177: /*@
178:   PetscSpaceSetFromOptions - sets parameters in a PetscSpace from the options database

180:   Collective on sp

182:   Input Parameter:
183: . sp - the PetscSpace object to set options for

185:   Options Database:
186: . -petscspace_degree the approximation order of the space

188:   Level: intermediate

190: .seealso PetscSpaceView()
191: @*/
192: PetscErrorCode PetscSpaceSetFromOptions(PetscSpace sp)
193: {
194:   const char    *defaultType;
195:   char           name[256];
196:   PetscBool      flg;

201:   if (!((PetscObject) sp)->type_name) {
202:     defaultType = PETSCSPACEPOLYNOMIAL;
203:   } else {
204:     defaultType = ((PetscObject) sp)->type_name;
205:   }
206:   if (!PetscSpaceRegisterAllCalled) {PetscSpaceRegisterAll();}

208:   PetscObjectOptionsBegin((PetscObject) sp);
209:   PetscOptionsFList("-petscspace_type", "Linear space", "PetscSpaceSetType", PetscSpaceList, defaultType, name, 256, &flg);
210:   if (flg) {
211:     PetscSpaceSetType(sp, name);
212:   } else if (!((PetscObject) sp)->type_name) {
213:     PetscSpaceSetType(sp, defaultType);
214:   }
215:   {
216:     PetscOptionsDeprecated("-petscspace_order","-petscspace_degree","3.11",NULL);
217:     PetscOptionsBoundedInt("-petscspace_order", "DEPRECATED: The approximation order", "PetscSpaceSetDegree", sp->degree, &sp->degree, NULL,0);
218:   }
219:   PetscOptionsBoundedInt("-petscspace_degree", "The (maximally included) polynomial degree", "PetscSpaceSetDegree", sp->degree, &sp->degree, NULL,0);
220:   PetscOptionsBoundedInt("-petscspace_variables", "The number of different variables, e.g. x and y", "PetscSpaceSetNumVariables", sp->Nv, &sp->Nv, NULL,0);
221:   PetscOptionsBoundedInt("-petscspace_components", "The number of components", "PetscSpaceSetNumComponents", sp->Nc, &sp->Nc, NULL,0);
222:   if (sp->ops->setfromoptions) {
223:     (*sp->ops->setfromoptions)(PetscOptionsObject,sp);
224:   }
225:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
226:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) sp);
227:   PetscOptionsEnd();
228:   PetscSpaceViewFromOptions(sp, NULL, "-petscspace_view");
229:   return(0);
230: }

232: /*@C
233:   PetscSpaceSetUp - Construct data structures for the PetscSpace

235:   Collective on sp

237:   Input Parameter:
238: . sp - the PetscSpace object to setup

240:   Level: intermediate

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

250:   if (sp->ops->setup) {(*sp->ops->setup)(sp);}
251:   return(0);
252: }

254: /*@
255:   PetscSpaceDestroy - Destroys a PetscSpace object

257:   Collective on sp

259:   Input Parameter:
260: . sp - the PetscSpace object to destroy

262:   Level: beginner

264: .seealso PetscSpaceView()
265: @*/
266: PetscErrorCode PetscSpaceDestroy(PetscSpace *sp)
267: {

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

274:   if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; return(0);}
275:   ((PetscObject) (*sp))->refct = 0;
276:   DMDestroy(&(*sp)->dm);

278:   (*(*sp)->ops->destroy)(*sp);
279:   PetscHeaderDestroy(sp);
280:   return(0);
281: }

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

286:   Collective

288:   Input Parameter:
289: . comm - The communicator for the PetscSpace object

291:   Output Parameter:
292: . sp - The PetscSpace object

294:   Level: beginner

296: .seealso: PetscSpaceSetType(), PETSCSPACEPOLYNOMIAL
297: @*/
298: PetscErrorCode PetscSpaceCreate(MPI_Comm comm, PetscSpace *sp)
299: {
300:   PetscSpace     s;

305:   PetscCitationsRegister(FECitation,&FEcite);
306:   *sp  = NULL;
307:   PetscFEInitializePackage();

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

311:   s->degree    = 0;
312:   s->maxDegree = PETSC_DETERMINE;
313:   s->Nc        = 1;
314:   s->Nv        = 0;
315:   s->dim       = PETSC_DETERMINE;
316:   DMShellCreate(comm, &s->dm);
317:   PetscSpaceSetType(s, PETSCSPACEPOLYNOMIAL);

319:   *sp = s;
320:   return(0);
321: }

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

326:   Input Parameter:
327: . sp - The PetscSpace

329:   Output Parameter:
330: . dim - The dimension

332:   Level: intermediate

334: .seealso: PetscSpaceGetDegree(), PetscSpaceCreate(), PetscSpace
335: @*/
336: PetscErrorCode PetscSpaceGetDimension(PetscSpace sp, PetscInt *dim)
337: {

343:   if (sp->dim == PETSC_DETERMINE) {
344:     if (sp->ops->getdimension) {(*sp->ops->getdimension)(sp, &sp->dim);}
345:   }
346:   *dim = sp->dim;
347:   return(0);
348: }

350: /*@
351:   PetscSpaceGetDegree - Return the polynomial degrees that characterize this space

353:   Input Parameter:
354: . sp - The PetscSpace

356:   Output Parameter:
357: + minDegree - The degree of the largest polynomial space contained in the space
358: - maxDegree - The degree of the smallest polynomial space containing the space


361:   Level: intermediate

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

376: /*@
377:   PetscSpaceSetDegree - Set the degree of approximation for this space.

379:   Input Parameters:
380: + sp - The PetscSpace
381: . degree - The degree of the largest polynomial space contained in the space
382: - maxDegree - The degree of the largest polynomial space containing the space.  One of degree and maxDegree can be PETSC_DETERMINE.

384:   Level: intermediate

386: .seealso: PetscSpaceGetDegree(), PetscSpaceCreate(), PetscSpace
387: @*/
388: PetscErrorCode PetscSpaceSetDegree(PetscSpace sp, PetscInt degree, PetscInt maxDegree)
389: {
392:   sp->degree = degree;
393:   sp->maxDegree = maxDegree;
394:   return(0);
395: }

397: /*@
398:   PetscSpaceGetNumComponents - Return the number of components for this space

400:   Input Parameter:
401: . sp - The PetscSpace

403:   Output Parameter:
404: . Nc - The number of components

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

408:   Level: intermediate

410: .seealso: PetscSpaceSetNumComponents(), PetscSpaceGetNumVariables(), PetscSpaceGetDimension(), PetscSpaceCreate(), PetscSpace
411: @*/
412: PetscErrorCode PetscSpaceGetNumComponents(PetscSpace sp, PetscInt *Nc)
413: {
417:   *Nc = sp->Nc;
418:   return(0);
419: }

421: /*@
422:   PetscSpaceSetNumComponents - Set the number of components for this space

424:   Input Parameters:
425: + sp - The PetscSpace
426: - order - The number of components

428:   Level: intermediate

430: .seealso: PetscSpaceGetNumComponents(), PetscSpaceSetNumVariables(), PetscSpaceCreate(), PetscSpace
431: @*/
432: PetscErrorCode PetscSpaceSetNumComponents(PetscSpace sp, PetscInt Nc)
433: {
436:   sp->Nc = Nc;
437:   return(0);
438: }

440: /*@
441:   PetscSpaceSetNumVariables - Set the number of variables for this space

443:   Input Parameters:
444: + sp - The PetscSpace
445: - n - The number of variables, e.g. x, y, z...

447:   Level: intermediate

449: .seealso: PetscSpaceGetNumVariables(), PetscSpaceSetNumComponents(), PetscSpaceCreate(), PetscSpace
450: @*/
451: PetscErrorCode PetscSpaceSetNumVariables(PetscSpace sp, PetscInt n)
452: {
455:   sp->Nv = n;
456:   return(0);
457: }

459: /*@
460:   PetscSpaceGetNumVariables - Return the number of variables for this space

462:   Input Parameter:
463: . sp - The PetscSpace

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

468:   Level: intermediate

470: .seealso: PetscSpaceSetNumVariables(), PetscSpaceGetNumComponents(), PetscSpaceGetDimension(), PetscSpaceCreate(), PetscSpace
471: @*/
472: PetscErrorCode PetscSpaceGetNumVariables(PetscSpace sp, PetscInt *n)
473: {
477:   *n = sp->Nv;
478:   return(0);
479: }

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

484:   Input Parameters:
485: + sp      - The PetscSpace
486: . npoints - The number of evaluation points, in reference coordinates
487: - points  - The point coordinates

489:   Output Parameters:
490: + B - The function evaluations in a npoints x nfuncs array
491: . D - The derivative evaluations in a npoints x nfuncs x dim array
492: - H - The second derivative evaluations in a npoints x nfuncs x dim x dim array

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

497:   Level: beginner

499: .seealso: PetscFEGetTabulation(), PetscFEGetDefaultTabulation(), PetscSpaceCreate()
500: @*/
501: PetscErrorCode PetscSpaceEvaluate(PetscSpace sp, PetscInt npoints, const PetscReal points[], PetscReal B[], PetscReal D[], PetscReal H[])
502: {

506:   if (!npoints) return(0);
512:   if (sp->ops->evaluate) {(*sp->ops->evaluate)(sp, npoints, points, B, D, H);}
513:   return(0);
514: }

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

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

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

525:   Not collective

527:   Input Parameters:
528: + sp - the PetscSpace object
529: - height - the height of the mesh point for which the subspace is desired

531:   Output Parameter:
532: . subsp - the subspace

534:   Level: advanced

536: .seealso: PetscDualSpaceGetHeightSubspace(), PetscSpace
537: @*/
538: PetscErrorCode PetscSpaceGetHeightSubspace(PetscSpace sp, PetscInt height, PetscSpace *subsp)
539: {

545:   *subsp = NULL;
546:   if (sp->ops->getheightsubspace) {
547:     (*sp->ops->getheightsubspace)(sp, height, subsp);
548:   }
549:   return(0);
550: }