xref: /petsc/src/dm/dt/fe/interface/fe.c (revision dce8aeba1c9b69b19f651c53d8a6b674bd7e9cbd)
120cf1dd8SToby Isaac /* Basis Jet Tabulation
220cf1dd8SToby Isaac 
320cf1dd8SToby Isaac We would like to tabulate the nodal basis functions and derivatives at a set of points, usually quadrature points. We
420cf1dd8SToby Isaac follow here the derviation in http://www.math.ttu.edu/~kirby/papers/fiat-toms-2004.pdf. The nodal basis $\psi_i$ can
520cf1dd8SToby Isaac be expressed in terms of a prime basis $\phi_i$ which can be stably evaluated. In PETSc, we will use the Legendre basis
620cf1dd8SToby Isaac as a prime basis.
720cf1dd8SToby Isaac 
820cf1dd8SToby Isaac   \psi_i = \sum_k \alpha_{ki} \phi_k
920cf1dd8SToby Isaac 
1020cf1dd8SToby Isaac Our nodal basis is defined in terms of the dual basis $n_j$
1120cf1dd8SToby Isaac 
1220cf1dd8SToby Isaac   n_j \cdot \psi_i = \delta_{ji}
1320cf1dd8SToby Isaac 
1420cf1dd8SToby Isaac and we may act on the first equation to obtain
1520cf1dd8SToby Isaac 
1620cf1dd8SToby Isaac   n_j \cdot \psi_i = \sum_k \alpha_{ki} n_j \cdot \phi_k
1720cf1dd8SToby Isaac        \delta_{ji} = \sum_k \alpha_{ki} V_{jk}
1820cf1dd8SToby Isaac                  I = V \alpha
1920cf1dd8SToby Isaac 
2020cf1dd8SToby Isaac so the coefficients of the nodal basis in the prime basis are
2120cf1dd8SToby Isaac 
2220cf1dd8SToby Isaac    \alpha = V^{-1}
2320cf1dd8SToby Isaac 
2420cf1dd8SToby Isaac We will define the dual basis vectors $n_j$ using a quadrature rule.
2520cf1dd8SToby Isaac 
2620cf1dd8SToby Isaac Right now, we will just use the polynomial spaces P^k. I know some elements use the space of symmetric polynomials
2720cf1dd8SToby Isaac (I think Nedelec), but we will neglect this for now. Constraints in the space, e.g. Arnold-Winther elements, can
2820cf1dd8SToby Isaac be implemented exactly as in FIAT using functionals $L_j$.
2920cf1dd8SToby Isaac 
3020cf1dd8SToby Isaac I will have to count the degrees correctly for the Legendre product when we are on simplices.
3120cf1dd8SToby Isaac 
3220cf1dd8SToby Isaac We will have three objects:
3320cf1dd8SToby Isaac  - Space, P: this just need point evaluation I think
3420cf1dd8SToby Isaac  - Dual Space, P'+K: This looks like a set of functionals that can act on members of P, each n is defined by a Q
3520cf1dd8SToby Isaac  - FEM: This keeps {P, P', Q}
3620cf1dd8SToby Isaac */
3720cf1dd8SToby Isaac #include <petsc/private/petscfeimpl.h> /*I "petscfe.h" I*/
3820cf1dd8SToby Isaac #include <petscdmplex.h>
3920cf1dd8SToby Isaac 
4020cf1dd8SToby Isaac PetscBool  FEcite       = PETSC_FALSE;
4120cf1dd8SToby Isaac const char FECitation[] = "@article{kirby2004,\n"
4220cf1dd8SToby Isaac                           "  title   = {Algorithm 839: FIAT, a New Paradigm for Computing Finite Element Basis Functions},\n"
4320cf1dd8SToby Isaac                           "  journal = {ACM Transactions on Mathematical Software},\n"
4420cf1dd8SToby Isaac                           "  author  = {Robert C. Kirby},\n"
4520cf1dd8SToby Isaac                           "  volume  = {30},\n"
4620cf1dd8SToby Isaac                           "  number  = {4},\n"
4720cf1dd8SToby Isaac                           "  pages   = {502--516},\n"
4820cf1dd8SToby Isaac                           "  doi     = {10.1145/1039813.1039820},\n"
4920cf1dd8SToby Isaac                           "  year    = {2004}\n}\n";
5020cf1dd8SToby Isaac 
5120cf1dd8SToby Isaac PetscClassId PETSCFE_CLASSID = 0;
5220cf1dd8SToby Isaac 
53ead873ccSMatthew G. Knepley PetscLogEvent PETSCFE_SetUp;
54ead873ccSMatthew G. Knepley 
5520cf1dd8SToby Isaac PetscFunctionList PetscFEList              = NULL;
5620cf1dd8SToby Isaac PetscBool         PetscFERegisterAllCalled = PETSC_FALSE;
5720cf1dd8SToby Isaac 
5820cf1dd8SToby Isaac /*@C
59*dce8aebaSBarry Smith   PetscFERegister - Adds a new `PetscFEType`
6020cf1dd8SToby Isaac 
6120cf1dd8SToby Isaac   Not Collective
6220cf1dd8SToby Isaac 
6320cf1dd8SToby Isaac   Input Parameters:
6420cf1dd8SToby Isaac + name        - The name of a new user-defined creation routine
6520cf1dd8SToby Isaac - create_func - The creation routine itself
6620cf1dd8SToby Isaac 
6720cf1dd8SToby Isaac   Sample usage:
6820cf1dd8SToby Isaac .vb
6920cf1dd8SToby Isaac     PetscFERegister("my_fe", MyPetscFECreate);
7020cf1dd8SToby Isaac .ve
7120cf1dd8SToby Isaac 
7220cf1dd8SToby Isaac   Then, your PetscFE type can be chosen with the procedural interface via
7320cf1dd8SToby Isaac .vb
7420cf1dd8SToby Isaac     PetscFECreate(MPI_Comm, PetscFE *);
7520cf1dd8SToby Isaac     PetscFESetType(PetscFE, "my_fe");
7620cf1dd8SToby Isaac .ve
7720cf1dd8SToby Isaac    or at runtime via the option
7820cf1dd8SToby Isaac .vb
7920cf1dd8SToby Isaac     -petscfe_type my_fe
8020cf1dd8SToby Isaac .ve
8120cf1dd8SToby Isaac 
8220cf1dd8SToby Isaac   Level: advanced
8320cf1dd8SToby Isaac 
84*dce8aebaSBarry Smith   Note:
85*dce8aebaSBarry Smith   `PetscFERegister()` may be called multiple times to add several user-defined `PetscFE`s
8620cf1dd8SToby Isaac 
87*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEType`, `PetscFERegisterAll()`, `PetscFERegisterDestroy()`
8820cf1dd8SToby Isaac @*/
89d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFERegister(const char sname[], PetscErrorCode (*function)(PetscFE))
90d71ae5a4SJacob Faibussowitsch {
9120cf1dd8SToby Isaac   PetscFunctionBegin;
929566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscFEList, sname, function));
9320cf1dd8SToby Isaac   PetscFunctionReturn(0);
9420cf1dd8SToby Isaac }
9520cf1dd8SToby Isaac 
9620cf1dd8SToby Isaac /*@C
97*dce8aebaSBarry Smith   PetscFESetType - Builds a particular `PetscFE`
9820cf1dd8SToby Isaac 
99d083f849SBarry Smith   Collective on fem
10020cf1dd8SToby Isaac 
10120cf1dd8SToby Isaac   Input Parameters:
102*dce8aebaSBarry Smith + fem  - The `PetscFE` object
10320cf1dd8SToby Isaac - name - The kind of FEM space
10420cf1dd8SToby Isaac 
10520cf1dd8SToby Isaac   Options Database Key:
10620cf1dd8SToby Isaac . -petscfe_type <type> - Sets the PetscFE type; use -help for a list of available types
10720cf1dd8SToby Isaac 
10820cf1dd8SToby Isaac   Level: intermediate
10920cf1dd8SToby Isaac 
110*dce8aebaSBarry Smith .seealso: `PetscFEType`, `PetscFE`, `PetscFEGetType()`, `PetscFECreate()`
11120cf1dd8SToby Isaac @*/
112d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetType(PetscFE fem, PetscFEType name)
113d71ae5a4SJacob Faibussowitsch {
11420cf1dd8SToby Isaac   PetscErrorCode (*r)(PetscFE);
11520cf1dd8SToby Isaac   PetscBool match;
11620cf1dd8SToby Isaac 
11720cf1dd8SToby Isaac   PetscFunctionBegin;
11820cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
1199566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)fem, name, &match));
12020cf1dd8SToby Isaac   if (match) PetscFunctionReturn(0);
12120cf1dd8SToby Isaac 
1229566063dSJacob Faibussowitsch   if (!PetscFERegisterAllCalled) PetscCall(PetscFERegisterAll());
1239566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscFEList, name, &r));
12428b400f6SJacob Faibussowitsch   PetscCheck(r, PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscFE type: %s", name);
12520cf1dd8SToby Isaac 
126dbbe0bcdSBarry Smith   PetscTryTypeMethod(fem, destroy);
12720cf1dd8SToby Isaac   fem->ops->destroy = NULL;
128dbbe0bcdSBarry Smith 
1299566063dSJacob Faibussowitsch   PetscCall((*r)(fem));
1309566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)fem, name));
13120cf1dd8SToby Isaac   PetscFunctionReturn(0);
13220cf1dd8SToby Isaac }
13320cf1dd8SToby Isaac 
13420cf1dd8SToby Isaac /*@C
135*dce8aebaSBarry Smith   PetscFEGetType - Gets the `PetscFEType` (as a string) from the `PetscFE` object.
13620cf1dd8SToby Isaac 
13720cf1dd8SToby Isaac   Not Collective
13820cf1dd8SToby Isaac 
13920cf1dd8SToby Isaac   Input Parameter:
140*dce8aebaSBarry Smith . fem  - The `PetscFE`
14120cf1dd8SToby Isaac 
14220cf1dd8SToby Isaac   Output Parameter:
143*dce8aebaSBarry Smith . name - The `PetscFEType` name
14420cf1dd8SToby Isaac 
14520cf1dd8SToby Isaac   Level: intermediate
14620cf1dd8SToby Isaac 
147*dce8aebaSBarry Smith .seealso: `PetscFEType`, `PetscFE`, `PetscFESetType()`, `PetscFECreate()`
14820cf1dd8SToby Isaac @*/
149d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetType(PetscFE fem, PetscFEType *name)
150d71ae5a4SJacob Faibussowitsch {
15120cf1dd8SToby Isaac   PetscFunctionBegin;
15220cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
15320cf1dd8SToby Isaac   PetscValidPointer(name, 2);
15448a46eb9SPierre Jolivet   if (!PetscFERegisterAllCalled) PetscCall(PetscFERegisterAll());
15520cf1dd8SToby Isaac   *name = ((PetscObject)fem)->type_name;
15620cf1dd8SToby Isaac   PetscFunctionReturn(0);
15720cf1dd8SToby Isaac }
15820cf1dd8SToby Isaac 
15920cf1dd8SToby Isaac /*@C
160*dce8aebaSBarry Smith    PetscFEViewFromOptions - View from a `PetscFE` based on values in the options database
161fe2efc57SMark 
162*dce8aebaSBarry Smith    Collective on A
163fe2efc57SMark 
164fe2efc57SMark    Input Parameters:
165*dce8aebaSBarry Smith +  A - the `PetscFE` object
166*dce8aebaSBarry Smith .  obj - Optional object that provides the options prefix
167*dce8aebaSBarry Smith -  name - command line option name
168fe2efc57SMark 
169fe2efc57SMark    Level: intermediate
170*dce8aebaSBarry Smith 
171*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEView()`, `PetscObjectViewFromOptions()`, `PetscFECreate()`
172fe2efc57SMark @*/
173d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEViewFromOptions(PetscFE A, PetscObject obj, const char name[])
174d71ae5a4SJacob Faibussowitsch {
175fe2efc57SMark   PetscFunctionBegin;
176fe2efc57SMark   PetscValidHeaderSpecific(A, PETSCFE_CLASSID, 1);
1779566063dSJacob Faibussowitsch   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
178fe2efc57SMark   PetscFunctionReturn(0);
179fe2efc57SMark }
180fe2efc57SMark 
181fe2efc57SMark /*@C
182*dce8aebaSBarry Smith   PetscFEView - Views a `PetscFE`
18320cf1dd8SToby Isaac 
184d083f849SBarry Smith   Collective on fem
18520cf1dd8SToby Isaac 
186d8d19677SJose E. Roman   Input Parameters:
187*dce8aebaSBarry Smith + fem - the `PetscFE` object to view
188d9bac1caSLisandro Dalcin - viewer   - the viewer
18920cf1dd8SToby Isaac 
1902b99622eSMatthew G. Knepley   Level: beginner
19120cf1dd8SToby Isaac 
192*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscViewer`, `PetscFEDestroy()`, `PetscFEViewFromOptions()`
19320cf1dd8SToby Isaac @*/
194d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEView(PetscFE fem, PetscViewer viewer)
195d71ae5a4SJacob Faibussowitsch {
196d9bac1caSLisandro Dalcin   PetscBool iascii;
19720cf1dd8SToby Isaac 
19820cf1dd8SToby Isaac   PetscFunctionBegin;
19920cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
200d9bac1caSLisandro Dalcin   if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
2019566063dSJacob Faibussowitsch   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)fem), &viewer));
2029566063dSJacob Faibussowitsch   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)fem, viewer));
2039566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
204dbbe0bcdSBarry Smith   PetscTryTypeMethod(fem, view, viewer);
20520cf1dd8SToby Isaac   PetscFunctionReturn(0);
20620cf1dd8SToby Isaac }
20720cf1dd8SToby Isaac 
20820cf1dd8SToby Isaac /*@
209*dce8aebaSBarry Smith   PetscFESetFromOptions - sets parameters in a `PetscFE` from the options database
21020cf1dd8SToby Isaac 
211d083f849SBarry Smith   Collective on fem
21220cf1dd8SToby Isaac 
21320cf1dd8SToby Isaac   Input Parameter:
214*dce8aebaSBarry Smith . fem - the `PetscFE` object to set options for
21520cf1dd8SToby Isaac 
216*dce8aebaSBarry Smith   Options Database Keys:
217a2b725a8SWilliam Gropp + -petscfe_num_blocks  - the number of cell blocks to integrate concurrently
218a2b725a8SWilliam Gropp - -petscfe_num_batches - the number of cell batches to integrate serially
21920cf1dd8SToby Isaac 
2202b99622eSMatthew G. Knepley   Level: intermediate
22120cf1dd8SToby Isaac 
222*dce8aebaSBarry Smith .seealso: `PetscFEV`, `PetscFEView()`
22320cf1dd8SToby Isaac @*/
224d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetFromOptions(PetscFE fem)
225d71ae5a4SJacob Faibussowitsch {
22620cf1dd8SToby Isaac   const char *defaultType;
22720cf1dd8SToby Isaac   char        name[256];
22820cf1dd8SToby Isaac   PetscBool   flg;
22920cf1dd8SToby Isaac 
23020cf1dd8SToby Isaac   PetscFunctionBegin;
23120cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
23220cf1dd8SToby Isaac   if (!((PetscObject)fem)->type_name) {
23320cf1dd8SToby Isaac     defaultType = PETSCFEBASIC;
23420cf1dd8SToby Isaac   } else {
23520cf1dd8SToby Isaac     defaultType = ((PetscObject)fem)->type_name;
23620cf1dd8SToby Isaac   }
2379566063dSJacob Faibussowitsch   if (!PetscFERegisterAllCalled) PetscCall(PetscFERegisterAll());
23820cf1dd8SToby Isaac 
239d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)fem);
2409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-petscfe_type", "Finite element space", "PetscFESetType", PetscFEList, defaultType, name, 256, &flg));
24120cf1dd8SToby Isaac   if (flg) {
2429566063dSJacob Faibussowitsch     PetscCall(PetscFESetType(fem, name));
24320cf1dd8SToby Isaac   } else if (!((PetscObject)fem)->type_name) {
2449566063dSJacob Faibussowitsch     PetscCall(PetscFESetType(fem, defaultType));
24520cf1dd8SToby Isaac   }
2469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-petscfe_num_blocks", "The number of cell blocks to integrate concurrently", "PetscSpaceSetTileSizes", fem->numBlocks, &fem->numBlocks, NULL, 1));
2479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-petscfe_num_batches", "The number of cell batches to integrate serially", "PetscSpaceSetTileSizes", fem->numBatches, &fem->numBatches, NULL, 1));
248dbbe0bcdSBarry Smith   PetscTryTypeMethod(fem, setfromoptions, PetscOptionsObject);
24920cf1dd8SToby Isaac   /* process any options handlers added with PetscObjectAddOptionsHandler() */
250dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)fem, PetscOptionsObject));
251d0609cedSBarry Smith   PetscOptionsEnd();
2529566063dSJacob Faibussowitsch   PetscCall(PetscFEViewFromOptions(fem, NULL, "-petscfe_view"));
25320cf1dd8SToby Isaac   PetscFunctionReturn(0);
25420cf1dd8SToby Isaac }
25520cf1dd8SToby Isaac 
25620cf1dd8SToby Isaac /*@C
257*dce8aebaSBarry Smith   PetscFESetUp - Construct data structures for the `PetscFE` after the `PetscFEType` has been set
25820cf1dd8SToby Isaac 
259d083f849SBarry Smith   Collective on fem
26020cf1dd8SToby Isaac 
26120cf1dd8SToby Isaac   Input Parameter:
262*dce8aebaSBarry Smith . fem - the `PetscFE` object to setup
26320cf1dd8SToby Isaac 
2642b99622eSMatthew G. Knepley   Level: intermediate
26520cf1dd8SToby Isaac 
266*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEView()`, `PetscFEDestroy()`
26720cf1dd8SToby Isaac @*/
268d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetUp(PetscFE fem)
269d71ae5a4SJacob Faibussowitsch {
27020cf1dd8SToby Isaac   PetscFunctionBegin;
27120cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
27220cf1dd8SToby Isaac   if (fem->setupcalled) PetscFunctionReturn(0);
2739566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(PETSCFE_SetUp, fem, 0, 0, 0));
27420cf1dd8SToby Isaac   fem->setupcalled = PETSC_TRUE;
275dbbe0bcdSBarry Smith   PetscTryTypeMethod(fem, setup);
2769566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(PETSCFE_SetUp, fem, 0, 0, 0));
27720cf1dd8SToby Isaac   PetscFunctionReturn(0);
27820cf1dd8SToby Isaac }
27920cf1dd8SToby Isaac 
28020cf1dd8SToby Isaac /*@
281*dce8aebaSBarry Smith   PetscFEDestroy - Destroys a `PetscFE` object
28220cf1dd8SToby Isaac 
283d083f849SBarry Smith   Collective on fem
28420cf1dd8SToby Isaac 
28520cf1dd8SToby Isaac   Input Parameter:
286*dce8aebaSBarry Smith . fem - the `PetscFE` object to destroy
28720cf1dd8SToby Isaac 
2882b99622eSMatthew G. Knepley   Level: beginner
28920cf1dd8SToby Isaac 
290*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEView()`
29120cf1dd8SToby Isaac @*/
292d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEDestroy(PetscFE *fem)
293d71ae5a4SJacob Faibussowitsch {
29420cf1dd8SToby Isaac   PetscFunctionBegin;
29520cf1dd8SToby Isaac   if (!*fem) PetscFunctionReturn(0);
29620cf1dd8SToby Isaac   PetscValidHeaderSpecific((*fem), PETSCFE_CLASSID, 1);
29720cf1dd8SToby Isaac 
2989371c9d4SSatish Balay   if (--((PetscObject)(*fem))->refct > 0) {
2999371c9d4SSatish Balay     *fem = NULL;
3009371c9d4SSatish Balay     PetscFunctionReturn(0);
3019371c9d4SSatish Balay   }
30220cf1dd8SToby Isaac   ((PetscObject)(*fem))->refct = 0;
30320cf1dd8SToby Isaac 
30420cf1dd8SToby Isaac   if ((*fem)->subspaces) {
30520cf1dd8SToby Isaac     PetscInt dim, d;
30620cf1dd8SToby Isaac 
3079566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetDimension((*fem)->dualSpace, &dim));
3089566063dSJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscFEDestroy(&(*fem)->subspaces[d]));
30920cf1dd8SToby Isaac   }
3109566063dSJacob Faibussowitsch   PetscCall(PetscFree((*fem)->subspaces));
3119566063dSJacob Faibussowitsch   PetscCall(PetscFree((*fem)->invV));
3129566063dSJacob Faibussowitsch   PetscCall(PetscTabulationDestroy(&(*fem)->T));
3139566063dSJacob Faibussowitsch   PetscCall(PetscTabulationDestroy(&(*fem)->Tf));
3149566063dSJacob Faibussowitsch   PetscCall(PetscTabulationDestroy(&(*fem)->Tc));
3159566063dSJacob Faibussowitsch   PetscCall(PetscSpaceDestroy(&(*fem)->basisSpace));
3169566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceDestroy(&(*fem)->dualSpace));
3179566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&(*fem)->quadrature));
3189566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&(*fem)->faceQuadrature));
319f918ec44SMatthew G. Knepley #ifdef PETSC_HAVE_LIBCEED
3209566063dSJacob Faibussowitsch   PetscCallCEED(CeedBasisDestroy(&(*fem)->ceedBasis));
3219566063dSJacob Faibussowitsch   PetscCallCEED(CeedDestroy(&(*fem)->ceed));
322f918ec44SMatthew G. Knepley #endif
32320cf1dd8SToby Isaac 
324dbbe0bcdSBarry Smith   PetscTryTypeMethod((*fem), destroy);
3259566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(fem));
32620cf1dd8SToby Isaac   PetscFunctionReturn(0);
32720cf1dd8SToby Isaac }
32820cf1dd8SToby Isaac 
32920cf1dd8SToby Isaac /*@
330*dce8aebaSBarry Smith   PetscFECreate - Creates an empty `PetscFE` object. The type can then be set with `PetscFESetType()`.
33120cf1dd8SToby Isaac 
332d083f849SBarry Smith   Collective
33320cf1dd8SToby Isaac 
33420cf1dd8SToby Isaac   Input Parameter:
335*dce8aebaSBarry Smith . comm - The communicator for the `PetscFE` object
33620cf1dd8SToby Isaac 
33720cf1dd8SToby Isaac   Output Parameter:
338*dce8aebaSBarry Smith . fem - The `PetscFE` object
33920cf1dd8SToby Isaac 
34020cf1dd8SToby Isaac   Level: beginner
34120cf1dd8SToby Isaac 
342*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEType`, `PetscFESetType()`, `PETSCFEGALERKIN`
34320cf1dd8SToby Isaac @*/
344d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreate(MPI_Comm comm, PetscFE *fem)
345d71ae5a4SJacob Faibussowitsch {
34620cf1dd8SToby Isaac   PetscFE f;
34720cf1dd8SToby Isaac 
34820cf1dd8SToby Isaac   PetscFunctionBegin;
34920cf1dd8SToby Isaac   PetscValidPointer(fem, 2);
3509566063dSJacob Faibussowitsch   PetscCall(PetscCitationsRegister(FECitation, &FEcite));
35120cf1dd8SToby Isaac   *fem = NULL;
3529566063dSJacob Faibussowitsch   PetscCall(PetscFEInitializePackage());
35320cf1dd8SToby Isaac 
3549566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(f, PETSCFE_CLASSID, "PetscFE", "Finite Element", "PetscFE", comm, PetscFEDestroy, PetscFEView));
35520cf1dd8SToby Isaac 
35620cf1dd8SToby Isaac   f->basisSpace    = NULL;
35720cf1dd8SToby Isaac   f->dualSpace     = NULL;
35820cf1dd8SToby Isaac   f->numComponents = 1;
35920cf1dd8SToby Isaac   f->subspaces     = NULL;
36020cf1dd8SToby Isaac   f->invV          = NULL;
361ef0bb6c7SMatthew G. Knepley   f->T             = NULL;
362ef0bb6c7SMatthew G. Knepley   f->Tf            = NULL;
363ef0bb6c7SMatthew G. Knepley   f->Tc            = NULL;
3649566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(&f->quadrature, 1));
3659566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(&f->faceQuadrature, 1));
36620cf1dd8SToby Isaac   f->blockSize  = 0;
36720cf1dd8SToby Isaac   f->numBlocks  = 1;
36820cf1dd8SToby Isaac   f->batchSize  = 0;
36920cf1dd8SToby Isaac   f->numBatches = 1;
37020cf1dd8SToby Isaac 
37120cf1dd8SToby Isaac   *fem = f;
37220cf1dd8SToby Isaac   PetscFunctionReturn(0);
37320cf1dd8SToby Isaac }
37420cf1dd8SToby Isaac 
37520cf1dd8SToby Isaac /*@
37620cf1dd8SToby Isaac   PetscFEGetSpatialDimension - Returns the spatial dimension of the element
37720cf1dd8SToby Isaac 
37820cf1dd8SToby Isaac   Not collective
37920cf1dd8SToby Isaac 
38020cf1dd8SToby Isaac   Input Parameter:
381*dce8aebaSBarry Smith . fem - The `PetscFE` object
38220cf1dd8SToby Isaac 
38320cf1dd8SToby Isaac   Output Parameter:
38420cf1dd8SToby Isaac . dim - The spatial dimension
38520cf1dd8SToby Isaac 
38620cf1dd8SToby Isaac   Level: intermediate
38720cf1dd8SToby Isaac 
388*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFECreate()`
38920cf1dd8SToby Isaac @*/
390d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetSpatialDimension(PetscFE fem, PetscInt *dim)
391d71ae5a4SJacob Faibussowitsch {
39220cf1dd8SToby Isaac   DM dm;
39320cf1dd8SToby Isaac 
39420cf1dd8SToby Isaac   PetscFunctionBegin;
39520cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
396dadcf809SJacob Faibussowitsch   PetscValidIntPointer(dim, 2);
3979566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDM(fem->dualSpace, &dm));
3989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, dim));
39920cf1dd8SToby Isaac   PetscFunctionReturn(0);
40020cf1dd8SToby Isaac }
40120cf1dd8SToby Isaac 
40220cf1dd8SToby Isaac /*@
403*dce8aebaSBarry Smith   PetscFESetNumComponents - Sets the number of field components in the element
40420cf1dd8SToby Isaac 
40520cf1dd8SToby Isaac   Not collective
40620cf1dd8SToby Isaac 
40720cf1dd8SToby Isaac   Input Parameters:
408*dce8aebaSBarry Smith + fem - The `PetscFE` object
40920cf1dd8SToby Isaac - comp - The number of field components
41020cf1dd8SToby Isaac 
41120cf1dd8SToby Isaac   Level: intermediate
41220cf1dd8SToby Isaac 
413*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFECreate()`, `PetscFEGetSpatialDimension()`, `PetscFEGetNumComponents()`
41420cf1dd8SToby Isaac @*/
415d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetNumComponents(PetscFE fem, PetscInt comp)
416d71ae5a4SJacob Faibussowitsch {
41720cf1dd8SToby Isaac   PetscFunctionBegin;
41820cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
41920cf1dd8SToby Isaac   fem->numComponents = comp;
42020cf1dd8SToby Isaac   PetscFunctionReturn(0);
42120cf1dd8SToby Isaac }
42220cf1dd8SToby Isaac 
42320cf1dd8SToby Isaac /*@
42420cf1dd8SToby Isaac   PetscFEGetNumComponents - Returns the number of components in the element
42520cf1dd8SToby Isaac 
42620cf1dd8SToby Isaac   Not collective
42720cf1dd8SToby Isaac 
42820cf1dd8SToby Isaac   Input Parameter:
429*dce8aebaSBarry Smith . fem - The `PetscFE` object
43020cf1dd8SToby Isaac 
43120cf1dd8SToby Isaac   Output Parameter:
43220cf1dd8SToby Isaac . comp - The number of field components
43320cf1dd8SToby Isaac 
43420cf1dd8SToby Isaac   Level: intermediate
43520cf1dd8SToby Isaac 
436*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFECreate()`, `PetscFEGetSpatialDimension()`, `PetscFEGetNumComponents()`
43720cf1dd8SToby Isaac @*/
438d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetNumComponents(PetscFE fem, PetscInt *comp)
439d71ae5a4SJacob Faibussowitsch {
44020cf1dd8SToby Isaac   PetscFunctionBegin;
44120cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
442dadcf809SJacob Faibussowitsch   PetscValidIntPointer(comp, 2);
44320cf1dd8SToby Isaac   *comp = fem->numComponents;
44420cf1dd8SToby Isaac   PetscFunctionReturn(0);
44520cf1dd8SToby Isaac }
44620cf1dd8SToby Isaac 
44720cf1dd8SToby Isaac /*@
44820cf1dd8SToby Isaac   PetscFESetTileSizes - Sets the tile sizes for evaluation
44920cf1dd8SToby Isaac 
45020cf1dd8SToby Isaac   Not collective
45120cf1dd8SToby Isaac 
45220cf1dd8SToby Isaac   Input Parameters:
453*dce8aebaSBarry Smith + fem - The `PetscFE` object
45420cf1dd8SToby Isaac . blockSize - The number of elements in a block
45520cf1dd8SToby Isaac . numBlocks - The number of blocks in a batch
45620cf1dd8SToby Isaac . batchSize - The number of elements in a batch
45720cf1dd8SToby Isaac - numBatches - The number of batches in a chunk
45820cf1dd8SToby Isaac 
45920cf1dd8SToby Isaac   Level: intermediate
46020cf1dd8SToby Isaac 
461*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFECreate()`, `PetscFEGetTileSizes()`
46220cf1dd8SToby Isaac @*/
463d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetTileSizes(PetscFE fem, PetscInt blockSize, PetscInt numBlocks, PetscInt batchSize, PetscInt numBatches)
464d71ae5a4SJacob Faibussowitsch {
46520cf1dd8SToby Isaac   PetscFunctionBegin;
46620cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
46720cf1dd8SToby Isaac   fem->blockSize  = blockSize;
46820cf1dd8SToby Isaac   fem->numBlocks  = numBlocks;
46920cf1dd8SToby Isaac   fem->batchSize  = batchSize;
47020cf1dd8SToby Isaac   fem->numBatches = numBatches;
47120cf1dd8SToby Isaac   PetscFunctionReturn(0);
47220cf1dd8SToby Isaac }
47320cf1dd8SToby Isaac 
47420cf1dd8SToby Isaac /*@
47520cf1dd8SToby Isaac   PetscFEGetTileSizes - Returns the tile sizes for evaluation
47620cf1dd8SToby Isaac 
47720cf1dd8SToby Isaac   Not collective
47820cf1dd8SToby Isaac 
47920cf1dd8SToby Isaac   Input Parameter:
480*dce8aebaSBarry Smith . fem - The `PetscFE` object
48120cf1dd8SToby Isaac 
48220cf1dd8SToby Isaac   Output Parameters:
48320cf1dd8SToby Isaac + blockSize - The number of elements in a block
48420cf1dd8SToby Isaac . numBlocks - The number of blocks in a batch
48520cf1dd8SToby Isaac . batchSize - The number of elements in a batch
48620cf1dd8SToby Isaac - numBatches - The number of batches in a chunk
48720cf1dd8SToby Isaac 
48820cf1dd8SToby Isaac   Level: intermediate
48920cf1dd8SToby Isaac 
490*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFECreate()`, `PetscFESetTileSizes()`
49120cf1dd8SToby Isaac @*/
492d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetTileSizes(PetscFE fem, PetscInt *blockSize, PetscInt *numBlocks, PetscInt *batchSize, PetscInt *numBatches)
493d71ae5a4SJacob Faibussowitsch {
49420cf1dd8SToby Isaac   PetscFunctionBegin;
49520cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
496dadcf809SJacob Faibussowitsch   if (blockSize) PetscValidIntPointer(blockSize, 2);
497dadcf809SJacob Faibussowitsch   if (numBlocks) PetscValidIntPointer(numBlocks, 3);
498dadcf809SJacob Faibussowitsch   if (batchSize) PetscValidIntPointer(batchSize, 4);
499dadcf809SJacob Faibussowitsch   if (numBatches) PetscValidIntPointer(numBatches, 5);
50020cf1dd8SToby Isaac   if (blockSize) *blockSize = fem->blockSize;
50120cf1dd8SToby Isaac   if (numBlocks) *numBlocks = fem->numBlocks;
50220cf1dd8SToby Isaac   if (batchSize) *batchSize = fem->batchSize;
50320cf1dd8SToby Isaac   if (numBatches) *numBatches = fem->numBatches;
50420cf1dd8SToby Isaac   PetscFunctionReturn(0);
50520cf1dd8SToby Isaac }
50620cf1dd8SToby Isaac 
50720cf1dd8SToby Isaac /*@
508*dce8aebaSBarry Smith   PetscFEGetBasisSpace - Returns the `PetscSpace` used for the approximation of the solution for the `PetscFE`
50920cf1dd8SToby Isaac 
51020cf1dd8SToby Isaac   Not collective
51120cf1dd8SToby Isaac 
51220cf1dd8SToby Isaac   Input Parameter:
513*dce8aebaSBarry Smith . fem - The `PetscFE` object
51420cf1dd8SToby Isaac 
51520cf1dd8SToby Isaac   Output Parameter:
516*dce8aebaSBarry Smith . sp - The `PetscSpace` object
51720cf1dd8SToby Isaac 
51820cf1dd8SToby Isaac   Level: intermediate
51920cf1dd8SToby Isaac 
520*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscFECreate()`
52120cf1dd8SToby Isaac @*/
522d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetBasisSpace(PetscFE fem, PetscSpace *sp)
523d71ae5a4SJacob Faibussowitsch {
52420cf1dd8SToby Isaac   PetscFunctionBegin;
52520cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
52620cf1dd8SToby Isaac   PetscValidPointer(sp, 2);
52720cf1dd8SToby Isaac   *sp = fem->basisSpace;
52820cf1dd8SToby Isaac   PetscFunctionReturn(0);
52920cf1dd8SToby Isaac }
53020cf1dd8SToby Isaac 
53120cf1dd8SToby Isaac /*@
532*dce8aebaSBarry Smith   PetscFESetBasisSpace - Sets the `PetscSpace` used for the approximation of the solution
53320cf1dd8SToby Isaac 
53420cf1dd8SToby Isaac   Not collective
53520cf1dd8SToby Isaac 
53620cf1dd8SToby Isaac   Input Parameters:
537*dce8aebaSBarry Smith + fem - The `PetscFE` object
538*dce8aebaSBarry Smith - sp - The `PetscSpace` object
53920cf1dd8SToby Isaac 
54020cf1dd8SToby Isaac   Level: intermediate
54120cf1dd8SToby Isaac 
542*dce8aebaSBarry Smith   Developer Note:
543*dce8aebaSBarry Smith   There is `PetscFESetBasisSpace()` but the `PetscFESetDualSpace()`, likely the Basis is unneeded in the function name
544*dce8aebaSBarry Smith 
545*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`, `PetscFESetDualSpace()`
54620cf1dd8SToby Isaac @*/
547d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetBasisSpace(PetscFE fem, PetscSpace sp)
548d71ae5a4SJacob Faibussowitsch {
54920cf1dd8SToby Isaac   PetscFunctionBegin;
55020cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
55120cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCSPACE_CLASSID, 2);
5529566063dSJacob Faibussowitsch   PetscCall(PetscSpaceDestroy(&fem->basisSpace));
55320cf1dd8SToby Isaac   fem->basisSpace = sp;
5549566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fem->basisSpace));
55520cf1dd8SToby Isaac   PetscFunctionReturn(0);
55620cf1dd8SToby Isaac }
55720cf1dd8SToby Isaac 
55820cf1dd8SToby Isaac /*@
559*dce8aebaSBarry Smith   PetscFEGetDualSpace - Returns the `PetscDualSpace` used to define the inner product for a `PetscFE`
56020cf1dd8SToby Isaac 
56120cf1dd8SToby Isaac   Not collective
56220cf1dd8SToby Isaac 
56320cf1dd8SToby Isaac   Input Parameter:
564*dce8aebaSBarry Smith . fem - The `PetscFE` object
56520cf1dd8SToby Isaac 
56620cf1dd8SToby Isaac   Output Parameter:
567*dce8aebaSBarry Smith . sp - The `PetscDualSpace` object
56820cf1dd8SToby Isaac 
56920cf1dd8SToby Isaac   Level: intermediate
57020cf1dd8SToby Isaac 
571*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`
57220cf1dd8SToby Isaac @*/
573d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetDualSpace(PetscFE fem, PetscDualSpace *sp)
574d71ae5a4SJacob Faibussowitsch {
57520cf1dd8SToby Isaac   PetscFunctionBegin;
57620cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
57720cf1dd8SToby Isaac   PetscValidPointer(sp, 2);
57820cf1dd8SToby Isaac   *sp = fem->dualSpace;
57920cf1dd8SToby Isaac   PetscFunctionReturn(0);
58020cf1dd8SToby Isaac }
58120cf1dd8SToby Isaac 
58220cf1dd8SToby Isaac /*@
583*dce8aebaSBarry Smith   PetscFESetDualSpace - Sets the `PetscDualSpace` used to define the inner product
58420cf1dd8SToby Isaac 
58520cf1dd8SToby Isaac   Not collective
58620cf1dd8SToby Isaac 
58720cf1dd8SToby Isaac   Input Parameters:
588*dce8aebaSBarry Smith + fem - The `PetscFE` object
589*dce8aebaSBarry Smith - sp - The `PetscDualSpace` object
59020cf1dd8SToby Isaac 
59120cf1dd8SToby Isaac   Level: intermediate
59220cf1dd8SToby Isaac 
593*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`, `PetscFESetBasisSpace()`
59420cf1dd8SToby Isaac @*/
595d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetDualSpace(PetscFE fem, PetscDualSpace sp)
596d71ae5a4SJacob Faibussowitsch {
59720cf1dd8SToby Isaac   PetscFunctionBegin;
59820cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
59920cf1dd8SToby Isaac   PetscValidHeaderSpecific(sp, PETSCDUALSPACE_CLASSID, 2);
6009566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceDestroy(&fem->dualSpace));
60120cf1dd8SToby Isaac   fem->dualSpace = sp;
6029566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)fem->dualSpace));
60320cf1dd8SToby Isaac   PetscFunctionReturn(0);
60420cf1dd8SToby Isaac }
60520cf1dd8SToby Isaac 
60620cf1dd8SToby Isaac /*@
607*dce8aebaSBarry Smith   PetscFEGetQuadrature - Returns the `PetscQuadrature` used to calculate inner products
60820cf1dd8SToby Isaac 
60920cf1dd8SToby Isaac   Not collective
61020cf1dd8SToby Isaac 
61120cf1dd8SToby Isaac   Input Parameter:
612*dce8aebaSBarry Smith . fem - The `PetscFE` object
61320cf1dd8SToby Isaac 
61420cf1dd8SToby Isaac   Output Parameter:
615*dce8aebaSBarry Smith . q - The `PetscQuadrature` object
61620cf1dd8SToby Isaac 
61720cf1dd8SToby Isaac   Level: intermediate
61820cf1dd8SToby Isaac 
619*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`
62020cf1dd8SToby Isaac @*/
621d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetQuadrature(PetscFE fem, PetscQuadrature *q)
622d71ae5a4SJacob Faibussowitsch {
62320cf1dd8SToby Isaac   PetscFunctionBegin;
62420cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
62520cf1dd8SToby Isaac   PetscValidPointer(q, 2);
62620cf1dd8SToby Isaac   *q = fem->quadrature;
62720cf1dd8SToby Isaac   PetscFunctionReturn(0);
62820cf1dd8SToby Isaac }
62920cf1dd8SToby Isaac 
63020cf1dd8SToby Isaac /*@
631*dce8aebaSBarry Smith   PetscFESetQuadrature - Sets the `PetscQuadrature` used to calculate inner products
63220cf1dd8SToby Isaac 
63320cf1dd8SToby Isaac   Not collective
63420cf1dd8SToby Isaac 
63520cf1dd8SToby Isaac   Input Parameters:
636*dce8aebaSBarry Smith + fem - The `PetscFE` object
637*dce8aebaSBarry Smith - q - The `PetscQuadrature` object
63820cf1dd8SToby Isaac 
63920cf1dd8SToby Isaac   Level: intermediate
64020cf1dd8SToby Isaac 
641*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFEGetFaceQuadrature()`
64220cf1dd8SToby Isaac @*/
643d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetQuadrature(PetscFE fem, PetscQuadrature q)
644d71ae5a4SJacob Faibussowitsch {
64520cf1dd8SToby Isaac   PetscInt Nc, qNc;
64620cf1dd8SToby Isaac 
64720cf1dd8SToby Isaac   PetscFunctionBegin;
64820cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
649fd2fdbddSMatthew G. Knepley   if (q == fem->quadrature) PetscFunctionReturn(0);
6509566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fem, &Nc));
6519566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetNumComponents(q, &qNc));
65263a3b9bcSJacob Faibussowitsch   PetscCheck(!(qNc != 1) || !(Nc != qNc), PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_SIZ, "FE components %" PetscInt_FMT " != Quadrature components %" PetscInt_FMT " and non-scalar quadrature", Nc, qNc);
6539566063dSJacob Faibussowitsch   PetscCall(PetscTabulationDestroy(&fem->T));
6549566063dSJacob Faibussowitsch   PetscCall(PetscTabulationDestroy(&fem->Tc));
6559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)q));
6569566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&fem->quadrature));
65720cf1dd8SToby Isaac   fem->quadrature = q;
65820cf1dd8SToby Isaac   PetscFunctionReturn(0);
65920cf1dd8SToby Isaac }
66020cf1dd8SToby Isaac 
66120cf1dd8SToby Isaac /*@
662*dce8aebaSBarry Smith   PetscFEGetFaceQuadrature - Returns the `PetscQuadrature` used to calculate inner products on faces
66320cf1dd8SToby Isaac 
66420cf1dd8SToby Isaac   Not collective
66520cf1dd8SToby Isaac 
66620cf1dd8SToby Isaac   Input Parameter:
667*dce8aebaSBarry Smith . fem - The `PetscFE` object
66820cf1dd8SToby Isaac 
66920cf1dd8SToby Isaac   Output Parameter:
670*dce8aebaSBarry Smith . q - The `PetscQuadrature` object
67120cf1dd8SToby Isaac 
67220cf1dd8SToby Isaac   Level: intermediate
67320cf1dd8SToby Isaac 
674*dce8aebaSBarry Smith   Developer Note:
675*dce8aebaSBarry Smith   There is a special face quadrature but not edge, likely this API would benifit from a refactorization
676*dce8aebaSBarry Smith 
677*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFESetQuadrature()`, `PetscFESetFaceQuadrature()`
67820cf1dd8SToby Isaac @*/
679d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetFaceQuadrature(PetscFE fem, PetscQuadrature *q)
680d71ae5a4SJacob Faibussowitsch {
68120cf1dd8SToby Isaac   PetscFunctionBegin;
68220cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
68320cf1dd8SToby Isaac   PetscValidPointer(q, 2);
68420cf1dd8SToby Isaac   *q = fem->faceQuadrature;
68520cf1dd8SToby Isaac   PetscFunctionReturn(0);
68620cf1dd8SToby Isaac }
68720cf1dd8SToby Isaac 
68820cf1dd8SToby Isaac /*@
689*dce8aebaSBarry Smith   PetscFESetFaceQuadrature - Sets the `PetscQuadrature` used to calculate inner products on faces
69020cf1dd8SToby Isaac 
69120cf1dd8SToby Isaac   Not collective
69220cf1dd8SToby Isaac 
69320cf1dd8SToby Isaac   Input Parameters:
694*dce8aebaSBarry Smith + fem - The `PetscFE` object
695*dce8aebaSBarry Smith - q - The `PetscQuadrature` object
69620cf1dd8SToby Isaac 
69720cf1dd8SToby Isaac   Level: intermediate
69820cf1dd8SToby Isaac 
699*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFESetQuadrature()`, `PetscFESetFaceQuadrature()`
70020cf1dd8SToby Isaac @*/
701d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetFaceQuadrature(PetscFE fem, PetscQuadrature q)
702d71ae5a4SJacob Faibussowitsch {
703ef0bb6c7SMatthew G. Knepley   PetscInt Nc, qNc;
70420cf1dd8SToby Isaac 
70520cf1dd8SToby Isaac   PetscFunctionBegin;
70620cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
7079566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fem, &Nc));
7089566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetNumComponents(q, &qNc));
70963a3b9bcSJacob Faibussowitsch   PetscCheck(!(qNc != 1) || !(Nc != qNc), PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_SIZ, "FE components %" PetscInt_FMT " != Quadrature components %" PetscInt_FMT " and non-scalar quadrature", Nc, qNc);
7109566063dSJacob Faibussowitsch   PetscCall(PetscTabulationDestroy(&fem->Tf));
7119566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&fem->faceQuadrature));
71220cf1dd8SToby Isaac   fem->faceQuadrature = q;
7139566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)q));
71420cf1dd8SToby Isaac   PetscFunctionReturn(0);
71520cf1dd8SToby Isaac }
71620cf1dd8SToby Isaac 
7175dc5c000SMatthew G. Knepley /*@
718*dce8aebaSBarry Smith   PetscFECopyQuadrature - Copy both volumetric and surface quadrature to a new `PetscFE`
7195dc5c000SMatthew G. Knepley 
7205dc5c000SMatthew G. Knepley   Not collective
7215dc5c000SMatthew G. Knepley 
7225dc5c000SMatthew G. Knepley   Input Parameters:
723*dce8aebaSBarry Smith + sfe - The `PetscFE` source for the quadratures
724*dce8aebaSBarry Smith - tfe - The `PetscFE` target for the quadratures
7255dc5c000SMatthew G. Knepley 
7265dc5c000SMatthew G. Knepley   Level: intermediate
7275dc5c000SMatthew G. Knepley 
728*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`, `PetscFECreate()`, `PetscFESetQuadrature()`, `PetscFESetFaceQuadrature()`
7295dc5c000SMatthew G. Knepley @*/
730d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECopyQuadrature(PetscFE sfe, PetscFE tfe)
731d71ae5a4SJacob Faibussowitsch {
7325dc5c000SMatthew G. Knepley   PetscQuadrature q;
7335dc5c000SMatthew G. Knepley 
7345dc5c000SMatthew G. Knepley   PetscFunctionBegin;
7355dc5c000SMatthew G. Knepley   PetscValidHeaderSpecific(sfe, PETSCFE_CLASSID, 1);
7365dc5c000SMatthew G. Knepley   PetscValidHeaderSpecific(tfe, PETSCFE_CLASSID, 2);
7379566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(sfe, &q));
7389566063dSJacob Faibussowitsch   PetscCall(PetscFESetQuadrature(tfe, q));
7399566063dSJacob Faibussowitsch   PetscCall(PetscFEGetFaceQuadrature(sfe, &q));
7409566063dSJacob Faibussowitsch   PetscCall(PetscFESetFaceQuadrature(tfe, q));
7415dc5c000SMatthew G. Knepley   PetscFunctionReturn(0);
7425dc5c000SMatthew G. Knepley }
7435dc5c000SMatthew G. Knepley 
74420cf1dd8SToby Isaac /*@C
74520cf1dd8SToby Isaac   PetscFEGetNumDof - Returns the number of dofs (dual basis vectors) associated to mesh points on the reference cell of a given dimension
74620cf1dd8SToby Isaac 
74720cf1dd8SToby Isaac   Not collective
74820cf1dd8SToby Isaac 
74920cf1dd8SToby Isaac   Input Parameter:
750*dce8aebaSBarry Smith . fem - The `PetscFE` object
75120cf1dd8SToby Isaac 
75220cf1dd8SToby Isaac   Output Parameter:
75320cf1dd8SToby Isaac . numDof - Array with the number of dofs per dimension
75420cf1dd8SToby Isaac 
75520cf1dd8SToby Isaac   Level: intermediate
75620cf1dd8SToby Isaac 
757*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscFECreate()`
75820cf1dd8SToby Isaac @*/
759d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetNumDof(PetscFE fem, const PetscInt **numDof)
760d71ae5a4SJacob Faibussowitsch {
76120cf1dd8SToby Isaac   PetscFunctionBegin;
76220cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
76320cf1dd8SToby Isaac   PetscValidPointer(numDof, 2);
7649566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetNumDof(fem->dualSpace, numDof));
76520cf1dd8SToby Isaac   PetscFunctionReturn(0);
76620cf1dd8SToby Isaac }
76720cf1dd8SToby Isaac 
76820cf1dd8SToby Isaac /*@C
769ef0bb6c7SMatthew G. Knepley   PetscFEGetCellTabulation - Returns the tabulation of the basis functions at the quadrature points on the reference cell
77020cf1dd8SToby Isaac 
77120cf1dd8SToby Isaac   Not collective
77220cf1dd8SToby Isaac 
773d8d19677SJose E. Roman   Input Parameters:
774*dce8aebaSBarry Smith + fem - The `PetscFE` object
775f9244615SMatthew G. Knepley - k   - The highest derivative we need to tabulate, very often 1
77620cf1dd8SToby Isaac 
777ef0bb6c7SMatthew G. Knepley   Output Parameter:
778ef0bb6c7SMatthew G. Knepley . T - The basis function values and derivatives at quadrature points
77920cf1dd8SToby Isaac 
78020cf1dd8SToby Isaac   Level: intermediate
78120cf1dd8SToby Isaac 
782*dce8aebaSBarry Smith   Note:
783*dce8aebaSBarry Smith .vb
784*dce8aebaSBarry Smith   T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
785*dce8aebaSBarry Smith   T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
786*dce8aebaSBarry Smith   T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
787*dce8aebaSBarry Smith .ve
788*dce8aebaSBarry Smith 
789*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscTabulation`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`
79020cf1dd8SToby Isaac @*/
791d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetCellTabulation(PetscFE fem, PetscInt k, PetscTabulation *T)
792d71ae5a4SJacob Faibussowitsch {
79320cf1dd8SToby Isaac   PetscInt         npoints;
79420cf1dd8SToby Isaac   const PetscReal *points;
79520cf1dd8SToby Isaac 
79620cf1dd8SToby Isaac   PetscFunctionBegin;
79720cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
798064a246eSJacob Faibussowitsch   PetscValidPointer(T, 3);
7999566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(fem->quadrature, NULL, NULL, &npoints, &points, NULL));
8009566063dSJacob Faibussowitsch   if (!fem->T) PetscCall(PetscFECreateTabulation(fem, 1, npoints, points, k, &fem->T));
8011dca8a05SBarry Smith   PetscCheck(!fem->T || k <= fem->T->K, PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_OUTOFRANGE, "Requested %" PetscInt_FMT " derivatives, but only tabulated %" PetscInt_FMT, k, fem->T->K);
802ef0bb6c7SMatthew G. Knepley   *T = fem->T;
80320cf1dd8SToby Isaac   PetscFunctionReturn(0);
80420cf1dd8SToby Isaac }
80520cf1dd8SToby Isaac 
8062b99622eSMatthew G. Knepley /*@C
807ef0bb6c7SMatthew G. Knepley   PetscFEGetFaceTabulation - Returns the tabulation of the basis functions at the face quadrature points for each face of the reference cell
8082b99622eSMatthew G. Knepley 
8092b99622eSMatthew G. Knepley   Not collective
8102b99622eSMatthew G. Knepley 
811d8d19677SJose E. Roman   Input Parameters:
812*dce8aebaSBarry Smith + fem - The `PetscFE` object
813f9244615SMatthew G. Knepley - k   - The highest derivative we need to tabulate, very often 1
8142b99622eSMatthew G. Knepley 
8152b99622eSMatthew G. Knepley   Output Parameters:
816a5b23f4aSJose E. Roman . Tf - The basis function values and derivatives at face quadrature points
8172b99622eSMatthew G. Knepley 
8182b99622eSMatthew G. Knepley   Level: intermediate
8192b99622eSMatthew G. Knepley 
820*dce8aebaSBarry Smith   Note:
821*dce8aebaSBarry Smith .vb
822*dce8aebaSBarry Smith   T->T[0] = Bf[((f*Nq + q)*pdim + i)*Nc + c] is the value at point f,q for basis function i and component c
823*dce8aebaSBarry Smith   T->T[1] = Df[(((f*Nq + q)*pdim + i)*Nc + c)*dim + d] is the derivative value at point f,q for basis function i, component c, in direction d
824*dce8aebaSBarry Smith   T->T[2] = Hf[((((f*Nq + q)*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point f,q for basis function i, component c, in directions d and e
825*dce8aebaSBarry Smith .ve
826*dce8aebaSBarry Smith 
827*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`
8282b99622eSMatthew G. Knepley @*/
829d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetFaceTabulation(PetscFE fem, PetscInt k, PetscTabulation *Tf)
830d71ae5a4SJacob Faibussowitsch {
83120cf1dd8SToby Isaac   PetscFunctionBegin;
83220cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
833064a246eSJacob Faibussowitsch   PetscValidPointer(Tf, 3);
834ef0bb6c7SMatthew G. Knepley   if (!fem->Tf) {
83520cf1dd8SToby Isaac     const PetscReal  xi0[3] = {-1., -1., -1.};
83620cf1dd8SToby Isaac     PetscReal        v0[3], J[9], detJ;
83720cf1dd8SToby Isaac     PetscQuadrature  fq;
83820cf1dd8SToby Isaac     PetscDualSpace   sp;
83920cf1dd8SToby Isaac     DM               dm;
84020cf1dd8SToby Isaac     const PetscInt  *faces;
84120cf1dd8SToby Isaac     PetscInt         dim, numFaces, f, npoints, q;
84220cf1dd8SToby Isaac     const PetscReal *points;
84320cf1dd8SToby Isaac     PetscReal       *facePoints;
84420cf1dd8SToby Isaac 
8459566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fem, &sp));
8469566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetDM(sp, &dm));
8479566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
8489566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, 0, &numFaces));
8499566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, 0, &faces));
8509566063dSJacob Faibussowitsch     PetscCall(PetscFEGetFaceQuadrature(fem, &fq));
85120cf1dd8SToby Isaac     if (fq) {
8529566063dSJacob Faibussowitsch       PetscCall(PetscQuadratureGetData(fq, NULL, NULL, &npoints, &points, NULL));
8539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numFaces * npoints * dim, &facePoints));
85420cf1dd8SToby Isaac       for (f = 0; f < numFaces; ++f) {
8559566063dSJacob Faibussowitsch         PetscCall(DMPlexComputeCellGeometryFEM(dm, faces[f], NULL, v0, J, NULL, &detJ));
85620cf1dd8SToby Isaac         for (q = 0; q < npoints; ++q) CoordinatesRefToReal(dim, dim - 1, xi0, v0, J, &points[q * (dim - 1)], &facePoints[(f * npoints + q) * dim]);
85720cf1dd8SToby Isaac       }
8589566063dSJacob Faibussowitsch       PetscCall(PetscFECreateTabulation(fem, numFaces, npoints, facePoints, k, &fem->Tf));
8599566063dSJacob Faibussowitsch       PetscCall(PetscFree(facePoints));
86020cf1dd8SToby Isaac     }
86120cf1dd8SToby Isaac   }
8621dca8a05SBarry Smith   PetscCheck(!fem->Tf || k <= fem->Tf->K, PetscObjectComm((PetscObject)fem), PETSC_ERR_ARG_OUTOFRANGE, "Requested %" PetscInt_FMT " derivatives, but only tabulated %" PetscInt_FMT, k, fem->Tf->K);
863ef0bb6c7SMatthew G. Knepley   *Tf = fem->Tf;
86420cf1dd8SToby Isaac   PetscFunctionReturn(0);
86520cf1dd8SToby Isaac }
86620cf1dd8SToby Isaac 
8672b99622eSMatthew G. Knepley /*@C
868ef0bb6c7SMatthew G. Knepley   PetscFEGetFaceCentroidTabulation - Returns the tabulation of the basis functions at the face centroid points
8692b99622eSMatthew G. Knepley 
8702b99622eSMatthew G. Knepley   Not collective
8712b99622eSMatthew G. Knepley 
8722b99622eSMatthew G. Knepley   Input Parameter:
873*dce8aebaSBarry Smith . fem - The `PetscFE` object
8742b99622eSMatthew G. Knepley 
8752b99622eSMatthew G. Knepley   Output Parameters:
876ef0bb6c7SMatthew G. Knepley . Tc - The basis function values at face centroid points
8772b99622eSMatthew G. Knepley 
8782b99622eSMatthew G. Knepley   Level: intermediate
8792b99622eSMatthew G. Knepley 
880*dce8aebaSBarry Smith   Note:
881*dce8aebaSBarry Smith .vb
882*dce8aebaSBarry Smith   T->T[0] = Bf[(f*pdim + i)*Nc + c] is the value at point f for basis function i and component c
883*dce8aebaSBarry Smith .ve
884*dce8aebaSBarry Smith 
885*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscTabulation`, `PetscFEGetFaceTabulation()`, `PetscFEGetCellTabulation()`, `PetscFECreateTabulation()`, `PetscTabulationDestroy()`
8862b99622eSMatthew G. Knepley @*/
887d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetFaceCentroidTabulation(PetscFE fem, PetscTabulation *Tc)
888d71ae5a4SJacob Faibussowitsch {
88920cf1dd8SToby Isaac   PetscFunctionBegin;
89020cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
891ef0bb6c7SMatthew G. Knepley   PetscValidPointer(Tc, 2);
892ef0bb6c7SMatthew G. Knepley   if (!fem->Tc) {
89320cf1dd8SToby Isaac     PetscDualSpace  sp;
89420cf1dd8SToby Isaac     DM              dm;
89520cf1dd8SToby Isaac     const PetscInt *cone;
89620cf1dd8SToby Isaac     PetscReal      *centroids;
89720cf1dd8SToby Isaac     PetscInt        dim, numFaces, f;
89820cf1dd8SToby Isaac 
8999566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fem, &sp));
9009566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetDM(sp, &dm));
9019566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
9029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, 0, &numFaces));
9039566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, 0, &cone));
9049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numFaces * dim, &centroids));
9059566063dSJacob Faibussowitsch     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexComputeCellGeometryFVM(dm, cone[f], NULL, &centroids[f * dim], NULL));
9069566063dSJacob Faibussowitsch     PetscCall(PetscFECreateTabulation(fem, 1, numFaces, centroids, 0, &fem->Tc));
9079566063dSJacob Faibussowitsch     PetscCall(PetscFree(centroids));
90820cf1dd8SToby Isaac   }
909ef0bb6c7SMatthew G. Knepley   *Tc = fem->Tc;
91020cf1dd8SToby Isaac   PetscFunctionReturn(0);
91120cf1dd8SToby Isaac }
91220cf1dd8SToby Isaac 
91320cf1dd8SToby Isaac /*@C
914ef0bb6c7SMatthew G. Knepley   PetscFECreateTabulation - Tabulates the basis functions, and perhaps derivatives, at the points provided.
91520cf1dd8SToby Isaac 
91620cf1dd8SToby Isaac   Not collective
91720cf1dd8SToby Isaac 
91820cf1dd8SToby Isaac   Input Parameters:
919*dce8aebaSBarry Smith + fem     - The `PetscFE` object
920ef0bb6c7SMatthew G. Knepley . nrepl   - The number of replicas
921ef0bb6c7SMatthew G. Knepley . npoints - The number of tabulation points in a replica
922ef0bb6c7SMatthew G. Knepley . points  - The tabulation point coordinates
923ef0bb6c7SMatthew G. Knepley - K       - The number of derivatives calculated
92420cf1dd8SToby Isaac 
925ef0bb6c7SMatthew G. Knepley   Output Parameter:
926ef0bb6c7SMatthew G. Knepley . T - The basis function values and derivatives at tabulation points
92720cf1dd8SToby Isaac 
92820cf1dd8SToby Isaac   Level: intermediate
92920cf1dd8SToby Isaac 
930*dce8aebaSBarry Smith   Note:
931*dce8aebaSBarry Smith .vb
932*dce8aebaSBarry Smith   T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
933*dce8aebaSBarry Smith   T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
934*dce8aebaSBarry Smith   T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
935*dce8aebaSBarry Smith 
936*dce8aebaSBarry Smith .seealso: `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscTabulationDestroy()`
93720cf1dd8SToby Isaac @*/
938d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateTabulation(PetscFE fem, PetscInt nrepl, PetscInt npoints, const PetscReal points[], PetscInt K, PetscTabulation *T)
939d71ae5a4SJacob Faibussowitsch {
94020cf1dd8SToby Isaac   DM             dm;
941ef0bb6c7SMatthew G. Knepley   PetscDualSpace Q;
942ef0bb6c7SMatthew G. Knepley   PetscInt       Nb;   /* Dimension of FE space P */
943ef0bb6c7SMatthew G. Knepley   PetscInt       Nc;   /* Field components */
944ef0bb6c7SMatthew G. Knepley   PetscInt       cdim; /* Reference coordinate dimension */
945ef0bb6c7SMatthew G. Knepley   PetscInt       k;
94620cf1dd8SToby Isaac 
94720cf1dd8SToby Isaac   PetscFunctionBegin;
948ef0bb6c7SMatthew G. Knepley   if (!npoints || !fem->dualSpace || K < 0) {
949ef0bb6c7SMatthew G. Knepley     *T = NULL;
95020cf1dd8SToby Isaac     PetscFunctionReturn(0);
95120cf1dd8SToby Isaac   }
95220cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
953dadcf809SJacob Faibussowitsch   PetscValidRealPointer(points, 4);
95440a2aa30SMatthew G. Knepley   PetscValidPointer(T, 6);
9559566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fem, &Q));
9569566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDM(Q, &dm));
9579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cdim));
9589566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDimension(Q, &Nb));
9599566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fem, &Nc));
9609566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(1, T));
961ef0bb6c7SMatthew G. Knepley   (*T)->K    = !cdim ? 0 : K;
962ef0bb6c7SMatthew G. Knepley   (*T)->Nr   = nrepl;
963ef0bb6c7SMatthew G. Knepley   (*T)->Np   = npoints;
964ef0bb6c7SMatthew G. Knepley   (*T)->Nb   = Nb;
965ef0bb6c7SMatthew G. Knepley   (*T)->Nc   = Nc;
966ef0bb6c7SMatthew G. Knepley   (*T)->cdim = cdim;
9679566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((*T)->K + 1, &(*T)->T));
96848a46eb9SPierre Jolivet   for (k = 0; k <= (*T)->K; ++k) PetscCall(PetscMalloc1(nrepl * npoints * Nb * Nc * PetscPowInt(cdim, k), &(*T)->T[k]));
969dbbe0bcdSBarry Smith   PetscUseTypeMethod(fem, createtabulation, nrepl * npoints, points, K, *T);
97020cf1dd8SToby Isaac   PetscFunctionReturn(0);
97120cf1dd8SToby Isaac }
97220cf1dd8SToby Isaac 
9732b99622eSMatthew G. Knepley /*@C
974ef0bb6c7SMatthew G. Knepley   PetscFEComputeTabulation - Tabulates the basis functions, and perhaps derivatives, at the points provided.
9752b99622eSMatthew G. Knepley 
9762b99622eSMatthew G. Knepley   Not collective
9772b99622eSMatthew G. Knepley 
9782b99622eSMatthew G. Knepley   Input Parameters:
979*dce8aebaSBarry Smith + fem     - The `PetscFE` object
9802b99622eSMatthew G. Knepley . npoints - The number of tabulation points
9812b99622eSMatthew G. Knepley . points  - The tabulation point coordinates
982ef0bb6c7SMatthew G. Knepley . K       - The number of derivatives calculated
983ef0bb6c7SMatthew G. Knepley - T       - An existing tabulation object with enough allocated space
984ef0bb6c7SMatthew G. Knepley 
985ef0bb6c7SMatthew G. Knepley   Output Parameter:
986ef0bb6c7SMatthew G. Knepley . T - The basis function values and derivatives at tabulation points
9872b99622eSMatthew G. Knepley 
9882b99622eSMatthew G. Knepley   Level: intermediate
9892b99622eSMatthew G. Knepley 
990*dce8aebaSBarry Smith   Note:
991*dce8aebaSBarry Smith .vb
992*dce8aebaSBarry Smith   T->T[0] = B[(p*pdim + i)*Nc + c] is the value at point p for basis function i and component c
993*dce8aebaSBarry Smith   T->T[1] = D[((p*pdim + i)*Nc + c)*dim + d] is the derivative value at point p for basis function i, component c, in direction d
994*dce8aebaSBarry Smith   T->T[2] = H[(((p*pdim + i)*Nc + c)*dim + d)*dim + e] is the value at point p for basis function i, component c, in directions d and e
995*dce8aebaSBarry Smith .ve
996*dce8aebaSBarry Smith 
997*dce8aebaSBarry Smith .seealso: `PetscTabulation`, `PetscFEGetCellTabulation()`, `PetscTabulationDestroy()`
9982b99622eSMatthew G. Knepley @*/
999d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEComputeTabulation(PetscFE fem, PetscInt npoints, const PetscReal points[], PetscInt K, PetscTabulation T)
1000d71ae5a4SJacob Faibussowitsch {
1001ef0bb6c7SMatthew G. Knepley   PetscFunctionBeginHot;
1002ef0bb6c7SMatthew G. Knepley   if (!npoints || !fem->dualSpace || K < 0) PetscFunctionReturn(0);
1003ef0bb6c7SMatthew G. Knepley   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
1004dadcf809SJacob Faibussowitsch   PetscValidRealPointer(points, 3);
1005ef0bb6c7SMatthew G. Knepley   PetscValidPointer(T, 5);
100676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
100720cf1dd8SToby Isaac     DM             dm;
1008ef0bb6c7SMatthew G. Knepley     PetscDualSpace Q;
1009ef0bb6c7SMatthew G. Knepley     PetscInt       Nb;   /* Dimension of FE space P */
1010ef0bb6c7SMatthew G. Knepley     PetscInt       Nc;   /* Field components */
1011ef0bb6c7SMatthew G. Knepley     PetscInt       cdim; /* Reference coordinate dimension */
1012ef0bb6c7SMatthew G. Knepley 
10139566063dSJacob Faibussowitsch     PetscCall(PetscFEGetDualSpace(fem, &Q));
10149566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetDM(Q, &dm));
10159566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &cdim));
10169566063dSJacob Faibussowitsch     PetscCall(PetscDualSpaceGetDimension(Q, &Nb));
10179566063dSJacob Faibussowitsch     PetscCall(PetscFEGetNumComponents(fem, &Nc));
101863a3b9bcSJacob Faibussowitsch     PetscCheck(T->K == (!cdim ? 0 : K), PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation K %" PetscInt_FMT " must match requested K %" PetscInt_FMT, T->K, !cdim ? 0 : K);
101963a3b9bcSJacob Faibussowitsch     PetscCheck(T->Nb == Nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation Nb %" PetscInt_FMT " must match requested Nb %" PetscInt_FMT, T->Nb, Nb);
102063a3b9bcSJacob Faibussowitsch     PetscCheck(T->Nc == Nc, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation Nc %" PetscInt_FMT " must match requested Nc %" PetscInt_FMT, T->Nc, Nc);
102163a3b9bcSJacob Faibussowitsch     PetscCheck(T->cdim == cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Tabulation cdim %" PetscInt_FMT " must match requested cdim %" PetscInt_FMT, T->cdim, cdim);
1022ef0bb6c7SMatthew G. Knepley   }
1023ef0bb6c7SMatthew G. Knepley   T->Nr = 1;
1024ef0bb6c7SMatthew G. Knepley   T->Np = npoints;
1025dbbe0bcdSBarry Smith   PetscUseTypeMethod(fem, createtabulation, npoints, points, K, T);
1026ef0bb6c7SMatthew G. Knepley   PetscFunctionReturn(0);
1027ef0bb6c7SMatthew G. Knepley }
1028ef0bb6c7SMatthew G. Knepley 
1029ef0bb6c7SMatthew G. Knepley /*@C
1030ef0bb6c7SMatthew G. Knepley   PetscTabulationDestroy - Frees memory from the associated tabulation.
1031ef0bb6c7SMatthew G. Knepley 
1032ef0bb6c7SMatthew G. Knepley   Not collective
1033ef0bb6c7SMatthew G. Knepley 
1034ef0bb6c7SMatthew G. Knepley   Input Parameter:
1035ef0bb6c7SMatthew G. Knepley . T - The tabulation
1036ef0bb6c7SMatthew G. Knepley 
1037ef0bb6c7SMatthew G. Knepley   Level: intermediate
1038ef0bb6c7SMatthew G. Knepley 
1039*dce8aebaSBarry Smith .seealso: `PetscTabulation`, `PetscFECreateTabulation()`, `PetscFEGetCellTabulation()`
1040ef0bb6c7SMatthew G. Knepley @*/
1041d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscTabulationDestroy(PetscTabulation *T)
1042d71ae5a4SJacob Faibussowitsch {
1043ef0bb6c7SMatthew G. Knepley   PetscInt k;
104420cf1dd8SToby Isaac 
104520cf1dd8SToby Isaac   PetscFunctionBegin;
1046ef0bb6c7SMatthew G. Knepley   PetscValidPointer(T, 1);
1047ef0bb6c7SMatthew G. Knepley   if (!T || !(*T)) PetscFunctionReturn(0);
10489566063dSJacob Faibussowitsch   for (k = 0; k <= (*T)->K; ++k) PetscCall(PetscFree((*T)->T[k]));
10499566063dSJacob Faibussowitsch   PetscCall(PetscFree((*T)->T));
10509566063dSJacob Faibussowitsch   PetscCall(PetscFree(*T));
1051ef0bb6c7SMatthew G. Knepley   *T = NULL;
105220cf1dd8SToby Isaac   PetscFunctionReturn(0);
105320cf1dd8SToby Isaac }
105420cf1dd8SToby Isaac 
1055d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFECreatePointTrace(PetscFE fe, PetscInt refPoint, PetscFE *trFE)
1056d71ae5a4SJacob Faibussowitsch {
105720cf1dd8SToby Isaac   PetscSpace      bsp, bsubsp;
105820cf1dd8SToby Isaac   PetscDualSpace  dsp, dsubsp;
105920cf1dd8SToby Isaac   PetscInt        dim, depth, numComp, i, j, coneSize, order;
106020cf1dd8SToby Isaac   PetscFEType     type;
106120cf1dd8SToby Isaac   DM              dm;
106220cf1dd8SToby Isaac   DMLabel         label;
106320cf1dd8SToby Isaac   PetscReal      *xi, *v, *J, detJ;
1064db11e2ebSMatthew G. Knepley   const char     *name;
106520cf1dd8SToby Isaac   PetscQuadrature origin, fullQuad, subQuad;
106620cf1dd8SToby Isaac 
106720cf1dd8SToby Isaac   PetscFunctionBegin;
106820cf1dd8SToby Isaac   PetscValidHeaderSpecific(fe, PETSCFE_CLASSID, 1);
106920cf1dd8SToby Isaac   PetscValidPointer(trFE, 3);
10709566063dSJacob Faibussowitsch   PetscCall(PetscFEGetBasisSpace(fe, &bsp));
10719566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fe, &dsp));
10729566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDM(dsp, &dm));
10739566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &label));
10759566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValue(label, refPoint, &depth));
10769566063dSJacob Faibussowitsch   PetscCall(PetscCalloc1(depth, &xi));
10779566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dim, &v));
10789566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dim * dim, &J));
107920cf1dd8SToby Isaac   for (i = 0; i < depth; i++) xi[i] = 0.;
10809566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &origin));
10819566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(origin, depth, 0, 1, xi, NULL));
10829566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, refPoint, origin, v, J, NULL, &detJ));
108320cf1dd8SToby Isaac   /* CellGeometryFEM computes the expanded Jacobian, we want the true jacobian */
108420cf1dd8SToby Isaac   for (i = 1; i < dim; i++) {
1085ad540459SPierre Jolivet     for (j = 0; j < depth; j++) J[i * depth + j] = J[i * dim + j];
108620cf1dd8SToby Isaac   }
10879566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&origin));
10889566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetPointSubspace(dsp, refPoint, &dsubsp));
10899566063dSJacob Faibussowitsch   PetscCall(PetscSpaceCreateSubspace(bsp, dsubsp, v, J, NULL, NULL, PETSC_OWN_POINTER, &bsubsp));
10909566063dSJacob Faibussowitsch   PetscCall(PetscSpaceSetUp(bsubsp));
10919566063dSJacob Faibussowitsch   PetscCall(PetscFECreate(PetscObjectComm((PetscObject)fe), trFE));
10929566063dSJacob Faibussowitsch   PetscCall(PetscFEGetType(fe, &type));
10939566063dSJacob Faibussowitsch   PetscCall(PetscFESetType(*trFE, type));
10949566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
10959566063dSJacob Faibussowitsch   PetscCall(PetscFESetNumComponents(*trFE, numComp));
10969566063dSJacob Faibussowitsch   PetscCall(PetscFESetBasisSpace(*trFE, bsubsp));
10979566063dSJacob Faibussowitsch   PetscCall(PetscFESetDualSpace(*trFE, dsubsp));
10989566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetName((PetscObject)fe, &name));
10999566063dSJacob Faibussowitsch   if (name) PetscCall(PetscFESetName(*trFE, name));
11009566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &fullQuad));
11019566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetOrder(fullQuad, &order));
11029566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, refPoint, &coneSize));
11031baa6e33SBarry Smith   if (coneSize == 2 * depth) PetscCall(PetscDTGaussTensorQuadrature(depth, 1, (order + 1) / 2, -1., 1., &subQuad));
11041baa6e33SBarry Smith   else PetscCall(PetscDTStroudConicalQuadrature(depth, 1, (order + 1) / 2, -1., 1., &subQuad));
11059566063dSJacob Faibussowitsch   PetscCall(PetscFESetQuadrature(*trFE, subQuad));
11069566063dSJacob Faibussowitsch   PetscCall(PetscFESetUp(*trFE));
11079566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&subQuad));
11089566063dSJacob Faibussowitsch   PetscCall(PetscSpaceDestroy(&bsubsp));
110920cf1dd8SToby Isaac   PetscFunctionReturn(0);
111020cf1dd8SToby Isaac }
111120cf1dd8SToby Isaac 
1112d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateHeightTrace(PetscFE fe, PetscInt height, PetscFE *trFE)
1113d71ae5a4SJacob Faibussowitsch {
111420cf1dd8SToby Isaac   PetscInt       hStart, hEnd;
111520cf1dd8SToby Isaac   PetscDualSpace dsp;
111620cf1dd8SToby Isaac   DM             dm;
111720cf1dd8SToby Isaac 
111820cf1dd8SToby Isaac   PetscFunctionBegin;
111920cf1dd8SToby Isaac   PetscValidHeaderSpecific(fe, PETSCFE_CLASSID, 1);
112020cf1dd8SToby Isaac   PetscValidPointer(trFE, 3);
112120cf1dd8SToby Isaac   *trFE = NULL;
11229566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fe, &dsp));
11239566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDM(dsp, &dm));
11249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, height, &hStart, &hEnd));
112520cf1dd8SToby Isaac   if (hEnd <= hStart) PetscFunctionReturn(0);
11269566063dSJacob Faibussowitsch   PetscCall(PetscFECreatePointTrace(fe, hStart, trFE));
112720cf1dd8SToby Isaac   PetscFunctionReturn(0);
112820cf1dd8SToby Isaac }
112920cf1dd8SToby Isaac 
113020cf1dd8SToby Isaac /*@
113120cf1dd8SToby Isaac   PetscFEGetDimension - Get the dimension of the finite element space on a cell
113220cf1dd8SToby Isaac 
113320cf1dd8SToby Isaac   Not collective
113420cf1dd8SToby Isaac 
113520cf1dd8SToby Isaac   Input Parameter:
1136*dce8aebaSBarry Smith . fe - The `PetscFE`
113720cf1dd8SToby Isaac 
113820cf1dd8SToby Isaac   Output Parameter:
113920cf1dd8SToby Isaac . dim - The dimension
114020cf1dd8SToby Isaac 
114120cf1dd8SToby Isaac   Level: intermediate
114220cf1dd8SToby Isaac 
1143*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFECreate()`, `PetscSpaceGetDimension()`, `PetscDualSpaceGetDimension()`
114420cf1dd8SToby Isaac @*/
1145d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetDimension(PetscFE fem, PetscInt *dim)
1146d71ae5a4SJacob Faibussowitsch {
114720cf1dd8SToby Isaac   PetscFunctionBegin;
114820cf1dd8SToby Isaac   PetscValidHeaderSpecific(fem, PETSCFE_CLASSID, 1);
1149dadcf809SJacob Faibussowitsch   PetscValidIntPointer(dim, 2);
1150dbbe0bcdSBarry Smith   PetscTryTypeMethod(fem, getdimension, dim);
115120cf1dd8SToby Isaac   PetscFunctionReturn(0);
115220cf1dd8SToby Isaac }
115320cf1dd8SToby Isaac 
11544bee2e38SMatthew G. Knepley /*@C
11554bee2e38SMatthew G. Knepley   PetscFEPushforward - Map the reference element function to real space
11564bee2e38SMatthew G. Knepley 
11574bee2e38SMatthew G. Knepley   Input Parameters:
1158*dce8aebaSBarry Smith + fe     - The `PetscFE`
11594bee2e38SMatthew G. Knepley . fegeom - The cell geometry
11604bee2e38SMatthew G. Knepley . Nv     - The number of function values
11614bee2e38SMatthew G. Knepley - vals   - The function values
11624bee2e38SMatthew G. Knepley 
11634bee2e38SMatthew G. Knepley   Output Parameter:
11644bee2e38SMatthew G. Knepley . vals   - The transformed function values
11654bee2e38SMatthew G. Knepley 
11664bee2e38SMatthew G. Knepley   Level: advanced
11674bee2e38SMatthew G. Knepley 
1168*dce8aebaSBarry Smith   Notes:
1169*dce8aebaSBarry Smith   This just forwards the call onto `PetscDualSpacePushforward()`.
11704bee2e38SMatthew G. Knepley 
1171*dce8aebaSBarry Smith   It only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
11722edcad52SToby Isaac 
1173*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEGeom`, `PetscDualSpace`, `PetscDualSpacePushforward()`
11744bee2e38SMatthew G. Knepley @*/
1175d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEPushforward(PetscFE fe, PetscFEGeom *fegeom, PetscInt Nv, PetscScalar vals[])
1176d71ae5a4SJacob Faibussowitsch {
11772ae266adSMatthew G. Knepley   PetscFunctionBeginHot;
11789566063dSJacob Faibussowitsch   PetscCall(PetscDualSpacePushforward(fe->dualSpace, fegeom, Nv, fe->numComponents, vals));
11794bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
11804bee2e38SMatthew G. Knepley }
11814bee2e38SMatthew G. Knepley 
11824bee2e38SMatthew G. Knepley /*@C
11834bee2e38SMatthew G. Knepley   PetscFEPushforwardGradient - Map the reference element function gradient to real space
11844bee2e38SMatthew G. Knepley 
11854bee2e38SMatthew G. Knepley   Input Parameters:
1186*dce8aebaSBarry Smith + fe     - The `PetscFE`
11874bee2e38SMatthew G. Knepley . fegeom - The cell geometry
11884bee2e38SMatthew G. Knepley . Nv     - The number of function gradient values
11894bee2e38SMatthew G. Knepley - vals   - The function gradient values
11904bee2e38SMatthew G. Knepley 
11914bee2e38SMatthew G. Knepley   Output Parameter:
11924bee2e38SMatthew G. Knepley . vals   - The transformed function gradient values
11934bee2e38SMatthew G. Knepley 
11944bee2e38SMatthew G. Knepley   Level: advanced
11954bee2e38SMatthew G. Knepley 
1196*dce8aebaSBarry Smith   Notes:
1197*dce8aebaSBarry Smith   This just forwards the call onto `PetscDualSpacePushforwardGradient()`.
11984bee2e38SMatthew G. Knepley 
1199*dce8aebaSBarry Smith   It only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
12002edcad52SToby Isaac 
1201*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEGeom`, `PetscDualSpace`, `PetscFEPushforward()`, `PetscDualSpacePushforwardGradient()`, `PetscDualSpacePushforward()`
12024bee2e38SMatthew G. Knepley @*/
1203d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEPushforwardGradient(PetscFE fe, PetscFEGeom *fegeom, PetscInt Nv, PetscScalar vals[])
1204d71ae5a4SJacob Faibussowitsch {
12052ae266adSMatthew G. Knepley   PetscFunctionBeginHot;
12069566063dSJacob Faibussowitsch   PetscCall(PetscDualSpacePushforwardGradient(fe->dualSpace, fegeom, Nv, fe->numComponents, vals));
12074bee2e38SMatthew G. Knepley   PetscFunctionReturn(0);
12084bee2e38SMatthew G. Knepley }
12094bee2e38SMatthew G. Knepley 
1210f9244615SMatthew G. Knepley /*@C
1211f9244615SMatthew G. Knepley   PetscFEPushforwardHessian - Map the reference element function Hessian to real space
1212f9244615SMatthew G. Knepley 
1213f9244615SMatthew G. Knepley   Input Parameters:
1214*dce8aebaSBarry Smith + fe     - The `PetscFE`
1215f9244615SMatthew G. Knepley . fegeom - The cell geometry
1216f9244615SMatthew G. Knepley . Nv     - The number of function Hessian values
1217f9244615SMatthew G. Knepley - vals   - The function Hessian values
1218f9244615SMatthew G. Knepley 
1219f9244615SMatthew G. Knepley   Output Parameter:
1220f9244615SMatthew G. Knepley . vals   - The transformed function Hessian values
1221f9244615SMatthew G. Knepley 
1222f9244615SMatthew G. Knepley   Level: advanced
1223f9244615SMatthew G. Knepley 
1224*dce8aebaSBarry Smith   Notes:
1225*dce8aebaSBarry Smith   This just forwards the call onto `PetscDualSpacePushforwardHessian()`.
1226f9244615SMatthew G. Knepley 
1227*dce8aebaSBarry Smith   It only handles transformations when the embedding dimension of the geometry in fegeom is the same as the reference dimension.
1228f9244615SMatthew G. Knepley 
1229*dce8aebaSBarry Smith   Developer Note:
1230*dce8aebaSBarry Smith   It is unclear why all these one line convenience routines are desirable
1231*dce8aebaSBarry Smith 
1232*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscFEGeom`, `PetscDualSpace`, `PetscFEPushforward()`, `PetscDualSpacePushforwardHessian()`, `PetscDualSpacePushforward()`
1233f9244615SMatthew G. Knepley @*/
1234d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEPushforwardHessian(PetscFE fe, PetscFEGeom *fegeom, PetscInt Nv, PetscScalar vals[])
1235d71ae5a4SJacob Faibussowitsch {
1236f9244615SMatthew G. Knepley   PetscFunctionBeginHot;
12379566063dSJacob Faibussowitsch   PetscCall(PetscDualSpacePushforwardHessian(fe->dualSpace, fegeom, Nv, fe->numComponents, vals));
1238f9244615SMatthew G. Knepley   PetscFunctionReturn(0);
1239f9244615SMatthew G. Knepley }
1240f9244615SMatthew G. Knepley 
124120cf1dd8SToby Isaac /*
124220cf1dd8SToby Isaac Purpose: Compute element vector for chunk of elements
124320cf1dd8SToby Isaac 
124420cf1dd8SToby Isaac Input:
124520cf1dd8SToby Isaac   Sizes:
124620cf1dd8SToby Isaac      Ne:  number of elements
124720cf1dd8SToby Isaac      Nf:  number of fields
124820cf1dd8SToby Isaac      PetscFE
124920cf1dd8SToby Isaac        dim: spatial dimension
125020cf1dd8SToby Isaac        Nb:  number of basis functions
125120cf1dd8SToby Isaac        Nc:  number of field components
125220cf1dd8SToby Isaac        PetscQuadrature
125320cf1dd8SToby Isaac          Nq:  number of quadrature points
125420cf1dd8SToby Isaac 
125520cf1dd8SToby Isaac   Geometry:
125620cf1dd8SToby Isaac      PetscFEGeom[Ne] possibly *Nq
125720cf1dd8SToby Isaac        PetscReal v0s[dim]
125820cf1dd8SToby Isaac        PetscReal n[dim]
125920cf1dd8SToby Isaac        PetscReal jacobians[dim*dim]
126020cf1dd8SToby Isaac        PetscReal jacobianInverses[dim*dim]
126120cf1dd8SToby Isaac        PetscReal jacobianDeterminants
126220cf1dd8SToby Isaac   FEM:
126320cf1dd8SToby Isaac      PetscFE
126420cf1dd8SToby Isaac        PetscQuadrature
126520cf1dd8SToby Isaac          PetscReal   quadPoints[Nq*dim]
126620cf1dd8SToby Isaac          PetscReal   quadWeights[Nq]
126720cf1dd8SToby Isaac        PetscReal   basis[Nq*Nb*Nc]
126820cf1dd8SToby Isaac        PetscReal   basisDer[Nq*Nb*Nc*dim]
126920cf1dd8SToby Isaac      PetscScalar coefficients[Ne*Nb*Nc]
127020cf1dd8SToby Isaac      PetscScalar elemVec[Ne*Nb*Nc]
127120cf1dd8SToby Isaac 
127220cf1dd8SToby Isaac   Problem:
127320cf1dd8SToby Isaac      PetscInt f: the active field
127420cf1dd8SToby Isaac      f0, f1
127520cf1dd8SToby Isaac 
127620cf1dd8SToby Isaac   Work Space:
127720cf1dd8SToby Isaac      PetscFE
127820cf1dd8SToby Isaac        PetscScalar f0[Nq*dim];
127920cf1dd8SToby Isaac        PetscScalar f1[Nq*dim*dim];
128020cf1dd8SToby Isaac        PetscScalar u[Nc];
128120cf1dd8SToby Isaac        PetscScalar gradU[Nc*dim];
128220cf1dd8SToby Isaac        PetscReal   x[dim];
128320cf1dd8SToby Isaac        PetscScalar realSpaceDer[dim];
128420cf1dd8SToby Isaac 
128520cf1dd8SToby Isaac Purpose: Compute element vector for N_cb batches of elements
128620cf1dd8SToby Isaac 
128720cf1dd8SToby Isaac Input:
128820cf1dd8SToby Isaac   Sizes:
128920cf1dd8SToby Isaac      N_cb: Number of serial cell batches
129020cf1dd8SToby Isaac 
129120cf1dd8SToby Isaac   Geometry:
129220cf1dd8SToby Isaac      PetscReal v0s[Ne*dim]
129320cf1dd8SToby Isaac      PetscReal jacobians[Ne*dim*dim]        possibly *Nq
129420cf1dd8SToby Isaac      PetscReal jacobianInverses[Ne*dim*dim] possibly *Nq
129520cf1dd8SToby Isaac      PetscReal jacobianDeterminants[Ne]     possibly *Nq
129620cf1dd8SToby Isaac   FEM:
129720cf1dd8SToby Isaac      static PetscReal   quadPoints[Nq*dim]
129820cf1dd8SToby Isaac      static PetscReal   quadWeights[Nq]
129920cf1dd8SToby Isaac      static PetscReal   basis[Nq*Nb*Nc]
130020cf1dd8SToby Isaac      static PetscReal   basisDer[Nq*Nb*Nc*dim]
130120cf1dd8SToby Isaac      PetscScalar coefficients[Ne*Nb*Nc]
130220cf1dd8SToby Isaac      PetscScalar elemVec[Ne*Nb*Nc]
130320cf1dd8SToby Isaac 
130420cf1dd8SToby Isaac ex62.c:
130520cf1dd8SToby Isaac   PetscErrorCode PetscFEIntegrateResidualBatch(PetscInt Ne, PetscInt numFields, PetscInt field, PetscQuadrature quad[], const PetscScalar coefficients[],
130620cf1dd8SToby Isaac                                                const PetscReal v0s[], const PetscReal jacobians[], const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[],
130720cf1dd8SToby Isaac                                                void (*f0_func)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f0[]),
130820cf1dd8SToby Isaac                                                void (*f1_func)(const PetscScalar u[], const PetscScalar gradU[], const PetscReal x[], PetscScalar f1[]), PetscScalar elemVec[])
130920cf1dd8SToby Isaac 
131020cf1dd8SToby Isaac ex52.c:
131120cf1dd8SToby Isaac   PetscErrorCode IntegrateLaplacianBatchCPU(PetscInt Ne, PetscInt Nb, const PetscScalar coefficients[], const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscInt Nq, const PetscReal quadPoints[], const PetscReal quadWeights[], const PetscReal basisTabulation[], const PetscReal basisDerTabulation[], PetscScalar elemVec[], AppCtx *user)
131220cf1dd8SToby Isaac   PetscErrorCode IntegrateElasticityBatchCPU(PetscInt Ne, PetscInt Nb, PetscInt Ncomp, const PetscScalar coefficients[], const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscInt Nq, const PetscReal quadPoints[], const PetscReal quadWeights[], const PetscReal basisTabulation[], const PetscReal basisDerTabulation[], PetscScalar elemVec[], AppCtx *user)
131320cf1dd8SToby Isaac 
131420cf1dd8SToby Isaac ex52_integrateElement.cu
131520cf1dd8SToby Isaac __global__ void integrateElementQuadrature(int N_cb, realType *coefficients, realType *jacobianInverses, realType *jacobianDeterminants, realType *elemVec)
131620cf1dd8SToby Isaac 
131720cf1dd8SToby Isaac PETSC_EXTERN PetscErrorCode IntegrateElementBatchGPU(PetscInt spatial_dim, PetscInt Ne, PetscInt Ncb, PetscInt Nbc, PetscInt Nbl, const PetscScalar coefficients[],
131820cf1dd8SToby Isaac                                                      const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscScalar elemVec[],
131920cf1dd8SToby Isaac                                                      PetscLogEvent event, PetscInt debug, PetscInt pde_op)
132020cf1dd8SToby Isaac 
132120cf1dd8SToby Isaac ex52_integrateElementOpenCL.c:
132220cf1dd8SToby Isaac PETSC_EXTERN PetscErrorCode IntegrateElementBatchGPU(PetscInt spatial_dim, PetscInt Ne, PetscInt Ncb, PetscInt Nbc, PetscInt N_bl, const PetscScalar coefficients[],
132320cf1dd8SToby Isaac                                                      const PetscReal jacobianInverses[], const PetscReal jacobianDeterminants[], PetscScalar elemVec[],
132420cf1dd8SToby Isaac                                                      PetscLogEvent event, PetscInt debug, PetscInt pde_op)
132520cf1dd8SToby Isaac 
132620cf1dd8SToby Isaac __kernel void integrateElementQuadrature(int N_cb, __global float *coefficients, __global float *jacobianInverses, __global float *jacobianDeterminants, __global float *elemVec)
132720cf1dd8SToby Isaac */
132820cf1dd8SToby Isaac 
132920cf1dd8SToby Isaac /*@C
133020cf1dd8SToby Isaac   PetscFEIntegrate - Produce the integral for the given field for a chunk of elements by quadrature integration
133120cf1dd8SToby Isaac 
133220cf1dd8SToby Isaac   Not collective
133320cf1dd8SToby Isaac 
133420cf1dd8SToby Isaac   Input Parameters:
1335*dce8aebaSBarry Smith + prob         - The `PetscDS` specifying the discretizations and continuum functions
133620cf1dd8SToby Isaac . field        - The field being integrated
133720cf1dd8SToby Isaac . Ne           - The number of elements in the chunk
133820cf1dd8SToby Isaac . cgeom        - The cell geometry for each cell in the chunk
133920cf1dd8SToby Isaac . coefficients - The array of FEM basis coefficients for the elements
1340*dce8aebaSBarry Smith . probAux      - The `PetscDS` specifying the auxiliary discretizations
134120cf1dd8SToby Isaac - coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
134220cf1dd8SToby Isaac 
13437a7aea1fSJed Brown   Output Parameter:
134420cf1dd8SToby Isaac . integral     - the integral for this field
134520cf1dd8SToby Isaac 
13462b99622eSMatthew G. Knepley   Level: intermediate
134720cf1dd8SToby Isaac 
1348*dce8aebaSBarry Smith   Developer Note:
1349*dce8aebaSBarry Smith   The function name begins with `PetscFE` and yet the first argument is `PetscDS` and it has no `PetscFE` arguments.
1350*dce8aebaSBarry Smith 
1351*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscDS`, `PetscFEIntegrateResidual()`, `PetscFEIntegrateBd()`
135220cf1dd8SToby Isaac @*/
1353d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrate(PetscDS prob, PetscInt field, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscScalar integral[])
1354d71ae5a4SJacob Faibussowitsch {
13554bee2e38SMatthew G. Knepley   PetscFE fe;
135620cf1dd8SToby Isaac 
135720cf1dd8SToby Isaac   PetscFunctionBegin;
13584bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 1);
13599566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(prob, field, (PetscObject *)&fe));
13609566063dSJacob Faibussowitsch   if (fe->ops->integrate) PetscCall((*fe->ops->integrate)(prob, field, Ne, cgeom, coefficients, probAux, coefficientsAux, integral));
136120cf1dd8SToby Isaac   PetscFunctionReturn(0);
136220cf1dd8SToby Isaac }
136320cf1dd8SToby Isaac 
136420cf1dd8SToby Isaac /*@C
1365afe6d6adSToby Isaac   PetscFEIntegrateBd - Produce the integral for the given field for a chunk of elements by quadrature integration
1366afe6d6adSToby Isaac 
1367afe6d6adSToby Isaac   Not collective
1368afe6d6adSToby Isaac 
1369afe6d6adSToby Isaac   Input Parameters:
1370*dce8aebaSBarry Smith + prob         - The `PetscDS` specifying the discretizations and continuum functions
1371afe6d6adSToby Isaac . field        - The field being integrated
1372afe6d6adSToby Isaac . obj_func     - The function to be integrated
1373afe6d6adSToby Isaac . Ne           - The number of elements in the chunk
1374afe6d6adSToby Isaac . fgeom        - The face geometry for each face in the chunk
1375afe6d6adSToby Isaac . coefficients - The array of FEM basis coefficients for the elements
1376*dce8aebaSBarry Smith . probAux      - The `PetscDS` specifying the auxiliary discretizations
1377afe6d6adSToby Isaac - coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
1378afe6d6adSToby Isaac 
13797a7aea1fSJed Brown   Output Parameter:
1380afe6d6adSToby Isaac . integral     - the integral for this field
1381afe6d6adSToby Isaac 
13822b99622eSMatthew G. Knepley   Level: intermediate
1383afe6d6adSToby Isaac 
1384*dce8aebaSBarry Smith   Developer Note:
1385*dce8aebaSBarry Smith   The function name begins with `PetscFE` and yet the first argument is `PetscDS` and it has no `PetscFE` arguments.
1386*dce8aebaSBarry Smith 
1387*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscDS`, `PetscFEIntegrateResidual()`, `PetscFEIntegrate()`
1388afe6d6adSToby Isaac @*/
1389d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateBd(PetscDS prob, PetscInt field, void (*obj_func)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), PetscInt Ne, PetscFEGeom *geom, const PetscScalar coefficients[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscScalar integral[])
1390d71ae5a4SJacob Faibussowitsch {
13914bee2e38SMatthew G. Knepley   PetscFE fe;
1392afe6d6adSToby Isaac 
1393afe6d6adSToby Isaac   PetscFunctionBegin;
13944bee2e38SMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 1);
13959566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(prob, field, (PetscObject *)&fe));
13969566063dSJacob Faibussowitsch   if (fe->ops->integratebd) PetscCall((*fe->ops->integratebd)(prob, field, obj_func, Ne, geom, coefficients, probAux, coefficientsAux, integral));
1397afe6d6adSToby Isaac   PetscFunctionReturn(0);
1398afe6d6adSToby Isaac }
1399afe6d6adSToby Isaac 
1400afe6d6adSToby Isaac /*@C
140120cf1dd8SToby Isaac   PetscFEIntegrateResidual - Produce the element residual vector for a chunk of elements by quadrature integration
140220cf1dd8SToby Isaac 
140320cf1dd8SToby Isaac   Not collective
140420cf1dd8SToby Isaac 
140520cf1dd8SToby Isaac   Input Parameters:
14066528b96dSMatthew G. Knepley + ds           - The PetscDS specifying the discretizations and continuum functions
14076528b96dSMatthew G. Knepley . key          - The (label+value, field) being integrated
140820cf1dd8SToby Isaac . Ne           - The number of elements in the chunk
140920cf1dd8SToby Isaac . cgeom        - The cell geometry for each cell in the chunk
141020cf1dd8SToby Isaac . coefficients - The array of FEM basis coefficients for the elements
141120cf1dd8SToby Isaac . coefficients_t - The array of FEM basis time derivative coefficients for the elements
141220cf1dd8SToby Isaac . probAux      - The PetscDS specifying the auxiliary discretizations
141320cf1dd8SToby Isaac . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
141420cf1dd8SToby Isaac - t            - The time
141520cf1dd8SToby Isaac 
14167a7aea1fSJed Brown   Output Parameter:
141720cf1dd8SToby Isaac . elemVec      - the element residual vectors from each element
141820cf1dd8SToby Isaac 
14192b99622eSMatthew G. Knepley   Level: intermediate
142020cf1dd8SToby Isaac 
1421*dce8aebaSBarry Smith   Note:
1422*dce8aebaSBarry Smith .vb
1423*dce8aebaSBarry Smith   Loop over batch of elements (e):
1424*dce8aebaSBarry Smith     Loop over quadrature points (q):
1425*dce8aebaSBarry Smith       Make u_q and gradU_q (loops over fields,Nb,Ncomp) and x_q
1426*dce8aebaSBarry Smith       Call f_0 and f_1
1427*dce8aebaSBarry Smith     Loop over element vector entries (f,fc --> i):
1428*dce8aebaSBarry Smith       elemVec[i] += \psi^{fc}_f(q) f0_{fc}(u, \nabla u) + \nabla\psi^{fc}_f(q) \cdot f1_{fc,df}(u, \nabla u)
1429*dce8aebaSBarry Smith .ve
1430*dce8aebaSBarry Smith 
1431db781477SPatrick Sanan .seealso: `PetscFEIntegrateResidual()`
143220cf1dd8SToby Isaac @*/
1433d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateResidual(PetscDS ds, PetscFormKey key, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
1434d71ae5a4SJacob Faibussowitsch {
14354bee2e38SMatthew G. Knepley   PetscFE fe;
143620cf1dd8SToby Isaac 
14376528b96dSMatthew G. Knepley   PetscFunctionBeginHot;
14386528b96dSMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 1);
14399566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(ds, key.field, (PetscObject *)&fe));
14409566063dSJacob Faibussowitsch   if (fe->ops->integrateresidual) PetscCall((*fe->ops->integrateresidual)(ds, key, Ne, cgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, elemVec));
144120cf1dd8SToby Isaac   PetscFunctionReturn(0);
144220cf1dd8SToby Isaac }
144320cf1dd8SToby Isaac 
144420cf1dd8SToby Isaac /*@C
144520cf1dd8SToby Isaac   PetscFEIntegrateBdResidual - Produce the element residual vector for a chunk of elements by quadrature integration over a boundary
144620cf1dd8SToby Isaac 
144720cf1dd8SToby Isaac   Not collective
144820cf1dd8SToby Isaac 
144920cf1dd8SToby Isaac   Input Parameters:
145006d8a0d3SMatthew G. Knepley + ds           - The PetscDS specifying the discretizations and continuum functions
145145480ffeSMatthew G. Knepley . wf           - The PetscWeakForm object holding the pointwise functions
145206d8a0d3SMatthew G. Knepley . key          - The (label+value, field) being integrated
145320cf1dd8SToby Isaac . Ne           - The number of elements in the chunk
145420cf1dd8SToby Isaac . fgeom        - The face geometry for each cell in the chunk
145520cf1dd8SToby Isaac . coefficients - The array of FEM basis coefficients for the elements
145620cf1dd8SToby Isaac . coefficients_t - The array of FEM basis time derivative coefficients for the elements
145720cf1dd8SToby Isaac . probAux      - The PetscDS specifying the auxiliary discretizations
145820cf1dd8SToby Isaac . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
145920cf1dd8SToby Isaac - t            - The time
146020cf1dd8SToby Isaac 
14617a7aea1fSJed Brown   Output Parameter:
146220cf1dd8SToby Isaac . elemVec      - the element residual vectors from each element
146320cf1dd8SToby Isaac 
14642b99622eSMatthew G. Knepley   Level: intermediate
146520cf1dd8SToby Isaac 
1466db781477SPatrick Sanan .seealso: `PetscFEIntegrateResidual()`
146720cf1dd8SToby Isaac @*/
1468d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateBdResidual(PetscDS ds, PetscWeakForm wf, PetscFormKey key, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
1469d71ae5a4SJacob Faibussowitsch {
14704bee2e38SMatthew G. Knepley   PetscFE fe;
147120cf1dd8SToby Isaac 
147220cf1dd8SToby Isaac   PetscFunctionBegin;
147306d8a0d3SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 1);
14749566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(ds, key.field, (PetscObject *)&fe));
14759566063dSJacob Faibussowitsch   if (fe->ops->integratebdresidual) PetscCall((*fe->ops->integratebdresidual)(ds, wf, key, Ne, fgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, elemVec));
147620cf1dd8SToby Isaac   PetscFunctionReturn(0);
147720cf1dd8SToby Isaac }
147820cf1dd8SToby Isaac 
147920cf1dd8SToby Isaac /*@C
148027f02ce8SMatthew G. Knepley   PetscFEIntegrateHybridResidual - Produce the element residual vector for a chunk of hybrid element faces by quadrature integration
148127f02ce8SMatthew G. Knepley 
148227f02ce8SMatthew G. Knepley   Not collective
148327f02ce8SMatthew G. Knepley 
148427f02ce8SMatthew G. Knepley   Input Parameters:
148527f02ce8SMatthew G. Knepley + prob         - The PetscDS specifying the discretizations and continuum functions
14866528b96dSMatthew G. Knepley . key          - The (label+value, field) being integrated
1487c2b7495fSMatthew G. Knepley . s            - The side of the cell being integrated, 0 for negative and 1 for positive
148827f02ce8SMatthew G. Knepley . Ne           - The number of elements in the chunk
148927f02ce8SMatthew G. Knepley . fgeom        - The face geometry for each cell in the chunk
149027f02ce8SMatthew G. Knepley . coefficients - The array of FEM basis coefficients for the elements
149127f02ce8SMatthew G. Knepley . coefficients_t - The array of FEM basis time derivative coefficients for the elements
149227f02ce8SMatthew G. Knepley . probAux      - The PetscDS specifying the auxiliary discretizations
149327f02ce8SMatthew G. Knepley . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
149427f02ce8SMatthew G. Knepley - t            - The time
149527f02ce8SMatthew G. Knepley 
149627f02ce8SMatthew G. Knepley   Output Parameter
149727f02ce8SMatthew G. Knepley . elemVec      - the element residual vectors from each element
149827f02ce8SMatthew G. Knepley 
149927f02ce8SMatthew G. Knepley   Level: developer
150027f02ce8SMatthew G. Knepley 
1501db781477SPatrick Sanan .seealso: `PetscFEIntegrateResidual()`
150227f02ce8SMatthew G. Knepley @*/
1503d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateHybridResidual(PetscDS prob, PetscFormKey key, PetscInt s, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscScalar elemVec[])
1504d71ae5a4SJacob Faibussowitsch {
150527f02ce8SMatthew G. Knepley   PetscFE fe;
150627f02ce8SMatthew G. Knepley 
150727f02ce8SMatthew G. Knepley   PetscFunctionBegin;
150827f02ce8SMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 1);
15099566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(prob, key.field, (PetscObject *)&fe));
15109566063dSJacob Faibussowitsch   if (fe->ops->integratehybridresidual) PetscCall((*fe->ops->integratehybridresidual)(prob, key, s, Ne, fgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, elemVec));
151127f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
151227f02ce8SMatthew G. Knepley }
151327f02ce8SMatthew G. Knepley 
151427f02ce8SMatthew G. Knepley /*@C
151520cf1dd8SToby Isaac   PetscFEIntegrateJacobian - Produce the element Jacobian for a chunk of elements by quadrature integration
151620cf1dd8SToby Isaac 
151720cf1dd8SToby Isaac   Not collective
151820cf1dd8SToby Isaac 
151920cf1dd8SToby Isaac   Input Parameters:
15206528b96dSMatthew G. Knepley + ds           - The PetscDS specifying the discretizations and continuum functions
152120cf1dd8SToby Isaac . jtype        - The type of matrix pointwise functions that should be used
15226528b96dSMatthew G. Knepley . key          - The (label+value, fieldI*Nf + fieldJ) being integrated
15235fedec97SMatthew G. Knepley . s            - The side of the cell being integrated, 0 for negative and 1 for positive
152420cf1dd8SToby Isaac . Ne           - The number of elements in the chunk
152520cf1dd8SToby Isaac . cgeom        - The cell geometry for each cell in the chunk
152620cf1dd8SToby Isaac . coefficients - The array of FEM basis coefficients for the elements for the Jacobian evaluation point
152720cf1dd8SToby Isaac . coefficients_t - The array of FEM basis time derivative coefficients for the elements
152820cf1dd8SToby Isaac . probAux      - The PetscDS specifying the auxiliary discretizations
152920cf1dd8SToby Isaac . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
153020cf1dd8SToby Isaac . t            - The time
153120cf1dd8SToby Isaac - u_tShift     - A multiplier for the dF/du_t term (as opposed to the dF/du term)
153220cf1dd8SToby Isaac 
15337a7aea1fSJed Brown   Output Parameter:
153420cf1dd8SToby Isaac . elemMat      - the element matrices for the Jacobian from each element
153520cf1dd8SToby Isaac 
15362b99622eSMatthew G. Knepley   Level: intermediate
153720cf1dd8SToby Isaac 
1538*dce8aebaSBarry Smith   Note:
1539*dce8aebaSBarry Smith .vb
1540*dce8aebaSBarry Smith   Loop over batch of elements (e):
1541*dce8aebaSBarry Smith     Loop over element matrix entries (f,fc,g,gc --> i,j):
1542*dce8aebaSBarry Smith       Loop over quadrature points (q):
1543*dce8aebaSBarry Smith         Make u_q and gradU_q (loops over fields,Nb,Ncomp)
1544*dce8aebaSBarry Smith           elemMat[i,j] += \psi^{fc}_f(q) g0_{fc,gc}(u, \nabla u) \phi^{gc}_g(q)
1545*dce8aebaSBarry Smith                        + \psi^{fc}_f(q) \cdot g1_{fc,gc,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1546*dce8aebaSBarry Smith                        + \nabla\psi^{fc}_f(q) \cdot g2_{fc,gc,df}(u, \nabla u) \phi^{gc}_g(q)
1547*dce8aebaSBarry Smith                        + \nabla\psi^{fc}_f(q) \cdot g3_{fc,gc,df,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1548*dce8aebaSBarry Smith .ve
1549*dce8aebaSBarry Smith 
1550db781477SPatrick Sanan .seealso: `PetscFEIntegrateResidual()`
155120cf1dd8SToby Isaac @*/
1552d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateJacobian(PetscDS ds, PetscFEJacobianType jtype, PetscFormKey key, PetscInt Ne, PetscFEGeom *cgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1553d71ae5a4SJacob Faibussowitsch {
15544bee2e38SMatthew G. Knepley   PetscFE  fe;
15556528b96dSMatthew G. Knepley   PetscInt Nf;
155620cf1dd8SToby Isaac 
155720cf1dd8SToby Isaac   PetscFunctionBegin;
15586528b96dSMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 1);
15599566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
15609566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(ds, key.field / Nf, (PetscObject *)&fe));
15619566063dSJacob Faibussowitsch   if (fe->ops->integratejacobian) PetscCall((*fe->ops->integratejacobian)(ds, jtype, key, Ne, cgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, u_tshift, elemMat));
156220cf1dd8SToby Isaac   PetscFunctionReturn(0);
156320cf1dd8SToby Isaac }
156420cf1dd8SToby Isaac 
156520cf1dd8SToby Isaac /*@C
156620cf1dd8SToby Isaac   PetscFEIntegrateBdJacobian - Produce the boundary element Jacobian for a chunk of elements by quadrature integration
156720cf1dd8SToby Isaac 
156820cf1dd8SToby Isaac   Not collective
156920cf1dd8SToby Isaac 
157020cf1dd8SToby Isaac   Input Parameters:
157145480ffeSMatthew G. Knepley + ds           - The PetscDS specifying the discretizations and continuum functions
157245480ffeSMatthew G. Knepley . wf           - The PetscWeakForm holding the pointwise functions
157345480ffeSMatthew G. Knepley . key          - The (label+value, fieldI*Nf + fieldJ) being integrated
157420cf1dd8SToby Isaac . Ne           - The number of elements in the chunk
157520cf1dd8SToby Isaac . fgeom        - The face geometry for each cell in the chunk
157620cf1dd8SToby Isaac . coefficients - The array of FEM basis coefficients for the elements for the Jacobian evaluation point
157720cf1dd8SToby Isaac . coefficients_t - The array of FEM basis time derivative coefficients for the elements
157820cf1dd8SToby Isaac . probAux      - The PetscDS specifying the auxiliary discretizations
157920cf1dd8SToby Isaac . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
158020cf1dd8SToby Isaac . t            - The time
158120cf1dd8SToby Isaac - u_tShift     - A multiplier for the dF/du_t term (as opposed to the dF/du term)
158220cf1dd8SToby Isaac 
15837a7aea1fSJed Brown   Output Parameter:
158420cf1dd8SToby Isaac . elemMat              - the element matrices for the Jacobian from each element
158520cf1dd8SToby Isaac 
15862b99622eSMatthew G. Knepley   Level: intermediate
158720cf1dd8SToby Isaac 
1588*dce8aebaSBarry Smith   Note:
1589*dce8aebaSBarry Smith .vb
1590*dce8aebaSBarry Smith   Loop over batch of elements (e):
1591*dce8aebaSBarry Smith     Loop over element matrix entries (f,fc,g,gc --> i,j):
1592*dce8aebaSBarry Smith       Loop over quadrature points (q):
1593*dce8aebaSBarry Smith         Make u_q and gradU_q (loops over fields,Nb,Ncomp)
1594*dce8aebaSBarry Smith           elemMat[i,j] += \psi^{fc}_f(q) g0_{fc,gc}(u, \nabla u) \phi^{gc}_g(q)
1595*dce8aebaSBarry Smith                        + \psi^{fc}_f(q) \cdot g1_{fc,gc,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1596*dce8aebaSBarry Smith                        + \nabla\psi^{fc}_f(q) \cdot g2_{fc,gc,df}(u, \nabla u) \phi^{gc}_g(q)
1597*dce8aebaSBarry Smith                        + \nabla\psi^{fc}_f(q) \cdot g3_{fc,gc,df,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1598*dce8aebaSBarry Smith .ve
1599*dce8aebaSBarry Smith 
1600db781477SPatrick Sanan .seealso: `PetscFEIntegrateJacobian()`, `PetscFEIntegrateResidual()`
160120cf1dd8SToby Isaac @*/
1602d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateBdJacobian(PetscDS ds, PetscWeakForm wf, PetscFormKey key, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1603d71ae5a4SJacob Faibussowitsch {
16044bee2e38SMatthew G. Knepley   PetscFE  fe;
160545480ffeSMatthew G. Knepley   PetscInt Nf;
160620cf1dd8SToby Isaac 
160720cf1dd8SToby Isaac   PetscFunctionBegin;
160845480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 1);
16099566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
16109566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(ds, key.field / Nf, (PetscObject *)&fe));
16119566063dSJacob Faibussowitsch   if (fe->ops->integratebdjacobian) PetscCall((*fe->ops->integratebdjacobian)(ds, wf, key, Ne, fgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, u_tshift, elemMat));
161220cf1dd8SToby Isaac   PetscFunctionReturn(0);
161320cf1dd8SToby Isaac }
161420cf1dd8SToby Isaac 
161527f02ce8SMatthew G. Knepley /*@C
161627f02ce8SMatthew G. Knepley   PetscFEIntegrateHybridJacobian - Produce the boundary element Jacobian for a chunk of hybrid elements by quadrature integration
161727f02ce8SMatthew G. Knepley 
161827f02ce8SMatthew G. Knepley   Not collective
161927f02ce8SMatthew G. Knepley 
162027f02ce8SMatthew G. Knepley   Input Parameters:
162145480ffeSMatthew G. Knepley + ds           - The PetscDS specifying the discretizations and continuum functions
162227f02ce8SMatthew G. Knepley . jtype        - The type of matrix pointwise functions that should be used
162345480ffeSMatthew G. Knepley . key          - The (label+value, fieldI*Nf + fieldJ) being integrated
16245fedec97SMatthew G. Knepley . s            - The side of the cell being integrated, 0 for negative and 1 for positive
162527f02ce8SMatthew G. Knepley . Ne           - The number of elements in the chunk
162627f02ce8SMatthew G. Knepley . fgeom        - The face geometry for each cell in the chunk
162727f02ce8SMatthew G. Knepley . coefficients - The array of FEM basis coefficients for the elements for the Jacobian evaluation point
162827f02ce8SMatthew G. Knepley . coefficients_t - The array of FEM basis time derivative coefficients for the elements
162927f02ce8SMatthew G. Knepley . probAux      - The PetscDS specifying the auxiliary discretizations
163027f02ce8SMatthew G. Knepley . coefficientsAux - The array of FEM auxiliary basis coefficients for the elements
163127f02ce8SMatthew G. Knepley . t            - The time
163227f02ce8SMatthew G. Knepley - u_tShift     - A multiplier for the dF/du_t term (as opposed to the dF/du term)
163327f02ce8SMatthew G. Knepley 
163427f02ce8SMatthew G. Knepley   Output Parameter
163527f02ce8SMatthew G. Knepley . elemMat              - the element matrices for the Jacobian from each element
163627f02ce8SMatthew G. Knepley 
163727f02ce8SMatthew G. Knepley   Level: developer
163827f02ce8SMatthew G. Knepley 
1639*dce8aebaSBarry Smith   Note:
1640*dce8aebaSBarry Smith .vb
1641*dce8aebaSBarry Smith   Loop over batch of elements (e):
1642*dce8aebaSBarry Smith     Loop over element matrix entries (f,fc,g,gc --> i,j):
1643*dce8aebaSBarry Smith       Loop over quadrature points (q):
1644*dce8aebaSBarry Smith         Make u_q and gradU_q (loops over fields,Nb,Ncomp)
1645*dce8aebaSBarry Smith           elemMat[i,j] += \psi^{fc}_f(q) g0_{fc,gc}(u, \nabla u) \phi^{gc}_g(q)
1646*dce8aebaSBarry Smith                        + \psi^{fc}_f(q) \cdot g1_{fc,gc,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1647*dce8aebaSBarry Smith                        + \nabla\psi^{fc}_f(q) \cdot g2_{fc,gc,df}(u, \nabla u) \phi^{gc}_g(q)
1648*dce8aebaSBarry Smith                        + \nabla\psi^{fc}_f(q) \cdot g3_{fc,gc,df,dg}(u, \nabla u) \nabla\phi^{gc}_g(q)
1649*dce8aebaSBarry Smith .ve
1650*dce8aebaSBarry Smith 
1651db781477SPatrick Sanan .seealso: `PetscFEIntegrateJacobian()`, `PetscFEIntegrateResidual()`
165227f02ce8SMatthew G. Knepley @*/
1653d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEIntegrateHybridJacobian(PetscDS ds, PetscFEJacobianType jtype, PetscFormKey key, PetscInt s, PetscInt Ne, PetscFEGeom *fgeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscDS probAux, const PetscScalar coefficientsAux[], PetscReal t, PetscReal u_tshift, PetscScalar elemMat[])
1654d71ae5a4SJacob Faibussowitsch {
165527f02ce8SMatthew G. Knepley   PetscFE  fe;
165645480ffeSMatthew G. Knepley   PetscInt Nf;
165727f02ce8SMatthew G. Knepley 
165827f02ce8SMatthew G. Knepley   PetscFunctionBegin;
165945480ffeSMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 1);
16609566063dSJacob Faibussowitsch   PetscCall(PetscDSGetNumFields(ds, &Nf));
16619566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(ds, key.field / Nf, (PetscObject *)&fe));
16629566063dSJacob Faibussowitsch   if (fe->ops->integratehybridjacobian) PetscCall((*fe->ops->integratehybridjacobian)(ds, jtype, key, s, Ne, fgeom, coefficients, coefficients_t, probAux, coefficientsAux, t, u_tshift, elemMat));
166327f02ce8SMatthew G. Knepley   PetscFunctionReturn(0);
166427f02ce8SMatthew G. Knepley }
166527f02ce8SMatthew G. Knepley 
16662b99622eSMatthew G. Knepley /*@
16672b99622eSMatthew G. Knepley   PetscFEGetHeightSubspace - Get the subspace of this space for a mesh point of a given height
16682b99622eSMatthew G. Knepley 
16692b99622eSMatthew G. Knepley   Input Parameters:
16702b99622eSMatthew G. Knepley + fe     - The finite element space
16712b99622eSMatthew G. Knepley - height - The height of the Plex point
16722b99622eSMatthew G. Knepley 
16732b99622eSMatthew G. Knepley   Output Parameter:
16742b99622eSMatthew G. Knepley . subfe  - The subspace of this FE space
16752b99622eSMatthew G. Knepley 
16762b99622eSMatthew G. Knepley   Level: advanced
16772b99622eSMatthew G. Knepley 
1678*dce8aebaSBarry Smith   Note:
1679*dce8aebaSBarry Smith   For example, if we want the subspace of this space for a face, we would choose height = 1.
1680*dce8aebaSBarry Smith 
1681db781477SPatrick Sanan .seealso: `PetscFECreateDefault()`
16822b99622eSMatthew G. Knepley @*/
1683d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEGetHeightSubspace(PetscFE fe, PetscInt height, PetscFE *subfe)
1684d71ae5a4SJacob Faibussowitsch {
168520cf1dd8SToby Isaac   PetscSpace      P, subP;
168620cf1dd8SToby Isaac   PetscDualSpace  Q, subQ;
168720cf1dd8SToby Isaac   PetscQuadrature subq;
168820cf1dd8SToby Isaac   PetscFEType     fetype;
168920cf1dd8SToby Isaac   PetscInt        dim, Nc;
169020cf1dd8SToby Isaac 
169120cf1dd8SToby Isaac   PetscFunctionBegin;
169220cf1dd8SToby Isaac   PetscValidHeaderSpecific(fe, PETSCFE_CLASSID, 1);
169320cf1dd8SToby Isaac   PetscValidPointer(subfe, 3);
169420cf1dd8SToby Isaac   if (height == 0) {
169520cf1dd8SToby Isaac     *subfe = fe;
169620cf1dd8SToby Isaac     PetscFunctionReturn(0);
169720cf1dd8SToby Isaac   }
16989566063dSJacob Faibussowitsch   PetscCall(PetscFEGetBasisSpace(fe, &P));
16999566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fe, &Q));
17009566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &Nc));
17019566063dSJacob Faibussowitsch   PetscCall(PetscFEGetFaceQuadrature(fe, &subq));
17029566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDimension(Q, &dim));
17031dca8a05SBarry Smith   PetscCheck(height <= dim && height >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Asked for space at height %" PetscInt_FMT " for dimension %" PetscInt_FMT " space", height, dim);
17049566063dSJacob Faibussowitsch   if (!fe->subspaces) PetscCall(PetscCalloc1(dim, &fe->subspaces));
170520cf1dd8SToby Isaac   if (height <= dim) {
170620cf1dd8SToby Isaac     if (!fe->subspaces[height - 1]) {
1707665f567fSMatthew G. Knepley       PetscFE     sub = NULL;
17083f6b16c7SMatthew G. Knepley       const char *name;
170920cf1dd8SToby Isaac 
17109566063dSJacob Faibussowitsch       PetscCall(PetscSpaceGetHeightSubspace(P, height, &subP));
17119566063dSJacob Faibussowitsch       PetscCall(PetscDualSpaceGetHeightSubspace(Q, height, &subQ));
1712665f567fSMatthew G. Knepley       if (subQ) {
17139566063dSJacob Faibussowitsch         PetscCall(PetscFECreate(PetscObjectComm((PetscObject)fe), &sub));
17149566063dSJacob Faibussowitsch         PetscCall(PetscObjectGetName((PetscObject)fe, &name));
17159566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)sub, name));
17169566063dSJacob Faibussowitsch         PetscCall(PetscFEGetType(fe, &fetype));
17179566063dSJacob Faibussowitsch         PetscCall(PetscFESetType(sub, fetype));
17189566063dSJacob Faibussowitsch         PetscCall(PetscFESetBasisSpace(sub, subP));
17199566063dSJacob Faibussowitsch         PetscCall(PetscFESetDualSpace(sub, subQ));
17209566063dSJacob Faibussowitsch         PetscCall(PetscFESetNumComponents(sub, Nc));
17219566063dSJacob Faibussowitsch         PetscCall(PetscFESetUp(sub));
17229566063dSJacob Faibussowitsch         PetscCall(PetscFESetQuadrature(sub, subq));
1723665f567fSMatthew G. Knepley       }
172420cf1dd8SToby Isaac       fe->subspaces[height - 1] = sub;
172520cf1dd8SToby Isaac     }
172620cf1dd8SToby Isaac     *subfe = fe->subspaces[height - 1];
172720cf1dd8SToby Isaac   } else {
172820cf1dd8SToby Isaac     *subfe = NULL;
172920cf1dd8SToby Isaac   }
173020cf1dd8SToby Isaac   PetscFunctionReturn(0);
173120cf1dd8SToby Isaac }
173220cf1dd8SToby Isaac 
173320cf1dd8SToby Isaac /*@
173420cf1dd8SToby Isaac   PetscFERefine - Create a "refined" PetscFE object that refines the reference cell into smaller copies. This is typically used
173520cf1dd8SToby Isaac   to precondition a higher order method with a lower order method on a refined mesh having the same number of dofs (but more
173620cf1dd8SToby Isaac   sparsity). It is also used to create an interpolation between regularly refined meshes.
173720cf1dd8SToby Isaac 
1738d083f849SBarry Smith   Collective on fem
173920cf1dd8SToby Isaac 
174020cf1dd8SToby Isaac   Input Parameter:
174120cf1dd8SToby Isaac . fe - The initial PetscFE
174220cf1dd8SToby Isaac 
174320cf1dd8SToby Isaac   Output Parameter:
174420cf1dd8SToby Isaac . feRef - The refined PetscFE
174520cf1dd8SToby Isaac 
17462b99622eSMatthew G. Knepley   Level: advanced
174720cf1dd8SToby Isaac 
1748db781477SPatrick Sanan .seealso: `PetscFEType`, `PetscFECreate()`, `PetscFESetType()`
174920cf1dd8SToby Isaac @*/
1750d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFERefine(PetscFE fe, PetscFE *feRef)
1751d71ae5a4SJacob Faibussowitsch {
175220cf1dd8SToby Isaac   PetscSpace       P, Pref;
175320cf1dd8SToby Isaac   PetscDualSpace   Q, Qref;
175420cf1dd8SToby Isaac   DM               K, Kref;
175520cf1dd8SToby Isaac   PetscQuadrature  q, qref;
175620cf1dd8SToby Isaac   const PetscReal *v0, *jac;
175720cf1dd8SToby Isaac   PetscInt         numComp, numSubelements;
17581ac17e89SToby Isaac   PetscInt         cStart, cEnd, c;
17591ac17e89SToby Isaac   PetscDualSpace  *cellSpaces;
176020cf1dd8SToby Isaac 
176120cf1dd8SToby Isaac   PetscFunctionBegin;
17629566063dSJacob Faibussowitsch   PetscCall(PetscFEGetBasisSpace(fe, &P));
17639566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fe, &Q));
17649566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &q));
17659566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDM(Q, &K));
176620cf1dd8SToby Isaac   /* Create space */
17679566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)P));
176820cf1dd8SToby Isaac   Pref = P;
176920cf1dd8SToby Isaac   /* Create dual space */
17709566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceDuplicate(Q, &Qref));
17719566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetType(Qref, PETSCDUALSPACEREFINED));
17729566063dSJacob Faibussowitsch   PetscCall(DMRefine(K, PetscObjectComm((PetscObject)fe), &Kref));
17739566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetDM(Qref, Kref));
17749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(Kref, 0, &cStart, &cEnd));
17759566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(cEnd - cStart, &cellSpaces));
17761ac17e89SToby Isaac   /* TODO: fix for non-uniform refinement */
17771ac17e89SToby Isaac   for (c = 0; c < cEnd - cStart; c++) cellSpaces[c] = Q;
17789566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceRefinedSetCellSpaces(Qref, cellSpaces));
17799566063dSJacob Faibussowitsch   PetscCall(PetscFree(cellSpaces));
17809566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&Kref));
17819566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetUp(Qref));
178220cf1dd8SToby Isaac   /* Create element */
17839566063dSJacob Faibussowitsch   PetscCall(PetscFECreate(PetscObjectComm((PetscObject)fe), feRef));
17849566063dSJacob Faibussowitsch   PetscCall(PetscFESetType(*feRef, PETSCFECOMPOSITE));
17859566063dSJacob Faibussowitsch   PetscCall(PetscFESetBasisSpace(*feRef, Pref));
17869566063dSJacob Faibussowitsch   PetscCall(PetscFESetDualSpace(*feRef, Qref));
17879566063dSJacob Faibussowitsch   PetscCall(PetscFEGetNumComponents(fe, &numComp));
17889566063dSJacob Faibussowitsch   PetscCall(PetscFESetNumComponents(*feRef, numComp));
17899566063dSJacob Faibussowitsch   PetscCall(PetscFESetUp(*feRef));
17909566063dSJacob Faibussowitsch   PetscCall(PetscSpaceDestroy(&Pref));
17919566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceDestroy(&Qref));
179220cf1dd8SToby Isaac   /* Create quadrature */
17939566063dSJacob Faibussowitsch   PetscCall(PetscFECompositeGetMapping(*feRef, &numSubelements, &v0, &jac, NULL));
17949566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureExpandComposite(q, numSubelements, v0, jac, &qref));
17959566063dSJacob Faibussowitsch   PetscCall(PetscFESetQuadrature(*feRef, qref));
17969566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureDestroy(&qref));
179720cf1dd8SToby Isaac   PetscFunctionReturn(0);
179820cf1dd8SToby Isaac }
179920cf1dd8SToby Isaac 
1800d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscFESetDefaultName_Private(PetscFE fe)
1801d71ae5a4SJacob Faibussowitsch {
18027c48043bSMatthew G. Knepley   PetscSpace     P;
18037c48043bSMatthew G. Knepley   PetscDualSpace Q;
18047c48043bSMatthew G. Knepley   DM             K;
18057c48043bSMatthew G. Knepley   DMPolytopeType ct;
18067c48043bSMatthew G. Knepley   PetscInt       degree;
18077c48043bSMatthew G. Knepley   char           name[64];
18087c48043bSMatthew G. Knepley 
18097c48043bSMatthew G. Knepley   PetscFunctionBegin;
18107c48043bSMatthew G. Knepley   PetscCall(PetscFEGetBasisSpace(fe, &P));
18117c48043bSMatthew G. Knepley   PetscCall(PetscSpaceGetDegree(P, &degree, NULL));
18127c48043bSMatthew G. Knepley   PetscCall(PetscFEGetDualSpace(fe, &Q));
18137c48043bSMatthew G. Knepley   PetscCall(PetscDualSpaceGetDM(Q, &K));
18147c48043bSMatthew G. Knepley   PetscCall(DMPlexGetCellType(K, 0, &ct));
18157c48043bSMatthew G. Knepley   switch (ct) {
18167c48043bSMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
18177c48043bSMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
18187c48043bSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
18197c48043bSMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
18207c48043bSMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
1821d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1822d71ae5a4SJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, sizeof(name), "Q%" PetscInt_FMT, degree));
1823d71ae5a4SJacob Faibussowitsch     break;
18247c48043bSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
1825d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TETRAHEDRON:
1826d71ae5a4SJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, sizeof(name), "P%" PetscInt_FMT, degree));
1827d71ae5a4SJacob Faibussowitsch     break;
18287c48043bSMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
1829d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_TRI_PRISM_TENSOR:
1830d71ae5a4SJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, sizeof(name), "P%" PetscInt_FMT "xQ%" PetscInt_FMT, degree, degree));
1831d71ae5a4SJacob Faibussowitsch     break;
1832d71ae5a4SJacob Faibussowitsch   default:
1833d71ae5a4SJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, sizeof(name), "FE"));
18347c48043bSMatthew G. Knepley   }
18357c48043bSMatthew G. Knepley   PetscCall(PetscFESetName(fe, name));
18367c48043bSMatthew G. Knepley   PetscFunctionReturn(0);
18377c48043bSMatthew G. Knepley }
18387c48043bSMatthew G. Knepley 
1839d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscFECreateDefaultQuadrature_Private(PetscInt dim, DMPolytopeType ct, PetscInt qorder, PetscQuadrature *q, PetscQuadrature *fq)
1840d71ae5a4SJacob Faibussowitsch {
18417c48043bSMatthew G. Knepley   const PetscInt quadPointsPerEdge = PetscMax(qorder + 1, 1);
18427c48043bSMatthew G. Knepley 
18437c48043bSMatthew G. Knepley   PetscFunctionBegin;
18447c48043bSMatthew G. Knepley   switch (ct) {
18457c48043bSMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
18467c48043bSMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
18477c48043bSMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
18487c48043bSMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
18497c48043bSMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
18507c48043bSMatthew G. Knepley   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
18517c48043bSMatthew G. Knepley     PetscCall(PetscDTGaussTensorQuadrature(dim, 1, quadPointsPerEdge, -1.0, 1.0, q));
18527c48043bSMatthew G. Knepley     PetscCall(PetscDTGaussTensorQuadrature(dim - 1, 1, quadPointsPerEdge, -1.0, 1.0, fq));
18537c48043bSMatthew G. Knepley     break;
18547c48043bSMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
18557c48043bSMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
18567c48043bSMatthew G. Knepley     PetscCall(PetscDTStroudConicalQuadrature(dim, 1, quadPointsPerEdge, -1.0, 1.0, q));
18577c48043bSMatthew G. Knepley     PetscCall(PetscDTStroudConicalQuadrature(dim - 1, 1, quadPointsPerEdge, -1.0, 1.0, fq));
18587c48043bSMatthew G. Knepley     break;
18597c48043bSMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
18609371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
18617c48043bSMatthew G. Knepley     PetscQuadrature q1, q2;
18627c48043bSMatthew G. Knepley 
18637c48043bSMatthew G. Knepley     PetscCall(PetscDTStroudConicalQuadrature(2, 1, quadPointsPerEdge, -1.0, 1.0, &q1));
18647c48043bSMatthew G. Knepley     PetscCall(PetscDTGaussTensorQuadrature(1, 1, quadPointsPerEdge, -1.0, 1.0, &q2));
18657c48043bSMatthew G. Knepley     PetscCall(PetscDTTensorQuadratureCreate(q1, q2, q));
18667c48043bSMatthew G. Knepley     PetscCall(PetscQuadratureDestroy(&q1));
18677c48043bSMatthew G. Knepley     PetscCall(PetscQuadratureDestroy(&q2));
18687c48043bSMatthew G. Knepley   }
18697c48043bSMatthew G. Knepley     PetscCall(PetscDTStroudConicalQuadrature(dim - 1, 1, quadPointsPerEdge, -1.0, 1.0, fq));
18707c48043bSMatthew G. Knepley     /* TODO Need separate quadratures for each face */
18717c48043bSMatthew G. Knepley     break;
1872d71ae5a4SJacob Faibussowitsch   default:
1873d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No quadrature for celltype %s", DMPolytopeTypes[PetscMin(ct, DM_POLYTOPE_UNKNOWN)]);
18747c48043bSMatthew G. Knepley   }
18757c48043bSMatthew G. Knepley   PetscFunctionReturn(0);
18767c48043bSMatthew G. Knepley }
18777c48043bSMatthew G. Knepley 
18787c48043bSMatthew G. Knepley /*@
1879*dce8aebaSBarry Smith   PetscFECreateFromSpaces - Create a `PetscFE` from the basis and dual spaces
18807c48043bSMatthew G. Knepley 
18817c48043bSMatthew G. Knepley   Collective
18827c48043bSMatthew G. Knepley 
18837c48043bSMatthew G. Knepley   Input Parameters:
18847c48043bSMatthew G. Knepley + P  - The basis space
18857c48043bSMatthew G. Knepley . Q  - The dual space
18867c48043bSMatthew G. Knepley . q  - The cell quadrature
18877c48043bSMatthew G. Knepley - fq - The face quadrature
18887c48043bSMatthew G. Knepley 
18897c48043bSMatthew G. Knepley   Output Parameter:
18907c48043bSMatthew G. Knepley . fem    - The PetscFE object
18917c48043bSMatthew G. Knepley 
18927c48043bSMatthew G. Knepley   Level: beginner
18937c48043bSMatthew G. Knepley 
1894*dce8aebaSBarry Smith   Note:
1895*dce8aebaSBarry Smith   The `PetscFE` takes ownership of these spaces by calling destroy on each. They should not be used after this call, and for borrowed references from `PetscFEGetSpace()` and the like, the caller must use `PetscObjectReference` before this call.
1896*dce8aebaSBarry Smith 
1897*dce8aebaSBarry Smith .seealso: `PetscFE`, `PetscSpace`, `PetscDualSpace`, `PetscQuadrature`,
1898*dce8aebaSBarry Smith           `PetscFECreateLagrangeByCell()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
18997c48043bSMatthew G. Knepley @*/
1900d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateFromSpaces(PetscSpace P, PetscDualSpace Q, PetscQuadrature q, PetscQuadrature fq, PetscFE *fem)
1901d71ae5a4SJacob Faibussowitsch {
19027c48043bSMatthew G. Knepley   PetscInt    Nc;
19037c48043bSMatthew G. Knepley   const char *prefix;
19047c48043bSMatthew G. Knepley 
19057c48043bSMatthew G. Knepley   PetscFunctionBegin;
19067c48043bSMatthew G. Knepley   PetscCall(PetscFECreate(PetscObjectComm((PetscObject)P), fem));
19077c48043bSMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)P, &prefix));
19087c48043bSMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*fem, prefix));
19097c48043bSMatthew G. Knepley   PetscCall(PetscFESetType(*fem, PETSCFEBASIC));
19107c48043bSMatthew G. Knepley   PetscCall(PetscFESetBasisSpace(*fem, P));
19117c48043bSMatthew G. Knepley   PetscCall(PetscFESetDualSpace(*fem, Q));
19127c48043bSMatthew G. Knepley   PetscCall(PetscSpaceGetNumComponents(P, &Nc));
19137c48043bSMatthew G. Knepley   PetscCall(PetscFESetNumComponents(*fem, Nc));
19147c48043bSMatthew G. Knepley   PetscCall(PetscFESetUp(*fem));
19157c48043bSMatthew G. Knepley   PetscCall(PetscSpaceDestroy(&P));
19167c48043bSMatthew G. Knepley   PetscCall(PetscDualSpaceDestroy(&Q));
19177c48043bSMatthew G. Knepley   PetscCall(PetscFESetQuadrature(*fem, q));
19187c48043bSMatthew G. Knepley   PetscCall(PetscFESetFaceQuadrature(*fem, fq));
19197c48043bSMatthew G. Knepley   PetscCall(PetscQuadratureDestroy(&q));
19207c48043bSMatthew G. Knepley   PetscCall(PetscQuadratureDestroy(&fq));
19217c48043bSMatthew G. Knepley   PetscCall(PetscFESetDefaultName_Private(*fem));
19227c48043bSMatthew G. Knepley   PetscFunctionReturn(0);
19237c48043bSMatthew G. Knepley }
19247c48043bSMatthew G. Knepley 
1925d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscFECreate_Internal(MPI_Comm comm, PetscInt dim, PetscInt Nc, DMPolytopeType ct, const char prefix[], PetscInt degree, PetscInt qorder, PetscBool setFromOptions, PetscFE *fem)
1926d71ae5a4SJacob Faibussowitsch {
19272df84da0SMatthew G. Knepley   DM              K;
19282df84da0SMatthew G. Knepley   PetscSpace      P;
19292df84da0SMatthew G. Knepley   PetscDualSpace  Q;
19307c48043bSMatthew G. Knepley   PetscQuadrature q, fq;
19312df84da0SMatthew G. Knepley   PetscBool       tensor;
19322df84da0SMatthew G. Knepley 
19332df84da0SMatthew G. Knepley   PetscFunctionBegin;
19342df84da0SMatthew G. Knepley   if (prefix) PetscValidCharPointer(prefix, 5);
19352df84da0SMatthew G. Knepley   PetscValidPointer(fem, 9);
19362df84da0SMatthew G. Knepley   switch (ct) {
19372df84da0SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
19382df84da0SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
19392df84da0SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
19402df84da0SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
19412df84da0SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
1942d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
1943d71ae5a4SJacob Faibussowitsch     tensor = PETSC_TRUE;
1944d71ae5a4SJacob Faibussowitsch     break;
1945d71ae5a4SJacob Faibussowitsch   default:
1946d71ae5a4SJacob Faibussowitsch     tensor = PETSC_FALSE;
19472df84da0SMatthew G. Knepley   }
19482df84da0SMatthew G. Knepley   /* Create space */
19499566063dSJacob Faibussowitsch   PetscCall(PetscSpaceCreate(comm, &P));
19509566063dSJacob Faibussowitsch   PetscCall(PetscSpaceSetType(P, PETSCSPACEPOLYNOMIAL));
19519566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)P, prefix));
19529566063dSJacob Faibussowitsch   PetscCall(PetscSpacePolynomialSetTensor(P, tensor));
19539566063dSJacob Faibussowitsch   PetscCall(PetscSpaceSetNumComponents(P, Nc));
19549566063dSJacob Faibussowitsch   PetscCall(PetscSpaceSetNumVariables(P, dim));
19552df84da0SMatthew G. Knepley   if (degree >= 0) {
19569566063dSJacob Faibussowitsch     PetscCall(PetscSpaceSetDegree(P, degree, PETSC_DETERMINE));
1957cfd33b42SLisandro Dalcin     if (ct == DM_POLYTOPE_TRI_PRISM || ct == DM_POLYTOPE_TRI_PRISM_TENSOR) {
19582df84da0SMatthew G. Knepley       PetscSpace Pend, Pside;
19592df84da0SMatthew G. Knepley 
19609566063dSJacob Faibussowitsch       PetscCall(PetscSpaceCreate(comm, &Pend));
19619566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetType(Pend, PETSCSPACEPOLYNOMIAL));
19629566063dSJacob Faibussowitsch       PetscCall(PetscSpacePolynomialSetTensor(Pend, PETSC_FALSE));
19639566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetNumComponents(Pend, Nc));
19649566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetNumVariables(Pend, dim - 1));
19659566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetDegree(Pend, degree, PETSC_DETERMINE));
19669566063dSJacob Faibussowitsch       PetscCall(PetscSpaceCreate(comm, &Pside));
19679566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetType(Pside, PETSCSPACEPOLYNOMIAL));
19689566063dSJacob Faibussowitsch       PetscCall(PetscSpacePolynomialSetTensor(Pside, PETSC_FALSE));
19699566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetNumComponents(Pside, 1));
19709566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetNumVariables(Pside, 1));
19719566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetDegree(Pside, degree, PETSC_DETERMINE));
19729566063dSJacob Faibussowitsch       PetscCall(PetscSpaceSetType(P, PETSCSPACETENSOR));
19739566063dSJacob Faibussowitsch       PetscCall(PetscSpaceTensorSetNumSubspaces(P, 2));
19749566063dSJacob Faibussowitsch       PetscCall(PetscSpaceTensorSetSubspace(P, 0, Pend));
19759566063dSJacob Faibussowitsch       PetscCall(PetscSpaceTensorSetSubspace(P, 1, Pside));
19769566063dSJacob Faibussowitsch       PetscCall(PetscSpaceDestroy(&Pend));
19779566063dSJacob Faibussowitsch       PetscCall(PetscSpaceDestroy(&Pside));
19782df84da0SMatthew G. Knepley     }
19792df84da0SMatthew G. Knepley   }
19809566063dSJacob Faibussowitsch   if (setFromOptions) PetscCall(PetscSpaceSetFromOptions(P));
19819566063dSJacob Faibussowitsch   PetscCall(PetscSpaceSetUp(P));
19829566063dSJacob Faibussowitsch   PetscCall(PetscSpaceGetDegree(P, &degree, NULL));
19839566063dSJacob Faibussowitsch   PetscCall(PetscSpacePolynomialGetTensor(P, &tensor));
19849566063dSJacob Faibussowitsch   PetscCall(PetscSpaceGetNumComponents(P, &Nc));
19852df84da0SMatthew G. Knepley   /* Create dual space */
19869566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceCreate(comm, &Q));
19879566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetType(Q, PETSCDUALSPACELAGRANGE));
19889566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)Q, prefix));
19899566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &K));
19909566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetDM(Q, K));
19919566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&K));
19929566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetNumComponents(Q, Nc));
19939566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetOrder(Q, degree));
19942df84da0SMatthew G. Knepley   /* TODO For some reason, we need a tensor dualspace with wedges */
19959566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceLagrangeSetTensor(Q, (tensor || (ct == DM_POLYTOPE_TRI_PRISM)) ? PETSC_TRUE : PETSC_FALSE));
19969566063dSJacob Faibussowitsch   if (setFromOptions) PetscCall(PetscDualSpaceSetFromOptions(Q));
19979566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceSetUp(Q));
19987c48043bSMatthew G. Knepley   /* Create quadrature */
19992df84da0SMatthew G. Knepley   qorder = qorder >= 0 ? qorder : degree;
20002df84da0SMatthew G. Knepley   if (setFromOptions) {
20017c48043bSMatthew G. Knepley     PetscObjectOptionsBegin((PetscObject)P);
20029566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBoundedInt("-petscfe_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "PetscFECreateDefault", qorder, &qorder, NULL, 0));
2003d0609cedSBarry Smith     PetscOptionsEnd();
20042df84da0SMatthew G. Knepley   }
20057c48043bSMatthew G. Knepley   PetscCall(PetscFECreateDefaultQuadrature_Private(dim, ct, qorder, &q, &fq));
20067c48043bSMatthew G. Knepley   /* Create finite element */
20077c48043bSMatthew G. Knepley   PetscCall(PetscFECreateFromSpaces(P, Q, q, fq, fem));
20087c48043bSMatthew G. Knepley   if (setFromOptions) PetscCall(PetscFESetFromOptions(*fem));
20092df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
20102df84da0SMatthew G. Knepley }
20112df84da0SMatthew G. Knepley 
201220cf1dd8SToby Isaac /*@C
201320cf1dd8SToby Isaac   PetscFECreateDefault - Create a PetscFE for basic FEM computation
201420cf1dd8SToby Isaac 
2015d083f849SBarry Smith   Collective
201620cf1dd8SToby Isaac 
201720cf1dd8SToby Isaac   Input Parameters:
20187be5e748SToby Isaac + comm      - The MPI comm
201920cf1dd8SToby Isaac . dim       - The spatial dimension
202020cf1dd8SToby Isaac . Nc        - The number of components
202120cf1dd8SToby Isaac . isSimplex - Flag for simplex reference cell, otherwise its a tensor product
202220cf1dd8SToby Isaac . prefix    - The options prefix, or NULL
2023727cddd5SJacob Faibussowitsch - qorder    - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
202420cf1dd8SToby Isaac 
202520cf1dd8SToby Isaac   Output Parameter:
202620cf1dd8SToby Isaac . fem - The PetscFE object
202720cf1dd8SToby Isaac 
2028*dce8aebaSBarry Smith   Level: beginner
2029*dce8aebaSBarry Smith 
2030e703855dSMatthew G. Knepley   Note:
20318f2aacc6SMatthew G. Knepley   Each subobject is SetFromOption() during creation, so that the object may be customized from the command line, using the prefix specified above. See the links below for the particular options available.
2032e703855dSMatthew G. Knepley 
2033db781477SPatrick Sanan .seealso: `PetscFECreateLagrange()`, `PetscFECreateByCell()`, `PetscSpaceSetFromOptions()`, `PetscDualSpaceSetFromOptions()`, `PetscFESetFromOptions()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
203420cf1dd8SToby Isaac @*/
2035d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateDefault(MPI_Comm comm, PetscInt dim, PetscInt Nc, PetscBool isSimplex, const char prefix[], PetscInt qorder, PetscFE *fem)
2036d71ae5a4SJacob Faibussowitsch {
203720cf1dd8SToby Isaac   PetscFunctionBegin;
20389566063dSJacob Faibussowitsch   PetscCall(PetscFECreate_Internal(comm, dim, Nc, DMPolytopeTypeSimpleShape(dim, isSimplex), prefix, PETSC_DECIDE, qorder, PETSC_TRUE, fem));
20392df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
204020cf1dd8SToby Isaac }
20412df84da0SMatthew G. Knepley 
20422df84da0SMatthew G. Knepley /*@C
20432df84da0SMatthew G. Knepley   PetscFECreateByCell - Create a PetscFE for basic FEM computation
20442df84da0SMatthew G. Knepley 
20452df84da0SMatthew G. Knepley   Collective
20462df84da0SMatthew G. Knepley 
20472df84da0SMatthew G. Knepley   Input Parameters:
20482df84da0SMatthew G. Knepley + comm   - The MPI comm
20492df84da0SMatthew G. Knepley . dim    - The spatial dimension
20502df84da0SMatthew G. Knepley . Nc     - The number of components
20512df84da0SMatthew G. Knepley . ct     - The celltype of the reference cell
20522df84da0SMatthew G. Knepley . prefix - The options prefix, or NULL
20532df84da0SMatthew G. Knepley - qorder - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
20542df84da0SMatthew G. Knepley 
20552df84da0SMatthew G. Knepley   Output Parameter:
20562df84da0SMatthew G. Knepley . fem - The PetscFE object
20572df84da0SMatthew G. Knepley 
2058*dce8aebaSBarry Smith   Level: beginner
2059*dce8aebaSBarry Smith 
20602df84da0SMatthew G. Knepley   Note:
20612df84da0SMatthew G. Knepley   Each subobject is SetFromOption() during creation, so that the object may be customized from the command line, using the prefix specified above. See the links below for the particular options available.
20622df84da0SMatthew G. Knepley 
2063db781477SPatrick Sanan .seealso: `PetscFECreateDefault()`, `PetscFECreateLagrange()`, `PetscSpaceSetFromOptions()`, `PetscDualSpaceSetFromOptions()`, `PetscFESetFromOptions()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
20642df84da0SMatthew G. Knepley @*/
2065d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateByCell(MPI_Comm comm, PetscInt dim, PetscInt Nc, DMPolytopeType ct, const char prefix[], PetscInt qorder, PetscFE *fem)
2066d71ae5a4SJacob Faibussowitsch {
20672df84da0SMatthew G. Knepley   PetscFunctionBegin;
20689566063dSJacob Faibussowitsch   PetscCall(PetscFECreate_Internal(comm, dim, Nc, ct, prefix, PETSC_DECIDE, qorder, PETSC_TRUE, fem));
206920cf1dd8SToby Isaac   PetscFunctionReturn(0);
207020cf1dd8SToby Isaac }
20713f6b16c7SMatthew G. Knepley 
2072e703855dSMatthew G. Knepley /*@
2073e703855dSMatthew G. Knepley   PetscFECreateLagrange - Create a PetscFE for the basic Lagrange space of degree k
2074e703855dSMatthew G. Knepley 
2075e703855dSMatthew G. Knepley   Collective
2076e703855dSMatthew G. Knepley 
2077e703855dSMatthew G. Knepley   Input Parameters:
2078e703855dSMatthew G. Knepley + comm      - The MPI comm
2079e703855dSMatthew G. Knepley . dim       - The spatial dimension
2080e703855dSMatthew G. Knepley . Nc        - The number of components
2081e703855dSMatthew G. Knepley . isSimplex - Flag for simplex reference cell, otherwise its a tensor product
2082e703855dSMatthew G. Knepley . k         - The degree k of the space
2083e703855dSMatthew G. Knepley - qorder    - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
2084e703855dSMatthew G. Knepley 
2085e703855dSMatthew G. Knepley   Output Parameter:
2086e703855dSMatthew G. Knepley . fem       - The PetscFE object
2087e703855dSMatthew G. Knepley 
2088e703855dSMatthew G. Knepley   Level: beginner
2089e703855dSMatthew G. Knepley 
2090*dce8aebaSBarry Smith   Note:
2091e703855dSMatthew G. Knepley   For simplices, this element is the space of maximum polynomial degree k, otherwise it is a tensor product of 1D polynomials, each with maximal degree k.
2092e703855dSMatthew G. Knepley 
2093db781477SPatrick Sanan .seealso: `PetscFECreateLagrangeByCell()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
2094e703855dSMatthew G. Knepley @*/
2095d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateLagrange(MPI_Comm comm, PetscInt dim, PetscInt Nc, PetscBool isSimplex, PetscInt k, PetscInt qorder, PetscFE *fem)
2096d71ae5a4SJacob Faibussowitsch {
2097e703855dSMatthew G. Knepley   PetscFunctionBegin;
20989566063dSJacob Faibussowitsch   PetscCall(PetscFECreate_Internal(comm, dim, Nc, DMPolytopeTypeSimpleShape(dim, isSimplex), NULL, k, qorder, PETSC_FALSE, fem));
20992df84da0SMatthew G. Knepley   PetscFunctionReturn(0);
2100e703855dSMatthew G. Knepley }
21012df84da0SMatthew G. Knepley 
21022df84da0SMatthew G. Knepley /*@
21032df84da0SMatthew G. Knepley   PetscFECreateLagrangeByCell - Create a PetscFE for the basic Lagrange space of degree k
21042df84da0SMatthew G. Knepley 
21052df84da0SMatthew G. Knepley   Collective
21062df84da0SMatthew G. Knepley 
21072df84da0SMatthew G. Knepley   Input Parameters:
21082df84da0SMatthew G. Knepley + comm      - The MPI comm
21092df84da0SMatthew G. Knepley . dim       - The spatial dimension
21102df84da0SMatthew G. Knepley . Nc        - The number of components
21112df84da0SMatthew G. Knepley . ct        - The celltype of the reference cell
21122df84da0SMatthew G. Knepley . k         - The degree k of the space
21132df84da0SMatthew G. Knepley - qorder    - The quadrature order or PETSC_DETERMINE to use PetscSpace polynomial degree
21142df84da0SMatthew G. Knepley 
21152df84da0SMatthew G. Knepley   Output Parameter:
21162df84da0SMatthew G. Knepley . fem       - The PetscFE object
21172df84da0SMatthew G. Knepley 
21182df84da0SMatthew G. Knepley   Level: beginner
21192df84da0SMatthew G. Knepley 
2120*dce8aebaSBarry Smith   Note:
21212df84da0SMatthew G. Knepley   For simplices, this element is the space of maximum polynomial degree k, otherwise it is a tensor product of 1D polynomials, each with maximal degree k.
21222df84da0SMatthew G. Knepley 
2123db781477SPatrick Sanan .seealso: `PetscFECreateLagrange()`, `PetscFECreateDefault()`, `PetscFECreateByCell()`, `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
21242df84da0SMatthew G. Knepley @*/
2125d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateLagrangeByCell(MPI_Comm comm, PetscInt dim, PetscInt Nc, DMPolytopeType ct, PetscInt k, PetscInt qorder, PetscFE *fem)
2126d71ae5a4SJacob Faibussowitsch {
21272df84da0SMatthew G. Knepley   PetscFunctionBegin;
21289566063dSJacob Faibussowitsch   PetscCall(PetscFECreate_Internal(comm, dim, Nc, ct, NULL, k, qorder, PETSC_FALSE, fem));
2129e703855dSMatthew G. Knepley   PetscFunctionReturn(0);
2130e703855dSMatthew G. Knepley }
2131e703855dSMatthew G. Knepley 
21323f6b16c7SMatthew G. Knepley /*@C
21333f6b16c7SMatthew G. Knepley   PetscFESetName - Names the FE and its subobjects
21343f6b16c7SMatthew G. Knepley 
21353f6b16c7SMatthew G. Knepley   Not collective
21363f6b16c7SMatthew G. Knepley 
21373f6b16c7SMatthew G. Knepley   Input Parameters:
21383f6b16c7SMatthew G. Knepley + fe   - The PetscFE
21393f6b16c7SMatthew G. Knepley - name - The name
21403f6b16c7SMatthew G. Knepley 
21412b99622eSMatthew G. Knepley   Level: intermediate
21423f6b16c7SMatthew G. Knepley 
2143db781477SPatrick Sanan .seealso: `PetscFECreate()`, `PetscSpaceCreate()`, `PetscDualSpaceCreate()`
21443f6b16c7SMatthew G. Knepley @*/
2145d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFESetName(PetscFE fe, const char name[])
2146d71ae5a4SJacob Faibussowitsch {
21473f6b16c7SMatthew G. Knepley   PetscSpace     P;
21483f6b16c7SMatthew G. Knepley   PetscDualSpace Q;
21493f6b16c7SMatthew G. Knepley 
21503f6b16c7SMatthew G. Knepley   PetscFunctionBegin;
21519566063dSJacob Faibussowitsch   PetscCall(PetscFEGetBasisSpace(fe, &P));
21529566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fe, &Q));
21539566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)fe, name));
21549566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)P, name));
21559566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)Q, name));
21563f6b16c7SMatthew G. Knepley   PetscFunctionReturn(0);
21573f6b16c7SMatthew G. Knepley }
2158a8f1f9e5SMatthew G. Knepley 
2159d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEEvaluateFieldJets_Internal(PetscDS ds, PetscInt Nf, PetscInt r, PetscInt q, PetscTabulation T[], PetscFEGeom *fegeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscScalar u[], PetscScalar u_x[], PetscScalar u_t[])
2160d71ae5a4SJacob Faibussowitsch {
2161f9244615SMatthew G. Knepley   PetscInt dOffset = 0, fOffset = 0, f, g;
2162a8f1f9e5SMatthew G. Knepley 
2163a8f1f9e5SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2164a8f1f9e5SMatthew G. Knepley     PetscFE          fe;
2165f9244615SMatthew G. Knepley     const PetscInt   k       = ds->jetDegree[f];
2166ef0bb6c7SMatthew G. Knepley     const PetscInt   cdim    = T[f]->cdim;
2167ef0bb6c7SMatthew G. Knepley     const PetscInt   Nq      = T[f]->Np;
2168ef0bb6c7SMatthew G. Knepley     const PetscInt   Nbf     = T[f]->Nb;
2169ef0bb6c7SMatthew G. Knepley     const PetscInt   Ncf     = T[f]->Nc;
2170ef0bb6c7SMatthew G. Knepley     const PetscReal *Bq      = &T[f]->T[0][(r * Nq + q) * Nbf * Ncf];
2171ef0bb6c7SMatthew G. Knepley     const PetscReal *Dq      = &T[f]->T[1][(r * Nq + q) * Nbf * Ncf * cdim];
2172f9244615SMatthew G. Knepley     const PetscReal *Hq      = k > 1 ? &T[f]->T[2][(r * Nq + q) * Nbf * Ncf * cdim * cdim] : NULL;
2173f9244615SMatthew G. Knepley     PetscInt         hOffset = 0, b, c, d;
2174a8f1f9e5SMatthew G. Knepley 
21759566063dSJacob Faibussowitsch     PetscCall(PetscDSGetDiscretization(ds, f, (PetscObject *)&fe));
2176a8f1f9e5SMatthew G. Knepley     for (c = 0; c < Ncf; ++c) u[fOffset + c] = 0.0;
2177ef0bb6c7SMatthew G. Knepley     for (d = 0; d < cdim * Ncf; ++d) u_x[fOffset * cdim + d] = 0.0;
2178a8f1f9e5SMatthew G. Knepley     for (b = 0; b < Nbf; ++b) {
2179a8f1f9e5SMatthew G. Knepley       for (c = 0; c < Ncf; ++c) {
2180a8f1f9e5SMatthew G. Knepley         const PetscInt cidx = b * Ncf + c;
2181a8f1f9e5SMatthew G. Knepley 
2182a8f1f9e5SMatthew G. Knepley         u[fOffset + c] += Bq[cidx] * coefficients[dOffset + b];
2183ef0bb6c7SMatthew G. Knepley         for (d = 0; d < cdim; ++d) u_x[(fOffset + c) * cdim + d] += Dq[cidx * cdim + d] * coefficients[dOffset + b];
2184a8f1f9e5SMatthew G. Knepley       }
2185a8f1f9e5SMatthew G. Knepley     }
2186f9244615SMatthew G. Knepley     if (k > 1) {
2187f9244615SMatthew G. Knepley       for (g = 0; g < Nf; ++g) hOffset += T[g]->Nc * cdim;
2188f9244615SMatthew G. Knepley       for (d = 0; d < cdim * cdim * Ncf; ++d) u_x[hOffset + fOffset * cdim * cdim + d] = 0.0;
2189f9244615SMatthew G. Knepley       for (b = 0; b < Nbf; ++b) {
2190f9244615SMatthew G. Knepley         for (c = 0; c < Ncf; ++c) {
2191f9244615SMatthew G. Knepley           const PetscInt cidx = b * Ncf + c;
2192f9244615SMatthew G. Knepley 
2193f9244615SMatthew G. Knepley           for (d = 0; d < cdim * cdim; ++d) u_x[hOffset + (fOffset + c) * cdim * cdim + d] += Hq[cidx * cdim * cdim + d] * coefficients[dOffset + b];
2194f9244615SMatthew G. Knepley         }
2195f9244615SMatthew G. Knepley       }
21969566063dSJacob Faibussowitsch       PetscCall(PetscFEPushforwardHessian(fe, fegeom, 1, &u_x[hOffset + fOffset * cdim * cdim]));
2197f9244615SMatthew G. Knepley     }
21989566063dSJacob Faibussowitsch     PetscCall(PetscFEPushforward(fe, fegeom, 1, &u[fOffset]));
21999566063dSJacob Faibussowitsch     PetscCall(PetscFEPushforwardGradient(fe, fegeom, 1, &u_x[fOffset * cdim]));
2200a8f1f9e5SMatthew G. Knepley     if (u_t) {
2201a8f1f9e5SMatthew G. Knepley       for (c = 0; c < Ncf; ++c) u_t[fOffset + c] = 0.0;
2202a8f1f9e5SMatthew G. Knepley       for (b = 0; b < Nbf; ++b) {
2203a8f1f9e5SMatthew G. Knepley         for (c = 0; c < Ncf; ++c) {
2204a8f1f9e5SMatthew G. Knepley           const PetscInt cidx = b * Ncf + c;
2205a8f1f9e5SMatthew G. Knepley 
2206a8f1f9e5SMatthew G. Knepley           u_t[fOffset + c] += Bq[cidx] * coefficients_t[dOffset + b];
2207a8f1f9e5SMatthew G. Knepley         }
2208a8f1f9e5SMatthew G. Knepley       }
22099566063dSJacob Faibussowitsch       PetscCall(PetscFEPushforward(fe, fegeom, 1, &u_t[fOffset]));
2210a8f1f9e5SMatthew G. Knepley     }
2211a8f1f9e5SMatthew G. Knepley     fOffset += Ncf;
2212a8f1f9e5SMatthew G. Knepley     dOffset += Nbf;
2213a8f1f9e5SMatthew G. Knepley   }
2214a8f1f9e5SMatthew G. Knepley   return 0;
2215a8f1f9e5SMatthew G. Knepley }
2216a8f1f9e5SMatthew G. Knepley 
2217d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEEvaluateFieldJets_Hybrid_Internal(PetscDS ds, PetscInt Nf, PetscInt r, PetscInt q, PetscTabulation T[], PetscFEGeom *fegeom, const PetscScalar coefficients[], const PetscScalar coefficients_t[], PetscScalar u[], PetscScalar u_x[], PetscScalar u_t[])
2218d71ae5a4SJacob Faibussowitsch {
22195fedec97SMatthew G. Knepley   PetscInt dOffset = 0, fOffset = 0, f, g;
222027f02ce8SMatthew G. Knepley 
22215fedec97SMatthew G. Knepley   /* f is the field number in the DS, g is the field number in u[] */
22225fedec97SMatthew G. Knepley   for (f = 0, g = 0; f < Nf; ++f) {
22235fedec97SMatthew G. Knepley     PetscFE          fe  = (PetscFE)ds->disc[f];
22249ee2af8cSMatthew G. Knepley     const PetscInt   dEt = T[f]->cdim;
22259ee2af8cSMatthew G. Knepley     const PetscInt   dE  = fegeom->dimEmbed;
2226665f567fSMatthew G. Knepley     const PetscInt   Nq  = T[f]->Np;
2227665f567fSMatthew G. Knepley     const PetscInt   Nbf = T[f]->Nb;
2228665f567fSMatthew G. Knepley     const PetscInt   Ncf = T[f]->Nc;
2229665f567fSMatthew G. Knepley     const PetscReal *Bq  = &T[f]->T[0][(r * Nq + q) * Nbf * Ncf];
22309ee2af8cSMatthew G. Knepley     const PetscReal *Dq  = &T[f]->T[1][(r * Nq + q) * Nbf * Ncf * dEt];
22315fedec97SMatthew G. Knepley     PetscBool        isCohesive;
22325fedec97SMatthew G. Knepley     PetscInt         Ns, s;
22335fedec97SMatthew G. Knepley 
22345fedec97SMatthew G. Knepley     if (!T[f]) continue;
22359566063dSJacob Faibussowitsch     PetscCall(PetscDSGetCohesive(ds, f, &isCohesive));
22365fedec97SMatthew G. Knepley     Ns = isCohesive ? 1 : 2;
22375fedec97SMatthew G. Knepley     for (s = 0; s < Ns; ++s, ++g) {
223827f02ce8SMatthew G. Knepley       PetscInt b, c, d;
223927f02ce8SMatthew G. Knepley 
224027f02ce8SMatthew G. Knepley       for (c = 0; c < Ncf; ++c) u[fOffset + c] = 0.0;
22419ee2af8cSMatthew G. Knepley       for (d = 0; d < dE * Ncf; ++d) u_x[fOffset * dE + d] = 0.0;
224227f02ce8SMatthew G. Knepley       for (b = 0; b < Nbf; ++b) {
224327f02ce8SMatthew G. Knepley         for (c = 0; c < Ncf; ++c) {
224427f02ce8SMatthew G. Knepley           const PetscInt cidx = b * Ncf + c;
224527f02ce8SMatthew G. Knepley 
224627f02ce8SMatthew G. Knepley           u[fOffset + c] += Bq[cidx] * coefficients[dOffset + b];
22479ee2af8cSMatthew G. Knepley           for (d = 0; d < dEt; ++d) u_x[(fOffset + c) * dE + d] += Dq[cidx * dEt + d] * coefficients[dOffset + b];
224827f02ce8SMatthew G. Knepley         }
224927f02ce8SMatthew G. Knepley       }
22509566063dSJacob Faibussowitsch       PetscCall(PetscFEPushforward(fe, fegeom, 1, &u[fOffset]));
22519566063dSJacob Faibussowitsch       PetscCall(PetscFEPushforwardGradient(fe, fegeom, 1, &u_x[fOffset * dE]));
225227f02ce8SMatthew G. Knepley       if (u_t) {
225327f02ce8SMatthew G. Knepley         for (c = 0; c < Ncf; ++c) u_t[fOffset + c] = 0.0;
225427f02ce8SMatthew G. Knepley         for (b = 0; b < Nbf; ++b) {
225527f02ce8SMatthew G. Knepley           for (c = 0; c < Ncf; ++c) {
225627f02ce8SMatthew G. Knepley             const PetscInt cidx = b * Ncf + c;
225727f02ce8SMatthew G. Knepley 
225827f02ce8SMatthew G. Knepley             u_t[fOffset + c] += Bq[cidx] * coefficients_t[dOffset + b];
225927f02ce8SMatthew G. Knepley           }
226027f02ce8SMatthew G. Knepley         }
22619566063dSJacob Faibussowitsch         PetscCall(PetscFEPushforward(fe, fegeom, 1, &u_t[fOffset]));
226227f02ce8SMatthew G. Knepley       }
226327f02ce8SMatthew G. Knepley       fOffset += Ncf;
226427f02ce8SMatthew G. Knepley       dOffset += Nbf;
226527f02ce8SMatthew G. Knepley     }
2266665f567fSMatthew G. Knepley   }
226727f02ce8SMatthew G. Knepley   return 0;
226827f02ce8SMatthew G. Knepley }
226927f02ce8SMatthew G. Knepley 
2270d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEEvaluateFaceFields_Internal(PetscDS prob, PetscInt field, PetscInt faceLoc, const PetscScalar coefficients[], PetscScalar u[])
2271d71ae5a4SJacob Faibussowitsch {
2272a8f1f9e5SMatthew G. Knepley   PetscFE         fe;
2273ef0bb6c7SMatthew G. Knepley   PetscTabulation Tc;
2274ef0bb6c7SMatthew G. Knepley   PetscInt        b, c;
2275a8f1f9e5SMatthew G. Knepley 
2276a8f1f9e5SMatthew G. Knepley   if (!prob) return 0;
22779566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(prob, field, (PetscObject *)&fe));
22789566063dSJacob Faibussowitsch   PetscCall(PetscFEGetFaceCentroidTabulation(fe, &Tc));
2279ef0bb6c7SMatthew G. Knepley   {
2280ef0bb6c7SMatthew G. Knepley     const PetscReal *faceBasis = Tc->T[0];
2281ef0bb6c7SMatthew G. Knepley     const PetscInt   Nb        = Tc->Nb;
2282ef0bb6c7SMatthew G. Knepley     const PetscInt   Nc        = Tc->Nc;
2283ef0bb6c7SMatthew G. Knepley 
2284ad540459SPierre Jolivet     for (c = 0; c < Nc; ++c) u[c] = 0.0;
2285a8f1f9e5SMatthew G. Knepley     for (b = 0; b < Nb; ++b) {
2286ad540459SPierre Jolivet       for (c = 0; c < Nc; ++c) u[c] += coefficients[b] * faceBasis[(faceLoc * Nb + b) * Nc + c];
2287a8f1f9e5SMatthew G. Knepley     }
2288ef0bb6c7SMatthew G. Knepley   }
2289a8f1f9e5SMatthew G. Knepley   return 0;
2290a8f1f9e5SMatthew G. Knepley }
2291a8f1f9e5SMatthew G. Knepley 
2292d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEUpdateElementVec_Internal(PetscFE fe, PetscTabulation T, PetscInt r, PetscScalar tmpBasis[], PetscScalar tmpBasisDer[], PetscInt e, PetscFEGeom *fegeom, PetscScalar f0[], PetscScalar f1[], PetscScalar elemVec[])
2293d71ae5a4SJacob Faibussowitsch {
22946587ee25SMatthew G. Knepley   PetscFEGeom      pgeom;
2295bc3a64adSMatthew G. Knepley   const PetscInt   dEt      = T->cdim;
2296bc3a64adSMatthew G. Knepley   const PetscInt   dE       = fegeom->dimEmbed;
2297ef0bb6c7SMatthew G. Knepley   const PetscInt   Nq       = T->Np;
2298ef0bb6c7SMatthew G. Knepley   const PetscInt   Nb       = T->Nb;
2299ef0bb6c7SMatthew G. Knepley   const PetscInt   Nc       = T->Nc;
2300ef0bb6c7SMatthew G. Knepley   const PetscReal *basis    = &T->T[0][r * Nq * Nb * Nc];
2301bc3a64adSMatthew G. Knepley   const PetscReal *basisDer = &T->T[1][r * Nq * Nb * Nc * dEt];
2302a8f1f9e5SMatthew G. Knepley   PetscInt         q, b, c, d;
2303a8f1f9e5SMatthew G. Knepley 
2304a8f1f9e5SMatthew G. Knepley   for (q = 0; q < Nq; ++q) {
2305a8f1f9e5SMatthew G. Knepley     for (b = 0; b < Nb; ++b) {
2306a8f1f9e5SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
2307a8f1f9e5SMatthew G. Knepley         const PetscInt bcidx = b * Nc + c;
2308a8f1f9e5SMatthew G. Knepley 
2309a8f1f9e5SMatthew G. Knepley         tmpBasis[bcidx] = basis[q * Nb * Nc + bcidx];
2310bc3a64adSMatthew G. Knepley         for (d = 0; d < dEt; ++d) tmpBasisDer[bcidx * dE + d] = basisDer[q * Nb * Nc * dEt + bcidx * dEt + d];
23119ee2af8cSMatthew G. Knepley         for (d = dEt; d < dE; ++d) tmpBasisDer[bcidx * dE + d] = 0.0;
2312a8f1f9e5SMatthew G. Knepley       }
2313a8f1f9e5SMatthew G. Knepley     }
23149566063dSJacob Faibussowitsch     PetscCall(PetscFEGeomGetCellPoint(fegeom, e, q, &pgeom));
23159566063dSJacob Faibussowitsch     PetscCall(PetscFEPushforward(fe, &pgeom, Nb, tmpBasis));
23169566063dSJacob Faibussowitsch     PetscCall(PetscFEPushforwardGradient(fe, &pgeom, Nb, tmpBasisDer));
2317a8f1f9e5SMatthew G. Knepley     for (b = 0; b < Nb; ++b) {
2318a8f1f9e5SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
2319a8f1f9e5SMatthew G. Knepley         const PetscInt bcidx = b * Nc + c;
2320a8f1f9e5SMatthew G. Knepley         const PetscInt qcidx = q * Nc + c;
2321a8f1f9e5SMatthew G. Knepley 
2322a8f1f9e5SMatthew G. Knepley         elemVec[b] += tmpBasis[bcidx] * f0[qcidx];
232327f02ce8SMatthew G. Knepley         for (d = 0; d < dE; ++d) elemVec[b] += tmpBasisDer[bcidx * dE + d] * f1[qcidx * dE + d];
232427f02ce8SMatthew G. Knepley       }
232527f02ce8SMatthew G. Knepley     }
232627f02ce8SMatthew G. Knepley   }
232727f02ce8SMatthew G. Knepley   return (0);
232827f02ce8SMatthew G. Knepley }
232927f02ce8SMatthew G. Knepley 
2330d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEUpdateElementVec_Hybrid_Internal(PetscFE fe, PetscTabulation T, PetscInt r, PetscInt s, PetscScalar tmpBasis[], PetscScalar tmpBasisDer[], PetscFEGeom *fegeom, PetscScalar f0[], PetscScalar f1[], PetscScalar elemVec[])
2331d71ae5a4SJacob Faibussowitsch {
233227f02ce8SMatthew G. Knepley   const PetscInt   dE       = T->cdim;
233327f02ce8SMatthew G. Knepley   const PetscInt   Nq       = T->Np;
233427f02ce8SMatthew G. Knepley   const PetscInt   Nb       = T->Nb;
233527f02ce8SMatthew G. Knepley   const PetscInt   Nc       = T->Nc;
233627f02ce8SMatthew G. Knepley   const PetscReal *basis    = &T->T[0][r * Nq * Nb * Nc];
233727f02ce8SMatthew G. Knepley   const PetscReal *basisDer = &T->T[1][r * Nq * Nb * Nc * dE];
2338c2b7495fSMatthew G. Knepley   PetscInt         q, b, c, d;
233927f02ce8SMatthew G. Knepley 
234027f02ce8SMatthew G. Knepley   for (q = 0; q < Nq; ++q) {
234127f02ce8SMatthew G. Knepley     for (b = 0; b < Nb; ++b) {
234227f02ce8SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
234327f02ce8SMatthew G. Knepley         const PetscInt bcidx = b * Nc + c;
234427f02ce8SMatthew G. Knepley 
234527f02ce8SMatthew G. Knepley         tmpBasis[bcidx] = basis[q * Nb * Nc + bcidx];
234627f02ce8SMatthew G. Knepley         for (d = 0; d < dE; ++d) tmpBasisDer[bcidx * dE + d] = basisDer[q * Nb * Nc * dE + bcidx * dE + d];
234727f02ce8SMatthew G. Knepley       }
234827f02ce8SMatthew G. Knepley     }
23499566063dSJacob Faibussowitsch     PetscCall(PetscFEPushforward(fe, fegeom, Nb, tmpBasis));
23509566063dSJacob Faibussowitsch     PetscCall(PetscFEPushforwardGradient(fe, fegeom, Nb, tmpBasisDer));
235127f02ce8SMatthew G. Knepley     for (b = 0; b < Nb; ++b) {
235227f02ce8SMatthew G. Knepley       for (c = 0; c < Nc; ++c) {
235327f02ce8SMatthew G. Knepley         const PetscInt bcidx = b * Nc + c;
2354c2b7495fSMatthew G. Knepley         const PetscInt qcidx = q * Nc + c;
235527f02ce8SMatthew G. Knepley 
235627f02ce8SMatthew G. Knepley         elemVec[Nb * s + b] += tmpBasis[bcidx] * f0[qcidx];
235727f02ce8SMatthew G. Knepley         for (d = 0; d < dE; ++d) elemVec[Nb * s + b] += tmpBasisDer[bcidx * dE + d] * f1[qcidx * dE + d];
235827f02ce8SMatthew G. Knepley       }
2359a8f1f9e5SMatthew G. Knepley     }
2360a8f1f9e5SMatthew G. Knepley   }
2361a8f1f9e5SMatthew G. Knepley   return (0);
2362a8f1f9e5SMatthew G. Knepley }
2363a8f1f9e5SMatthew G. Knepley 
2364d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEUpdateElementMat_Internal(PetscFE feI, PetscFE feJ, PetscInt r, PetscInt q, PetscTabulation TI, PetscScalar tmpBasisI[], PetscScalar tmpBasisDerI[], PetscTabulation TJ, PetscScalar tmpBasisJ[], PetscScalar tmpBasisDerJ[], PetscFEGeom *fegeom, const PetscScalar g0[], const PetscScalar g1[], const PetscScalar g2[], const PetscScalar g3[], PetscInt eOffset, PetscInt totDim, PetscInt offsetI, PetscInt offsetJ, PetscScalar elemMat[])
2365d71ae5a4SJacob Faibussowitsch {
236627f02ce8SMatthew G. Knepley   const PetscInt   dE        = TI->cdim;
2367ef0bb6c7SMatthew G. Knepley   const PetscInt   NqI       = TI->Np;
2368ef0bb6c7SMatthew G. Knepley   const PetscInt   NbI       = TI->Nb;
2369ef0bb6c7SMatthew G. Knepley   const PetscInt   NcI       = TI->Nc;
2370ef0bb6c7SMatthew G. Knepley   const PetscReal *basisI    = &TI->T[0][(r * NqI + q) * NbI * NcI];
2371665f567fSMatthew G. Knepley   const PetscReal *basisDerI = &TI->T[1][(r * NqI + q) * NbI * NcI * dE];
2372ef0bb6c7SMatthew G. Knepley   const PetscInt   NqJ       = TJ->Np;
2373ef0bb6c7SMatthew G. Knepley   const PetscInt   NbJ       = TJ->Nb;
2374ef0bb6c7SMatthew G. Knepley   const PetscInt   NcJ       = TJ->Nc;
2375ef0bb6c7SMatthew G. Knepley   const PetscReal *basisJ    = &TJ->T[0][(r * NqJ + q) * NbJ * NcJ];
2376665f567fSMatthew G. Knepley   const PetscReal *basisDerJ = &TJ->T[1][(r * NqJ + q) * NbJ * NcJ * dE];
2377a8f1f9e5SMatthew G. Knepley   PetscInt         f, fc, g, gc, df, dg;
2378a8f1f9e5SMatthew G. Knepley 
2379a8f1f9e5SMatthew G. Knepley   for (f = 0; f < NbI; ++f) {
2380a8f1f9e5SMatthew G. Knepley     for (fc = 0; fc < NcI; ++fc) {
2381a8f1f9e5SMatthew G. Knepley       const PetscInt fidx = f * NcI + fc; /* Test function basis index */
2382a8f1f9e5SMatthew G. Knepley 
2383a8f1f9e5SMatthew G. Knepley       tmpBasisI[fidx] = basisI[fidx];
238427f02ce8SMatthew G. Knepley       for (df = 0; df < dE; ++df) tmpBasisDerI[fidx * dE + df] = basisDerI[fidx * dE + df];
2385a8f1f9e5SMatthew G. Knepley     }
2386a8f1f9e5SMatthew G. Knepley   }
23879566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforward(feI, fegeom, NbI, tmpBasisI));
23889566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforwardGradient(feI, fegeom, NbI, tmpBasisDerI));
2389a8f1f9e5SMatthew G. Knepley   for (g = 0; g < NbJ; ++g) {
2390a8f1f9e5SMatthew G. Knepley     for (gc = 0; gc < NcJ; ++gc) {
2391a8f1f9e5SMatthew G. Knepley       const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */
2392a8f1f9e5SMatthew G. Knepley 
2393a8f1f9e5SMatthew G. Knepley       tmpBasisJ[gidx] = basisJ[gidx];
239427f02ce8SMatthew G. Knepley       for (dg = 0; dg < dE; ++dg) tmpBasisDerJ[gidx * dE + dg] = basisDerJ[gidx * dE + dg];
2395a8f1f9e5SMatthew G. Knepley     }
2396a8f1f9e5SMatthew G. Knepley   }
23979566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforward(feJ, fegeom, NbJ, tmpBasisJ));
23989566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforwardGradient(feJ, fegeom, NbJ, tmpBasisDerJ));
2399a8f1f9e5SMatthew G. Knepley   for (f = 0; f < NbI; ++f) {
2400a8f1f9e5SMatthew G. Knepley     for (fc = 0; fc < NcI; ++fc) {
2401a8f1f9e5SMatthew G. Knepley       const PetscInt fidx = f * NcI + fc; /* Test function basis index */
2402a8f1f9e5SMatthew G. Knepley       const PetscInt i    = offsetI + f;  /* Element matrix row */
2403a8f1f9e5SMatthew G. Knepley       for (g = 0; g < NbJ; ++g) {
2404a8f1f9e5SMatthew G. Knepley         for (gc = 0; gc < NcJ; ++gc) {
2405a8f1f9e5SMatthew G. Knepley           const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */
2406a8f1f9e5SMatthew G. Knepley           const PetscInt j    = offsetJ + g;  /* Element matrix column */
2407a8f1f9e5SMatthew G. Knepley           const PetscInt fOff = eOffset + i * totDim + j;
2408a8f1f9e5SMatthew G. Knepley 
2409a8f1f9e5SMatthew G. Knepley           elemMat[fOff] += tmpBasisI[fidx] * g0[fc * NcJ + gc] * tmpBasisJ[gidx];
241027f02ce8SMatthew G. Knepley           for (df = 0; df < dE; ++df) {
241127f02ce8SMatthew G. Knepley             elemMat[fOff] += tmpBasisI[fidx] * g1[(fc * NcJ + gc) * dE + df] * tmpBasisDerJ[gidx * dE + df];
241227f02ce8SMatthew G. Knepley             elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g2[(fc * NcJ + gc) * dE + df] * tmpBasisJ[gidx];
2413ad540459SPierre Jolivet             for (dg = 0; dg < dE; ++dg) elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g3[((fc * NcJ + gc) * dE + df) * dE + dg] * tmpBasisDerJ[gidx * dE + dg];
241427f02ce8SMatthew G. Knepley           }
241527f02ce8SMatthew G. Knepley         }
241627f02ce8SMatthew G. Knepley       }
241727f02ce8SMatthew G. Knepley     }
241827f02ce8SMatthew G. Knepley   }
241927f02ce8SMatthew G. Knepley   return (0);
242027f02ce8SMatthew G. Knepley }
242127f02ce8SMatthew G. Knepley 
2422d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEUpdateElementMat_Hybrid_Internal(PetscFE feI, PetscBool isHybridI, PetscFE feJ, PetscBool isHybridJ, PetscInt r, PetscInt s, PetscInt q, PetscTabulation TI, PetscScalar tmpBasisI[], PetscScalar tmpBasisDerI[], PetscTabulation TJ, PetscScalar tmpBasisJ[], PetscScalar tmpBasisDerJ[], PetscFEGeom *fegeom, const PetscScalar g0[], const PetscScalar g1[], const PetscScalar g2[], const PetscScalar g3[], PetscInt eOffset, PetscInt totDim, PetscInt offsetI, PetscInt offsetJ, PetscScalar elemMat[])
2423d71ae5a4SJacob Faibussowitsch {
2424665f567fSMatthew G. Knepley   const PetscInt   dE        = TI->cdim;
2425665f567fSMatthew G. Knepley   const PetscInt   NqI       = TI->Np;
2426665f567fSMatthew G. Knepley   const PetscInt   NbI       = TI->Nb;
2427665f567fSMatthew G. Knepley   const PetscInt   NcI       = TI->Nc;
2428665f567fSMatthew G. Knepley   const PetscReal *basisI    = &TI->T[0][(r * NqI + q) * NbI * NcI];
2429665f567fSMatthew G. Knepley   const PetscReal *basisDerI = &TI->T[1][(r * NqI + q) * NbI * NcI * dE];
2430665f567fSMatthew G. Knepley   const PetscInt   NqJ       = TJ->Np;
2431665f567fSMatthew G. Knepley   const PetscInt   NbJ       = TJ->Nb;
2432665f567fSMatthew G. Knepley   const PetscInt   NcJ       = TJ->Nc;
2433665f567fSMatthew G. Knepley   const PetscReal *basisJ    = &TJ->T[0][(r * NqJ + q) * NbJ * NcJ];
2434665f567fSMatthew G. Knepley   const PetscReal *basisDerJ = &TJ->T[1][(r * NqJ + q) * NbJ * NcJ * dE];
24355fedec97SMatthew G. Knepley   const PetscInt   so        = isHybridI ? 0 : s;
24365fedec97SMatthew G. Knepley   const PetscInt   to        = isHybridJ ? 0 : s;
24375fedec97SMatthew G. Knepley   PetscInt         f, fc, g, gc, df, dg;
243827f02ce8SMatthew G. Knepley 
243927f02ce8SMatthew G. Knepley   for (f = 0; f < NbI; ++f) {
244027f02ce8SMatthew G. Knepley     for (fc = 0; fc < NcI; ++fc) {
244127f02ce8SMatthew G. Knepley       const PetscInt fidx = f * NcI + fc; /* Test function basis index */
244227f02ce8SMatthew G. Knepley 
244327f02ce8SMatthew G. Knepley       tmpBasisI[fidx] = basisI[fidx];
2444665f567fSMatthew G. Knepley       for (df = 0; df < dE; ++df) tmpBasisDerI[fidx * dE + df] = basisDerI[fidx * dE + df];
244527f02ce8SMatthew G. Knepley     }
244627f02ce8SMatthew G. Knepley   }
24479566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforward(feI, fegeom, NbI, tmpBasisI));
24489566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforwardGradient(feI, fegeom, NbI, tmpBasisDerI));
244927f02ce8SMatthew G. Knepley   for (g = 0; g < NbJ; ++g) {
245027f02ce8SMatthew G. Knepley     for (gc = 0; gc < NcJ; ++gc) {
245127f02ce8SMatthew G. Knepley       const PetscInt gidx = g * NcJ + gc; /* Trial function basis index */
245227f02ce8SMatthew G. Knepley 
245327f02ce8SMatthew G. Knepley       tmpBasisJ[gidx] = basisJ[gidx];
2454665f567fSMatthew G. Knepley       for (dg = 0; dg < dE; ++dg) tmpBasisDerJ[gidx * dE + dg] = basisDerJ[gidx * dE + dg];
245527f02ce8SMatthew G. Knepley     }
245627f02ce8SMatthew G. Knepley   }
24579566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforward(feJ, fegeom, NbJ, tmpBasisJ));
24589566063dSJacob Faibussowitsch   PetscCall(PetscFEPushforwardGradient(feJ, fegeom, NbJ, tmpBasisDerJ));
245927f02ce8SMatthew G. Knepley   for (f = 0; f < NbI; ++f) {
246027f02ce8SMatthew G. Knepley     for (fc = 0; fc < NcI; ++fc) {
246127f02ce8SMatthew G. Knepley       const PetscInt fidx = f * NcI + fc;           /* Test function basis index */
24625fedec97SMatthew G. Knepley       const PetscInt i    = offsetI + NbI * so + f; /* Element matrix row */
246327f02ce8SMatthew G. Knepley       for (g = 0; g < NbJ; ++g) {
246427f02ce8SMatthew G. Knepley         for (gc = 0; gc < NcJ; ++gc) {
246527f02ce8SMatthew G. Knepley           const PetscInt gidx = g * NcJ + gc;           /* Trial function basis index */
24665fedec97SMatthew G. Knepley           const PetscInt j    = offsetJ + NbJ * to + g; /* Element matrix column */
246727f02ce8SMatthew G. Knepley           const PetscInt fOff = eOffset + i * totDim + j;
246827f02ce8SMatthew G. Knepley 
24695fedec97SMatthew G. Knepley           elemMat[fOff] += tmpBasisI[fidx] * g0[fc * NcJ + gc] * tmpBasisJ[gidx];
247027f02ce8SMatthew G. Knepley           for (df = 0; df < dE; ++df) {
24715fedec97SMatthew G. Knepley             elemMat[fOff] += tmpBasisI[fidx] * g1[(fc * NcJ + gc) * dE + df] * tmpBasisDerJ[gidx * dE + df];
24725fedec97SMatthew G. Knepley             elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g2[(fc * NcJ + gc) * dE + df] * tmpBasisJ[gidx];
2473ad540459SPierre Jolivet             for (dg = 0; dg < dE; ++dg) elemMat[fOff] += tmpBasisDerI[fidx * dE + df] * g3[((fc * NcJ + gc) * dE + df) * dE + dg] * tmpBasisDerJ[gidx * dE + dg];
2474a8f1f9e5SMatthew G. Knepley           }
2475a8f1f9e5SMatthew G. Knepley         }
2476a8f1f9e5SMatthew G. Knepley       }
2477a8f1f9e5SMatthew G. Knepley     }
2478a8f1f9e5SMatthew G. Knepley   }
2479a8f1f9e5SMatthew G. Knepley   return (0);
2480a8f1f9e5SMatthew G. Knepley }
2481c9ba7969SMatthew G. Knepley 
2482d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFECreateCellGeometry(PetscFE fe, PetscQuadrature quad, PetscFEGeom *cgeom)
2483d71ae5a4SJacob Faibussowitsch {
2484c9ba7969SMatthew G. Knepley   PetscDualSpace  dsp;
2485c9ba7969SMatthew G. Knepley   DM              dm;
2486c9ba7969SMatthew G. Knepley   PetscQuadrature quadDef;
2487c9ba7969SMatthew G. Knepley   PetscInt        dim, cdim, Nq;
2488c9ba7969SMatthew G. Knepley 
2489c9ba7969SMatthew G. Knepley   PetscFunctionBegin;
24909566063dSJacob Faibussowitsch   PetscCall(PetscFEGetDualSpace(fe, &dsp));
24919566063dSJacob Faibussowitsch   PetscCall(PetscDualSpaceGetDM(dsp, &dm));
24929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
24949566063dSJacob Faibussowitsch   PetscCall(PetscFEGetQuadrature(fe, &quadDef));
2495c9ba7969SMatthew G. Knepley   quad = quad ? quad : quadDef;
24969566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL));
24979566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nq * cdim, &cgeom->v));
24989566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nq * cdim * cdim, &cgeom->J));
24999566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nq * cdim * cdim, &cgeom->invJ));
25009566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nq, &cgeom->detJ));
2501c9ba7969SMatthew G. Knepley   cgeom->dim       = dim;
2502c9ba7969SMatthew G. Knepley   cgeom->dimEmbed  = cdim;
2503c9ba7969SMatthew G. Knepley   cgeom->numCells  = 1;
2504c9ba7969SMatthew G. Knepley   cgeom->numPoints = Nq;
25059566063dSJacob Faibussowitsch   PetscCall(DMPlexComputeCellGeometryFEM(dm, 0, quad, cgeom->v, cgeom->J, cgeom->invJ, cgeom->detJ));
2506c9ba7969SMatthew G. Knepley   PetscFunctionReturn(0);
2507c9ba7969SMatthew G. Knepley }
2508c9ba7969SMatthew G. Knepley 
2509d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFEDestroyCellGeometry(PetscFE fe, PetscFEGeom *cgeom)
2510d71ae5a4SJacob Faibussowitsch {
2511c9ba7969SMatthew G. Knepley   PetscFunctionBegin;
25129566063dSJacob Faibussowitsch   PetscCall(PetscFree(cgeom->v));
25139566063dSJacob Faibussowitsch   PetscCall(PetscFree(cgeom->J));
25149566063dSJacob Faibussowitsch   PetscCall(PetscFree(cgeom->invJ));
25159566063dSJacob Faibussowitsch   PetscCall(PetscFree(cgeom->detJ));
2516c9ba7969SMatthew G. Knepley   PetscFunctionReturn(0);
2517c9ba7969SMatthew G. Knepley }
2518