xref: /petsc/src/dm/dt/dualspace/interface/dualspace.c (revision 8f2aacc6d75d8d949f320be22961cf8915dbac9a)
120cf1dd8SToby Isaac #include <petsc/private/petscfeimpl.h> /*I "petscfe.h" I*/
220cf1dd8SToby Isaac #include <petscdmplex.h>
320cf1dd8SToby Isaac 
420cf1dd8SToby Isaac PetscClassId PETSCDUALSPACE_CLASSID = 0;
520cf1dd8SToby Isaac 
6ead873ccSMatthew G. Knepley PetscLogEvent PETSCDUALSPACE_SetUp;
7ead873ccSMatthew G. Knepley 
820cf1dd8SToby Isaac PetscFunctionList PetscDualSpaceList              = NULL;
920cf1dd8SToby Isaac PetscBool         PetscDualSpaceRegisterAllCalled = PETSC_FALSE;
1020cf1dd8SToby Isaac 
11ea78f98cSLisandro Dalcin const char *const PetscDualSpaceReferenceCells[] = {"SIMPLEX", "TENSOR", "PetscDualSpaceReferenceCell", "PETSCDUALSPACE_REFCELL_", NULL};
1255cc6565SMatthew G. Knepley 
136f905325SMatthew G. Knepley /*
146f905325SMatthew G. Knepley   PetscDualSpaceLatticePointLexicographic_Internal - Returns all tuples of size 'len' with nonnegative integers that sum up to at most 'max'.
156f905325SMatthew G. Knepley                                                      Ordering is lexicographic with lowest index as least significant in ordering.
16b4457527SToby Isaac                                                      e.g. for len == 2 and max == 2, this will return, in order, {0,0}, {1,0}, {2,0}, {0,1}, {1,1}, {0,2}.
176f905325SMatthew G. Knepley 
186f905325SMatthew G. Knepley   Input Parameters:
196f905325SMatthew G. Knepley + len - The length of the tuple
206f905325SMatthew G. Knepley . max - The maximum sum
216f905325SMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
226f905325SMatthew G. Knepley 
236f905325SMatthew G. Knepley   Output Parameter:
246f905325SMatthew G. Knepley . tup - A tuple of len integers whos sum is at most 'max'
256f905325SMatthew G. Knepley 
266f905325SMatthew G. Knepley   Level: developer
276f905325SMatthew G. Knepley 
286f905325SMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal()
296f905325SMatthew G. Knepley */
306f905325SMatthew G. Knepley PetscErrorCode PetscDualSpaceLatticePointLexicographic_Internal(PetscInt len, PetscInt max, PetscInt tup[])
316f905325SMatthew G. Knepley {
326f905325SMatthew G. Knepley   PetscFunctionBegin;
336f905325SMatthew G. Knepley   while (len--) {
346f905325SMatthew G. Knepley     max -= tup[len];
356f905325SMatthew G. Knepley     if (!max) {
366f905325SMatthew G. Knepley       tup[len] = 0;
376f905325SMatthew G. Knepley       break;
386f905325SMatthew G. Knepley     }
396f905325SMatthew G. Knepley   }
406f905325SMatthew G. Knepley   tup[++len]++;
416f905325SMatthew G. Knepley   PetscFunctionReturn(0);
426f905325SMatthew G. Knepley }
436f905325SMatthew G. Knepley 
446f905325SMatthew G. Knepley /*
456f905325SMatthew G. Knepley   PetscDualSpaceTensorPointLexicographic_Internal - Returns all tuples of size 'len' with nonnegative integers that are all less than or equal to 'max'.
466f905325SMatthew G. Knepley                                                     Ordering is lexicographic with lowest index as least significant in ordering.
476f905325SMatthew G. Knepley                                                     e.g. for len == 2 and max == 2, this will return, in order, {0,0}, {1,0}, {2,0}, {0,1}, {1,1}, {2,1}, {0,2}, {1,2}, {2,2}.
486f905325SMatthew G. Knepley 
496f905325SMatthew G. Knepley   Input Parameters:
506f905325SMatthew G. Knepley + len - The length of the tuple
516f905325SMatthew G. Knepley . max - The maximum value
526f905325SMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
536f905325SMatthew G. Knepley 
546f905325SMatthew G. Knepley   Output Parameter:
556f905325SMatthew G. Knepley . tup - A tuple of len integers whos sum is at most 'max'
566f905325SMatthew G. Knepley 
576f905325SMatthew G. Knepley   Level: developer
586f905325SMatthew G. Knepley 
596f905325SMatthew G. Knepley .seealso: PetscDualSpaceLatticePointLexicographic_Internal()
606f905325SMatthew G. Knepley */
616f905325SMatthew G. Knepley PetscErrorCode PetscDualSpaceTensorPointLexicographic_Internal(PetscInt len, PetscInt max, PetscInt tup[])
626f905325SMatthew G. Knepley {
636f905325SMatthew G. Knepley   PetscInt       i;
646f905325SMatthew G. Knepley 
656f905325SMatthew G. Knepley   PetscFunctionBegin;
666f905325SMatthew G. Knepley   for (i = 0; i < len; i++) {
676f905325SMatthew G. Knepley     if (tup[i] < max) {
686f905325SMatthew G. Knepley       break;
696f905325SMatthew G. Knepley     } else {
706f905325SMatthew G. Knepley       tup[i] = 0;
716f905325SMatthew G. Knepley     }
726f905325SMatthew G. Knepley   }
736f905325SMatthew G. Knepley   tup[i]++;
746f905325SMatthew G. Knepley   PetscFunctionReturn(0);
756f905325SMatthew G. Knepley }
766f905325SMatthew G. Knepley 
7720cf1dd8SToby Isaac /*@C
7820cf1dd8SToby Isaac   PetscDualSpaceRegister - Adds a new PetscDualSpace implementation
7920cf1dd8SToby Isaac 
8020cf1dd8SToby Isaac   Not Collective
8120cf1dd8SToby Isaac 
8220cf1dd8SToby Isaac   Input Parameters:
8320cf1dd8SToby Isaac + name        - The name of a new user-defined creation routine
8420cf1dd8SToby Isaac - create_func - The creation routine itself
8520cf1dd8SToby Isaac 
8620cf1dd8SToby Isaac   Notes:
8720cf1dd8SToby Isaac   PetscDualSpaceRegister() may be called multiple times to add several user-defined PetscDualSpaces
8820cf1dd8SToby Isaac 
8920cf1dd8SToby Isaac   Sample usage:
9020cf1dd8SToby Isaac .vb
9120cf1dd8SToby Isaac     PetscDualSpaceRegister("my_space", MyPetscDualSpaceCreate);
9220cf1dd8SToby Isaac .ve
9320cf1dd8SToby Isaac 
9420cf1dd8SToby Isaac   Then, your PetscDualSpace type can be chosen with the procedural interface via
9520cf1dd8SToby Isaac .vb
9620cf1dd8SToby Isaac     PetscDualSpaceCreate(MPI_Comm, PetscDualSpace *);
9720cf1dd8SToby Isaac     PetscDualSpaceSetType(PetscDualSpace, "my_dual_space");
9820cf1dd8SToby Isaac .ve
9920cf1dd8SToby Isaac    or at runtime via the option
10020cf1dd8SToby Isaac .vb
10120cf1dd8SToby Isaac     -petscdualspace_type my_dual_space
10220cf1dd8SToby Isaac .ve
10320cf1dd8SToby Isaac 
10420cf1dd8SToby Isaac   Level: advanced
10520cf1dd8SToby Isaac 
10620cf1dd8SToby Isaac .seealso: PetscDualSpaceRegisterAll(), PetscDualSpaceRegisterDestroy()
10720cf1dd8SToby Isaac 
10820cf1dd8SToby Isaac @*/
10920cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceRegister(const char sname[], PetscErrorCode (*function)(PetscDualSpace))
11020cf1dd8SToby Isaac {
11120cf1dd8SToby Isaac   PetscErrorCode ierr;
11220cf1dd8SToby Isaac 
11320cf1dd8SToby Isaac   PetscFunctionBegin;
11420cf1dd8SToby Isaac   ierr = PetscFunctionListAdd(&PetscDualSpaceList, sname, function);CHKERRQ(ierr);
11520cf1dd8SToby Isaac   PetscFunctionReturn(0);
11620cf1dd8SToby Isaac }
11720cf1dd8SToby Isaac 
11820cf1dd8SToby Isaac /*@C
11920cf1dd8SToby Isaac   PetscDualSpaceSetType - Builds a particular PetscDualSpace
12020cf1dd8SToby Isaac 
121d083f849SBarry Smith   Collective on sp
12220cf1dd8SToby Isaac 
12320cf1dd8SToby Isaac   Input Parameters:
12420cf1dd8SToby Isaac + sp   - The PetscDualSpace object
12520cf1dd8SToby Isaac - name - The kind of space
12620cf1dd8SToby Isaac 
12720cf1dd8SToby Isaac   Options Database Key:
12820cf1dd8SToby Isaac . -petscdualspace_type <type> - Sets the PetscDualSpace type; use -help for a list of available types
12920cf1dd8SToby Isaac 
13020cf1dd8SToby Isaac   Level: intermediate
13120cf1dd8SToby Isaac 
13220cf1dd8SToby Isaac .seealso: PetscDualSpaceGetType(), PetscDualSpaceCreate()
13320cf1dd8SToby Isaac @*/
13420cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceSetType(PetscDualSpace sp, PetscDualSpaceType name)
13520cf1dd8SToby Isaac {
13620cf1dd8SToby Isaac   PetscErrorCode (*r)(PetscDualSpace);
13720cf1dd8SToby Isaac   PetscBool      match;
13820cf1dd8SToby Isaac   PetscErrorCode ierr;
13920cf1dd8SToby Isaac 
14020cf1dd8SToby Isaac   PetscFunctionBegin;
14120cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
14220cf1dd8SToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) sp, name, &match);CHKERRQ(ierr);
14320cf1dd8SToby Isaac   if (match) PetscFunctionReturn(0);
14420cf1dd8SToby Isaac 
14520cf1dd8SToby Isaac   if (!PetscDualSpaceRegisterAllCalled) {ierr = PetscDualSpaceRegisterAll();CHKERRQ(ierr);}
14620cf1dd8SToby Isaac   ierr = PetscFunctionListFind(PetscDualSpaceList, name, &r);CHKERRQ(ierr);
14720cf1dd8SToby Isaac   if (!r) SETERRQ1(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscDualSpace type: %s", name);
14820cf1dd8SToby Isaac 
14920cf1dd8SToby Isaac   if (sp->ops->destroy) {
15020cf1dd8SToby Isaac     ierr             = (*sp->ops->destroy)(sp);CHKERRQ(ierr);
15120cf1dd8SToby Isaac     sp->ops->destroy = NULL;
15220cf1dd8SToby Isaac   }
15320cf1dd8SToby Isaac   ierr = (*r)(sp);CHKERRQ(ierr);
15420cf1dd8SToby Isaac   ierr = PetscObjectChangeTypeName((PetscObject) sp, name);CHKERRQ(ierr);
15520cf1dd8SToby Isaac   PetscFunctionReturn(0);
15620cf1dd8SToby Isaac }
15720cf1dd8SToby Isaac 
15820cf1dd8SToby Isaac /*@C
15920cf1dd8SToby Isaac   PetscDualSpaceGetType - Gets the PetscDualSpace type name (as a string) from the object.
16020cf1dd8SToby Isaac 
16120cf1dd8SToby Isaac   Not Collective
16220cf1dd8SToby Isaac 
16320cf1dd8SToby Isaac   Input Parameter:
16420cf1dd8SToby Isaac . sp  - The PetscDualSpace
16520cf1dd8SToby Isaac 
16620cf1dd8SToby Isaac   Output Parameter:
16720cf1dd8SToby Isaac . name - The PetscDualSpace type name
16820cf1dd8SToby Isaac 
16920cf1dd8SToby Isaac   Level: intermediate
17020cf1dd8SToby Isaac 
17120cf1dd8SToby Isaac .seealso: PetscDualSpaceSetType(), PetscDualSpaceCreate()
17220cf1dd8SToby Isaac @*/
17320cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetType(PetscDualSpace sp, PetscDualSpaceType *name)
17420cf1dd8SToby Isaac {
17520cf1dd8SToby Isaac   PetscErrorCode ierr;
17620cf1dd8SToby Isaac 
17720cf1dd8SToby Isaac   PetscFunctionBegin;
17820cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
17920cf1dd8SToby Isaac   PetscValidPointer(name, 2);
18020cf1dd8SToby Isaac   if (!PetscDualSpaceRegisterAllCalled) {
18120cf1dd8SToby Isaac     ierr = PetscDualSpaceRegisterAll();CHKERRQ(ierr);
18220cf1dd8SToby Isaac   }
18320cf1dd8SToby Isaac   *name = ((PetscObject) sp)->type_name;
18420cf1dd8SToby Isaac   PetscFunctionReturn(0);
18520cf1dd8SToby Isaac }
18620cf1dd8SToby Isaac 
187221d6281SMatthew G. Knepley static PetscErrorCode PetscDualSpaceView_ASCII(PetscDualSpace sp, PetscViewer v)
188221d6281SMatthew G. Knepley {
189221d6281SMatthew G. Knepley   PetscViewerFormat format;
190221d6281SMatthew G. Knepley   PetscInt          pdim, f;
191221d6281SMatthew G. Knepley   PetscErrorCode    ierr;
192221d6281SMatthew G. Knepley 
193221d6281SMatthew G. Knepley   PetscFunctionBegin;
194221d6281SMatthew G. Knepley   ierr = PetscDualSpaceGetDimension(sp, &pdim);CHKERRQ(ierr);
195221d6281SMatthew G. Knepley   ierr = PetscObjectPrintClassNamePrefixType((PetscObject) sp, v);CHKERRQ(ierr);
196221d6281SMatthew G. Knepley   ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
197b4457527SToby Isaac   if (sp->k) {
198b4457527SToby Isaac     ierr = PetscViewerASCIIPrintf(v, "Dual space for %D-forms %swith %D components, size %D\n", PetscAbsInt(sp->k), sp->k < 0 ? "(stored in dual form) ": "", sp->Nc, pdim);CHKERRQ(ierr);
199b4457527SToby Isaac   } else {
200221d6281SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(v, "Dual space with %D components, size %D\n", sp->Nc, pdim);CHKERRQ(ierr);
201b4457527SToby Isaac   }
202221d6281SMatthew G. Knepley   if (sp->ops->view) {ierr = (*sp->ops->view)(sp, v);CHKERRQ(ierr);}
203221d6281SMatthew G. Knepley   ierr = PetscViewerGetFormat(v, &format);CHKERRQ(ierr);
204221d6281SMatthew G. Knepley   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
205221d6281SMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
206221d6281SMatthew G. Knepley     for (f = 0; f < pdim; ++f) {
207221d6281SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(v, "Dual basis vector %D\n", f);CHKERRQ(ierr);
208221d6281SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(v);CHKERRQ(ierr);
209221d6281SMatthew G. Knepley       ierr = PetscQuadratureView(sp->functional[f], v);CHKERRQ(ierr);
210221d6281SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
211221d6281SMatthew G. Knepley     }
212221d6281SMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
213221d6281SMatthew G. Knepley   }
214221d6281SMatthew G. Knepley   ierr = PetscViewerASCIIPopTab(v);CHKERRQ(ierr);
215221d6281SMatthew G. Knepley   PetscFunctionReturn(0);
216221d6281SMatthew G. Knepley }
217221d6281SMatthew G. Knepley 
218fe2efc57SMark /*@C
219fe2efc57SMark    PetscDualSpaceViewFromOptions - View from Options
220fe2efc57SMark 
221fe2efc57SMark    Collective on PetscDualSpace
222fe2efc57SMark 
223fe2efc57SMark    Input Parameters:
224fe2efc57SMark +  A - the PetscDualSpace object
225736c3998SJose E. Roman .  obj - Optional object, proivides prefix
226736c3998SJose E. Roman -  name - command line option
227fe2efc57SMark 
228fe2efc57SMark    Level: intermediate
229fe2efc57SMark .seealso:  PetscDualSpace, PetscDualSpaceView(), PetscObjectViewFromOptions(), PetscDualSpaceCreate()
230fe2efc57SMark @*/
231fe2efc57SMark PetscErrorCode  PetscDualSpaceViewFromOptions(PetscDualSpace A,PetscObject obj,const char name[])
232fe2efc57SMark {
233fe2efc57SMark   PetscErrorCode ierr;
234fe2efc57SMark 
235fe2efc57SMark   PetscFunctionBegin;
236fe2efc57SMark   PetscValidHeaderSpecific(A,PETSCDUALSPACE_CLASSID,1);
237fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr);
238fe2efc57SMark   PetscFunctionReturn(0);
239fe2efc57SMark }
240fe2efc57SMark 
24120cf1dd8SToby Isaac /*@
24220cf1dd8SToby Isaac   PetscDualSpaceView - Views a PetscDualSpace
24320cf1dd8SToby Isaac 
244d083f849SBarry Smith   Collective on sp
24520cf1dd8SToby Isaac 
24620cf1dd8SToby Isaac   Input Parameter:
24720cf1dd8SToby Isaac + sp - the PetscDualSpace object to view
24820cf1dd8SToby Isaac - v  - the viewer
24920cf1dd8SToby Isaac 
250a4ce7ad1SMatthew G. Knepley   Level: beginner
25120cf1dd8SToby Isaac 
252fe2efc57SMark .seealso PetscDualSpaceDestroy(), PetscDualSpace
25320cf1dd8SToby Isaac @*/
25420cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceView(PetscDualSpace sp, PetscViewer v)
25520cf1dd8SToby Isaac {
256d9bac1caSLisandro Dalcin   PetscBool      iascii;
25720cf1dd8SToby Isaac   PetscErrorCode ierr;
25820cf1dd8SToby Isaac 
25920cf1dd8SToby Isaac   PetscFunctionBegin;
26020cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
261d9bac1caSLisandro Dalcin   if (v) PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 2);
26220cf1dd8SToby Isaac   if (!v) {ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject) sp), &v);CHKERRQ(ierr);}
263d9bac1caSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) v, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
264221d6281SMatthew G. Knepley   if (iascii) {ierr = PetscDualSpaceView_ASCII(sp, v);CHKERRQ(ierr);}
26520cf1dd8SToby Isaac   PetscFunctionReturn(0);
26620cf1dd8SToby Isaac }
26720cf1dd8SToby Isaac 
26820cf1dd8SToby Isaac /*@
26920cf1dd8SToby Isaac   PetscDualSpaceSetFromOptions - sets parameters in a PetscDualSpace from the options database
27020cf1dd8SToby Isaac 
271d083f849SBarry Smith   Collective on sp
27220cf1dd8SToby Isaac 
27320cf1dd8SToby Isaac   Input Parameter:
27420cf1dd8SToby Isaac . sp - the PetscDualSpace object to set options for
27520cf1dd8SToby Isaac 
27620cf1dd8SToby Isaac   Options Database:
277*8f2aacc6SMatthew G. Knepley + -petscdualspace_order <order>      - the approximation order of the space
278*8f2aacc6SMatthew G. Knepley . -petscspace_form_degree <deg>      - the form degree, say 0 for point evaluations, or 2 for area integrals
279*8f2aacc6SMatthew G. Knepley . -petscdualspace_components <c>     - the number of components, say d for a vector field
280*8f2aacc6SMatthew G. Knepley . -petscdualspace_refdim <d>         - The spatial dimension of the reference cell
281*8f2aacc6SMatthew G. Knepley - -petscdualspace_refcell <celltype> - Reference cell type name
28220cf1dd8SToby Isaac 
283a4ce7ad1SMatthew G. Knepley   Level: intermediate
28420cf1dd8SToby Isaac 
285fe2efc57SMark .seealso PetscDualSpaceView(), PetscDualSpace, PetscObjectSetFromOptions()
28620cf1dd8SToby Isaac @*/
28720cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceSetFromOptions(PetscDualSpace sp)
28820cf1dd8SToby Isaac {
289063ee4adSMatthew G. Knepley   PetscDualSpaceReferenceCell refCell = PETSCDUALSPACE_REFCELL_SIMPLEX;
290063ee4adSMatthew G. Knepley   PetscInt                    refDim  = 0;
291063ee4adSMatthew G. Knepley   PetscBool                   flg;
29220cf1dd8SToby Isaac   const char                 *defaultType;
29320cf1dd8SToby Isaac   char                        name[256];
29420cf1dd8SToby Isaac   PetscErrorCode              ierr;
29520cf1dd8SToby Isaac 
29620cf1dd8SToby Isaac   PetscFunctionBegin;
29720cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
29820cf1dd8SToby Isaac   if (!((PetscObject) sp)->type_name) {
29920cf1dd8SToby Isaac     defaultType = PETSCDUALSPACELAGRANGE;
30020cf1dd8SToby Isaac   } else {
30120cf1dd8SToby Isaac     defaultType = ((PetscObject) sp)->type_name;
30220cf1dd8SToby Isaac   }
30320cf1dd8SToby Isaac   if (!PetscSpaceRegisterAllCalled) {ierr = PetscSpaceRegisterAll();CHKERRQ(ierr);}
30420cf1dd8SToby Isaac 
30520cf1dd8SToby Isaac   ierr = PetscObjectOptionsBegin((PetscObject) sp);CHKERRQ(ierr);
30620cf1dd8SToby Isaac   ierr = PetscOptionsFList("-petscdualspace_type", "Dual space", "PetscDualSpaceSetType", PetscDualSpaceList, defaultType, name, 256, &flg);CHKERRQ(ierr);
30720cf1dd8SToby Isaac   if (flg) {
30820cf1dd8SToby Isaac     ierr = PetscDualSpaceSetType(sp, name);CHKERRQ(ierr);
30920cf1dd8SToby Isaac   } else if (!((PetscObject) sp)->type_name) {
31020cf1dd8SToby Isaac     ierr = PetscDualSpaceSetType(sp, defaultType);CHKERRQ(ierr);
31120cf1dd8SToby Isaac   }
312b4457527SToby Isaac   ierr = PetscOptionsBoundedInt("-petscdualspace_order", "The approximation order", "PetscDualSpaceSetOrder", sp->order, &sp->order, NULL,0);CHKERRQ(ierr);
313b4457527SToby Isaac   ierr = PetscOptionsInt("-petscdualspace_form_degree", "The form degree of the dofs", "PetscDualSpaceSetFormDegree", sp->k, &sp->k, NULL);CHKERRQ(ierr);
3145a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-petscdualspace_components", "The number of components", "PetscDualSpaceSetNumComponents", sp->Nc, &sp->Nc, NULL,1);CHKERRQ(ierr);
31520cf1dd8SToby Isaac   if (sp->ops->setfromoptions) {
31620cf1dd8SToby Isaac     ierr = (*sp->ops->setfromoptions)(PetscOptionsObject,sp);CHKERRQ(ierr);
31720cf1dd8SToby Isaac   }
3185a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-petscdualspace_refdim", "The spatial dimension of the reference cell", "PetscDualSpaceSetReferenceCell", refDim, &refDim, NULL,0);CHKERRQ(ierr);
319063ee4adSMatthew G. Knepley   ierr = PetscOptionsEnum("-petscdualspace_refcell", "Reference cell", "PetscDualSpaceSetReferenceCell", PetscDualSpaceReferenceCells, (PetscEnum) refCell, (PetscEnum *) &refCell, &flg);CHKERRQ(ierr);
320063ee4adSMatthew G. Knepley   if (flg) {
321063ee4adSMatthew G. Knepley     DM K;
322063ee4adSMatthew G. Knepley 
323063ee4adSMatthew G. Knepley     if (!refDim) SETERRQ(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_INCOMP, "Reference cell specified without a dimension. Use -petscdualspace_refdim.");
324063ee4adSMatthew G. Knepley     ierr = PetscDualSpaceCreateReferenceCell(sp, refDim, refCell == PETSCDUALSPACE_REFCELL_SIMPLEX ? PETSC_TRUE : PETSC_FALSE, &K);CHKERRQ(ierr);
325063ee4adSMatthew G. Knepley     ierr = PetscDualSpaceSetDM(sp, K);CHKERRQ(ierr);
326063ee4adSMatthew G. Knepley     ierr = DMDestroy(&K);CHKERRQ(ierr);
327063ee4adSMatthew G. Knepley   }
328063ee4adSMatthew G. Knepley 
32920cf1dd8SToby Isaac   /* process any options handlers added with PetscObjectAddOptionsHandler() */
33020cf1dd8SToby Isaac   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) sp);CHKERRQ(ierr);
33120cf1dd8SToby Isaac   ierr = PetscOptionsEnd();CHKERRQ(ierr);
332063ee4adSMatthew G. Knepley   sp->setfromoptionscalled = PETSC_TRUE;
33320cf1dd8SToby Isaac   PetscFunctionReturn(0);
33420cf1dd8SToby Isaac }
33520cf1dd8SToby Isaac 
33620cf1dd8SToby Isaac /*@
33720cf1dd8SToby Isaac   PetscDualSpaceSetUp - Construct a basis for the PetscDualSpace
33820cf1dd8SToby Isaac 
339d083f849SBarry Smith   Collective on sp
34020cf1dd8SToby Isaac 
34120cf1dd8SToby Isaac   Input Parameter:
34220cf1dd8SToby Isaac . sp - the PetscDualSpace object to setup
34320cf1dd8SToby Isaac 
344a4ce7ad1SMatthew G. Knepley   Level: intermediate
34520cf1dd8SToby Isaac 
346fe2efc57SMark .seealso PetscDualSpaceView(), PetscDualSpaceDestroy(), PetscDualSpace
34720cf1dd8SToby Isaac @*/
34820cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceSetUp(PetscDualSpace sp)
34920cf1dd8SToby Isaac {
35020cf1dd8SToby Isaac   PetscErrorCode ierr;
35120cf1dd8SToby Isaac 
35220cf1dd8SToby Isaac   PetscFunctionBegin;
35320cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
35420cf1dd8SToby Isaac   if (sp->setupcalled) PetscFunctionReturn(0);
355ead873ccSMatthew G. Knepley   ierr = PetscLogEventBegin(PETSCDUALSPACE_SetUp, sp, 0, 0, 0);CHKERRQ(ierr);
35620cf1dd8SToby Isaac   sp->setupcalled = PETSC_TRUE;
35720cf1dd8SToby Isaac   if (sp->ops->setup) {ierr = (*sp->ops->setup)(sp);CHKERRQ(ierr);}
358ead873ccSMatthew G. Knepley   ierr = PetscLogEventEnd(PETSCDUALSPACE_SetUp, sp, 0, 0, 0);CHKERRQ(ierr);
359063ee4adSMatthew G. Knepley   if (sp->setfromoptionscalled) {ierr = PetscDualSpaceViewFromOptions(sp, NULL, "-petscdualspace_view");CHKERRQ(ierr);}
36020cf1dd8SToby Isaac   PetscFunctionReturn(0);
36120cf1dd8SToby Isaac }
36220cf1dd8SToby Isaac 
363b4457527SToby Isaac static PetscErrorCode PetscDualSpaceClearDMData_Internal(PetscDualSpace sp, DM dm)
364b4457527SToby Isaac {
365b4457527SToby Isaac   PetscInt       pStart = -1, pEnd = -1, depth = -1;
366b4457527SToby Isaac   PetscErrorCode ierr;
367b4457527SToby Isaac 
368b4457527SToby Isaac   PetscFunctionBegin;
369b4457527SToby Isaac   if (!dm) PetscFunctionReturn(0);
370b4457527SToby Isaac   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
371b4457527SToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
372b4457527SToby Isaac 
373b4457527SToby Isaac   if (sp->pointSpaces) {
374b4457527SToby Isaac     PetscInt i;
375b4457527SToby Isaac 
376b4457527SToby Isaac     for (i = 0; i < pEnd - pStart; i++) {
377b4457527SToby Isaac       ierr = PetscDualSpaceDestroy(&(sp->pointSpaces[i]));CHKERRQ(ierr);
378b4457527SToby Isaac     }
379b4457527SToby Isaac   }
380b4457527SToby Isaac   ierr = PetscFree(sp->pointSpaces);CHKERRQ(ierr);
381b4457527SToby Isaac 
382b4457527SToby Isaac   if (sp->heightSpaces) {
383b4457527SToby Isaac     PetscInt i;
384b4457527SToby Isaac 
385b4457527SToby Isaac     for (i = 0; i <= depth; i++) {
386b4457527SToby Isaac       ierr = PetscDualSpaceDestroy(&(sp->heightSpaces[i]));CHKERRQ(ierr);
387b4457527SToby Isaac     }
388b4457527SToby Isaac   }
389b4457527SToby Isaac   ierr = PetscFree(sp->heightSpaces);CHKERRQ(ierr);
390b4457527SToby Isaac 
391b4457527SToby Isaac   ierr = PetscSectionDestroy(&(sp->pointSection));CHKERRQ(ierr);
392b4457527SToby Isaac   ierr = PetscQuadratureDestroy(&(sp->intNodes));CHKERRQ(ierr);
393b4457527SToby Isaac   ierr = VecDestroy(&(sp->intDofValues));CHKERRQ(ierr);
394b4457527SToby Isaac   ierr = VecDestroy(&(sp->intNodeValues));CHKERRQ(ierr);
395b4457527SToby Isaac   ierr = MatDestroy(&(sp->intMat));CHKERRQ(ierr);
396b4457527SToby Isaac   ierr = PetscQuadratureDestroy(&(sp->allNodes));CHKERRQ(ierr);
397b4457527SToby Isaac   ierr = VecDestroy(&(sp->allDofValues));CHKERRQ(ierr);
398b4457527SToby Isaac   ierr = VecDestroy(&(sp->allNodeValues));CHKERRQ(ierr);
399b4457527SToby Isaac   ierr = MatDestroy(&(sp->allMat));CHKERRQ(ierr);
400b4457527SToby Isaac   ierr = PetscFree(sp->numDof);CHKERRQ(ierr);
401b4457527SToby Isaac   PetscFunctionReturn(0);
402b4457527SToby Isaac }
403b4457527SToby Isaac 
404b4457527SToby Isaac 
40520cf1dd8SToby Isaac /*@
40620cf1dd8SToby Isaac   PetscDualSpaceDestroy - Destroys a PetscDualSpace object
40720cf1dd8SToby Isaac 
408d083f849SBarry Smith   Collective on sp
40920cf1dd8SToby Isaac 
41020cf1dd8SToby Isaac   Input Parameter:
41120cf1dd8SToby Isaac . sp - the PetscDualSpace object to destroy
41220cf1dd8SToby Isaac 
413a4ce7ad1SMatthew G. Knepley   Level: beginner
41420cf1dd8SToby Isaac 
415fe2efc57SMark .seealso PetscDualSpaceView(), PetscDualSpace(), PetscDualSpaceCreate()
41620cf1dd8SToby Isaac @*/
41720cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceDestroy(PetscDualSpace *sp)
41820cf1dd8SToby Isaac {
41920cf1dd8SToby Isaac   PetscInt       dim, f;
420b4457527SToby Isaac   DM             dm;
42120cf1dd8SToby Isaac   PetscErrorCode ierr;
42220cf1dd8SToby Isaac 
42320cf1dd8SToby Isaac   PetscFunctionBegin;
42420cf1dd8SToby Isaac   if (!*sp) PetscFunctionReturn(0);
42520cf1dd8SToby Isaac   PetscValidHeaderSpecific((*sp), PETSCDUALSPACE_CLASSID, 1);
42620cf1dd8SToby Isaac 
427ea78f98cSLisandro Dalcin   if (--((PetscObject)(*sp))->refct > 0) {*sp = NULL; PetscFunctionReturn(0);}
42820cf1dd8SToby Isaac   ((PetscObject) (*sp))->refct = 0;
42920cf1dd8SToby Isaac 
43020cf1dd8SToby Isaac   ierr = PetscDualSpaceGetDimension(*sp, &dim);CHKERRQ(ierr);
431b4457527SToby Isaac   dm = (*sp)->dm;
432b4457527SToby Isaac 
433b4457527SToby Isaac   if ((*sp)->ops->destroy) {ierr = (*(*sp)->ops->destroy)(*sp);CHKERRQ(ierr);}
434b4457527SToby Isaac   ierr = PetscDualSpaceClearDMData_Internal(*sp, dm);CHKERRQ(ierr);
435b4457527SToby Isaac 
43620cf1dd8SToby Isaac   for (f = 0; f < dim; ++f) {
43720cf1dd8SToby Isaac     ierr = PetscQuadratureDestroy(&(*sp)->functional[f]);CHKERRQ(ierr);
43820cf1dd8SToby Isaac   }
43920cf1dd8SToby Isaac   ierr = PetscFree((*sp)->functional);CHKERRQ(ierr);
44020cf1dd8SToby Isaac   ierr = DMDestroy(&(*sp)->dm);CHKERRQ(ierr);
44120cf1dd8SToby Isaac   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
44220cf1dd8SToby Isaac   PetscFunctionReturn(0);
44320cf1dd8SToby Isaac }
44420cf1dd8SToby Isaac 
44520cf1dd8SToby Isaac /*@
44620cf1dd8SToby Isaac   PetscDualSpaceCreate - Creates an empty PetscDualSpace object. The type can then be set with PetscDualSpaceSetType().
44720cf1dd8SToby Isaac 
448d083f849SBarry Smith   Collective
44920cf1dd8SToby Isaac 
45020cf1dd8SToby Isaac   Input Parameter:
45120cf1dd8SToby Isaac . comm - The communicator for the PetscDualSpace object
45220cf1dd8SToby Isaac 
45320cf1dd8SToby Isaac   Output Parameter:
45420cf1dd8SToby Isaac . sp - The PetscDualSpace object
45520cf1dd8SToby Isaac 
45620cf1dd8SToby Isaac   Level: beginner
45720cf1dd8SToby Isaac 
45820cf1dd8SToby Isaac .seealso: PetscDualSpaceSetType(), PETSCDUALSPACELAGRANGE
45920cf1dd8SToby Isaac @*/
46020cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceCreate(MPI_Comm comm, PetscDualSpace *sp)
46120cf1dd8SToby Isaac {
46220cf1dd8SToby Isaac   PetscDualSpace s;
46320cf1dd8SToby Isaac   PetscErrorCode ierr;
46420cf1dd8SToby Isaac 
46520cf1dd8SToby Isaac   PetscFunctionBegin;
46620cf1dd8SToby Isaac   PetscValidPointer(sp, 2);
46720cf1dd8SToby Isaac   ierr = PetscCitationsRegister(FECitation,&FEcite);CHKERRQ(ierr);
46820cf1dd8SToby Isaac   *sp  = NULL;
46920cf1dd8SToby Isaac   ierr = PetscFEInitializePackage();CHKERRQ(ierr);
47020cf1dd8SToby Isaac 
47120cf1dd8SToby Isaac   ierr = PetscHeaderCreate(s, PETSCDUALSPACE_CLASSID, "PetscDualSpace", "Dual Space", "PetscDualSpace", comm, PetscDualSpaceDestroy, PetscDualSpaceView);CHKERRQ(ierr);
47220cf1dd8SToby Isaac 
47320cf1dd8SToby Isaac   s->order       = 0;
47420cf1dd8SToby Isaac   s->Nc          = 1;
4754bee2e38SMatthew G. Knepley   s->k           = 0;
476b4457527SToby Isaac   s->spdim       = -1;
477b4457527SToby Isaac   s->spintdim    = -1;
478b4457527SToby Isaac   s->uniform     = PETSC_TRUE;
47920cf1dd8SToby Isaac   s->setupcalled = PETSC_FALSE;
48020cf1dd8SToby Isaac 
48120cf1dd8SToby Isaac   *sp = s;
48220cf1dd8SToby Isaac   PetscFunctionReturn(0);
48320cf1dd8SToby Isaac }
48420cf1dd8SToby Isaac 
48520cf1dd8SToby Isaac /*@
48620cf1dd8SToby Isaac   PetscDualSpaceDuplicate - Creates a duplicate PetscDualSpace object, however it is not setup.
48720cf1dd8SToby Isaac 
488d083f849SBarry Smith   Collective on sp
48920cf1dd8SToby Isaac 
49020cf1dd8SToby Isaac   Input Parameter:
49120cf1dd8SToby Isaac . sp - The original PetscDualSpace
49220cf1dd8SToby Isaac 
49320cf1dd8SToby Isaac   Output Parameter:
49420cf1dd8SToby Isaac . spNew - The duplicate PetscDualSpace
49520cf1dd8SToby Isaac 
49620cf1dd8SToby Isaac   Level: beginner
49720cf1dd8SToby Isaac 
49820cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate(), PetscDualSpaceSetType()
49920cf1dd8SToby Isaac @*/
50020cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceDuplicate(PetscDualSpace sp, PetscDualSpace *spNew)
50120cf1dd8SToby Isaac {
502b4457527SToby Isaac   DM             dm;
503b4457527SToby Isaac   PetscDualSpaceType type;
504b4457527SToby Isaac   const char     *name;
50520cf1dd8SToby Isaac   PetscErrorCode ierr;
50620cf1dd8SToby Isaac 
50720cf1dd8SToby Isaac   PetscFunctionBegin;
50820cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
50920cf1dd8SToby Isaac   PetscValidPointer(spNew, 2);
510b4457527SToby Isaac   ierr = PetscDualSpaceCreate(PetscObjectComm((PetscObject)sp), spNew);CHKERRQ(ierr);
511b4457527SToby Isaac   ierr = PetscObjectGetName((PetscObject) sp,     &name);CHKERRQ(ierr);
512b4457527SToby Isaac   ierr = PetscObjectSetName((PetscObject) *spNew,  name);CHKERRQ(ierr);
513b4457527SToby Isaac   ierr = PetscDualSpaceGetType(sp, &type);CHKERRQ(ierr);
514b4457527SToby Isaac   ierr = PetscDualSpaceSetType(*spNew, type);CHKERRQ(ierr);
515b4457527SToby Isaac   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
516b4457527SToby Isaac   ierr = PetscDualSpaceSetDM(*spNew, dm);CHKERRQ(ierr);
517b4457527SToby Isaac 
518b4457527SToby Isaac   (*spNew)->order   = sp->order;
519b4457527SToby Isaac   (*spNew)->k       = sp->k;
520b4457527SToby Isaac   (*spNew)->Nc      = sp->Nc;
521b4457527SToby Isaac   (*spNew)->uniform = sp->uniform;
522b4457527SToby Isaac   if (sp->ops->duplicate) {ierr = (*sp->ops->duplicate)(sp, *spNew);CHKERRQ(ierr);}
52320cf1dd8SToby Isaac   PetscFunctionReturn(0);
52420cf1dd8SToby Isaac }
52520cf1dd8SToby Isaac 
52620cf1dd8SToby Isaac /*@
52720cf1dd8SToby Isaac   PetscDualSpaceGetDM - Get the DM representing the reference cell
52820cf1dd8SToby Isaac 
52920cf1dd8SToby Isaac   Not collective
53020cf1dd8SToby Isaac 
53120cf1dd8SToby Isaac   Input Parameter:
53220cf1dd8SToby Isaac . sp - The PetscDualSpace
53320cf1dd8SToby Isaac 
53420cf1dd8SToby Isaac   Output Parameter:
53520cf1dd8SToby Isaac . dm - The reference cell
53620cf1dd8SToby Isaac 
53720cf1dd8SToby Isaac   Level: intermediate
53820cf1dd8SToby Isaac 
53920cf1dd8SToby Isaac .seealso: PetscDualSpaceSetDM(), PetscDualSpaceCreate()
54020cf1dd8SToby Isaac @*/
54120cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetDM(PetscDualSpace sp, DM *dm)
54220cf1dd8SToby Isaac {
54320cf1dd8SToby Isaac   PetscFunctionBegin;
54420cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
54520cf1dd8SToby Isaac   PetscValidPointer(dm, 2);
54620cf1dd8SToby Isaac   *dm = sp->dm;
54720cf1dd8SToby Isaac   PetscFunctionReturn(0);
54820cf1dd8SToby Isaac }
54920cf1dd8SToby Isaac 
55020cf1dd8SToby Isaac /*@
55120cf1dd8SToby Isaac   PetscDualSpaceSetDM - Get the DM representing the reference cell
55220cf1dd8SToby Isaac 
55320cf1dd8SToby Isaac   Not collective
55420cf1dd8SToby Isaac 
55520cf1dd8SToby Isaac   Input Parameters:
55620cf1dd8SToby Isaac + sp - The PetscDualSpace
55720cf1dd8SToby Isaac - dm - The reference cell
55820cf1dd8SToby Isaac 
55920cf1dd8SToby Isaac   Level: intermediate
56020cf1dd8SToby Isaac 
56120cf1dd8SToby Isaac .seealso: PetscDualSpaceGetDM(), PetscDualSpaceCreate()
56220cf1dd8SToby Isaac @*/
56320cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceSetDM(PetscDualSpace sp, DM dm)
56420cf1dd8SToby Isaac {
56520cf1dd8SToby Isaac   PetscErrorCode ierr;
56620cf1dd8SToby Isaac 
56720cf1dd8SToby Isaac   PetscFunctionBegin;
56820cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
56920cf1dd8SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
570b4457527SToby Isaac   if (sp->setupcalled) SETERRQ(PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Cannot change DM after dualspace is set up");
57120cf1dd8SToby Isaac   ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
572b4457527SToby Isaac   if (sp->dm && sp->dm != dm) {
573b4457527SToby Isaac     ierr = PetscDualSpaceClearDMData_Internal(sp, sp->dm);CHKERRQ(ierr);
574b4457527SToby Isaac   }
575b4457527SToby Isaac   ierr = DMDestroy(&sp->dm);CHKERRQ(ierr);
57620cf1dd8SToby Isaac   sp->dm = dm;
57720cf1dd8SToby Isaac   PetscFunctionReturn(0);
57820cf1dd8SToby Isaac }
57920cf1dd8SToby Isaac 
58020cf1dd8SToby Isaac /*@
58120cf1dd8SToby Isaac   PetscDualSpaceGetOrder - Get the order of the dual space
58220cf1dd8SToby Isaac 
58320cf1dd8SToby Isaac   Not collective
58420cf1dd8SToby Isaac 
58520cf1dd8SToby Isaac   Input Parameter:
58620cf1dd8SToby Isaac . sp - The PetscDualSpace
58720cf1dd8SToby Isaac 
58820cf1dd8SToby Isaac   Output Parameter:
58920cf1dd8SToby Isaac . order - The order
59020cf1dd8SToby Isaac 
59120cf1dd8SToby Isaac   Level: intermediate
59220cf1dd8SToby Isaac 
59320cf1dd8SToby Isaac .seealso: PetscDualSpaceSetOrder(), PetscDualSpaceCreate()
59420cf1dd8SToby Isaac @*/
59520cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetOrder(PetscDualSpace sp, PetscInt *order)
59620cf1dd8SToby Isaac {
59720cf1dd8SToby Isaac   PetscFunctionBegin;
59820cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
59920cf1dd8SToby Isaac   PetscValidPointer(order, 2);
60020cf1dd8SToby Isaac   *order = sp->order;
60120cf1dd8SToby Isaac   PetscFunctionReturn(0);
60220cf1dd8SToby Isaac }
60320cf1dd8SToby Isaac 
60420cf1dd8SToby Isaac /*@
60520cf1dd8SToby Isaac   PetscDualSpaceSetOrder - Set the order of the dual space
60620cf1dd8SToby Isaac 
60720cf1dd8SToby Isaac   Not collective
60820cf1dd8SToby Isaac 
60920cf1dd8SToby Isaac   Input Parameters:
61020cf1dd8SToby Isaac + sp - The PetscDualSpace
61120cf1dd8SToby Isaac - order - The order
61220cf1dd8SToby Isaac 
61320cf1dd8SToby Isaac   Level: intermediate
61420cf1dd8SToby Isaac 
61520cf1dd8SToby Isaac .seealso: PetscDualSpaceGetOrder(), PetscDualSpaceCreate()
61620cf1dd8SToby Isaac @*/
61720cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceSetOrder(PetscDualSpace sp, PetscInt order)
61820cf1dd8SToby Isaac {
61920cf1dd8SToby Isaac   PetscFunctionBegin;
62020cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
621b4457527SToby Isaac   if (sp->setupcalled) SETERRQ(PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Cannot change order after dualspace is set up");
62220cf1dd8SToby Isaac   sp->order = order;
62320cf1dd8SToby Isaac   PetscFunctionReturn(0);
62420cf1dd8SToby Isaac }
62520cf1dd8SToby Isaac 
62620cf1dd8SToby Isaac /*@
62720cf1dd8SToby Isaac   PetscDualSpaceGetNumComponents - Return the number of components for this space
62820cf1dd8SToby Isaac 
62920cf1dd8SToby Isaac   Input Parameter:
63020cf1dd8SToby Isaac . sp - The PetscDualSpace
63120cf1dd8SToby Isaac 
63220cf1dd8SToby Isaac   Output Parameter:
63320cf1dd8SToby Isaac . Nc - The number of components
63420cf1dd8SToby Isaac 
63520cf1dd8SToby Isaac   Note: A vector space, for example, will have d components, where d is the spatial dimension
63620cf1dd8SToby Isaac 
63720cf1dd8SToby Isaac   Level: intermediate
63820cf1dd8SToby Isaac 
63920cf1dd8SToby Isaac .seealso: PetscDualSpaceSetNumComponents(), PetscDualSpaceGetDimension(), PetscDualSpaceCreate(), PetscDualSpace
64020cf1dd8SToby Isaac @*/
64120cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetNumComponents(PetscDualSpace sp, PetscInt *Nc)
64220cf1dd8SToby Isaac {
64320cf1dd8SToby Isaac   PetscFunctionBegin;
64420cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
64520cf1dd8SToby Isaac   PetscValidPointer(Nc, 2);
64620cf1dd8SToby Isaac   *Nc = sp->Nc;
64720cf1dd8SToby Isaac   PetscFunctionReturn(0);
64820cf1dd8SToby Isaac }
64920cf1dd8SToby Isaac 
65020cf1dd8SToby Isaac /*@
65120cf1dd8SToby Isaac   PetscDualSpaceSetNumComponents - Set the number of components for this space
65220cf1dd8SToby Isaac 
65320cf1dd8SToby Isaac   Input Parameters:
65420cf1dd8SToby Isaac + sp - The PetscDualSpace
65520cf1dd8SToby Isaac - order - The number of components
65620cf1dd8SToby Isaac 
65720cf1dd8SToby Isaac   Level: intermediate
65820cf1dd8SToby Isaac 
65920cf1dd8SToby Isaac .seealso: PetscDualSpaceGetNumComponents(), PetscDualSpaceCreate(), PetscDualSpace
66020cf1dd8SToby Isaac @*/
66120cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceSetNumComponents(PetscDualSpace sp, PetscInt Nc)
66220cf1dd8SToby Isaac {
66320cf1dd8SToby Isaac   PetscFunctionBegin;
66420cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
665b4457527SToby Isaac   if (sp->setupcalled) SETERRQ(PetscObjectComm((PetscObject)sp), PETSC_ERR_ARG_WRONGSTATE, "Cannot change number of components after dualspace is set up");
66620cf1dd8SToby Isaac   sp->Nc = Nc;
66720cf1dd8SToby Isaac   PetscFunctionReturn(0);
66820cf1dd8SToby Isaac }
66920cf1dd8SToby Isaac 
67020cf1dd8SToby Isaac /*@
67120cf1dd8SToby Isaac   PetscDualSpaceGetFunctional - Get the i-th basis functional in the dual space
67220cf1dd8SToby Isaac 
67320cf1dd8SToby Isaac   Not collective
67420cf1dd8SToby Isaac 
67520cf1dd8SToby Isaac   Input Parameters:
67620cf1dd8SToby Isaac + sp - The PetscDualSpace
67720cf1dd8SToby Isaac - i  - The basis number
67820cf1dd8SToby Isaac 
67920cf1dd8SToby Isaac   Output Parameter:
68020cf1dd8SToby Isaac . functional - The basis functional
68120cf1dd8SToby Isaac 
68220cf1dd8SToby Isaac   Level: intermediate
68320cf1dd8SToby Isaac 
68420cf1dd8SToby Isaac .seealso: PetscDualSpaceGetDimension(), PetscDualSpaceCreate()
68520cf1dd8SToby Isaac @*/
68620cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetFunctional(PetscDualSpace sp, PetscInt i, PetscQuadrature *functional)
68720cf1dd8SToby Isaac {
68820cf1dd8SToby Isaac   PetscInt       dim;
68920cf1dd8SToby Isaac   PetscErrorCode ierr;
69020cf1dd8SToby Isaac 
69120cf1dd8SToby Isaac   PetscFunctionBegin;
69220cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
69320cf1dd8SToby Isaac   PetscValidPointer(functional, 3);
69420cf1dd8SToby Isaac   ierr = PetscDualSpaceGetDimension(sp, &dim);CHKERRQ(ierr);
69520cf1dd8SToby Isaac   if ((i < 0) || (i >= dim)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Functional index %d must be in [0, %d)", i, dim);
69620cf1dd8SToby Isaac   *functional = sp->functional[i];
69720cf1dd8SToby Isaac   PetscFunctionReturn(0);
69820cf1dd8SToby Isaac }
69920cf1dd8SToby Isaac 
70020cf1dd8SToby Isaac /*@
70120cf1dd8SToby Isaac   PetscDualSpaceGetDimension - Get the dimension of the dual space, i.e. the number of basis functionals
70220cf1dd8SToby Isaac 
70320cf1dd8SToby Isaac   Not collective
70420cf1dd8SToby Isaac 
70520cf1dd8SToby Isaac   Input Parameter:
70620cf1dd8SToby Isaac . sp - The PetscDualSpace
70720cf1dd8SToby Isaac 
70820cf1dd8SToby Isaac   Output Parameter:
70920cf1dd8SToby Isaac . dim - The dimension
71020cf1dd8SToby Isaac 
71120cf1dd8SToby Isaac   Level: intermediate
71220cf1dd8SToby Isaac 
71320cf1dd8SToby Isaac .seealso: PetscDualSpaceGetFunctional(), PetscDualSpaceCreate()
71420cf1dd8SToby Isaac @*/
71520cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetDimension(PetscDualSpace sp, PetscInt *dim)
71620cf1dd8SToby Isaac {
71720cf1dd8SToby Isaac   PetscErrorCode ierr;
71820cf1dd8SToby Isaac 
71920cf1dd8SToby Isaac   PetscFunctionBegin;
72020cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
72120cf1dd8SToby Isaac   PetscValidPointer(dim, 2);
722b4457527SToby Isaac   if (sp->spdim < 0) {
723b4457527SToby Isaac     PetscSection section;
724b4457527SToby Isaac 
725b4457527SToby Isaac     ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
726b4457527SToby Isaac     if (section) {
727b4457527SToby Isaac       ierr = PetscSectionGetStorageSize(section, &(sp->spdim));CHKERRQ(ierr);
728b4457527SToby Isaac     } else sp->spdim = 0;
729b4457527SToby Isaac   }
730b4457527SToby Isaac   *dim = sp->spdim;
73120cf1dd8SToby Isaac   PetscFunctionReturn(0);
73220cf1dd8SToby Isaac }
73320cf1dd8SToby Isaac 
734b4457527SToby Isaac /*@
735b4457527SToby Isaac   PetscDualSpaceGetInteriorDimension - Get the interior dimension of the dual space, i.e. the number of basis functionals assigned to the interior of the reference domain
736b4457527SToby Isaac 
737b4457527SToby Isaac   Not collective
738b4457527SToby Isaac 
739b4457527SToby Isaac   Input Parameter:
740b4457527SToby Isaac . sp - The PetscDualSpace
741b4457527SToby Isaac 
742b4457527SToby Isaac   Output Parameter:
743b4457527SToby Isaac . dim - The dimension
744b4457527SToby Isaac 
745b4457527SToby Isaac   Level: intermediate
746b4457527SToby Isaac 
747b4457527SToby Isaac .seealso: PetscDualSpaceGetFunctional(), PetscDualSpaceCreate()
748b4457527SToby Isaac @*/
749b4457527SToby Isaac PetscErrorCode PetscDualSpaceGetInteriorDimension(PetscDualSpace sp, PetscInt *intdim)
750b4457527SToby Isaac {
751b4457527SToby Isaac   PetscErrorCode ierr;
752b4457527SToby Isaac 
753b4457527SToby Isaac   PetscFunctionBegin;
754b4457527SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
755b4457527SToby Isaac   PetscValidPointer(intdim, 2);
756b4457527SToby Isaac   if (sp->spintdim < 0) {
757b4457527SToby Isaac     PetscSection section;
758b4457527SToby Isaac 
759b4457527SToby Isaac     ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
760b4457527SToby Isaac     if (section) {
761b4457527SToby Isaac       ierr = PetscSectionGetConstrainedStorageSize(section, &(sp->spintdim));CHKERRQ(ierr);
762b4457527SToby Isaac     } else sp->spintdim = 0;
763b4457527SToby Isaac   }
764b4457527SToby Isaac   *intdim = sp->spintdim;
765b4457527SToby Isaac   PetscFunctionReturn(0);
766b4457527SToby Isaac }
767b4457527SToby Isaac 
768b4457527SToby Isaac /*@
769b4457527SToby Isaac    PetscDualSpaceGetUniform - Whether this dual space is uniform
770b4457527SToby Isaac 
771b4457527SToby Isaac    Not collective
772b4457527SToby Isaac 
773b4457527SToby Isaac    Input Parameters:
774b4457527SToby Isaac .  sp - A dual space
775b4457527SToby Isaac 
776b4457527SToby Isaac    Output Parameters:
777b4457527SToby Isaac .  uniform - PETSC_TRUE if (a) the dual space is the same for each point in a stratum of the reference DMPlex, and
778b4457527SToby Isaac              (b) every symmetry of each point in the reference DMPlex is also a symmetry of the point's dual space.
779b4457527SToby Isaac 
780b4457527SToby Isaac 
781b4457527SToby Isaac    Level: advanced
782b4457527SToby Isaac 
783b4457527SToby Isaac    Note: all of the usual spaces on simplex or tensor-product elements will be uniform, only reference cells
784b4457527SToby Isaac    with non-uniform strata (like trianguar-prisms) or anisotropic hp dual spaces will not be uniform.
785b4457527SToby Isaac 
786b4457527SToby Isaac .seealso: PetscDualSpaceGetPointSubspace(), PetscDualSpaceGetSymmetries()
787b4457527SToby Isaac @*/
788b4457527SToby Isaac PetscErrorCode PetscDualSpaceGetUniform(PetscDualSpace sp, PetscBool *uniform)
789b4457527SToby Isaac {
790b4457527SToby Isaac   PetscFunctionBegin;
791b4457527SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
792b4457527SToby Isaac   PetscValidPointer(uniform, 2);
793b4457527SToby Isaac   *uniform = sp->uniform;
794b4457527SToby Isaac   PetscFunctionReturn(0);
795b4457527SToby Isaac }
796b4457527SToby Isaac 
797b4457527SToby Isaac 
79820cf1dd8SToby Isaac /*@C
79920cf1dd8SToby Isaac   PetscDualSpaceGetNumDof - Get the number of degrees of freedom for each spatial (topological) dimension
80020cf1dd8SToby Isaac 
80120cf1dd8SToby Isaac   Not collective
80220cf1dd8SToby Isaac 
80320cf1dd8SToby Isaac   Input Parameter:
80420cf1dd8SToby Isaac . sp - The PetscDualSpace
80520cf1dd8SToby Isaac 
80620cf1dd8SToby Isaac   Output Parameter:
80720cf1dd8SToby Isaac . numDof - An array of length dim+1 which holds the number of dofs for each dimension
80820cf1dd8SToby Isaac 
80920cf1dd8SToby Isaac   Level: intermediate
81020cf1dd8SToby Isaac 
81120cf1dd8SToby Isaac .seealso: PetscDualSpaceGetFunctional(), PetscDualSpaceCreate()
81220cf1dd8SToby Isaac @*/
81320cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetNumDof(PetscDualSpace sp, const PetscInt **numDof)
81420cf1dd8SToby Isaac {
81520cf1dd8SToby Isaac   PetscErrorCode ierr;
81620cf1dd8SToby Isaac 
81720cf1dd8SToby Isaac   PetscFunctionBegin;
81820cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
81920cf1dd8SToby Isaac   PetscValidPointer(numDof, 2);
820b4457527SToby Isaac   if (!sp->uniform) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "A non-uniform space does not have a fixed number of dofs for each height");
821b4457527SToby Isaac   if (!sp->numDof) {
822b4457527SToby Isaac     DM       dm;
823b4457527SToby Isaac     PetscInt depth, d;
824b4457527SToby Isaac     PetscSection section;
825b4457527SToby Isaac 
826b4457527SToby Isaac     ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
827b4457527SToby Isaac     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
828b4457527SToby Isaac     ierr = PetscCalloc1(depth+1,&(sp->numDof));CHKERRQ(ierr);
829b4457527SToby Isaac     ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
830b4457527SToby Isaac     for (d = 0; d <= depth; d++) {
831b4457527SToby Isaac       PetscInt dStart, dEnd;
832b4457527SToby Isaac 
833b4457527SToby Isaac       ierr = DMPlexGetDepthStratum(dm, d, &dStart, &dEnd);CHKERRQ(ierr);
834b4457527SToby Isaac       if (dEnd <= dStart) continue;
835b4457527SToby Isaac       ierr = PetscSectionGetDof(section, dStart, &(sp->numDof[d]));CHKERRQ(ierr);
836b4457527SToby Isaac 
837b4457527SToby Isaac     }
838b4457527SToby Isaac   }
839b4457527SToby Isaac   *numDof = sp->numDof;
84020cf1dd8SToby Isaac   if (!*numDof) SETERRQ(PetscObjectComm((PetscObject) sp), PETSC_ERR_LIB, "Empty numDof[] returned from dual space implementation");
84120cf1dd8SToby Isaac   PetscFunctionReturn(0);
84220cf1dd8SToby Isaac }
84320cf1dd8SToby Isaac 
844b4457527SToby Isaac /* create the section of the right size and set a permutation for topological ordering */
845b4457527SToby Isaac PetscErrorCode PetscDualSpaceSectionCreate_Internal(PetscDualSpace sp, PetscSection *topSection)
846b4457527SToby Isaac {
847b4457527SToby Isaac   DM             dm;
848b4457527SToby Isaac   PetscInt       pStart, pEnd, cStart, cEnd, c, depth, count, i;
849b4457527SToby Isaac   PetscInt       *seen, *perm;
850b4457527SToby Isaac   PetscSection   section;
851b4457527SToby Isaac   PetscErrorCode ierr;
852b4457527SToby Isaac 
853b4457527SToby Isaac   PetscFunctionBegin;
854b4457527SToby Isaac   dm = sp->dm;
855b4457527SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF, &section);CHKERRQ(ierr);
856b4457527SToby Isaac   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
857b4457527SToby Isaac   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
858b4457527SToby Isaac   ierr = PetscCalloc1(pEnd - pStart, &seen);CHKERRQ(ierr);
859b4457527SToby Isaac   ierr = PetscMalloc1(pEnd - pStart, &perm);CHKERRQ(ierr);
860b4457527SToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
861b4457527SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
862b4457527SToby Isaac   for (c = cStart, count = 0; c < cEnd; c++) {
863b4457527SToby Isaac     PetscInt closureSize = -1, e;
864b4457527SToby Isaac     PetscInt *closure = NULL;
865b4457527SToby Isaac 
866b4457527SToby Isaac     perm[count++] = c;
867b4457527SToby Isaac     seen[c-pStart] = 1;
868b4457527SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
869b4457527SToby Isaac     for (e = 0; e < closureSize; e++) {
870b4457527SToby Isaac       PetscInt point = closure[2*e];
871b4457527SToby Isaac 
872b4457527SToby Isaac       if (seen[point-pStart]) continue;
873b4457527SToby Isaac       perm[count++] = point;
874b4457527SToby Isaac       seen[point-pStart] = 1;
875b4457527SToby Isaac     }
876b4457527SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
877b4457527SToby Isaac   }
878b4457527SToby Isaac   if (count != pEnd - pStart) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Bad topological ordering");
879b4457527SToby Isaac   for (i = 0; i < pEnd - pStart; i++) if (perm[i] != i) break;
880b4457527SToby Isaac   if (i < pEnd - pStart) {
881b4457527SToby Isaac     IS permIS;
882b4457527SToby Isaac 
883b4457527SToby Isaac     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd - pStart, perm, PETSC_OWN_POINTER, &permIS);CHKERRQ(ierr);
884b4457527SToby Isaac     ierr = ISSetPermutation(permIS);CHKERRQ(ierr);
885b4457527SToby Isaac     ierr = PetscSectionSetPermutation(section, permIS);CHKERRQ(ierr);
886b4457527SToby Isaac     ierr = ISDestroy(&permIS);CHKERRQ(ierr);
887b4457527SToby Isaac   } else {
888b4457527SToby Isaac     ierr = PetscFree(perm);CHKERRQ(ierr);
889b4457527SToby Isaac   }
890b4457527SToby Isaac   ierr = PetscFree(seen);CHKERRQ(ierr);
891b4457527SToby Isaac   *topSection = section;
892b4457527SToby Isaac   PetscFunctionReturn(0);
893b4457527SToby Isaac }
894b4457527SToby Isaac 
895b4457527SToby Isaac /* mark boundary points and set up */
896b4457527SToby Isaac PetscErrorCode PetscDualSpaceSectionSetUp_Internal(PetscDualSpace sp, PetscSection section)
897b4457527SToby Isaac {
898b4457527SToby Isaac   DM             dm;
899b4457527SToby Isaac   DMLabel        boundary;
900b4457527SToby Isaac   PetscInt       pStart, pEnd, p;
901b4457527SToby Isaac   PetscErrorCode ierr;
902b4457527SToby Isaac 
903b4457527SToby Isaac   PetscFunctionBegin;
904b4457527SToby Isaac   dm = sp->dm;
905b4457527SToby Isaac   ierr = DMLabelCreate(PETSC_COMM_SELF,"boundary",&boundary);CHKERRQ(ierr);
906b4457527SToby Isaac   ierr = PetscDualSpaceGetDM(sp,&dm);CHKERRQ(ierr);
907b4457527SToby Isaac   ierr = DMPlexMarkBoundaryFaces(dm,1,boundary);CHKERRQ(ierr);
908b4457527SToby Isaac   ierr = DMPlexLabelComplete(dm,boundary);CHKERRQ(ierr);
909b4457527SToby Isaac   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
910b4457527SToby Isaac   for (p = pStart; p < pEnd; p++) {
911b4457527SToby Isaac     PetscInt bval;
912b4457527SToby Isaac 
913b4457527SToby Isaac     ierr = DMLabelGetValue(boundary, p, &bval);CHKERRQ(ierr);
914b4457527SToby Isaac     if (bval == 1) {
915b4457527SToby Isaac       PetscInt dof;
916b4457527SToby Isaac 
917b4457527SToby Isaac       ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
918b4457527SToby Isaac       ierr = PetscSectionSetConstraintDof(section, p, dof);CHKERRQ(ierr);
919b4457527SToby Isaac     }
920b4457527SToby Isaac   }
921b4457527SToby Isaac   ierr = DMLabelDestroy(&boundary);CHKERRQ(ierr);
922b4457527SToby Isaac   ierr = PetscSectionSetUp(section);
923b4457527SToby Isaac   PetscFunctionReturn(0);
924b4457527SToby Isaac }
925b4457527SToby Isaac 
926a4ce7ad1SMatthew G. Knepley /*@
927b4457527SToby Isaac   PetscDualSpaceGetSection - Create a PetscSection over the reference cell with the layout from this space
928a4ce7ad1SMatthew G. Knepley 
929a4ce7ad1SMatthew G. Knepley   Collective on sp
930a4ce7ad1SMatthew G. Knepley 
931a4ce7ad1SMatthew G. Knepley   Input Parameters:
932f0fc11ceSJed Brown . sp      - The PetscDualSpace
933a4ce7ad1SMatthew G. Knepley 
934a4ce7ad1SMatthew G. Knepley   Output Parameter:
935a4ce7ad1SMatthew G. Knepley . section - The section
936a4ce7ad1SMatthew G. Knepley 
937a4ce7ad1SMatthew G. Knepley   Level: advanced
938a4ce7ad1SMatthew G. Knepley 
939a4ce7ad1SMatthew G. Knepley .seealso: PetscDualSpaceCreate(), DMPLEX
940a4ce7ad1SMatthew G. Knepley @*/
941b4457527SToby Isaac PetscErrorCode PetscDualSpaceGetSection(PetscDualSpace sp, PetscSection *section)
94220cf1dd8SToby Isaac {
943b4457527SToby Isaac   PetscInt       pStart, pEnd, p;
944b4457527SToby Isaac   PetscErrorCode ierr;
945b4457527SToby Isaac 
946b4457527SToby Isaac   PetscFunctionBegin;
947b4457527SToby Isaac   if (!sp->pointSection) {
948b4457527SToby Isaac     /* mark the boundary */
949b4457527SToby Isaac     ierr = PetscDualSpaceSectionCreate_Internal(sp, &(sp->pointSection));CHKERRQ(ierr);
950b4457527SToby Isaac     ierr = DMPlexGetChart(sp->dm,&pStart,&pEnd);CHKERRQ(ierr);
951b4457527SToby Isaac     for (p = pStart; p < pEnd; p++) {
952b4457527SToby Isaac       PetscDualSpace psp;
953b4457527SToby Isaac 
954b4457527SToby Isaac       ierr = PetscDualSpaceGetPointSubspace(sp, p, &psp);CHKERRQ(ierr);
955b4457527SToby Isaac       if (psp) {
956b4457527SToby Isaac         PetscInt dof;
957b4457527SToby Isaac 
958b4457527SToby Isaac         ierr = PetscDualSpaceGetInteriorDimension(psp, &dof);CHKERRQ(ierr);
959b4457527SToby Isaac         ierr = PetscSectionSetDof(sp->pointSection,p,dof);CHKERRQ(ierr);
960b4457527SToby Isaac       }
961b4457527SToby Isaac     }
962b4457527SToby Isaac     ierr = PetscDualSpaceSectionSetUp_Internal(sp,sp->pointSection);CHKERRQ(ierr);
963b4457527SToby Isaac   }
964b4457527SToby Isaac   *section = sp->pointSection;
965b4457527SToby Isaac   PetscFunctionReturn(0);
966b4457527SToby Isaac }
967b4457527SToby Isaac 
968b4457527SToby Isaac /* this assumes that all of the point dual spaces store their interior dofs first, which is true when the point DMs
969b4457527SToby Isaac  * have one cell */
970b4457527SToby Isaac PetscErrorCode PetscDualSpacePushForwardSubspaces_Internal(PetscDualSpace sp, PetscInt sStart, PetscInt sEnd)
971b4457527SToby Isaac {
972b4457527SToby Isaac   PetscReal *sv0, *v0, *J;
973b4457527SToby Isaac   PetscSection section;
974b4457527SToby Isaac   PetscInt     dim, s, k;
97520cf1dd8SToby Isaac   DM             dm;
97620cf1dd8SToby Isaac   PetscErrorCode ierr;
97720cf1dd8SToby Isaac 
97820cf1dd8SToby Isaac   PetscFunctionBegin;
97920cf1dd8SToby Isaac   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
980b4457527SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
981b4457527SToby Isaac   ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
982b4457527SToby Isaac   ierr = PetscMalloc3(dim, &v0, dim, &sv0, dim*dim, &J);CHKERRQ(ierr);
983b4457527SToby Isaac   ierr = PetscDualSpaceGetFormDegree(sp, &k);CHKERRQ(ierr);
984b4457527SToby Isaac   for (s = sStart; s < sEnd; s++) {
985b4457527SToby Isaac     PetscReal detJ, hdetJ;
986b4457527SToby Isaac     PetscDualSpace ssp;
987b4457527SToby Isaac     PetscInt dof, off, f, sdim;
988b4457527SToby Isaac     PetscInt i, j;
989b4457527SToby Isaac     DM sdm;
99020cf1dd8SToby Isaac 
991b4457527SToby Isaac     ierr = PetscDualSpaceGetPointSubspace(sp, s, &ssp);CHKERRQ(ierr);
992b4457527SToby Isaac     if (!ssp) continue;
993b4457527SToby Isaac     ierr = PetscSectionGetDof(section, s, &dof);CHKERRQ(ierr);
994b4457527SToby Isaac     ierr = PetscSectionGetOffset(section, s, &off);CHKERRQ(ierr);
995b4457527SToby Isaac     /* get the first vertex of the reference cell */
996b4457527SToby Isaac     ierr = PetscDualSpaceGetDM(ssp, &sdm);CHKERRQ(ierr);
997b4457527SToby Isaac     ierr = DMGetDimension(sdm, &sdim);CHKERRQ(ierr);
998b4457527SToby Isaac     ierr = DMPlexComputeCellGeometryAffineFEM(sdm, 0, sv0, NULL, NULL, &hdetJ);CHKERRQ(ierr);
999b4457527SToby Isaac     ierr = DMPlexComputeCellGeometryAffineFEM(dm, s, v0, J, NULL, &detJ);CHKERRQ(ierr);
1000b4457527SToby Isaac     /* compactify Jacobian */
1001b4457527SToby Isaac     for (i = 0; i < dim; i++) for (j = 0; j < sdim; j++) J[i* sdim + j] = J[i * dim + j];
1002b4457527SToby Isaac     for (f = 0; f < dof; f++) {
1003b4457527SToby Isaac       PetscQuadrature fn;
100420cf1dd8SToby Isaac 
1005b4457527SToby Isaac       ierr = PetscDualSpaceGetFunctional(ssp, f, &fn);CHKERRQ(ierr);
1006b4457527SToby Isaac       ierr = PetscQuadraturePushForward(fn, dim, sv0, v0, J, k, &(sp->functional[off+f]));CHKERRQ(ierr);
100720cf1dd8SToby Isaac     }
100820cf1dd8SToby Isaac   }
1009b4457527SToby Isaac   ierr = PetscFree3(v0, sv0, J);CHKERRQ(ierr);
101020cf1dd8SToby Isaac   PetscFunctionReturn(0);
101120cf1dd8SToby Isaac }
101220cf1dd8SToby Isaac 
101320cf1dd8SToby Isaac /*@
101420cf1dd8SToby Isaac   PetscDualSpaceCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
101520cf1dd8SToby Isaac 
1016d083f849SBarry Smith   Collective on sp
101720cf1dd8SToby Isaac 
101820cf1dd8SToby Isaac   Input Parameters:
101920cf1dd8SToby Isaac + sp      - The PetscDualSpace
102020cf1dd8SToby Isaac . dim     - The spatial dimension
102120cf1dd8SToby Isaac - simplex - Flag for simplex, otherwise use a tensor-product cell
102220cf1dd8SToby Isaac 
102320cf1dd8SToby Isaac   Output Parameter:
102420cf1dd8SToby Isaac . refdm - The reference cell
102520cf1dd8SToby Isaac 
1026a4ce7ad1SMatthew G. Knepley   Level: intermediate
102720cf1dd8SToby Isaac 
102820cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate(), DMPLEX
102920cf1dd8SToby Isaac @*/
103020cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceCreateReferenceCell(PetscDualSpace sp, PetscInt dim, PetscBool simplex, DM *refdm)
103120cf1dd8SToby Isaac {
103220cf1dd8SToby Isaac   PetscErrorCode ierr;
103320cf1dd8SToby Isaac 
103420cf1dd8SToby Isaac   PetscFunctionBeginUser;
103520cf1dd8SToby Isaac   ierr = DMPlexCreateReferenceCell(PetscObjectComm((PetscObject) sp), dim, simplex, refdm);CHKERRQ(ierr);
103620cf1dd8SToby Isaac   PetscFunctionReturn(0);
103720cf1dd8SToby Isaac }
103820cf1dd8SToby Isaac 
103920cf1dd8SToby Isaac /*@C
104020cf1dd8SToby Isaac   PetscDualSpaceApply - Apply a functional from the dual space basis to an input function
104120cf1dd8SToby Isaac 
104220cf1dd8SToby Isaac   Input Parameters:
104320cf1dd8SToby Isaac + sp      - The PetscDualSpace object
104420cf1dd8SToby Isaac . f       - The basis functional index
104520cf1dd8SToby Isaac . time    - The time
104620cf1dd8SToby Isaac . cgeom   - A context with geometric information for this cell, we use v0 (the initial vertex) and J (the Jacobian) (or evaluated at the coordinates of the functional)
104720cf1dd8SToby Isaac . numComp - The number of components for the function
104820cf1dd8SToby Isaac . func    - The input function
104920cf1dd8SToby Isaac - ctx     - A context for the function
105020cf1dd8SToby Isaac 
105120cf1dd8SToby Isaac   Output Parameter:
105220cf1dd8SToby Isaac . value   - numComp output values
105320cf1dd8SToby Isaac 
105420cf1dd8SToby Isaac   Note: The calling sequence for the callback func is given by:
105520cf1dd8SToby Isaac 
105620cf1dd8SToby Isaac $ func(PetscInt dim, PetscReal time, const PetscReal x[],
105720cf1dd8SToby Isaac $      PetscInt numComponents, PetscScalar values[], void *ctx)
105820cf1dd8SToby Isaac 
1059a4ce7ad1SMatthew G. Knepley   Level: beginner
106020cf1dd8SToby Isaac 
106120cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate()
106220cf1dd8SToby Isaac @*/
106320cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceApply(PetscDualSpace sp, PetscInt f, PetscReal time, PetscFEGeom *cgeom, PetscInt numComp, PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void *ctx, PetscScalar *value)
106420cf1dd8SToby Isaac {
106520cf1dd8SToby Isaac   PetscErrorCode ierr;
106620cf1dd8SToby Isaac 
106720cf1dd8SToby Isaac   PetscFunctionBegin;
106820cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
106920cf1dd8SToby Isaac   PetscValidPointer(cgeom, 4);
107020cf1dd8SToby Isaac   PetscValidPointer(value, 8);
107120cf1dd8SToby Isaac   ierr = (*sp->ops->apply)(sp, f, time, cgeom, numComp, func, ctx, value);CHKERRQ(ierr);
107220cf1dd8SToby Isaac   PetscFunctionReturn(0);
107320cf1dd8SToby Isaac }
107420cf1dd8SToby Isaac 
107520cf1dd8SToby Isaac /*@C
1076b4457527SToby Isaac   PetscDualSpaceApplyAll - Apply all functionals from the dual space basis to the result of an evaluation at the points returned by PetscDualSpaceGetAllData()
107720cf1dd8SToby Isaac 
107820cf1dd8SToby Isaac   Input Parameters:
107920cf1dd8SToby Isaac + sp        - The PetscDualSpace object
1080b4457527SToby Isaac - pointEval - Evaluation at the points returned by PetscDualSpaceGetAllData()
108120cf1dd8SToby Isaac 
108220cf1dd8SToby Isaac   Output Parameter:
108320cf1dd8SToby Isaac . spValue   - The values of all dual space functionals
108420cf1dd8SToby Isaac 
1085a4ce7ad1SMatthew G. Knepley   Level: beginner
108620cf1dd8SToby Isaac 
108720cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate()
108820cf1dd8SToby Isaac @*/
108920cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceApplyAll(PetscDualSpace sp, const PetscScalar *pointEval, PetscScalar *spValue)
109020cf1dd8SToby Isaac {
109120cf1dd8SToby Isaac   PetscErrorCode ierr;
109220cf1dd8SToby Isaac 
109320cf1dd8SToby Isaac   PetscFunctionBegin;
109420cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
109520cf1dd8SToby Isaac   ierr = (*sp->ops->applyall)(sp, pointEval, spValue);CHKERRQ(ierr);
109620cf1dd8SToby Isaac   PetscFunctionReturn(0);
109720cf1dd8SToby Isaac }
109820cf1dd8SToby Isaac 
109920cf1dd8SToby Isaac /*@C
1100b4457527SToby Isaac   PetscDualSpaceApplyInterior - Apply interior functionals from the dual space basis to the result of an evaluation at the points returned by PetscDualSpaceGetInteriorData()
1101b4457527SToby Isaac 
1102b4457527SToby Isaac   Input Parameters:
1103b4457527SToby Isaac + sp        - The PetscDualSpace object
1104b4457527SToby Isaac - pointEval - Evaluation at the points returned by PetscDualSpaceGetInteriorData()
1105b4457527SToby Isaac 
1106b4457527SToby Isaac   Output Parameter:
1107b4457527SToby Isaac . spValue   - The values of interior dual space functionals
1108b4457527SToby Isaac 
1109b4457527SToby Isaac   Level: beginner
1110b4457527SToby Isaac 
1111b4457527SToby Isaac .seealso: PetscDualSpaceCreate()
1112b4457527SToby Isaac @*/
1113b4457527SToby Isaac PetscErrorCode PetscDualSpaceApplyInterior(PetscDualSpace sp, const PetscScalar *pointEval, PetscScalar *spValue)
1114b4457527SToby Isaac {
1115b4457527SToby Isaac   PetscErrorCode ierr;
1116b4457527SToby Isaac 
1117b4457527SToby Isaac   PetscFunctionBegin;
1118b4457527SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
1119b4457527SToby Isaac   ierr = (*sp->ops->applyint)(sp, pointEval, spValue);CHKERRQ(ierr);
1120b4457527SToby Isaac   PetscFunctionReturn(0);
1121b4457527SToby Isaac }
1122b4457527SToby Isaac 
1123b4457527SToby Isaac /*@C
112420cf1dd8SToby Isaac   PetscDualSpaceApplyDefault - Apply a functional from the dual space basis to an input function by assuming a point evaluation functional.
112520cf1dd8SToby Isaac 
112620cf1dd8SToby Isaac   Input Parameters:
112720cf1dd8SToby Isaac + sp    - The PetscDualSpace object
112820cf1dd8SToby Isaac . f     - The basis functional index
112920cf1dd8SToby Isaac . time  - The time
113020cf1dd8SToby Isaac . cgeom - A context with geometric information for this cell, we use v0 (the initial vertex) and J (the Jacobian)
113120cf1dd8SToby Isaac . Nc    - The number of components for the function
113220cf1dd8SToby Isaac . func  - The input function
113320cf1dd8SToby Isaac - ctx   - A context for the function
113420cf1dd8SToby Isaac 
113520cf1dd8SToby Isaac   Output Parameter:
113620cf1dd8SToby Isaac . value   - The output value
113720cf1dd8SToby Isaac 
113820cf1dd8SToby Isaac   Note: The calling sequence for the callback func is given by:
113920cf1dd8SToby Isaac 
114020cf1dd8SToby Isaac $ func(PetscInt dim, PetscReal time, const PetscReal x[],
114120cf1dd8SToby Isaac $      PetscInt numComponents, PetscScalar values[], void *ctx)
114220cf1dd8SToby Isaac 
114320cf1dd8SToby Isaac and the idea is to evaluate the functional as an integral
114420cf1dd8SToby Isaac 
114520cf1dd8SToby Isaac $ n(f) = int dx n(x) . f(x)
114620cf1dd8SToby Isaac 
114720cf1dd8SToby Isaac where both n and f have Nc components.
114820cf1dd8SToby Isaac 
1149a4ce7ad1SMatthew G. Knepley   Level: beginner
115020cf1dd8SToby Isaac 
115120cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate()
115220cf1dd8SToby Isaac @*/
115320cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceApplyDefault(PetscDualSpace sp, PetscInt f, PetscReal time, PetscFEGeom *cgeom, PetscInt Nc, PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void *ctx, PetscScalar *value)
115420cf1dd8SToby Isaac {
115520cf1dd8SToby Isaac   DM               dm;
115620cf1dd8SToby Isaac   PetscQuadrature  n;
115720cf1dd8SToby Isaac   const PetscReal *points, *weights;
115820cf1dd8SToby Isaac   PetscReal        x[3];
115920cf1dd8SToby Isaac   PetscScalar     *val;
116020cf1dd8SToby Isaac   PetscInt         dim, dE, qNc, c, Nq, q;
116120cf1dd8SToby Isaac   PetscBool        isAffine;
116220cf1dd8SToby Isaac   PetscErrorCode   ierr;
116320cf1dd8SToby Isaac 
116420cf1dd8SToby Isaac   PetscFunctionBegin;
116520cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
116620cf1dd8SToby Isaac   PetscValidPointer(value, 5);
116720cf1dd8SToby Isaac   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
116820cf1dd8SToby Isaac   ierr = PetscDualSpaceGetFunctional(sp, f, &n);CHKERRQ(ierr);
116920cf1dd8SToby Isaac   ierr = PetscQuadratureGetData(n, &dim, &qNc, &Nq, &points, &weights);CHKERRQ(ierr);
117020cf1dd8SToby Isaac   if (dim != cgeom->dim) SETERRQ2(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_SIZ, "The quadrature spatial dimension %D != cell geometry dimension %D", dim, cgeom->dim);
117120cf1dd8SToby Isaac   if (qNc != Nc) SETERRQ2(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_SIZ, "The quadrature components %D != function components %D", qNc, Nc);
117220cf1dd8SToby Isaac   ierr = DMGetWorkArray(dm, Nc, MPIU_SCALAR, &val);CHKERRQ(ierr);
117320cf1dd8SToby Isaac   *value = 0.0;
117420cf1dd8SToby Isaac   isAffine = cgeom->isAffine;
117520cf1dd8SToby Isaac   dE = cgeom->dimEmbed;
117620cf1dd8SToby Isaac   for (q = 0; q < Nq; ++q) {
117720cf1dd8SToby Isaac     if (isAffine) {
117820cf1dd8SToby Isaac       CoordinatesRefToReal(dE, cgeom->dim, cgeom->xi, cgeom->v, cgeom->J, &points[q*dim], x);
117920cf1dd8SToby Isaac       ierr = (*func)(dE, time, x, Nc, val, ctx);CHKERRQ(ierr);
118020cf1dd8SToby Isaac     } else {
118120cf1dd8SToby Isaac       ierr = (*func)(dE, time, &cgeom->v[dE*q], Nc, val, ctx);CHKERRQ(ierr);
118220cf1dd8SToby Isaac     }
118320cf1dd8SToby Isaac     for (c = 0; c < Nc; ++c) {
118420cf1dd8SToby Isaac       *value += val[c]*weights[q*Nc+c];
118520cf1dd8SToby Isaac     }
118620cf1dd8SToby Isaac   }
118720cf1dd8SToby Isaac   ierr = DMRestoreWorkArray(dm, Nc, MPIU_SCALAR, &val);CHKERRQ(ierr);
118820cf1dd8SToby Isaac   PetscFunctionReturn(0);
118920cf1dd8SToby Isaac }
119020cf1dd8SToby Isaac 
119120cf1dd8SToby Isaac /*@C
1192b4457527SToby Isaac   PetscDualSpaceApplyAllDefault - Apply all functionals from the dual space basis to the result of an evaluation at the points returned by PetscDualSpaceGetAllData()
119320cf1dd8SToby Isaac 
119420cf1dd8SToby Isaac   Input Parameters:
119520cf1dd8SToby Isaac + sp        - The PetscDualSpace object
1196b4457527SToby Isaac - pointEval - Evaluation at the points returned by PetscDualSpaceGetAllData()
119720cf1dd8SToby Isaac 
119820cf1dd8SToby Isaac   Output Parameter:
119920cf1dd8SToby Isaac . spValue   - The values of all dual space functionals
120020cf1dd8SToby Isaac 
1201a4ce7ad1SMatthew G. Knepley   Level: beginner
120220cf1dd8SToby Isaac 
120320cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate()
120420cf1dd8SToby Isaac @*/
120520cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceApplyAllDefault(PetscDualSpace sp, const PetscScalar *pointEval, PetscScalar *spValue)
120620cf1dd8SToby Isaac {
1207b4457527SToby Isaac   Vec              pointValues, dofValues;
1208b4457527SToby Isaac   Mat              allMat;
120920cf1dd8SToby Isaac   PetscErrorCode   ierr;
121020cf1dd8SToby Isaac 
121120cf1dd8SToby Isaac   PetscFunctionBegin;
121220cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
121320cf1dd8SToby Isaac   PetscValidScalarPointer(pointEval, 2);
121420cf1dd8SToby Isaac   PetscValidScalarPointer(spValue, 5);
1215b4457527SToby Isaac   ierr = PetscDualSpaceGetAllData(sp, NULL, &allMat);CHKERRQ(ierr);
1216b4457527SToby Isaac   if (!(sp->allNodeValues)) {
1217b4457527SToby Isaac     ierr = MatCreateVecs(allMat, &(sp->allNodeValues), NULL);CHKERRQ(ierr);
121820cf1dd8SToby Isaac   }
1219b4457527SToby Isaac   pointValues = sp->allNodeValues;
1220b4457527SToby Isaac   if (!(sp->allDofValues)) {
1221b4457527SToby Isaac     ierr = MatCreateVecs(allMat, NULL, &(sp->allDofValues));CHKERRQ(ierr);
122220cf1dd8SToby Isaac   }
1223b4457527SToby Isaac   dofValues = sp->allDofValues;
1224b4457527SToby Isaac   ierr = VecPlaceArray(pointValues, pointEval);CHKERRQ(ierr);
1225b4457527SToby Isaac   ierr = VecPlaceArray(dofValues, spValue);CHKERRQ(ierr);
1226b4457527SToby Isaac   ierr = MatMult(allMat, pointValues, dofValues);CHKERRQ(ierr);
1227b4457527SToby Isaac   ierr = VecResetArray(dofValues);CHKERRQ(ierr);
1228b4457527SToby Isaac   ierr = VecResetArray(pointValues);CHKERRQ(ierr);
1229b4457527SToby Isaac   PetscFunctionReturn(0);
123020cf1dd8SToby Isaac }
1231b4457527SToby Isaac 
1232b4457527SToby Isaac /*@C
1233b4457527SToby Isaac   PetscDualSpaceApplyInteriorDefault - Apply interior functionals from the dual space basis to the result of an evaluation at the points returned by PetscDualSpaceGetInteriorData()
1234b4457527SToby Isaac 
1235b4457527SToby Isaac   Input Parameters:
1236b4457527SToby Isaac + sp        - The PetscDualSpace object
1237b4457527SToby Isaac - pointEval - Evaluation at the points returned by PetscDualSpaceGetInteriorData()
1238b4457527SToby Isaac 
1239b4457527SToby Isaac   Output Parameter:
1240b4457527SToby Isaac . spValue   - The values of interior dual space functionals
1241b4457527SToby Isaac 
1242b4457527SToby Isaac   Level: beginner
1243b4457527SToby Isaac 
1244b4457527SToby Isaac .seealso: PetscDualSpaceCreate()
1245b4457527SToby Isaac @*/
1246b4457527SToby Isaac PetscErrorCode PetscDualSpaceApplyInteriorDefault(PetscDualSpace sp, const PetscScalar *pointEval, PetscScalar *spValue)
1247b4457527SToby Isaac {
1248b4457527SToby Isaac   Vec              pointValues, dofValues;
1249b4457527SToby Isaac   Mat              intMat;
1250b4457527SToby Isaac   PetscErrorCode   ierr;
1251b4457527SToby Isaac 
1252b4457527SToby Isaac   PetscFunctionBegin;
1253b4457527SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
1254b4457527SToby Isaac   PetscValidScalarPointer(pointEval, 2);
1255b4457527SToby Isaac   PetscValidScalarPointer(spValue, 5);
1256b4457527SToby Isaac   ierr = PetscDualSpaceGetInteriorData(sp, NULL, &intMat);CHKERRQ(ierr);
1257b4457527SToby Isaac   if (!(sp->intNodeValues)) {
1258b4457527SToby Isaac     ierr = MatCreateVecs(intMat, &(sp->intNodeValues), NULL);CHKERRQ(ierr);
1259b4457527SToby Isaac   }
1260b4457527SToby Isaac   pointValues = sp->intNodeValues;
1261b4457527SToby Isaac   if (!(sp->intDofValues)) {
1262b4457527SToby Isaac     ierr = MatCreateVecs(intMat, NULL, &(sp->intDofValues));CHKERRQ(ierr);
1263b4457527SToby Isaac   }
1264b4457527SToby Isaac   dofValues = sp->intDofValues;
1265b4457527SToby Isaac   ierr = VecPlaceArray(pointValues, pointEval);CHKERRQ(ierr);
1266b4457527SToby Isaac   ierr = VecPlaceArray(dofValues, spValue);CHKERRQ(ierr);
1267b4457527SToby Isaac   ierr = MatMult(intMat, pointValues, dofValues);CHKERRQ(ierr);
1268b4457527SToby Isaac   ierr = VecResetArray(dofValues);CHKERRQ(ierr);
1269b4457527SToby Isaac   ierr = VecResetArray(pointValues);CHKERRQ(ierr);
127020cf1dd8SToby Isaac   PetscFunctionReturn(0);
127120cf1dd8SToby Isaac }
127220cf1dd8SToby Isaac 
1273a4ce7ad1SMatthew G. Knepley /*@
1274b4457527SToby Isaac   PetscDualSpaceGetAllData - Get all quadrature nodes from this space, and the matrix that sends quadrature node values to degree-of-freedom values
1275a4ce7ad1SMatthew G. Knepley 
1276a4ce7ad1SMatthew G. Knepley   Input Parameter:
1277a4ce7ad1SMatthew G. Knepley . sp - The dualspace
1278a4ce7ad1SMatthew G. Knepley 
1279a4ce7ad1SMatthew G. Knepley   Output Parameter:
1280b4457527SToby Isaac + allNodes - A PetscQuadrature object containing all evaluation nodes
1281b4457527SToby Isaac - allMat - A Mat for the node-to-dof transformation
1282a4ce7ad1SMatthew G. Knepley 
1283a4ce7ad1SMatthew G. Knepley   Level: advanced
1284a4ce7ad1SMatthew G. Knepley 
1285a4ce7ad1SMatthew G. Knepley .seealso: PetscDualSpaceCreate()
1286a4ce7ad1SMatthew G. Knepley @*/
1287b4457527SToby Isaac PetscErrorCode PetscDualSpaceGetAllData(PetscDualSpace sp, PetscQuadrature *allNodes, Mat *allMat)
128820cf1dd8SToby Isaac {
128920cf1dd8SToby Isaac   PetscErrorCode ierr;
129020cf1dd8SToby Isaac 
129120cf1dd8SToby Isaac   PetscFunctionBegin;
129220cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
1293b4457527SToby Isaac   if (allNodes) PetscValidPointer(allNodes,2);
1294b4457527SToby Isaac   if (allMat) PetscValidPointer(allMat,3);
1295b4457527SToby Isaac   if ((!sp->allNodes || !sp->allMat) && sp->ops->createalldata) {
1296b4457527SToby Isaac     PetscQuadrature qpoints;
1297b4457527SToby Isaac     Mat amat;
1298b4457527SToby Isaac 
1299b4457527SToby Isaac     ierr = (*sp->ops->createalldata)(sp,&qpoints,&amat);CHKERRQ(ierr);
1300b4457527SToby Isaac     ierr = PetscQuadratureDestroy(&(sp->allNodes));CHKERRQ(ierr);
1301b4457527SToby Isaac     ierr = MatDestroy(&(sp->allMat));CHKERRQ(ierr);
1302b4457527SToby Isaac     sp->allNodes = qpoints;
1303b4457527SToby Isaac     sp->allMat = amat;
130420cf1dd8SToby Isaac   }
1305b4457527SToby Isaac   if (allNodes) *allNodes = sp->allNodes;
1306b4457527SToby Isaac   if (allMat) *allMat = sp->allMat;
130720cf1dd8SToby Isaac   PetscFunctionReturn(0);
130820cf1dd8SToby Isaac }
130920cf1dd8SToby Isaac 
1310a4ce7ad1SMatthew G. Knepley /*@
1311b4457527SToby Isaac   PetscDualSpaceCreateAllDataDefault - Create all evaluation nodes and the node-to-dof matrix by examining functionals
1312a4ce7ad1SMatthew G. Knepley 
1313a4ce7ad1SMatthew G. Knepley   Input Parameter:
1314a4ce7ad1SMatthew G. Knepley . sp - The dualspace
1315a4ce7ad1SMatthew G. Knepley 
1316a4ce7ad1SMatthew G. Knepley   Output Parameter:
1317b4457527SToby Isaac + allNodes - A PetscQuadrature object containing all evaluation nodes
1318b4457527SToby Isaac - allMat - A Mat for the node-to-dof transformation
1319a4ce7ad1SMatthew G. Knepley 
1320a4ce7ad1SMatthew G. Knepley   Level: advanced
1321a4ce7ad1SMatthew G. Knepley 
1322a4ce7ad1SMatthew G. Knepley .seealso: PetscDualSpaceCreate()
1323a4ce7ad1SMatthew G. Knepley @*/
1324b4457527SToby Isaac PetscErrorCode PetscDualSpaceCreateAllDataDefault(PetscDualSpace sp, PetscQuadrature *allNodes, Mat *allMat)
132520cf1dd8SToby Isaac {
132620cf1dd8SToby Isaac   PetscInt        spdim;
132720cf1dd8SToby Isaac   PetscInt        numPoints, offset;
132820cf1dd8SToby Isaac   PetscReal       *points;
132920cf1dd8SToby Isaac   PetscInt        f, dim;
1330b4457527SToby Isaac   PetscInt        Nc, nrows, ncols;
1331b4457527SToby Isaac   PetscInt        maxNumPoints;
133220cf1dd8SToby Isaac   PetscQuadrature q;
1333b4457527SToby Isaac   Mat             A;
133420cf1dd8SToby Isaac   PetscErrorCode  ierr;
133520cf1dd8SToby Isaac 
133620cf1dd8SToby Isaac   PetscFunctionBegin;
1337b4457527SToby Isaac   ierr = PetscDualSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr);
133820cf1dd8SToby Isaac   ierr = PetscDualSpaceGetDimension(sp,&spdim);CHKERRQ(ierr);
133920cf1dd8SToby Isaac   if (!spdim) {
1340b4457527SToby Isaac     ierr = PetscQuadratureCreate(PETSC_COMM_SELF,allNodes);CHKERRQ(ierr);
1341b4457527SToby Isaac     ierr = PetscQuadratureSetData(*allNodes,0,0,0,NULL,NULL);CHKERRQ(ierr);
134220cf1dd8SToby Isaac   }
1343b4457527SToby Isaac   nrows = spdim;
134420cf1dd8SToby Isaac   ierr = PetscDualSpaceGetFunctional(sp,0,&q);CHKERRQ(ierr);
134520cf1dd8SToby Isaac   ierr = PetscQuadratureGetData(q,&dim,NULL,&numPoints,NULL,NULL);CHKERRQ(ierr);
1346b4457527SToby Isaac   maxNumPoints = numPoints;
134720cf1dd8SToby Isaac   for (f = 1; f < spdim; f++) {
134820cf1dd8SToby Isaac     PetscInt Np;
134920cf1dd8SToby Isaac 
135020cf1dd8SToby Isaac     ierr = PetscDualSpaceGetFunctional(sp,f,&q);CHKERRQ(ierr);
135120cf1dd8SToby Isaac     ierr = PetscQuadratureGetData(q,NULL,NULL,&Np,NULL,NULL);CHKERRQ(ierr);
135220cf1dd8SToby Isaac     numPoints += Np;
1353b4457527SToby Isaac     maxNumPoints = PetscMax(maxNumPoints,Np);
135420cf1dd8SToby Isaac   }
1355b4457527SToby Isaac   ncols = numPoints * Nc;
135620cf1dd8SToby Isaac   ierr = PetscMalloc1(dim*numPoints,&points);CHKERRQ(ierr);
1357b4457527SToby Isaac   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, nrows, ncols, maxNumPoints * Nc, NULL, &A);CHKERRQ(ierr);
135820cf1dd8SToby Isaac   for (f = 0, offset = 0; f < spdim; f++) {
1359b4457527SToby Isaac     const PetscReal *p, *w;
136020cf1dd8SToby Isaac     PetscInt        Np, i;
1361b4457527SToby Isaac     PetscInt        fnc;
136220cf1dd8SToby Isaac 
136320cf1dd8SToby Isaac     ierr = PetscDualSpaceGetFunctional(sp,f,&q);CHKERRQ(ierr);
1364b4457527SToby Isaac     ierr = PetscQuadratureGetData(q,NULL,&fnc,&Np,&p,&w);CHKERRQ(ierr);
1365b4457527SToby Isaac     if (fnc != Nc) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "functional component mismatch");
1366b4457527SToby Isaac     for (i = 0; i < Np * dim; i++) {
1367b4457527SToby Isaac       points[offset* dim + i] = p[i];
1368b4457527SToby Isaac     }
1369b4457527SToby Isaac     for (i = 0; i < Np * Nc; i++) {
1370b4457527SToby Isaac       ierr = MatSetValue(A, f, offset * Nc, w[i], INSERT_VALUES);CHKERRQ(ierr);
1371b4457527SToby Isaac     }
1372b4457527SToby Isaac     offset += Np;
1373b4457527SToby Isaac   }
1374b4457527SToby Isaac   ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1375b4457527SToby Isaac   ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1376b4457527SToby Isaac   ierr = PetscQuadratureCreate(PETSC_COMM_SELF,allNodes);CHKERRQ(ierr);
1377b4457527SToby Isaac   ierr = PetscQuadratureSetData(*allNodes,dim,0,numPoints,points,NULL);CHKERRQ(ierr);
1378b4457527SToby Isaac   *allMat = A;
1379b4457527SToby Isaac   PetscFunctionReturn(0);
1380b4457527SToby Isaac }
1381b4457527SToby Isaac 
1382b4457527SToby Isaac /*@
1383b4457527SToby Isaac   PetscDualSpaceGetInteriorData - Get all quadrature points necessary to compute the interior degrees of freedom from
1384b4457527SToby Isaac   this space, as well as the matrix that computes the degrees of freedom from the quadrature values.  Degrees of
1385b4457527SToby Isaac   freedom are interior degrees of freedom if they belong (by PetscDualSpaceGetSection()) to interior points in the
1386b4457527SToby Isaac   reference DMPlex: complementary boundary degrees of freedom are marked as constrained in the section returned by
1387b4457527SToby Isaac   PetscDualSpaceGetSection()).
1388b4457527SToby Isaac 
1389b4457527SToby Isaac   Input Parameter:
1390b4457527SToby Isaac . sp - The dualspace
1391b4457527SToby Isaac 
1392b4457527SToby Isaac   Output Parameter:
1393b4457527SToby Isaac + intNodes - A PetscQuadrature object containing all evaluation points needed to evaluate interior degrees of freedom
1394b4457527SToby Isaac - intMat   - A matrix that computes dual space values from point values: size [spdim0 x (npoints * nc)], where spdim0 is
1395b4457527SToby Isaac              the size of the constrained layout (PetscSectionGetConstrainStorageSize()) of the dual space section,
1396b4457527SToby Isaac              npoints is the number of points in intNodes and nc is PetscDualSpaceGetNumComponents().
1397b4457527SToby Isaac 
1398b4457527SToby Isaac   Level: advanced
1399b4457527SToby Isaac 
1400b4457527SToby Isaac .seealso: PetscDualSpaceCreate(), PetscDualSpaceGetDimension(), PetscDualSpaceGetNumComponents(), PetscQuadratureGetData()
1401b4457527SToby Isaac @*/
1402b4457527SToby Isaac PetscErrorCode PetscDualSpaceGetInteriorData(PetscDualSpace sp, PetscQuadrature *intNodes, Mat *intMat)
1403b4457527SToby Isaac {
1404b4457527SToby Isaac   PetscErrorCode ierr;
1405b4457527SToby Isaac 
1406b4457527SToby Isaac   PetscFunctionBegin;
1407b4457527SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
1408b4457527SToby Isaac   if (intNodes) PetscValidPointer(intNodes,2);
1409b4457527SToby Isaac   if (intMat) PetscValidPointer(intMat,3);
1410b4457527SToby Isaac   if ((!sp->intNodes || !sp->intMat) && sp->ops->createintdata) {
1411b4457527SToby Isaac     PetscQuadrature qpoints;
1412b4457527SToby Isaac     Mat imat;
1413b4457527SToby Isaac 
1414b4457527SToby Isaac     ierr = (*sp->ops->createintdata)(sp,&qpoints,&imat);CHKERRQ(ierr);
1415b4457527SToby Isaac     ierr = PetscQuadratureDestroy(&(sp->intNodes));CHKERRQ(ierr);
1416b4457527SToby Isaac     ierr = MatDestroy(&(sp->intMat));CHKERRQ(ierr);
1417b4457527SToby Isaac     sp->intNodes = qpoints;
1418b4457527SToby Isaac     sp->intMat = imat;
1419b4457527SToby Isaac   }
1420b4457527SToby Isaac   if (intNodes) *intNodes = sp->intNodes;
1421b4457527SToby Isaac   if (intMat) *intMat = sp->intMat;
1422b4457527SToby Isaac   PetscFunctionReturn(0);
1423b4457527SToby Isaac }
1424b4457527SToby Isaac 
1425b4457527SToby Isaac /*@
1426b4457527SToby Isaac   PetscDualSpaceCreateInteriorDataDefault - Create quadrature points by examining interior functionals and create the matrix mapping quadrature point values to interior dual space values
1427b4457527SToby Isaac 
1428b4457527SToby Isaac   Input Parameter:
1429b4457527SToby Isaac . sp - The dualspace
1430b4457527SToby Isaac 
1431b4457527SToby Isaac   Output Parameter:
1432b4457527SToby Isaac + intNodes - A PetscQuadrature object containing all evaluation points needed to evaluate interior degrees of freedom
1433b4457527SToby Isaac - intMat    - A matrix that computes dual space values from point values: size [spdim0 x (npoints * nc)], where spdim0 is
1434b4457527SToby Isaac               the size of the constrained layout (PetscSectionGetConstrainStorageSize()) of the dual space section,
1435b4457527SToby Isaac               npoints is the number of points in allNodes and nc is PetscDualSpaceGetNumComponents().
1436b4457527SToby Isaac 
1437b4457527SToby Isaac   Level: advanced
1438b4457527SToby Isaac 
1439b4457527SToby Isaac .seealso: PetscDualSpaceCreate(), PetscDualSpaceGetInteriorData()
1440b4457527SToby Isaac @*/
1441b4457527SToby Isaac PetscErrorCode PetscDualSpaceCreateInteriorDataDefault(PetscDualSpace sp, PetscQuadrature *intNodes, Mat *intMat)
1442b4457527SToby Isaac {
1443b4457527SToby Isaac   DM              dm;
1444b4457527SToby Isaac   PetscInt        spdim0;
1445b4457527SToby Isaac   PetscInt        Nc;
1446b4457527SToby Isaac   PetscInt        pStart, pEnd, p, f;
1447b4457527SToby Isaac   PetscSection    section;
1448b4457527SToby Isaac   PetscInt        numPoints, offset, matoffset;
1449b4457527SToby Isaac   PetscReal       *points;
1450b4457527SToby Isaac   PetscInt        dim;
1451b4457527SToby Isaac   PetscInt        *nnz;
1452b4457527SToby Isaac   PetscQuadrature q;
1453b4457527SToby Isaac   Mat             imat;
1454b4457527SToby Isaac   PetscErrorCode  ierr;
1455b4457527SToby Isaac 
1456b4457527SToby Isaac   PetscFunctionBegin;
1457b4457527SToby Isaac   PetscValidHeaderSpecific(sp,PETSCDUALSPACE_CLASSID,1);
1458b4457527SToby Isaac   ierr = PetscDualSpaceGetSection(sp, &section);CHKERRQ(ierr);
1459b4457527SToby Isaac   ierr = PetscSectionGetConstrainedStorageSize(section, &spdim0);CHKERRQ(ierr);
1460b4457527SToby Isaac   if (!spdim0) {
1461b4457527SToby Isaac     *intNodes = NULL;
1462b4457527SToby Isaac     *intMat = NULL;
1463b4457527SToby Isaac     PetscFunctionReturn(0);
1464b4457527SToby Isaac   }
1465b4457527SToby Isaac   ierr = PetscDualSpaceGetNumComponents(sp, &Nc);CHKERRQ(ierr);
1466b4457527SToby Isaac   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1467b4457527SToby Isaac   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
1468b4457527SToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1469b4457527SToby Isaac   ierr = PetscMalloc1(spdim0, &nnz);CHKERRQ(ierr);
1470b4457527SToby Isaac   for (p = pStart, f = 0, numPoints = 0; p < pEnd; p++) {
1471b4457527SToby Isaac     PetscInt dof, cdof, off, d;
1472b4457527SToby Isaac 
1473b4457527SToby Isaac     ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1474b4457527SToby Isaac     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1475b4457527SToby Isaac     if (!(dof - cdof)) continue;
1476b4457527SToby Isaac     ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
1477b4457527SToby Isaac     for (d = 0; d < dof; d++, off++, f++) {
1478b4457527SToby Isaac       PetscInt Np;
1479b4457527SToby Isaac 
1480b4457527SToby Isaac       ierr = PetscDualSpaceGetFunctional(sp,off,&q);CHKERRQ(ierr);
1481b4457527SToby Isaac       ierr = PetscQuadratureGetData(q,NULL,NULL,&Np,NULL,NULL);CHKERRQ(ierr);
1482b4457527SToby Isaac       nnz[f] = Np * Nc;
1483b4457527SToby Isaac       numPoints += Np;
1484b4457527SToby Isaac     }
1485b4457527SToby Isaac   }
1486b4457527SToby Isaac   ierr = MatCreateSeqAIJ(PETSC_COMM_SELF, spdim0, numPoints * Nc, 0, nnz, &imat);CHKERRQ(ierr);
1487b4457527SToby Isaac   ierr = PetscFree(nnz);CHKERRQ(ierr);
1488b4457527SToby Isaac   ierr = PetscMalloc1(dim*numPoints,&points);CHKERRQ(ierr);
1489b4457527SToby Isaac   for (p = pStart, f = 0, offset = 0, matoffset = 0; p < pEnd; p++) {
1490b4457527SToby Isaac     PetscInt dof, cdof, off, d;
1491b4457527SToby Isaac 
1492b4457527SToby Isaac     ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1493b4457527SToby Isaac     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1494b4457527SToby Isaac     if (!(dof - cdof)) continue;
1495b4457527SToby Isaac     ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
1496b4457527SToby Isaac     for (d = 0; d < dof; d++, off++, f++) {
1497b4457527SToby Isaac       const PetscReal *p;
1498b4457527SToby Isaac       const PetscReal *w;
1499b4457527SToby Isaac       PetscInt        Np, i;
1500b4457527SToby Isaac 
1501b4457527SToby Isaac       ierr = PetscDualSpaceGetFunctional(sp,off,&q);CHKERRQ(ierr);
1502b4457527SToby Isaac       ierr = PetscQuadratureGetData(q,NULL,NULL,&Np,&p,&w);CHKERRQ(ierr);
150320cf1dd8SToby Isaac       for (i = 0; i < Np * dim; i++) {
150420cf1dd8SToby Isaac         points[offset + i] = p[i];
150520cf1dd8SToby Isaac       }
1506b4457527SToby Isaac       for (i = 0; i < Np * Nc; i++) {
1507b4457527SToby Isaac         ierr = MatSetValue(imat, f, matoffset + i, w[i],INSERT_VALUES);CHKERRQ(ierr);
150820cf1dd8SToby Isaac       }
1509b4457527SToby Isaac       offset += Np * dim;
1510b4457527SToby Isaac       matoffset += Np * Nc;
1511b4457527SToby Isaac     }
1512b4457527SToby Isaac   }
1513b4457527SToby Isaac   ierr = PetscQuadratureCreate(PETSC_COMM_SELF,intNodes);CHKERRQ(ierr);
1514b4457527SToby Isaac   ierr = PetscQuadratureSetData(*intNodes,dim,0,numPoints,points,NULL);CHKERRQ(ierr);
1515b4457527SToby Isaac   ierr = MatAssemblyBegin(imat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1516b4457527SToby Isaac   ierr = MatAssemblyEnd(imat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1517b4457527SToby Isaac   *intMat = imat;
151820cf1dd8SToby Isaac   PetscFunctionReturn(0);
151920cf1dd8SToby Isaac }
152020cf1dd8SToby Isaac 
152120cf1dd8SToby Isaac /*@C
152220cf1dd8SToby Isaac   PetscDualSpaceApplyFVM - Apply a functional from the dual space basis to an input function by assuming a point evaluation functional at the cell centroid.
152320cf1dd8SToby Isaac 
152420cf1dd8SToby Isaac   Input Parameters:
152520cf1dd8SToby Isaac + sp    - The PetscDualSpace object
152620cf1dd8SToby Isaac . f     - The basis functional index
152720cf1dd8SToby Isaac . time  - The time
152820cf1dd8SToby Isaac . cgeom - A context with geometric information for this cell, we currently just use the centroid
152920cf1dd8SToby Isaac . Nc    - The number of components for the function
153020cf1dd8SToby Isaac . func  - The input function
153120cf1dd8SToby Isaac - ctx   - A context for the function
153220cf1dd8SToby Isaac 
153320cf1dd8SToby Isaac   Output Parameter:
153420cf1dd8SToby Isaac . value - The output value (scalar)
153520cf1dd8SToby Isaac 
153620cf1dd8SToby Isaac   Note: The calling sequence for the callback func is given by:
153720cf1dd8SToby Isaac 
153820cf1dd8SToby Isaac $ func(PetscInt dim, PetscReal time, const PetscReal x[],
153920cf1dd8SToby Isaac $      PetscInt numComponents, PetscScalar values[], void *ctx)
154020cf1dd8SToby Isaac 
154120cf1dd8SToby Isaac and the idea is to evaluate the functional as an integral
154220cf1dd8SToby Isaac 
154320cf1dd8SToby Isaac $ n(f) = int dx n(x) . f(x)
154420cf1dd8SToby Isaac 
154520cf1dd8SToby Isaac where both n and f have Nc components.
154620cf1dd8SToby Isaac 
1547a4ce7ad1SMatthew G. Knepley   Level: beginner
154820cf1dd8SToby Isaac 
154920cf1dd8SToby Isaac .seealso: PetscDualSpaceCreate()
155020cf1dd8SToby Isaac @*/
155120cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceApplyFVM(PetscDualSpace sp, PetscInt f, PetscReal time, PetscFVCellGeom *cgeom, PetscInt Nc, PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void *ctx, PetscScalar *value)
155220cf1dd8SToby Isaac {
155320cf1dd8SToby Isaac   DM               dm;
155420cf1dd8SToby Isaac   PetscQuadrature  n;
155520cf1dd8SToby Isaac   const PetscReal *points, *weights;
155620cf1dd8SToby Isaac   PetscScalar     *val;
155720cf1dd8SToby Isaac   PetscInt         dimEmbed, qNc, c, Nq, q;
155820cf1dd8SToby Isaac   PetscErrorCode   ierr;
155920cf1dd8SToby Isaac 
156020cf1dd8SToby Isaac   PetscFunctionBegin;
156120cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
156220cf1dd8SToby Isaac   PetscValidPointer(value, 5);
156320cf1dd8SToby Isaac   ierr = PetscDualSpaceGetDM(sp, &dm);CHKERRQ(ierr);
156420cf1dd8SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
156520cf1dd8SToby Isaac   ierr = PetscDualSpaceGetFunctional(sp, f, &n);CHKERRQ(ierr);
156620cf1dd8SToby Isaac   ierr = PetscQuadratureGetData(n, NULL, &qNc, &Nq, &points, &weights);CHKERRQ(ierr);
156720cf1dd8SToby Isaac   if (qNc != Nc) SETERRQ2(PetscObjectComm((PetscObject) sp), PETSC_ERR_ARG_SIZ, "The quadrature components %D != function components %D", qNc, Nc);
156820cf1dd8SToby Isaac   ierr = DMGetWorkArray(dm, Nc, MPIU_SCALAR, &val);CHKERRQ(ierr);
156920cf1dd8SToby Isaac   *value = 0.;
157020cf1dd8SToby Isaac   for (q = 0; q < Nq; ++q) {
157120cf1dd8SToby Isaac     ierr = (*func)(dimEmbed, time, cgeom->centroid, Nc, val, ctx);CHKERRQ(ierr);
157220cf1dd8SToby Isaac     for (c = 0; c < Nc; ++c) {
157320cf1dd8SToby Isaac       *value += val[c]*weights[q*Nc+c];
157420cf1dd8SToby Isaac     }
157520cf1dd8SToby Isaac   }
157620cf1dd8SToby Isaac   ierr = DMRestoreWorkArray(dm, Nc, MPIU_SCALAR, &val);CHKERRQ(ierr);
157720cf1dd8SToby Isaac   PetscFunctionReturn(0);
157820cf1dd8SToby Isaac }
157920cf1dd8SToby Isaac 
158020cf1dd8SToby Isaac /*@
158120cf1dd8SToby Isaac   PetscDualSpaceGetHeightSubspace - Get the subset of the dual space basis that is supported on a mesh point of a
158220cf1dd8SToby Isaac   given height.  This assumes that the reference cell is symmetric over points of this height.
158320cf1dd8SToby Isaac 
158420cf1dd8SToby Isaac   If the dual space is not defined on mesh points of the given height (e.g. if the space is discontinuous and
158520cf1dd8SToby Isaac   pointwise values are not defined on the element boundaries), or if the implementation of PetscDualSpace does not
158620cf1dd8SToby Isaac   support extracting subspaces, then NULL is returned.
158720cf1dd8SToby Isaac 
158820cf1dd8SToby Isaac   This does not increment the reference count on the returned dual space, and the user should not destroy it.
158920cf1dd8SToby Isaac 
159020cf1dd8SToby Isaac   Not collective
159120cf1dd8SToby Isaac 
159220cf1dd8SToby Isaac   Input Parameters:
159320cf1dd8SToby Isaac + sp - the PetscDualSpace object
159420cf1dd8SToby Isaac - height - the height of the mesh point for which the subspace is desired
159520cf1dd8SToby Isaac 
159620cf1dd8SToby Isaac   Output Parameter:
159720cf1dd8SToby Isaac . subsp - the subspace.  Note that the functionals in the subspace are with respect to the intrinsic geometry of the
159820cf1dd8SToby Isaac   point, which will be of lesser dimension if height > 0.
159920cf1dd8SToby Isaac 
160020cf1dd8SToby Isaac   Level: advanced
160120cf1dd8SToby Isaac 
160220cf1dd8SToby Isaac .seealso: PetscSpaceGetHeightSubspace(), PetscDualSpace
160320cf1dd8SToby Isaac @*/
160420cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetHeightSubspace(PetscDualSpace sp, PetscInt height, PetscDualSpace *subsp)
160520cf1dd8SToby Isaac {
1606b4457527SToby Isaac   PetscInt       depth = -1, cStart, cEnd;
1607b4457527SToby Isaac   DM             dm;
160820cf1dd8SToby Isaac   PetscErrorCode ierr;
160920cf1dd8SToby Isaac 
161020cf1dd8SToby Isaac   PetscFunctionBegin;
161120cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
1612b4457527SToby Isaac   PetscValidPointer(subsp,2);
1613b4457527SToby Isaac   if (!(sp->uniform)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "A non-uniform dual space does not have a single dual space at each height");
161420cf1dd8SToby Isaac   *subsp = NULL;
1615b4457527SToby Isaac   dm = sp->dm;
1616b4457527SToby Isaac   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1617b4457527SToby Isaac   if (height < 0 || height > depth) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid height");
1618b4457527SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
1619b4457527SToby Isaac   if (height == 0 && cEnd == cStart + 1) {
1620b4457527SToby Isaac     *subsp = sp;
1621b4457527SToby Isaac     PetscFunctionReturn(0);
1622b4457527SToby Isaac   }
1623b4457527SToby Isaac   if (!sp->heightSpaces) {
1624b4457527SToby Isaac     PetscInt h;
1625b4457527SToby Isaac     ierr = PetscCalloc1(depth+1, &(sp->heightSpaces));CHKERRQ(ierr);
1626b4457527SToby Isaac 
1627b4457527SToby Isaac     for (h = 0; h <= depth; h++) {
1628b4457527SToby Isaac       if (h == 0 && cEnd == cStart + 1) continue;
1629b4457527SToby Isaac       if (sp->ops->createheightsubspace) {ierr = (*sp->ops->createheightsubspace)(sp,height,&(sp->heightSpaces[h]));CHKERRQ(ierr);}
1630b4457527SToby Isaac       else if (sp->pointSpaces) {
1631b4457527SToby Isaac         PetscInt hStart, hEnd;
1632b4457527SToby Isaac 
1633b4457527SToby Isaac         ierr = DMPlexGetHeightStratum(dm,h,&hStart,&hEnd);CHKERRQ(ierr);
1634b4457527SToby Isaac         if (hEnd > hStart) {
1635665f567fSMatthew G. Knepley           const char *name;
1636665f567fSMatthew G. Knepley 
1637b4457527SToby Isaac           ierr = PetscObjectReference((PetscObject)(sp->pointSpaces[hStart]));CHKERRQ(ierr);
1638665f567fSMatthew G. Knepley           if (sp->pointSpaces[hStart]) {
1639665f567fSMatthew G. Knepley             ierr = PetscObjectGetName((PetscObject) sp,                     &name);CHKERRQ(ierr);
1640665f567fSMatthew G. Knepley             ierr = PetscObjectSetName((PetscObject) sp->pointSpaces[hStart], name);CHKERRQ(ierr);
1641665f567fSMatthew G. Knepley           }
1642b4457527SToby Isaac           sp->heightSpaces[h] = sp->pointSpaces[hStart];
1643b4457527SToby Isaac         }
1644b4457527SToby Isaac       }
1645b4457527SToby Isaac     }
1646b4457527SToby Isaac   }
1647b4457527SToby Isaac   *subsp = sp->heightSpaces[height];
164820cf1dd8SToby Isaac   PetscFunctionReturn(0);
164920cf1dd8SToby Isaac }
165020cf1dd8SToby Isaac 
165120cf1dd8SToby Isaac /*@
165220cf1dd8SToby Isaac   PetscDualSpaceGetPointSubspace - Get the subset of the dual space basis that is supported on a particular mesh point.
165320cf1dd8SToby Isaac 
165420cf1dd8SToby Isaac   If the dual space is not defined on the mesh point (e.g. if the space is discontinuous and pointwise values are not
165520cf1dd8SToby Isaac   defined on the element boundaries), or if the implementation of PetscDualSpace does not support extracting
165620cf1dd8SToby Isaac   subspaces, then NULL is returned.
165720cf1dd8SToby Isaac 
165820cf1dd8SToby Isaac   This does not increment the reference count on the returned dual space, and the user should not destroy it.
165920cf1dd8SToby Isaac 
166020cf1dd8SToby Isaac   Not collective
166120cf1dd8SToby Isaac 
166220cf1dd8SToby Isaac   Input Parameters:
166320cf1dd8SToby Isaac + sp - the PetscDualSpace object
166420cf1dd8SToby Isaac - point - the point (in the dual space's DM) for which the subspace is desired
166520cf1dd8SToby Isaac 
166620cf1dd8SToby Isaac   Output Parameters:
166720cf1dd8SToby Isaac   bdsp - the subspace.  Note that the functionals in the subspace are with respect to the intrinsic geometry of the
166820cf1dd8SToby Isaac   point, which will be of lesser dimension if height > 0.
166920cf1dd8SToby Isaac 
167020cf1dd8SToby Isaac   Level: advanced
167120cf1dd8SToby Isaac 
167220cf1dd8SToby Isaac .seealso: PetscDualSpace
167320cf1dd8SToby Isaac @*/
167420cf1dd8SToby Isaac PetscErrorCode PetscDualSpaceGetPointSubspace(PetscDualSpace sp, PetscInt point, PetscDualSpace *bdsp)
167520cf1dd8SToby Isaac {
1676b4457527SToby Isaac   PetscInt       pStart = 0, pEnd = 0, cStart, cEnd;
1677b4457527SToby Isaac   DM             dm;
167820cf1dd8SToby Isaac   PetscErrorCode ierr;
167920cf1dd8SToby Isaac 
168020cf1dd8SToby Isaac   PetscFunctionBegin;
168120cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 1);
168220cf1dd8SToby Isaac   PetscValidPointer(bdsp,2);
168320cf1dd8SToby Isaac   *bdsp = NULL;
1684b4457527SToby Isaac   dm = sp->dm;
1685b4457527SToby Isaac   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
1686b4457527SToby Isaac   if (point < pStart || point > pEnd) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid point");
1687b4457527SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
1688b4457527SToby Isaac   if (point == cStart && cEnd == cStart + 1) { /* the dual space is only equivalent to the dual space on a cell if the reference mesh has just one cell */
1689b4457527SToby Isaac     *bdsp = sp;
1690b4457527SToby Isaac     PetscFunctionReturn(0);
1691b4457527SToby Isaac   }
1692b4457527SToby Isaac   if (!sp->pointSpaces) {
1693b4457527SToby Isaac     PetscInt p;
1694b4457527SToby Isaac     ierr = PetscCalloc1(pEnd - pStart, &(sp->pointSpaces));CHKERRQ(ierr);
169520cf1dd8SToby Isaac 
1696b4457527SToby Isaac     for (p = 0; p < pEnd - pStart; p++) {
1697b4457527SToby Isaac       if (p + pStart == cStart && cEnd == cStart + 1) continue;
1698b4457527SToby Isaac       if (sp->ops->createpointsubspace) {ierr = (*sp->ops->createpointsubspace)(sp,p+pStart,&(sp->pointSpaces[p]));CHKERRQ(ierr);}
1699b4457527SToby Isaac       else if (sp->heightSpaces || sp->ops->createheightsubspace) {
1700b4457527SToby Isaac         PetscInt dim, depth, height;
1701b4457527SToby Isaac         DMLabel  label;
1702b4457527SToby Isaac 
170320cf1dd8SToby Isaac         ierr = DMPlexGetDepth(dm,&dim);CHKERRQ(ierr);
170420cf1dd8SToby Isaac         ierr = DMPlexGetDepthLabel(dm,&label);CHKERRQ(ierr);
1705b4457527SToby Isaac         ierr = DMLabelGetValue(label,p+pStart,&depth);CHKERRQ(ierr);
170620cf1dd8SToby Isaac         height = dim - depth;
1707b4457527SToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(sp, height, &(sp->pointSpaces[p]));CHKERRQ(ierr);
1708b4457527SToby Isaac         ierr = PetscObjectReference((PetscObject)sp->pointSpaces[p]);CHKERRQ(ierr);
170920cf1dd8SToby Isaac       }
1710b4457527SToby Isaac     }
1711b4457527SToby Isaac   }
1712b4457527SToby Isaac   *bdsp = sp->pointSpaces[point - pStart];
171320cf1dd8SToby Isaac   PetscFunctionReturn(0);
171420cf1dd8SToby Isaac }
171520cf1dd8SToby Isaac 
17166f905325SMatthew G. Knepley /*@C
17176f905325SMatthew G. Knepley   PetscDualSpaceGetSymmetries - Returns a description of the symmetries of this basis
17186f905325SMatthew G. Knepley 
17196f905325SMatthew G. Knepley   Not collective
17206f905325SMatthew G. Knepley 
17216f905325SMatthew G. Knepley   Input Parameter:
17226f905325SMatthew G. Knepley . sp - the PetscDualSpace object
17236f905325SMatthew G. Knepley 
17246f905325SMatthew G. Knepley   Output Parameters:
1725b4457527SToby Isaac + perms - Permutations of the interior degrees of freedom, parameterized by the point orientation
1726b4457527SToby Isaac - flips - Sign reversal of the interior degrees of freedom, parameterized by the point orientation
17276f905325SMatthew G. Knepley 
17286f905325SMatthew G. Knepley   Note: The permutation and flip arrays are organized in the following way
17296f905325SMatthew G. Knepley $ perms[p][ornt][dof # on point] = new local dof #
17306f905325SMatthew G. Knepley $ flips[p][ornt][dof # on point] = reversal or not
17316f905325SMatthew G. Knepley 
17326f905325SMatthew G. Knepley   Level: developer
17336f905325SMatthew G. Knepley 
17346f905325SMatthew G. Knepley @*/
17356f905325SMatthew G. Knepley PetscErrorCode PetscDualSpaceGetSymmetries(PetscDualSpace sp, const PetscInt ****perms, const PetscScalar ****flips)
17366f905325SMatthew G. Knepley {
17376f905325SMatthew G. Knepley   PetscErrorCode ierr;
17386f905325SMatthew G. Knepley 
17396f905325SMatthew G. Knepley   PetscFunctionBegin;
17406f905325SMatthew G. Knepley   PetscValidHeaderSpecific(sp,PETSCDUALSPACE_CLASSID,1);
17416f905325SMatthew G. Knepley   if (perms) {PetscValidPointer(perms,2); *perms = NULL;}
17426f905325SMatthew G. Knepley   if (flips) {PetscValidPointer(flips,3); *flips = NULL;}
17436f905325SMatthew G. Knepley   if (sp->ops->getsymmetries) {ierr = (sp->ops->getsymmetries)(sp,perms,flips);CHKERRQ(ierr);}
17446f905325SMatthew G. Knepley   PetscFunctionReturn(0);
17456f905325SMatthew G. Knepley }
17464bee2e38SMatthew G. Knepley 
17474bee2e38SMatthew G. Knepley /*@
1748b4457527SToby Isaac   PetscDualSpaceGetFormDegree - Get the form degree k for the k-form the describes the pushforwards/pullbacks of this
1749b4457527SToby Isaac   dual space's functionals.
1750b4457527SToby Isaac 
1751b4457527SToby Isaac   Input Parameter:
1752b4457527SToby Isaac . dsp - The PetscDualSpace
1753b4457527SToby Isaac 
1754b4457527SToby Isaac   Output Parameter:
1755b4457527SToby Isaac . k   - The *signed* degree k of the k.  If k >= 0, this means that the degrees of freedom are k-forms, and are stored
1756b4457527SToby Isaac         in lexicographic order according to the basis of k-forms constructed from the wedge product of 1-forms.  So for example,
1757b4457527SToby Isaac         the 1-form basis in 3-D is (dx, dy, dz), and the 2-form basis in 3-D is (dx wedge dy, dx wedge dz, dy wedge dz).
1758b4457527SToby Isaac         If k < 0, this means that the degrees transform as k-forms, but are stored as (N-k) forms according to the
1759b4457527SToby Isaac         Hodge star map.  So for example if k = -2 and N = 3, this means that the degrees of freedom transform as 2-forms
1760b4457527SToby Isaac         but are stored as 1-forms.
1761b4457527SToby Isaac 
1762b4457527SToby Isaac   Level: developer
1763b4457527SToby Isaac 
1764b4457527SToby Isaac .seealso: PetscDTAltV, PetscDualSpacePullback(), PetscDualSpacePushforward(), PetscDualSpaceTransform(), PetscDualSpaceTransformType
1765b4457527SToby Isaac @*/
1766b4457527SToby Isaac PetscErrorCode PetscDualSpaceGetFormDegree(PetscDualSpace dsp, PetscInt *k)
1767b4457527SToby Isaac {
1768b4457527SToby Isaac   PetscFunctionBeginHot;
1769b4457527SToby Isaac   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
1770b4457527SToby Isaac   PetscValidPointer(k, 2);
1771b4457527SToby Isaac   *k = dsp->k;
1772b4457527SToby Isaac   PetscFunctionReturn(0);
1773b4457527SToby Isaac }
1774b4457527SToby Isaac 
1775b4457527SToby Isaac /*@
1776b4457527SToby Isaac   PetscDualSpaceSetFormDegree - Set the form degree k for the k-form the describes the pushforwards/pullbacks of this
1777b4457527SToby Isaac   dual space's functionals.
1778b4457527SToby Isaac 
1779b4457527SToby Isaac   Input Parameter:
1780b4457527SToby Isaac + dsp - The PetscDualSpace
1781b4457527SToby Isaac - k   - The *signed* degree k of the k.  If k >= 0, this means that the degrees of freedom are k-forms, and are stored
1782b4457527SToby Isaac         in lexicographic order according to the basis of k-forms constructed from the wedge product of 1-forms.  So for example,
1783b4457527SToby Isaac         the 1-form basis in 3-D is (dx, dy, dz), and the 2-form basis in 3-D is (dx wedge dy, dx wedge dz, dy wedge dz).
1784b4457527SToby Isaac         If k < 0, this means that the degrees transform as k-forms, but are stored as (N-k) forms according to the
1785b4457527SToby Isaac         Hodge star map.  So for example if k = -2 and N = 3, this means that the degrees of freedom transform as 2-forms
1786b4457527SToby Isaac         but are stored as 1-forms.
1787b4457527SToby Isaac 
1788b4457527SToby Isaac   Level: developer
1789b4457527SToby Isaac 
1790b4457527SToby Isaac .seealso: PetscDTAltV, PetscDualSpacePullback(), PetscDualSpacePushforward(), PetscDualSpaceTransform(), PetscDualSpaceTransformType
1791b4457527SToby Isaac @*/
1792b4457527SToby Isaac PetscErrorCode PetscDualSpaceSetFormDegree(PetscDualSpace dsp, PetscInt k)
1793b4457527SToby Isaac {
1794b4457527SToby Isaac   PetscInt dim;
1795b4457527SToby Isaac 
1796b4457527SToby Isaac   PetscFunctionBeginHot;
1797b4457527SToby Isaac   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
1798b4457527SToby Isaac   if (dsp->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dsp), PETSC_ERR_ARG_WRONGSTATE, "Cannot change number of components after dualspace is set up");
1799b4457527SToby Isaac   dim = dsp->dm->dim;
1800b4457527SToby Isaac   if (k < -dim || k > dim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unsupported %D-form on %D-dimensional reference cell", PetscAbsInt(k), dim);
1801b4457527SToby Isaac   dsp->k = k;
1802b4457527SToby Isaac   PetscFunctionReturn(0);
1803b4457527SToby Isaac }
1804b4457527SToby Isaac 
1805b4457527SToby Isaac /*@
18064bee2e38SMatthew G. Knepley   PetscDualSpaceGetDeRahm - Get the k-simplex associated with the functionals in this dual space
18074bee2e38SMatthew G. Knepley 
18084bee2e38SMatthew G. Knepley   Input Parameter:
18094bee2e38SMatthew G. Knepley . dsp - The PetscDualSpace
18104bee2e38SMatthew G. Knepley 
18114bee2e38SMatthew G. Knepley   Output Parameter:
18124bee2e38SMatthew G. Knepley . k   - The simplex dimension
18134bee2e38SMatthew G. Knepley 
1814a4ce7ad1SMatthew G. Knepley   Level: developer
18154bee2e38SMatthew G. Knepley 
18164bee2e38SMatthew G. Knepley   Note: Currently supported values are
18174bee2e38SMatthew G. Knepley $ 0: These are H_1 methods that only transform coordinates
18184bee2e38SMatthew G. Knepley $ 1: These are Hcurl methods that transform functions using the covariant Piola transform (COVARIANT_PIOLA_TRANSFORM)
18194bee2e38SMatthew G. Knepley $ 2: These are the same as 1
18204bee2e38SMatthew G. Knepley $ 3: These are Hdiv methods that transform functions using the contravariant Piola transform (CONTRAVARIANT_PIOLA_TRANSFORM)
18214bee2e38SMatthew G. Knepley 
18224bee2e38SMatthew G. Knepley .seealso: PetscDualSpacePullback(), PetscDualSpacePushforward(), PetscDualSpaceTransform(), PetscDualSpaceTransformType
18234bee2e38SMatthew G. Knepley @*/
18244bee2e38SMatthew G. Knepley PetscErrorCode PetscDualSpaceGetDeRahm(PetscDualSpace dsp, PetscInt *k)
18254bee2e38SMatthew G. Knepley {
1826b4457527SToby Isaac   PetscInt dim;
1827b4457527SToby Isaac 
18284bee2e38SMatthew G. Knepley   PetscFunctionBeginHot;
18294bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
18304bee2e38SMatthew G. Knepley   PetscValidPointer(k, 2);
1831b4457527SToby Isaac   dim = dsp->dm->dim;
1832b4457527SToby Isaac   if (!dsp->k) *k = IDENTITY_TRANSFORM;
1833b4457527SToby Isaac   else if (dsp->k == 1) *k = COVARIANT_PIOLA_TRANSFORM;
1834b4457527SToby Isaac   else if (dsp->k == -(dim - 1)) *k = CONTRAVARIANT_PIOLA_TRANSFORM;
1835b4457527SToby Isaac   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unsupported transformation");
18364bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
18374bee2e38SMatthew G. Knepley }
18384bee2e38SMatthew G. Knepley 
18394bee2e38SMatthew G. Knepley /*@C
18404bee2e38SMatthew G. Knepley   PetscDualSpaceTransform - Transform the function values
18414bee2e38SMatthew G. Knepley 
18424bee2e38SMatthew G. Knepley   Input Parameters:
18434bee2e38SMatthew G. Knepley + dsp       - The PetscDualSpace
18444bee2e38SMatthew G. Knepley . trans     - The type of transform
18454bee2e38SMatthew G. Knepley . isInverse - Flag to invert the transform
18464bee2e38SMatthew G. Knepley . fegeom    - The cell geometry
18474bee2e38SMatthew G. Knepley . Nv        - The number of function samples
18484bee2e38SMatthew G. Knepley . Nc        - The number of function components
18494bee2e38SMatthew G. Knepley - vals      - The function values
18504bee2e38SMatthew G. Knepley 
18514bee2e38SMatthew G. Knepley   Output Parameter:
18524bee2e38SMatthew G. Knepley . vals      - The transformed function values
18534bee2e38SMatthew G. Knepley 
1854a4ce7ad1SMatthew G. Knepley   Level: intermediate
18554bee2e38SMatthew G. Knepley 
1856f9244615SMatthew G. Knepley   Note: This only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
18572edcad52SToby Isaac 
1858f9244615SMatthew G. Knepley .seealso: PetscDualSpaceTransformGradient(), PetscDualSpaceTransformHessian(), PetscDualSpacePullback(), PetscDualSpacePushforward(), PetscDualSpaceTransformType
18594bee2e38SMatthew G. Knepley @*/
18604bee2e38SMatthew G. Knepley PetscErrorCode PetscDualSpaceTransform(PetscDualSpace dsp, PetscDualSpaceTransformType trans, PetscBool isInverse, PetscFEGeom *fegeom, PetscInt Nv, PetscInt Nc, PetscScalar vals[])
18614bee2e38SMatthew G. Knepley {
1862b4457527SToby Isaac   PetscReal Jstar[9] = {0};
1863b4457527SToby Isaac   PetscInt dim, v, c, Nk;
1864b4457527SToby Isaac   PetscErrorCode ierr;
18654bee2e38SMatthew G. Knepley 
18664bee2e38SMatthew G. Knepley   PetscFunctionBeginHot;
18674bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
18684bee2e38SMatthew G. Knepley   PetscValidPointer(fegeom, 4);
18694bee2e38SMatthew G. Knepley   PetscValidPointer(vals, 7);
1870b4457527SToby Isaac   /* TODO: not handling dimEmbed != dim right now */
18712ae266adSMatthew G. Knepley   dim = dsp->dm->dim;
1872b4457527SToby Isaac   /* No change needed for 0-forms */
1873b4457527SToby Isaac   if (!dsp->k) PetscFunctionReturn(0);
1874b4457527SToby Isaac   ierr = PetscDTBinomialInt(dim, PetscAbsInt(dsp->k), &Nk);CHKERRQ(ierr);
1875b4457527SToby Isaac   /* TODO: use fegeom->isAffine */
1876b4457527SToby Isaac   ierr = PetscDTAltVPullbackMatrix(dim, dim, isInverse ? fegeom->J : fegeom->invJ, dsp->k, Jstar);CHKERRQ(ierr);
18774bee2e38SMatthew G. Knepley   for (v = 0; v < Nv; ++v) {
1878b4457527SToby Isaac     switch (Nk) {
1879b4457527SToby Isaac     case 1:
1880b4457527SToby Isaac       for (c = 0; c < Nc; c++) vals[v*Nc + c] *= Jstar[0];
18814bee2e38SMatthew G. Knepley       break;
1882b4457527SToby Isaac     case 2:
1883b4457527SToby Isaac       for (c = 0; c < Nc; c += 2) DMPlex_Mult2DReal_Internal(Jstar, 1, &vals[v*Nc + c], &vals[v*Nc + c]);
18844bee2e38SMatthew G. Knepley       break;
1885b4457527SToby Isaac     case 3:
1886b4457527SToby Isaac       for (c = 0; c < Nc; c += 3) DMPlex_Mult3DReal_Internal(Jstar, 1, &vals[v*Nc + c], &vals[v*Nc + c]);
1887b4457527SToby Isaac       break;
1888b4457527SToby Isaac     default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported form size %D for transformation", Nk);
1889b4457527SToby Isaac     }
18904bee2e38SMatthew G. Knepley   }
18914bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
18924bee2e38SMatthew G. Knepley }
1893b4457527SToby Isaac 
18944bee2e38SMatthew G. Knepley /*@C
18954bee2e38SMatthew G. Knepley   PetscDualSpaceTransformGradient - Transform the function gradient values
18964bee2e38SMatthew G. Knepley 
18974bee2e38SMatthew G. Knepley   Input Parameters:
18984bee2e38SMatthew G. Knepley + dsp       - The PetscDualSpace
18994bee2e38SMatthew G. Knepley . trans     - The type of transform
19004bee2e38SMatthew G. Knepley . isInverse - Flag to invert the transform
19014bee2e38SMatthew G. Knepley . fegeom    - The cell geometry
19024bee2e38SMatthew G. Knepley . Nv        - The number of function gradient samples
19034bee2e38SMatthew G. Knepley . Nc        - The number of function components
19044bee2e38SMatthew G. Knepley - vals      - The function gradient values
19054bee2e38SMatthew G. Knepley 
19064bee2e38SMatthew G. Knepley   Output Parameter:
1907f9244615SMatthew G. Knepley . vals      - The transformed function gradient values
19084bee2e38SMatthew G. Knepley 
1909a4ce7ad1SMatthew G. Knepley   Level: intermediate
19104bee2e38SMatthew G. Knepley 
1911f9244615SMatthew G. Knepley   Note: This only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
19122edcad52SToby Isaac 
1913625e0966SMatthew G. Knepley .seealso: PetscDualSpaceTransform(), PetscDualSpacePullback(), PetscDualSpacePushforward(), PetscDualSpaceTransformType
19144bee2e38SMatthew G. Knepley @*/
19154bee2e38SMatthew G. Knepley PetscErrorCode PetscDualSpaceTransformGradient(PetscDualSpace dsp, PetscDualSpaceTransformType trans, PetscBool isInverse, PetscFEGeom *fegeom, PetscInt Nv, PetscInt Nc, PetscScalar vals[])
19164bee2e38SMatthew G. Knepley {
191727f02ce8SMatthew G. Knepley   const PetscInt dim = dsp->dm->dim, dE = fegeom->dimEmbed;
191827f02ce8SMatthew G. Knepley   PetscInt       v, c, d;
19194bee2e38SMatthew G. Knepley 
19204bee2e38SMatthew G. Knepley   PetscFunctionBeginHot;
19214bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
19224bee2e38SMatthew G. Knepley   PetscValidPointer(fegeom, 4);
19234bee2e38SMatthew G. Knepley   PetscValidPointer(vals, 7);
192427f02ce8SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
192527f02ce8SMatthew G. Knepley   if (dE <= 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid embedding dimension %D", dE);
192627f02ce8SMatthew G. Knepley #endif
19274bee2e38SMatthew G. Knepley   /* Transform gradient */
192827f02ce8SMatthew G. Knepley   if (dim == dE) {
19294bee2e38SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
19304bee2e38SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
19314bee2e38SMatthew G. Knepley         switch (dim)
19324bee2e38SMatthew G. Knepley         {
1933100a78e1SStefano Zampini           case 1: vals[(v*Nc+c)*dim] *= fegeom->invJ[0];break;
19346142fa51SMatthew G. Knepley           case 2: DMPlex_MultTranspose2DReal_Internal(fegeom->invJ, 1, &vals[(v*Nc+c)*dim], &vals[(v*Nc+c)*dim]);break;
19356142fa51SMatthew G. Knepley           case 3: DMPlex_MultTranspose3DReal_Internal(fegeom->invJ, 1, &vals[(v*Nc+c)*dim], &vals[(v*Nc+c)*dim]);break;
19364bee2e38SMatthew G. Knepley           default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dim %D for transformation", dim);
19374bee2e38SMatthew G. Knepley         }
19384bee2e38SMatthew G. Knepley       }
19394bee2e38SMatthew G. Knepley     }
194027f02ce8SMatthew G. Knepley   } else {
194127f02ce8SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
194227f02ce8SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
194327f02ce8SMatthew G. Knepley         DMPlex_MultTransposeReal_Internal(fegeom->invJ, dim, dE, 1, &vals[(v*Nc+c)*dE], &vals[(v*Nc+c)*dE]);
194427f02ce8SMatthew G. Knepley       }
194527f02ce8SMatthew G. Knepley     }
194627f02ce8SMatthew G. Knepley   }
19474bee2e38SMatthew G. Knepley   /* Assume its a vector, otherwise assume its a bunch of scalars */
19484bee2e38SMatthew G. Knepley   if (Nc == 1 || Nc != dim) PetscFunctionReturn(0);
19494bee2e38SMatthew G. Knepley   switch (trans) {
19504bee2e38SMatthew G. Knepley     case IDENTITY_TRANSFORM: break;
19514bee2e38SMatthew G. Knepley     case COVARIANT_PIOLA_TRANSFORM: /* Covariant Piola mapping $\sigma^*(F) = J^{-T} F \circ \phi^{-1)$ */
19524bee2e38SMatthew G. Knepley     if (isInverse) {
19534bee2e38SMatthew G. Knepley       for (v = 0; v < Nv; ++v) {
19544bee2e38SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19554bee2e38SMatthew G. Knepley           switch (dim)
19564bee2e38SMatthew G. Knepley           {
19576142fa51SMatthew G. Knepley             case 2: DMPlex_MultTranspose2DReal_Internal(fegeom->J, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19586142fa51SMatthew G. Knepley             case 3: DMPlex_MultTranspose3DReal_Internal(fegeom->J, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19594bee2e38SMatthew G. Knepley             default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dim %D for transformation", dim);
19604bee2e38SMatthew G. Knepley           }
19614bee2e38SMatthew G. Knepley         }
19624bee2e38SMatthew G. Knepley       }
19634bee2e38SMatthew G. Knepley     } else {
19644bee2e38SMatthew G. Knepley       for (v = 0; v < Nv; ++v) {
19654bee2e38SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19664bee2e38SMatthew G. Knepley           switch (dim)
19674bee2e38SMatthew G. Knepley           {
19686142fa51SMatthew G. Knepley             case 2: DMPlex_MultTranspose2DReal_Internal(fegeom->invJ, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19696142fa51SMatthew G. Knepley             case 3: DMPlex_MultTranspose3DReal_Internal(fegeom->invJ, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19704bee2e38SMatthew G. Knepley             default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dim %D for transformation", dim);
19714bee2e38SMatthew G. Knepley           }
19724bee2e38SMatthew G. Knepley         }
19734bee2e38SMatthew G. Knepley       }
19744bee2e38SMatthew G. Knepley     }
19754bee2e38SMatthew G. Knepley     break;
19764bee2e38SMatthew G. Knepley     case CONTRAVARIANT_PIOLA_TRANSFORM: /* Contravariant Piola mapping $\sigma^*(F) = \frac{1}{|\det J|} J F \circ \phi^{-1}$ */
19774bee2e38SMatthew G. Knepley     if (isInverse) {
19784bee2e38SMatthew G. Knepley       for (v = 0; v < Nv; ++v) {
19794bee2e38SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19804bee2e38SMatthew G. Knepley           switch (dim)
19814bee2e38SMatthew G. Knepley           {
19826142fa51SMatthew G. Knepley             case 2: DMPlex_Mult2DReal_Internal(fegeom->invJ, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19836142fa51SMatthew G. Knepley             case 3: DMPlex_Mult3DReal_Internal(fegeom->invJ, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19844bee2e38SMatthew G. Knepley             default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dim %D for transformation", dim);
19854bee2e38SMatthew G. Knepley           }
19864bee2e38SMatthew G. Knepley           for (c = 0; c < Nc; ++c) vals[(v*Nc+c)*dim+d] *= fegeom->detJ[0];
19874bee2e38SMatthew G. Knepley         }
19884bee2e38SMatthew G. Knepley       }
19894bee2e38SMatthew G. Knepley     } else {
19904bee2e38SMatthew G. Knepley       for (v = 0; v < Nv; ++v) {
19914bee2e38SMatthew G. Knepley         for (d = 0; d < dim; ++d) {
19924bee2e38SMatthew G. Knepley           switch (dim)
19934bee2e38SMatthew G. Knepley           {
19946142fa51SMatthew G. Knepley             case 2: DMPlex_Mult2DReal_Internal(fegeom->J, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19956142fa51SMatthew G. Knepley             case 3: DMPlex_Mult3DReal_Internal(fegeom->J, dim, &vals[v*Nc*dim+d], &vals[v*Nc*dim+d]);break;
19964bee2e38SMatthew G. Knepley             default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dim %D for transformation", dim);
19974bee2e38SMatthew G. Knepley           }
19984bee2e38SMatthew G. Knepley           for (c = 0; c < Nc; ++c) vals[(v*Nc+c)*dim+d] /= fegeom->detJ[0];
19994bee2e38SMatthew G. Knepley         }
20004bee2e38SMatthew G. Knepley       }
20014bee2e38SMatthew G. Knepley     }
20024bee2e38SMatthew G. Knepley     break;
20034bee2e38SMatthew G. Knepley   }
20044bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
20054bee2e38SMatthew G. Knepley }
20064bee2e38SMatthew G. Knepley 
20074bee2e38SMatthew G. Knepley /*@C
2008f9244615SMatthew G. Knepley   PetscDualSpaceTransformHessian - Transform the function Hessian values
2009f9244615SMatthew G. Knepley 
2010f9244615SMatthew G. Knepley   Input Parameters:
2011f9244615SMatthew G. Knepley + dsp       - The PetscDualSpace
2012f9244615SMatthew G. Knepley . trans     - The type of transform
2013f9244615SMatthew G. Knepley . isInverse - Flag to invert the transform
2014f9244615SMatthew G. Knepley . fegeom    - The cell geometry
2015f9244615SMatthew G. Knepley . Nv        - The number of function Hessian samples
2016f9244615SMatthew G. Knepley . Nc        - The number of function components
2017f9244615SMatthew G. Knepley - vals      - The function gradient values
2018f9244615SMatthew G. Knepley 
2019f9244615SMatthew G. Knepley   Output Parameter:
2020f9244615SMatthew G. Knepley . vals      - The transformed function Hessian values
2021f9244615SMatthew G. Knepley 
2022f9244615SMatthew G. Knepley   Level: intermediate
2023f9244615SMatthew G. Knepley 
2024f9244615SMatthew G. Knepley   Note: This only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
2025f9244615SMatthew G. Knepley 
2026f9244615SMatthew G. Knepley .seealso: PetscDualSpaceTransform(), PetscDualSpacePullback(), PetscDualSpacePushforward(), PetscDualSpaceTransformType
2027f9244615SMatthew G. Knepley @*/
2028f9244615SMatthew G. Knepley PetscErrorCode PetscDualSpaceTransformHessian(PetscDualSpace dsp, PetscDualSpaceTransformType trans, PetscBool isInverse, PetscFEGeom *fegeom, PetscInt Nv, PetscInt Nc, PetscScalar vals[])
2029f9244615SMatthew G. Knepley {
2030f9244615SMatthew G. Knepley   const PetscInt dim = dsp->dm->dim, dE = fegeom->dimEmbed;
2031f9244615SMatthew G. Knepley   PetscInt       v, c;
2032f9244615SMatthew G. Knepley 
2033f9244615SMatthew G. Knepley   PetscFunctionBeginHot;
2034f9244615SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
2035f9244615SMatthew G. Knepley   PetscValidPointer(fegeom, 4);
2036f9244615SMatthew G. Knepley   PetscValidPointer(vals, 7);
2037f9244615SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
2038f9244615SMatthew G. Knepley   if (dE <= 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid embedding dimension %D", dE);
2039f9244615SMatthew G. Knepley #endif
2040f9244615SMatthew G. Knepley   /* Transform Hessian: J^{-T}_{ik} J^{-T}_{jl} H(f)_{kl} = J^{-T}_{ik} H(f)_{kl} J^{-1}_{lj} */
2041f9244615SMatthew G. Knepley   if (dim == dE) {
2042f9244615SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
2043f9244615SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
2044f9244615SMatthew G. Knepley         switch (dim)
2045f9244615SMatthew G. Knepley         {
2046f9244615SMatthew G. Knepley           case 1: vals[(v*Nc+c)*dim*dim] *= PetscSqr(fegeom->invJ[0]);break;
2047f9244615SMatthew G. Knepley           case 2: DMPlex_PTAP2DReal_Internal(fegeom->invJ, &vals[(v*Nc+c)*dim*dim], &vals[(v*Nc+c)*dim*dim]);break;
2048f9244615SMatthew G. Knepley           case 3: DMPlex_PTAP3DReal_Internal(fegeom->invJ, &vals[(v*Nc+c)*dim*dim], &vals[(v*Nc+c)*dim*dim]);break;
2049f9244615SMatthew G. Knepley           default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dim %D for transformation", dim);
2050f9244615SMatthew G. Knepley         }
2051f9244615SMatthew G. Knepley       }
2052f9244615SMatthew G. Knepley     }
2053f9244615SMatthew G. Knepley   } else {
2054f9244615SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
2055f9244615SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
2056f9244615SMatthew G. Knepley         DMPlex_PTAPReal_Internal(fegeom->invJ, dim, dE, &vals[(v*Nc+c)*dE*dE], &vals[(v*Nc+c)*dE*dE]);
2057f9244615SMatthew G. Knepley       }
2058f9244615SMatthew G. Knepley     }
2059f9244615SMatthew G. Knepley   }
2060f9244615SMatthew G. Knepley   /* Assume its a vector, otherwise assume its a bunch of scalars */
2061f9244615SMatthew G. Knepley   if (Nc == 1 || Nc != dim) PetscFunctionReturn(0);
2062f9244615SMatthew G. Knepley   switch (trans) {
2063f9244615SMatthew G. Knepley     case IDENTITY_TRANSFORM: break;
2064f9244615SMatthew G. Knepley     case COVARIANT_PIOLA_TRANSFORM: /* Covariant Piola mapping $\sigma^*(F) = J^{-T} F \circ \phi^{-1)$ */
2065f9244615SMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Piola mapping for Hessians not yet supported");
2066f9244615SMatthew G. Knepley     case CONTRAVARIANT_PIOLA_TRANSFORM: /* Contravariant Piola mapping $\sigma^*(F) = \frac{1}{|\det J|} J F \circ \phi^{-1}$ */
2067f9244615SMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Piola mapping for Hessians not yet supported");
2068f9244615SMatthew G. Knepley   }
2069f9244615SMatthew G. Knepley   PetscFunctionReturn(0);
2070f9244615SMatthew G. Knepley }
2071f9244615SMatthew G. Knepley 
2072f9244615SMatthew G. Knepley /*@C
20734bee2e38SMatthew G. Knepley   PetscDualSpacePullback - Transform the given functional so that it operates on real space, rather than the reference element. Operationally, this means that we map the function evaluations depending on continuity requirements of our finite element method.
20744bee2e38SMatthew G. Knepley 
20754bee2e38SMatthew G. Knepley   Input Parameters:
20764bee2e38SMatthew G. Knepley + dsp        - The PetscDualSpace
20774bee2e38SMatthew G. Knepley . fegeom     - The geometry for this cell
20784bee2e38SMatthew G. Knepley . Nq         - The number of function samples
20794bee2e38SMatthew G. Knepley . Nc         - The number of function components
20804bee2e38SMatthew G. Knepley - pointEval  - The function values
20814bee2e38SMatthew G. Knepley 
20824bee2e38SMatthew G. Knepley   Output Parameter:
20834bee2e38SMatthew G. Knepley . pointEval  - The transformed function values
20844bee2e38SMatthew G. Knepley 
20854bee2e38SMatthew G. Knepley   Level: advanced
20864bee2e38SMatthew G. Knepley 
20874bee2e38SMatthew G. Knepley   Note: Functions transform in a complementary way (pushforward) to functionals, so that the scalar product is invariant. The type of transform is dependent on the associated k-simplex from the DeRahm complex.
20884bee2e38SMatthew G. Knepley 
20892edcad52SToby Isaac   Note: This only handles tranformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
20902edcad52SToby Isaac 
20914bee2e38SMatthew G. Knepley .seealso: PetscDualSpacePushforward(), PetscDualSpaceTransform(), PetscDualSpaceGetDeRahm()
20924bee2e38SMatthew G. Knepley @*/
20932edcad52SToby Isaac PetscErrorCode PetscDualSpacePullback(PetscDualSpace dsp, PetscFEGeom *fegeom, PetscInt Nq, PetscInt Nc, PetscScalar pointEval[])
20944bee2e38SMatthew G. Knepley {
20954bee2e38SMatthew G. Knepley   PetscDualSpaceTransformType trans;
2096b4457527SToby Isaac   PetscInt                    k;
20974bee2e38SMatthew G. Knepley   PetscErrorCode              ierr;
20984bee2e38SMatthew G. Knepley 
20994bee2e38SMatthew G. Knepley   PetscFunctionBeginHot;
21004bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
21014bee2e38SMatthew G. Knepley   PetscValidPointer(fegeom, 2);
21022edcad52SToby Isaac   PetscValidPointer(pointEval, 5);
21034bee2e38SMatthew G. Knepley   /* The dualspace dofs correspond to some simplex in the DeRahm complex, which we label by k.
21044bee2e38SMatthew G. Knepley      This determines their transformation properties. */
2105b4457527SToby Isaac   ierr = PetscDualSpaceGetDeRahm(dsp, &k);CHKERRQ(ierr);
2106b4457527SToby Isaac   switch (k)
21074bee2e38SMatthew G. Knepley   {
21084bee2e38SMatthew G. Knepley     case 0: /* H^1 point evaluations */
21094bee2e38SMatthew G. Knepley     trans = IDENTITY_TRANSFORM;break;
21104bee2e38SMatthew G. Knepley     case 1: /* Hcurl preserves tangential edge traces  */
21114bee2e38SMatthew G. Knepley     trans = COVARIANT_PIOLA_TRANSFORM;break;
2112b4457527SToby Isaac     case 2:
21134bee2e38SMatthew G. Knepley     case 3: /* Hdiv preserve normal traces */
21144bee2e38SMatthew G. Knepley     trans = CONTRAVARIANT_PIOLA_TRANSFORM;break;
2115b4457527SToby Isaac     default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported simplex dim %D for transformation", k);
21164bee2e38SMatthew G. Knepley   }
21172edcad52SToby Isaac   ierr = PetscDualSpaceTransform(dsp, trans, PETSC_TRUE, fegeom, Nq, Nc, pointEval);CHKERRQ(ierr);
21184bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
21194bee2e38SMatthew G. Knepley }
21204bee2e38SMatthew G. Knepley 
21214bee2e38SMatthew G. Knepley /*@C
21224bee2e38SMatthew G. Knepley   PetscDualSpacePushforward - Transform the given function so that it operates on real space, rather than the reference element. Operationally, this means that we map the function evaluations depending on continuity requirements of our finite element method.
21234bee2e38SMatthew G. Knepley 
21244bee2e38SMatthew G. Knepley   Input Parameters:
21254bee2e38SMatthew G. Knepley + dsp        - The PetscDualSpace
21264bee2e38SMatthew G. Knepley . fegeom     - The geometry for this cell
21274bee2e38SMatthew G. Knepley . Nq         - The number of function samples
21284bee2e38SMatthew G. Knepley . Nc         - The number of function components
21294bee2e38SMatthew G. Knepley - pointEval  - The function values
21304bee2e38SMatthew G. Knepley 
21314bee2e38SMatthew G. Knepley   Output Parameter:
21324bee2e38SMatthew G. Knepley . pointEval  - The transformed function values
21334bee2e38SMatthew G. Knepley 
21344bee2e38SMatthew G. Knepley   Level: advanced
21354bee2e38SMatthew G. Knepley 
21364bee2e38SMatthew G. Knepley   Note: Functionals transform in a complementary way (pullback) to functions, so that the scalar product is invariant. The type of transform is dependent on the associated k-simplex from the DeRahm complex.
21374bee2e38SMatthew G. Knepley 
2138f9244615SMatthew G. Knepley   Note: This only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
21392edcad52SToby Isaac 
21404bee2e38SMatthew G. Knepley .seealso: PetscDualSpacePullback(), PetscDualSpaceTransform(), PetscDualSpaceGetDeRahm()
21414bee2e38SMatthew G. Knepley @*/
21422edcad52SToby Isaac PetscErrorCode PetscDualSpacePushforward(PetscDualSpace dsp, PetscFEGeom *fegeom, PetscInt Nq, PetscInt Nc, PetscScalar pointEval[])
21434bee2e38SMatthew G. Knepley {
21444bee2e38SMatthew G. Knepley   PetscDualSpaceTransformType trans;
2145b4457527SToby Isaac   PetscInt                    k;
21464bee2e38SMatthew G. Knepley   PetscErrorCode              ierr;
21474bee2e38SMatthew G. Knepley 
21484bee2e38SMatthew G. Knepley   PetscFunctionBeginHot;
21494bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
21504bee2e38SMatthew G. Knepley   PetscValidPointer(fegeom, 2);
21512edcad52SToby Isaac   PetscValidPointer(pointEval, 5);
21524bee2e38SMatthew G. Knepley   /* The dualspace dofs correspond to some simplex in the DeRahm complex, which we label by k.
21534bee2e38SMatthew G. Knepley      This determines their transformation properties. */
2154b4457527SToby Isaac   ierr = PetscDualSpaceGetDeRahm(dsp, &k);CHKERRQ(ierr);
2155b4457527SToby Isaac   switch (k)
21564bee2e38SMatthew G. Knepley   {
21574bee2e38SMatthew G. Knepley     case 0: /* H^1 point evaluations */
21584bee2e38SMatthew G. Knepley     trans = IDENTITY_TRANSFORM;break;
21594bee2e38SMatthew G. Knepley     case 1: /* Hcurl preserves tangential edge traces  */
21604bee2e38SMatthew G. Knepley     trans = COVARIANT_PIOLA_TRANSFORM;break;
2161b4457527SToby Isaac     case 2:
21624bee2e38SMatthew G. Knepley     case 3: /* Hdiv preserve normal traces */
21634bee2e38SMatthew G. Knepley     trans = CONTRAVARIANT_PIOLA_TRANSFORM;break;
2164b4457527SToby Isaac     default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported simplex dim %D for transformation", k);
21654bee2e38SMatthew G. Knepley   }
21662edcad52SToby Isaac   ierr = PetscDualSpaceTransform(dsp, trans, PETSC_FALSE, fegeom, Nq, Nc, pointEval);CHKERRQ(ierr);
21674bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
21684bee2e38SMatthew G. Knepley }
21694bee2e38SMatthew G. Knepley 
21704bee2e38SMatthew G. Knepley /*@C
21714bee2e38SMatthew G. Knepley   PetscDualSpacePushforwardGradient - Transform the given function gradient so that it operates on real space, rather than the reference element. Operationally, this means that we map the function evaluations depending on continuity requirements of our finite element method.
21724bee2e38SMatthew G. Knepley 
21734bee2e38SMatthew G. Knepley   Input Parameters:
21744bee2e38SMatthew G. Knepley + dsp        - The PetscDualSpace
21754bee2e38SMatthew G. Knepley . fegeom     - The geometry for this cell
21764bee2e38SMatthew G. Knepley . Nq         - The number of function gradient samples
21774bee2e38SMatthew G. Knepley . Nc         - The number of function components
21784bee2e38SMatthew G. Knepley - pointEval  - The function gradient values
21794bee2e38SMatthew G. Knepley 
21804bee2e38SMatthew G. Knepley   Output Parameter:
21814bee2e38SMatthew G. Knepley . pointEval  - The transformed function gradient values
21824bee2e38SMatthew G. Knepley 
21834bee2e38SMatthew G. Knepley   Level: advanced
21844bee2e38SMatthew G. Knepley 
21854bee2e38SMatthew G. Knepley   Note: Functionals transform in a complementary way (pullback) to functions, so that the scalar product is invariant. The type of transform is dependent on the associated k-simplex from the DeRahm complex.
21864bee2e38SMatthew G. Knepley 
2187f9244615SMatthew G. Knepley   Note: This only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
21882edcad52SToby Isaac 
21894bee2e38SMatthew G. Knepley .seealso: PetscDualSpacePushforward(), PPetscDualSpacePullback(), PetscDualSpaceTransform(), PetscDualSpaceGetDeRahm()
2190dc0529c6SBarry Smith @*/
21912edcad52SToby Isaac PetscErrorCode PetscDualSpacePushforwardGradient(PetscDualSpace dsp, PetscFEGeom *fegeom, PetscInt Nq, PetscInt Nc, PetscScalar pointEval[])
21924bee2e38SMatthew G. Knepley {
21934bee2e38SMatthew G. Knepley   PetscDualSpaceTransformType trans;
2194b4457527SToby Isaac   PetscInt                    k;
21954bee2e38SMatthew G. Knepley   PetscErrorCode              ierr;
21964bee2e38SMatthew G. Knepley 
21974bee2e38SMatthew G. Knepley   PetscFunctionBeginHot;
21984bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
21994bee2e38SMatthew G. Knepley   PetscValidPointer(fegeom, 2);
22002edcad52SToby Isaac   PetscValidPointer(pointEval, 5);
22014bee2e38SMatthew G. Knepley   /* The dualspace dofs correspond to some simplex in the DeRahm complex, which we label by k.
22024bee2e38SMatthew G. Knepley      This determines their transformation properties. */
2203b4457527SToby Isaac   ierr = PetscDualSpaceGetDeRahm(dsp, &k);CHKERRQ(ierr);
2204b4457527SToby Isaac   switch (k)
22054bee2e38SMatthew G. Knepley   {
22064bee2e38SMatthew G. Knepley     case 0: /* H^1 point evaluations */
22074bee2e38SMatthew G. Knepley     trans = IDENTITY_TRANSFORM;break;
22084bee2e38SMatthew G. Knepley     case 1: /* Hcurl preserves tangential edge traces  */
22094bee2e38SMatthew G. Knepley     trans = COVARIANT_PIOLA_TRANSFORM;break;
2210b4457527SToby Isaac     case 2:
22114bee2e38SMatthew G. Knepley     case 3: /* Hdiv preserve normal traces */
22124bee2e38SMatthew G. Knepley     trans = CONTRAVARIANT_PIOLA_TRANSFORM;break;
2213b4457527SToby Isaac     default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported simplex dim %D for transformation", k);
22144bee2e38SMatthew G. Knepley   }
22152edcad52SToby Isaac   ierr = PetscDualSpaceTransformGradient(dsp, trans, PETSC_FALSE, fegeom, Nq, Nc, pointEval);CHKERRQ(ierr);
22164bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
22174bee2e38SMatthew G. Knepley }
2218f9244615SMatthew G. Knepley 
2219f9244615SMatthew G. Knepley /*@C
2220f9244615SMatthew G. Knepley   PetscDualSpacePushforwardHessian - Transform the given function Hessian so that it operates on real space, rather than the reference element. Operationally, this means that we map the function evaluations depending on continuity requirements of our finite element method.
2221f9244615SMatthew G. Knepley 
2222f9244615SMatthew G. Knepley   Input Parameters:
2223f9244615SMatthew G. Knepley + dsp        - The PetscDualSpace
2224f9244615SMatthew G. Knepley . fegeom     - The geometry for this cell
2225f9244615SMatthew G. Knepley . Nq         - The number of function Hessian samples
2226f9244615SMatthew G. Knepley . Nc         - The number of function components
2227f9244615SMatthew G. Knepley - pointEval  - The function gradient values
2228f9244615SMatthew G. Knepley 
2229f9244615SMatthew G. Knepley   Output Parameter:
2230f9244615SMatthew G. Knepley . pointEval  - The transformed function Hessian values
2231f9244615SMatthew G. Knepley 
2232f9244615SMatthew G. Knepley   Level: advanced
2233f9244615SMatthew G. Knepley 
2234f9244615SMatthew G. Knepley   Note: Functionals transform in a complementary way (pullback) to functions, so that the scalar product is invariant. The type of transform is dependent on the associated k-simplex from the DeRahm complex.
2235f9244615SMatthew G. Knepley 
2236f9244615SMatthew G. Knepley   Note: This only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
2237f9244615SMatthew G. Knepley 
2238f9244615SMatthew G. Knepley .seealso: PetscDualSpacePushforward(), PPetscDualSpacePullback(), PetscDualSpaceTransform(), PetscDualSpaceGetDeRahm()
2239f9244615SMatthew G. Knepley @*/
2240f9244615SMatthew G. Knepley PetscErrorCode PetscDualSpacePushforwardHessian(PetscDualSpace dsp, PetscFEGeom *fegeom, PetscInt Nq, PetscInt Nc, PetscScalar pointEval[])
2241f9244615SMatthew G. Knepley {
2242f9244615SMatthew G. Knepley   PetscDualSpaceTransformType trans;
2243f9244615SMatthew G. Knepley   PetscInt                    k;
2244f9244615SMatthew G. Knepley   PetscErrorCode              ierr;
2245f9244615SMatthew G. Knepley 
2246f9244615SMatthew G. Knepley   PetscFunctionBeginHot;
2247f9244615SMatthew G. Knepley   PetscValidHeaderSpecific(dsp, PETSCDUALSPACE_CLASSID, 1);
2248f9244615SMatthew G. Knepley   PetscValidPointer(fegeom, 2);
2249f9244615SMatthew G. Knepley   PetscValidPointer(pointEval, 5);
2250f9244615SMatthew G. Knepley   /* The dualspace dofs correspond to some simplex in the DeRahm complex, which we label by k.
2251f9244615SMatthew G. Knepley      This determines their transformation properties. */
2252f9244615SMatthew G. Knepley   ierr = PetscDualSpaceGetDeRahm(dsp, &k);CHKERRQ(ierr);
2253f9244615SMatthew G. Knepley   switch (k)
2254f9244615SMatthew G. Knepley   {
2255f9244615SMatthew G. Knepley     case 0: /* H^1 point evaluations */
2256f9244615SMatthew G. Knepley     trans = IDENTITY_TRANSFORM;break;
2257f9244615SMatthew G. Knepley     case 1: /* Hcurl preserves tangential edge traces  */
2258f9244615SMatthew G. Knepley     trans = COVARIANT_PIOLA_TRANSFORM;break;
2259f9244615SMatthew G. Knepley     case 2:
2260f9244615SMatthew G. Knepley     case 3: /* Hdiv preserve normal traces */
2261f9244615SMatthew G. Knepley     trans = CONTRAVARIANT_PIOLA_TRANSFORM;break;
2262f9244615SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dsp), PETSC_ERR_ARG_OUTOFRANGE, "Unsupported simplex dim %D for transformation", k);
2263f9244615SMatthew G. Knepley   }
2264f9244615SMatthew G. Knepley   ierr = PetscDualSpaceTransformHessian(dsp, trans, PETSC_FALSE, fegeom, Nq, Nc, pointEval);CHKERRQ(ierr);
2265f9244615SMatthew G. Knepley   PetscFunctionReturn(0);
2266f9244615SMatthew G. Knepley }
2267