xref: /petsc/src/dm/dt/interface/dt.c (revision 4e8208cbcbc709572b8abe32f33c78b69c819375)
137045ce4SJed Brown /* Discretization tools */
237045ce4SJed Brown 
30c35b76eSJed Brown #include <petscdt.h> /*I "petscdt.h" I*/
437045ce4SJed Brown #include <petscblaslapack.h>
5af0996ceSBarry Smith #include <petsc/private/petscimpl.h>
6af0996ceSBarry Smith #include <petsc/private/dtimpl.h>
707218a29SMatthew G. Knepley #include <petsc/private/petscfeimpl.h> /* For CoordinatesRefToReal() */
8665c2dedSJed Brown #include <petscviewer.h>
959804f93SMatthew G. Knepley #include <petscdmplex.h>
1059804f93SMatthew G. Knepley #include <petscdmshell.h>
1137045ce4SJed Brown 
1298c04793SMatthew G. Knepley #if defined(PETSC_HAVE_MPFR)
1398c04793SMatthew G. Knepley   #include <mpfr.h>
1498c04793SMatthew G. Knepley #endif
1598c04793SMatthew G. Knepley 
16f2c64c88SMatthew G. Knepley const char *const        PetscDTNodeTypes_shifted[] = {"default", "gaussjacobi", "equispaced", "tanhsinh", "PetscDTNodeType", "PETSCDTNODES_", NULL};
17d3c69ad0SToby Isaac const char *const *const PetscDTNodeTypes           = PetscDTNodeTypes_shifted + 1;
18d3c69ad0SToby Isaac 
19f2c64c88SMatthew G. Knepley const char *const        PetscDTSimplexQuadratureTypes_shifted[] = {"default", "conic", "minsym", "diagsym", "PetscDTSimplexQuadratureType", "PETSCDTSIMPLEXQUAD_", NULL};
20d3c69ad0SToby Isaac const char *const *const PetscDTSimplexQuadratureTypes           = PetscDTSimplexQuadratureTypes_shifted + 1;
21d4afb720SToby Isaac 
22e6a796c3SToby Isaac static PetscBool GolubWelschCite       = PETSC_FALSE;
23e6a796c3SToby Isaac const char       GolubWelschCitation[] = "@article{GolubWelsch1969,\n"
240bfcf5a5SMatthew G. Knepley                                          "  author  = {Golub and Welsch},\n"
250bfcf5a5SMatthew G. Knepley                                          "  title   = {Calculation of Quadrature Rules},\n"
260bfcf5a5SMatthew G. Knepley                                          "  journal = {Math. Comp.},\n"
270bfcf5a5SMatthew G. Knepley                                          "  volume  = {23},\n"
280bfcf5a5SMatthew G. Knepley                                          "  number  = {106},\n"
290bfcf5a5SMatthew G. Knepley                                          "  pages   = {221--230},\n"
300bfcf5a5SMatthew G. Knepley                                          "  year    = {1969}\n}\n";
310bfcf5a5SMatthew G. Knepley 
32c4762a1bSJed Brown /* Numerical tests in src/dm/dt/tests/ex1.c show that when computing the nodes and weights of Gauss-Jacobi
3394e21283SToby Isaac    quadrature rules:
34e6a796c3SToby Isaac 
3594e21283SToby Isaac    - in double precision, Newton's method and Golub & Welsch both work for moderate degrees (< 100),
3694e21283SToby Isaac    - in single precision, Newton's method starts producing incorrect roots around n = 15, but
3794e21283SToby Isaac      the weights from Golub & Welsch become a problem before then: they produces errors
3894e21283SToby Isaac      in computing the Jacobi-polynomial Gram matrix around n = 6.
3994e21283SToby Isaac 
4094e21283SToby Isaac    So we default to Newton's method (required fewer dependencies) */
4194e21283SToby Isaac PetscBool PetscDTGaussQuadratureNewton_Internal = PETSC_TRUE;
422cd22861SMatthew G. Knepley 
432cd22861SMatthew G. Knepley PetscClassId PETSCQUADRATURE_CLASSID = 0;
442cd22861SMatthew G. Knepley 
4540d8ff71SMatthew G. Knepley /*@
46dce8aebaSBarry Smith   PetscQuadratureCreate - Create a `PetscQuadrature` object
4740d8ff71SMatthew G. Knepley 
48d083f849SBarry Smith   Collective
4940d8ff71SMatthew G. Knepley 
5040d8ff71SMatthew G. Knepley   Input Parameter:
51dce8aebaSBarry Smith . comm - The communicator for the `PetscQuadrature` object
5240d8ff71SMatthew G. Knepley 
5340d8ff71SMatthew G. Knepley   Output Parameter:
5420f4b53cSBarry Smith . q - The `PetscQuadrature` object
5540d8ff71SMatthew G. Knepley 
5640d8ff71SMatthew G. Knepley   Level: beginner
5740d8ff71SMatthew G. Knepley 
58dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `Petscquadraturedestroy()`, `PetscQuadratureGetData()`
5940d8ff71SMatthew G. Knepley @*/
PetscQuadratureCreate(MPI_Comm comm,PetscQuadrature * q)60d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q)
61d71ae5a4SJacob Faibussowitsch {
6221454ff5SMatthew G. Knepley   PetscFunctionBegin;
634f572ea9SToby Isaac   PetscAssertPointer(q, 2);
649566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
659566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*q, PETSCQUADRATURE_CLASSID, "PetscQuadrature", "Quadrature", "DT", comm, PetscQuadratureDestroy, PetscQuadratureView));
664366bac7SMatthew G. Knepley   (*q)->ct        = DM_POLYTOPE_UNKNOWN;
6721454ff5SMatthew G. Knepley   (*q)->dim       = -1;
68a6b92713SMatthew G. Knepley   (*q)->Nc        = 1;
69bcede257SMatthew G. Knepley   (*q)->order     = -1;
7021454ff5SMatthew G. Knepley   (*q)->numPoints = 0;
7121454ff5SMatthew G. Knepley   (*q)->points    = NULL;
7221454ff5SMatthew G. Knepley   (*q)->weights   = NULL;
733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7421454ff5SMatthew G. Knepley }
7521454ff5SMatthew G. Knepley 
76c9638911SMatthew G. Knepley /*@
77dce8aebaSBarry Smith   PetscQuadratureDuplicate - Create a deep copy of the `PetscQuadrature` object
78c9638911SMatthew G. Knepley 
7920f4b53cSBarry Smith   Collective
80c9638911SMatthew G. Knepley 
81c9638911SMatthew G. Knepley   Input Parameter:
82dce8aebaSBarry Smith . q - The `PetscQuadrature` object
83c9638911SMatthew G. Knepley 
84c9638911SMatthew G. Knepley   Output Parameter:
85dce8aebaSBarry Smith . r - The new `PetscQuadrature` object
86c9638911SMatthew G. Knepley 
87c9638911SMatthew G. Knepley   Level: beginner
88c9638911SMatthew G. Knepley 
89dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureDestroy()`, `PetscQuadratureGetData()`
90c9638911SMatthew G. Knepley @*/
PetscQuadratureDuplicate(PetscQuadrature q,PetscQuadrature * r)91d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r)
92d71ae5a4SJacob Faibussowitsch {
934366bac7SMatthew G. Knepley   DMPolytopeType   ct;
94a6b92713SMatthew G. Knepley   PetscInt         order, dim, Nc, Nq;
95c9638911SMatthew G. Knepley   const PetscReal *points, *weights;
96c9638911SMatthew G. Knepley   PetscReal       *p, *w;
97c9638911SMatthew G. Knepley 
98c9638911SMatthew G. Knepley   PetscFunctionBegin;
994f572ea9SToby Isaac   PetscAssertPointer(q, 1);
1009566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject)q), r));
1014366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureGetCellType(q, &ct));
1024366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureSetCellType(*r, ct));
1039566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetOrder(q, &order));
1049566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetOrder(*r, order));
1059566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights));
1069566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nq * dim, &p));
1079566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nq * Nc, &w));
1089566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(p, points, Nq * dim));
1099566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(w, weights, Nc * Nq));
1109566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*r, dim, Nc, Nq, p, w));
1113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
112c9638911SMatthew G. Knepley }
113c9638911SMatthew G. Knepley 
11440d8ff71SMatthew G. Knepley /*@
115dce8aebaSBarry Smith   PetscQuadratureDestroy - Destroys a `PetscQuadrature` object
11640d8ff71SMatthew G. Knepley 
11720f4b53cSBarry Smith   Collective
11840d8ff71SMatthew G. Knepley 
11940d8ff71SMatthew G. Knepley   Input Parameter:
120dce8aebaSBarry Smith . q - The `PetscQuadrature` object
12140d8ff71SMatthew G. Knepley 
12240d8ff71SMatthew G. Knepley   Level: beginner
12340d8ff71SMatthew G. Knepley 
124dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureGetData()`
12540d8ff71SMatthew G. Knepley @*/
PetscQuadratureDestroy(PetscQuadrature * q)126d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q)
127d71ae5a4SJacob Faibussowitsch {
128bfa639d9SMatthew G. Knepley   PetscFunctionBegin;
1293ba16761SJacob Faibussowitsch   if (!*q) PetscFunctionReturn(PETSC_SUCCESS);
130f4f49eeaSPierre Jolivet   PetscValidHeaderSpecific(*q, PETSCQUADRATURE_CLASSID, 1);
131f4f49eeaSPierre Jolivet   if (--((PetscObject)*q)->refct > 0) {
13221454ff5SMatthew G. Knepley     *q = NULL;
1333ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
13421454ff5SMatthew G. Knepley   }
1359566063dSJacob Faibussowitsch   PetscCall(PetscFree((*q)->points));
1369566063dSJacob Faibussowitsch   PetscCall(PetscFree((*q)->weights));
1379566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(q));
1383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13921454ff5SMatthew G. Knepley }
14021454ff5SMatthew G. Knepley 
141bcede257SMatthew G. Knepley /*@
1424366bac7SMatthew G. Knepley   PetscQuadratureGetCellType - Return the cell type of the integration domain
1434366bac7SMatthew G. Knepley 
1444366bac7SMatthew G. Knepley   Not Collective
1454366bac7SMatthew G. Knepley 
1464366bac7SMatthew G. Knepley   Input Parameter:
1474366bac7SMatthew G. Knepley . q - The `PetscQuadrature` object
1484366bac7SMatthew G. Knepley 
1494366bac7SMatthew G. Knepley   Output Parameter:
1504366bac7SMatthew G. Knepley . ct - The cell type of the integration domain
1514366bac7SMatthew G. Knepley 
1524366bac7SMatthew G. Knepley   Level: intermediate
1534366bac7SMatthew G. Knepley 
1544366bac7SMatthew G. Knepley .seealso: `PetscQuadrature`, `PetscQuadratureSetCellType()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
1554366bac7SMatthew G. Knepley @*/
PetscQuadratureGetCellType(PetscQuadrature q,DMPolytopeType * ct)1564366bac7SMatthew G. Knepley PetscErrorCode PetscQuadratureGetCellType(PetscQuadrature q, DMPolytopeType *ct)
1574366bac7SMatthew G. Knepley {
1584366bac7SMatthew G. Knepley   PetscFunctionBegin;
1594366bac7SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
1604f572ea9SToby Isaac   PetscAssertPointer(ct, 2);
1614366bac7SMatthew G. Knepley   *ct = q->ct;
1624366bac7SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
1634366bac7SMatthew G. Knepley }
1644366bac7SMatthew G. Knepley 
1654366bac7SMatthew G. Knepley /*@
1664366bac7SMatthew G. Knepley   PetscQuadratureSetCellType - Set the cell type of the integration domain
1674366bac7SMatthew G. Knepley 
1684366bac7SMatthew G. Knepley   Not Collective
1694366bac7SMatthew G. Knepley 
1704366bac7SMatthew G. Knepley   Input Parameters:
1714366bac7SMatthew G. Knepley + q  - The `PetscQuadrature` object
1724366bac7SMatthew G. Knepley - ct - The cell type of the integration domain
1734366bac7SMatthew G. Knepley 
1744366bac7SMatthew G. Knepley   Level: intermediate
1754366bac7SMatthew G. Knepley 
1764366bac7SMatthew G. Knepley .seealso: `PetscQuadrature`, `PetscQuadratureGetCellType()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
1774366bac7SMatthew G. Knepley @*/
PetscQuadratureSetCellType(PetscQuadrature q,DMPolytopeType ct)1784366bac7SMatthew G. Knepley PetscErrorCode PetscQuadratureSetCellType(PetscQuadrature q, DMPolytopeType ct)
1794366bac7SMatthew G. Knepley {
1804366bac7SMatthew G. Knepley   PetscFunctionBegin;
1814366bac7SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
1824366bac7SMatthew G. Knepley   q->ct = ct;
1834366bac7SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
1844366bac7SMatthew G. Knepley }
1854366bac7SMatthew G. Knepley 
1864366bac7SMatthew G. Knepley /*@
187dce8aebaSBarry Smith   PetscQuadratureGetOrder - Return the order of the method in the `PetscQuadrature`
188bcede257SMatthew G. Knepley 
18920f4b53cSBarry Smith   Not Collective
190bcede257SMatthew G. Knepley 
191bcede257SMatthew G. Knepley   Input Parameter:
192dce8aebaSBarry Smith . q - The `PetscQuadrature` object
193bcede257SMatthew G. Knepley 
194bcede257SMatthew G. Knepley   Output Parameter:
195bcede257SMatthew G. Knepley . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated
196bcede257SMatthew G. Knepley 
197bcede257SMatthew G. Knepley   Level: intermediate
198bcede257SMatthew G. Knepley 
199dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureSetOrder()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
200bcede257SMatthew G. Knepley @*/
PetscQuadratureGetOrder(PetscQuadrature q,PetscInt * order)201d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order)
202d71ae5a4SJacob Faibussowitsch {
203bcede257SMatthew G. Knepley   PetscFunctionBegin;
2042cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
2054f572ea9SToby Isaac   PetscAssertPointer(order, 2);
206bcede257SMatthew G. Knepley   *order = q->order;
2073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
208bcede257SMatthew G. Knepley }
209bcede257SMatthew G. Knepley 
210bcede257SMatthew G. Knepley /*@
211dce8aebaSBarry Smith   PetscQuadratureSetOrder - Set the order of the method in the `PetscQuadrature`
212bcede257SMatthew G. Knepley 
21320f4b53cSBarry Smith   Not Collective
214bcede257SMatthew G. Knepley 
215bcede257SMatthew G. Knepley   Input Parameters:
216dce8aebaSBarry Smith + q     - The `PetscQuadrature` object
217bcede257SMatthew G. Knepley - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated
218bcede257SMatthew G. Knepley 
219bcede257SMatthew G. Knepley   Level: intermediate
220bcede257SMatthew G. Knepley 
221dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureGetOrder()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
222bcede257SMatthew G. Knepley @*/
PetscQuadratureSetOrder(PetscQuadrature q,PetscInt order)223d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order)
224d71ae5a4SJacob Faibussowitsch {
225bcede257SMatthew G. Knepley   PetscFunctionBegin;
2262cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
227bcede257SMatthew G. Knepley   q->order = order;
2283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
229bcede257SMatthew G. Knepley }
230bcede257SMatthew G. Knepley 
231a6b92713SMatthew G. Knepley /*@
232a6b92713SMatthew G. Knepley   PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated
233a6b92713SMatthew G. Knepley 
23420f4b53cSBarry Smith   Not Collective
235a6b92713SMatthew G. Knepley 
236a6b92713SMatthew G. Knepley   Input Parameter:
237dce8aebaSBarry Smith . q - The `PetscQuadrature` object
238a6b92713SMatthew G. Knepley 
239a6b92713SMatthew G. Knepley   Output Parameter:
240a6b92713SMatthew G. Knepley . Nc - The number of components
241a6b92713SMatthew G. Knepley 
24220f4b53cSBarry Smith   Level: intermediate
24320f4b53cSBarry Smith 
244dce8aebaSBarry Smith   Note:
2451d27aa22SBarry Smith   We are performing an integral $\int f(x) w(x) dx$, where both $f$ and $w$ (the weight) have `Nc` components.
246a6b92713SMatthew G. Knepley 
247dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureSetNumComponents()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
248a6b92713SMatthew G. Knepley @*/
PetscQuadratureGetNumComponents(PetscQuadrature q,PetscInt * Nc)249d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc)
250d71ae5a4SJacob Faibussowitsch {
251a6b92713SMatthew G. Knepley   PetscFunctionBegin;
2522cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
2534f572ea9SToby Isaac   PetscAssertPointer(Nc, 2);
254a6b92713SMatthew G. Knepley   *Nc = q->Nc;
2553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
256a6b92713SMatthew G. Knepley }
257a6b92713SMatthew G. Knepley 
258a6b92713SMatthew G. Knepley /*@
2591d27aa22SBarry Smith   PetscQuadratureSetNumComponents - Sets the number of components for functions to be integrated
260a6b92713SMatthew G. Knepley 
26120f4b53cSBarry Smith   Not Collective
262a6b92713SMatthew G. Knepley 
263a6b92713SMatthew G. Knepley   Input Parameters:
2642fe279fdSBarry Smith + q  - The `PetscQuadrature` object
265a6b92713SMatthew G. Knepley - Nc - The number of components
266a6b92713SMatthew G. Knepley 
26720f4b53cSBarry Smith   Level: intermediate
26820f4b53cSBarry Smith 
269dce8aebaSBarry Smith   Note:
2701d27aa22SBarry Smith   We are performing an integral $\int f(x) w(x) dx$, where both $f$ and $w$ (the weight) have `Nc` components.
271a6b92713SMatthew G. Knepley 
272dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureGetNumComponents()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
273a6b92713SMatthew G. Knepley @*/
PetscQuadratureSetNumComponents(PetscQuadrature q,PetscInt Nc)274d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc)
275d71ae5a4SJacob Faibussowitsch {
276a6b92713SMatthew G. Knepley   PetscFunctionBegin;
2772cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
278a6b92713SMatthew G. Knepley   q->Nc = Nc;
2793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
280a6b92713SMatthew G. Knepley }
281a6b92713SMatthew G. Knepley 
28240d8ff71SMatthew G. Knepley /*@C
283dce8aebaSBarry Smith   PetscQuadratureGetData - Returns the data defining the `PetscQuadrature`
28440d8ff71SMatthew G. Knepley 
28520f4b53cSBarry Smith   Not Collective
28640d8ff71SMatthew G. Knepley 
28740d8ff71SMatthew G. Knepley   Input Parameter:
288dce8aebaSBarry Smith . q - The `PetscQuadrature` object
28940d8ff71SMatthew G. Knepley 
29040d8ff71SMatthew G. Knepley   Output Parameters:
29140d8ff71SMatthew G. Knepley + dim     - The spatial dimension
292805e7170SToby Isaac . Nc      - The number of components
29340d8ff71SMatthew G. Knepley . npoints - The number of quadrature points
29440d8ff71SMatthew G. Knepley . points  - The coordinates of each quadrature point
29540d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point
29640d8ff71SMatthew G. Knepley 
29740d8ff71SMatthew G. Knepley   Level: intermediate
29840d8ff71SMatthew G. Knepley 
299ce78bad3SBarry Smith   Note:
300ce78bad3SBarry Smith   All output arguments are optional, pass `NULL` for any argument not required
301ce78bad3SBarry Smith 
3021d27aa22SBarry Smith   Fortran Note:
3031d27aa22SBarry Smith   Call `PetscQuadratureRestoreData()` when you are done with the data
3041fd49c25SBarry Smith 
305dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureSetData()`
30640d8ff71SMatthew G. Knepley @*/
PetscQuadratureGetData(PetscQuadrature q,PeOp PetscInt * dim,PeOp PetscInt * Nc,PeOp PetscInt * npoints,PeOp const PetscReal * points[],PeOp const PetscReal * weights[])307ce78bad3SBarry Smith PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PeOp PetscInt *dim, PeOp PetscInt *Nc, PeOp PetscInt *npoints, PeOp const PetscReal *points[], PeOp const PetscReal *weights[])
308d71ae5a4SJacob Faibussowitsch {
30921454ff5SMatthew G. Knepley   PetscFunctionBegin;
3102cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
31121454ff5SMatthew G. Knepley   if (dim) {
3124f572ea9SToby Isaac     PetscAssertPointer(dim, 2);
31321454ff5SMatthew G. Knepley     *dim = q->dim;
31421454ff5SMatthew G. Knepley   }
315a6b92713SMatthew G. Knepley   if (Nc) {
3164f572ea9SToby Isaac     PetscAssertPointer(Nc, 3);
317a6b92713SMatthew G. Knepley     *Nc = q->Nc;
318a6b92713SMatthew G. Knepley   }
31921454ff5SMatthew G. Knepley   if (npoints) {
3204f572ea9SToby Isaac     PetscAssertPointer(npoints, 4);
32121454ff5SMatthew G. Knepley     *npoints = q->numPoints;
32221454ff5SMatthew G. Knepley   }
32321454ff5SMatthew G. Knepley   if (points) {
3244f572ea9SToby Isaac     PetscAssertPointer(points, 5);
32521454ff5SMatthew G. Knepley     *points = q->points;
32621454ff5SMatthew G. Knepley   }
32721454ff5SMatthew G. Knepley   if (weights) {
3284f572ea9SToby Isaac     PetscAssertPointer(weights, 6);
32921454ff5SMatthew G. Knepley     *weights = q->weights;
33021454ff5SMatthew G. Knepley   }
3313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33221454ff5SMatthew G. Knepley }
33321454ff5SMatthew G. Knepley 
3344f9ab2b4SJed Brown /*@
3354f9ab2b4SJed Brown   PetscQuadratureEqual - determine whether two quadratures are equivalent
3364f9ab2b4SJed Brown 
3374f9ab2b4SJed Brown   Input Parameters:
338dce8aebaSBarry Smith + A - A `PetscQuadrature` object
339dce8aebaSBarry Smith - B - Another `PetscQuadrature` object
3404f9ab2b4SJed Brown 
3412fe279fdSBarry Smith   Output Parameter:
342dce8aebaSBarry Smith . equal - `PETSC_TRUE` if the quadratures are the same
3434f9ab2b4SJed Brown 
3444f9ab2b4SJed Brown   Level: intermediate
3454f9ab2b4SJed Brown 
346dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`
3474f9ab2b4SJed Brown @*/
PetscQuadratureEqual(PetscQuadrature A,PetscQuadrature B,PetscBool * equal)348d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureEqual(PetscQuadrature A, PetscQuadrature B, PetscBool *equal)
349d71ae5a4SJacob Faibussowitsch {
3504f9ab2b4SJed Brown   PetscFunctionBegin;
3514f9ab2b4SJed Brown   PetscValidHeaderSpecific(A, PETSCQUADRATURE_CLASSID, 1);
3524f9ab2b4SJed Brown   PetscValidHeaderSpecific(B, PETSCQUADRATURE_CLASSID, 2);
3534f572ea9SToby Isaac   PetscAssertPointer(equal, 3);
3544f9ab2b4SJed Brown   *equal = PETSC_FALSE;
3554366bac7SMatthew G. Knepley   if (A->ct != B->ct || A->dim != B->dim || A->Nc != B->Nc || A->order != B->order || A->numPoints != B->numPoints) PetscFunctionReturn(PETSC_SUCCESS);
3564f9ab2b4SJed Brown   for (PetscInt i = 0; i < A->numPoints * A->dim; i++) {
3573ba16761SJacob Faibussowitsch     if (PetscAbsReal(A->points[i] - B->points[i]) > PETSC_SMALL) PetscFunctionReturn(PETSC_SUCCESS);
3584f9ab2b4SJed Brown   }
3594f9ab2b4SJed Brown   if (!A->weights && !B->weights) {
3604f9ab2b4SJed Brown     *equal = PETSC_TRUE;
3613ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3624f9ab2b4SJed Brown   }
3634f9ab2b4SJed Brown   if (A->weights && B->weights) {
3644f9ab2b4SJed Brown     for (PetscInt i = 0; i < A->numPoints; i++) {
3653ba16761SJacob Faibussowitsch       if (PetscAbsReal(A->weights[i] - B->weights[i]) > PETSC_SMALL) PetscFunctionReturn(PETSC_SUCCESS);
3664f9ab2b4SJed Brown     }
3674f9ab2b4SJed Brown     *equal = PETSC_TRUE;
3684f9ab2b4SJed Brown   }
3693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3704f9ab2b4SJed Brown }
3714f9ab2b4SJed Brown 
PetscDTJacobianInverse_Internal(PetscInt m,PetscInt n,const PetscReal J[],PetscReal Jinv[])372d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[])
373d71ae5a4SJacob Faibussowitsch {
374907761f8SToby Isaac   PetscScalar *Js, *Jinvs;
375907761f8SToby Isaac   PetscInt     i, j, k;
376907761f8SToby Isaac   PetscBLASInt bm, bn, info;
377907761f8SToby Isaac 
378907761f8SToby Isaac   PetscFunctionBegin;
3793ba16761SJacob Faibussowitsch   if (!m || !n) PetscFunctionReturn(PETSC_SUCCESS);
3809566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(m, &bm));
3819566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(n, &bn));
382907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX)
3839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(m * n, &Js, m * n, &Jinvs));
38428222859SToby Isaac   for (i = 0; i < m * n; i++) Js[i] = J[i];
385907761f8SToby Isaac #else
386907761f8SToby Isaac   Js    = (PetscReal *)J;
387907761f8SToby Isaac   Jinvs = Jinv;
388907761f8SToby Isaac #endif
389907761f8SToby Isaac   if (m == n) {
390907761f8SToby Isaac     PetscBLASInt *pivots;
391907761f8SToby Isaac     PetscScalar  *W;
392907761f8SToby Isaac 
3939566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(m, &pivots, m, &W));
394907761f8SToby Isaac 
3959566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(Jinvs, Js, m * m));
396792fecdfSBarry Smith     PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info));
397835f2295SStefano Zampini     PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetrf %" PetscBLASInt_FMT, info);
398792fecdfSBarry Smith     PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info));
399835f2295SStefano Zampini     PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetri %" PetscBLASInt_FMT, info);
4009566063dSJacob Faibussowitsch     PetscCall(PetscFree2(pivots, W));
401907761f8SToby Isaac   } else if (m < n) {
402907761f8SToby Isaac     PetscScalar  *JJT;
403907761f8SToby Isaac     PetscBLASInt *pivots;
404907761f8SToby Isaac     PetscScalar  *W;
405907761f8SToby Isaac 
4069566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(m * m, &JJT));
4079566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(m, &pivots, m, &W));
408907761f8SToby Isaac     for (i = 0; i < m; i++) {
409907761f8SToby Isaac       for (j = 0; j < m; j++) {
410907761f8SToby Isaac         PetscScalar val = 0.;
411907761f8SToby Isaac 
412907761f8SToby Isaac         for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k];
413907761f8SToby Isaac         JJT[i * m + j] = val;
414907761f8SToby Isaac       }
415907761f8SToby Isaac     }
416907761f8SToby Isaac 
417792fecdfSBarry Smith     PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info));
418835f2295SStefano Zampini     PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetrf %" PetscBLASInt_FMT, info);
419792fecdfSBarry Smith     PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info));
420835f2295SStefano Zampini     PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetri %" PetscBLASInt_FMT, info);
421907761f8SToby Isaac     for (i = 0; i < n; i++) {
422907761f8SToby Isaac       for (j = 0; j < m; j++) {
423907761f8SToby Isaac         PetscScalar val = 0.;
424907761f8SToby Isaac 
425907761f8SToby Isaac         for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j];
426907761f8SToby Isaac         Jinvs[i * m + j] = val;
427907761f8SToby Isaac       }
428907761f8SToby Isaac     }
4299566063dSJacob Faibussowitsch     PetscCall(PetscFree2(pivots, W));
4309566063dSJacob Faibussowitsch     PetscCall(PetscFree(JJT));
431907761f8SToby Isaac   } else {
432907761f8SToby Isaac     PetscScalar  *JTJ;
433907761f8SToby Isaac     PetscBLASInt *pivots;
434907761f8SToby Isaac     PetscScalar  *W;
435907761f8SToby Isaac 
4369566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n * n, &JTJ));
4379566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(n, &pivots, n, &W));
438907761f8SToby Isaac     for (i = 0; i < n; i++) {
439907761f8SToby Isaac       for (j = 0; j < n; j++) {
440907761f8SToby Isaac         PetscScalar val = 0.;
441907761f8SToby Isaac 
442907761f8SToby Isaac         for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j];
443907761f8SToby Isaac         JTJ[i * n + j] = val;
444907761f8SToby Isaac       }
445907761f8SToby Isaac     }
446907761f8SToby Isaac 
447792fecdfSBarry Smith     PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info));
448835f2295SStefano Zampini     PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetrf %" PetscBLASInt_FMT, info);
449792fecdfSBarry Smith     PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info));
450835f2295SStefano Zampini     PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetri %" PetscBLASInt_FMT, info);
451907761f8SToby Isaac     for (i = 0; i < n; i++) {
452907761f8SToby Isaac       for (j = 0; j < m; j++) {
453907761f8SToby Isaac         PetscScalar val = 0.;
454907761f8SToby Isaac 
455907761f8SToby Isaac         for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k];
456907761f8SToby Isaac         Jinvs[i * m + j] = val;
457907761f8SToby Isaac       }
458907761f8SToby Isaac     }
4599566063dSJacob Faibussowitsch     PetscCall(PetscFree2(pivots, W));
4609566063dSJacob Faibussowitsch     PetscCall(PetscFree(JTJ));
461907761f8SToby Isaac   }
462907761f8SToby Isaac #if defined(PETSC_USE_COMPLEX)
46328222859SToby Isaac   for (i = 0; i < m * n; i++) Jinv[i] = PetscRealPart(Jinvs[i]);
4649566063dSJacob Faibussowitsch   PetscCall(PetscFree2(Js, Jinvs));
465907761f8SToby Isaac #endif
4663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
467907761f8SToby Isaac }
468907761f8SToby Isaac 
469907761f8SToby Isaac /*@
470907761f8SToby Isaac   PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation.
471907761f8SToby Isaac 
47220f4b53cSBarry Smith   Collective
473907761f8SToby Isaac 
4744165533cSJose E. Roman   Input Parameters:
475907761f8SToby Isaac + q           - the quadrature functional
476907761f8SToby Isaac . imageDim    - the dimension of the image of the transformation
477907761f8SToby Isaac . origin      - a point in the original space
478907761f8SToby Isaac . originImage - the image of the origin under the transformation
479907761f8SToby Isaac . J           - the Jacobian of the image: an [imageDim x dim] matrix in row major order
4801d27aa22SBarry Smith - formDegree  - transform the quadrature weights as k-forms of this form degree (if the number of components is a multiple of (dim choose `formDegree`),
4811d27aa22SBarry Smith                 it is assumed that they represent multiple k-forms) [see `PetscDTAltVPullback()` for interpretation of `formDegree`]
482907761f8SToby Isaac 
4832fe279fdSBarry Smith   Output Parameter:
4841d27aa22SBarry Smith . Jinvstarq - a quadrature rule where each point is the image of a point in the original quadrature rule, and where the k-form weights have
4851d27aa22SBarry Smith   been pulled-back by the pseudoinverse of `J` to the k-form weights in the image space.
486907761f8SToby Isaac 
4876c877ef6SSatish Balay   Level: intermediate
4886c877ef6SSatish Balay 
489dce8aebaSBarry Smith   Note:
4901d27aa22SBarry Smith   The new quadrature rule will have a different number of components if spaces have different dimensions.
4911d27aa22SBarry Smith   For example, pushing a 2-form forward from a two dimensional space to a three dimensional space changes the number of components from 1 to 3.
492dce8aebaSBarry Smith 
493dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscDTAltVPullback()`, `PetscDTAltVPullbackMatrix()`
494907761f8SToby Isaac @*/
PetscQuadraturePushForward(PetscQuadrature q,PetscInt imageDim,const PetscReal origin[],const PetscReal originImage[],const PetscReal J[],PetscInt formDegree,PetscQuadrature * Jinvstarq)495d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq)
496d71ae5a4SJacob Faibussowitsch {
497907761f8SToby Isaac   PetscInt         dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c;
498907761f8SToby Isaac   const PetscReal *points;
499907761f8SToby Isaac   const PetscReal *weights;
500907761f8SToby Isaac   PetscReal       *imagePoints, *imageWeights;
501907761f8SToby Isaac   PetscReal       *Jinv;
502907761f8SToby Isaac   PetscReal       *Jinvstar;
503907761f8SToby Isaac 
504907761f8SToby Isaac   PetscFunctionBegin;
505d4afb720SToby Isaac   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
50663a3b9bcSJacob Faibussowitsch   PetscCheck(imageDim >= PetscAbsInt(formDegree), PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %" PetscInt_FMT "-form in %" PetscInt_FMT " dimensions", PetscAbsInt(formDegree), imageDim);
5079566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights));
5089566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize));
50963a3b9bcSJacob Faibussowitsch   PetscCheck(Nc % formSize == 0, PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Number of components %" PetscInt_FMT " is not a multiple of formSize %" PetscInt_FMT, Nc, formSize);
510907761f8SToby Isaac   Ncopies = Nc / formSize;
5119566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize));
512907761f8SToby Isaac   imageNc = Ncopies * imageFormSize;
5139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Npoints * imageDim, &imagePoints));
5149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Npoints * imageNc, &imageWeights));
5159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar));
5169566063dSJacob Faibussowitsch   PetscCall(PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv));
5179566063dSJacob Faibussowitsch   PetscCall(PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar));
518907761f8SToby Isaac   for (pt = 0; pt < Npoints; pt++) {
5198e3a54c0SPierre Jolivet     const PetscReal *point      = PetscSafePointerPlusOffset(points, pt * dim);
520907761f8SToby Isaac     PetscReal       *imagePoint = &imagePoints[pt * imageDim];
521907761f8SToby Isaac 
522907761f8SToby Isaac     for (i = 0; i < imageDim; i++) {
523907761f8SToby Isaac       PetscReal val = originImage[i];
524907761f8SToby Isaac 
525907761f8SToby Isaac       for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]);
526907761f8SToby Isaac       imagePoint[i] = val;
527907761f8SToby Isaac     }
528907761f8SToby Isaac     for (c = 0; c < Ncopies; c++) {
529907761f8SToby Isaac       const PetscReal *form      = &weights[pt * Nc + c * formSize];
530907761f8SToby Isaac       PetscReal       *imageForm = &imageWeights[pt * imageNc + c * imageFormSize];
531907761f8SToby Isaac 
532907761f8SToby Isaac       for (i = 0; i < imageFormSize; i++) {
533907761f8SToby Isaac         PetscReal val = 0.;
534907761f8SToby Isaac 
535907761f8SToby Isaac         for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j];
536907761f8SToby Isaac         imageForm[i] = val;
537907761f8SToby Isaac       }
538907761f8SToby Isaac     }
539907761f8SToby Isaac   }
5409566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq));
5419566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights));
5429566063dSJacob Faibussowitsch   PetscCall(PetscFree2(Jinv, Jinvstar));
5433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
544907761f8SToby Isaac }
545907761f8SToby Isaac 
54640d8ff71SMatthew G. Knepley /*@C
54740d8ff71SMatthew G. Knepley   PetscQuadratureSetData - Sets the data defining the quadrature
54840d8ff71SMatthew G. Knepley 
54920f4b53cSBarry Smith   Not Collective
55040d8ff71SMatthew G. Knepley 
55140d8ff71SMatthew G. Knepley   Input Parameters:
552dce8aebaSBarry Smith + q       - The `PetscQuadrature` object
55340d8ff71SMatthew G. Knepley . dim     - The spatial dimension
554e2b35d93SBarry Smith . Nc      - The number of components
55540d8ff71SMatthew G. Knepley . npoints - The number of quadrature points
55640d8ff71SMatthew G. Knepley . points  - The coordinates of each quadrature point
55740d8ff71SMatthew G. Knepley - weights - The weight of each quadrature point
55840d8ff71SMatthew G. Knepley 
55940d8ff71SMatthew G. Knepley   Level: intermediate
56040d8ff71SMatthew G. Knepley 
561dce8aebaSBarry Smith   Note:
562ce78bad3SBarry Smith   `q` owns the references to points and weights, so they must be allocated using `PetscMalloc()` and the user should not free them.
563dce8aebaSBarry Smith 
564dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureGetData()`
56540d8ff71SMatthew G. Knepley @*/
PetscQuadratureSetData(PetscQuadrature q,PetscInt dim,PetscInt Nc,PetscInt npoints,const PetscReal points[],const PetscReal weights[])566c080761bSJose E. Roman PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) PeNSS
567d71ae5a4SJacob Faibussowitsch {
56821454ff5SMatthew G. Knepley   PetscFunctionBegin;
5692cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
57021454ff5SMatthew G. Knepley   if (dim >= 0) q->dim = dim;
571a6b92713SMatthew G. Knepley   if (Nc >= 0) q->Nc = Nc;
57221454ff5SMatthew G. Knepley   if (npoints >= 0) q->numPoints = npoints;
57321454ff5SMatthew G. Knepley   if (points) {
5744f572ea9SToby Isaac     PetscAssertPointer(points, 5);
57521454ff5SMatthew G. Knepley     q->points = points;
57621454ff5SMatthew G. Knepley   }
57721454ff5SMatthew G. Knepley   if (weights) {
5784f572ea9SToby Isaac     PetscAssertPointer(weights, 6);
57921454ff5SMatthew G. Knepley     q->weights = weights;
58021454ff5SMatthew G. Knepley   }
5813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
582f9fd7fdbSMatthew G. Knepley }
583f9fd7fdbSMatthew G. Knepley 
PetscQuadratureView_Ascii(PetscQuadrature quad,PetscViewer v)584d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v)
585d71ae5a4SJacob Faibussowitsch {
586d9bac1caSLisandro Dalcin   PetscInt          q, d, c;
587d9bac1caSLisandro Dalcin   PetscViewerFormat format;
588d9bac1caSLisandro Dalcin 
589d9bac1caSLisandro Dalcin   PetscFunctionBegin;
5904366bac7SMatthew G. Knepley   if (quad->Nc > 1)
5914366bac7SMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(v, "Quadrature on a %s of order %" PetscInt_FMT " on %" PetscInt_FMT " points (dim %" PetscInt_FMT ") with %" PetscInt_FMT " components\n", DMPolytopeTypes[quad->ct], quad->order, quad->numPoints, quad->dim, quad->Nc));
5924366bac7SMatthew G. Knepley   else PetscCall(PetscViewerASCIIPrintf(v, "Quadrature on a %s of order %" PetscInt_FMT " on %" PetscInt_FMT " points (dim %" PetscInt_FMT ")\n", DMPolytopeTypes[quad->ct], quad->order, quad->numPoints, quad->dim));
5939566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(v, &format));
5943ba16761SJacob Faibussowitsch   if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
595d9bac1caSLisandro Dalcin   for (q = 0; q < quad->numPoints; ++q) {
59663a3b9bcSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(v, "p%" PetscInt_FMT " (", q));
5979566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIUseTabs(v, PETSC_FALSE));
598d9bac1caSLisandro Dalcin     for (d = 0; d < quad->dim; ++d) {
5999566063dSJacob Faibussowitsch       if (d) PetscCall(PetscViewerASCIIPrintf(v, ", "));
6009566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q * quad->dim + d]));
601d9bac1caSLisandro Dalcin     }
6029566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(v, ") "));
60363a3b9bcSJacob Faibussowitsch     if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, "w%" PetscInt_FMT " (", q));
604d9bac1caSLisandro Dalcin     for (c = 0; c < quad->Nc; ++c) {
6059566063dSJacob Faibussowitsch       if (c) PetscCall(PetscViewerASCIIPrintf(v, ", "));
6069566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q * quad->Nc + c]));
607d9bac1caSLisandro Dalcin     }
6089566063dSJacob Faibussowitsch     if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, ")"));
6099566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(v, "\n"));
6109566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIUseTabs(v, PETSC_TRUE));
611d9bac1caSLisandro Dalcin   }
6123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
613d9bac1caSLisandro Dalcin }
614d9bac1caSLisandro Dalcin 
615ffeef943SBarry Smith /*@
616dce8aebaSBarry Smith   PetscQuadratureView - View a `PetscQuadrature` object
61740d8ff71SMatthew G. Knepley 
61820f4b53cSBarry Smith   Collective
61940d8ff71SMatthew G. Knepley 
62040d8ff71SMatthew G. Knepley   Input Parameters:
621dce8aebaSBarry Smith + quad   - The `PetscQuadrature` object
622dce8aebaSBarry Smith - viewer - The `PetscViewer` object
62340d8ff71SMatthew G. Knepley 
62440d8ff71SMatthew G. Knepley   Level: beginner
62540d8ff71SMatthew G. Knepley 
626dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscViewer`, `PetscQuadratureCreate()`, `PetscQuadratureGetData()`
62740d8ff71SMatthew G. Knepley @*/
PetscQuadratureView(PetscQuadrature quad,PetscViewer viewer)628d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer)
629d71ae5a4SJacob Faibussowitsch {
6309f196a02SMartin Diehl   PetscBool isascii;
631f9fd7fdbSMatthew G. Knepley 
632f9fd7fdbSMatthew G. Knepley   PetscFunctionBegin;
633d9bac1caSLisandro Dalcin   PetscValidHeader(quad, 1);
634d9bac1caSLisandro Dalcin   if (viewer) PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
6359566063dSJacob Faibussowitsch   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)quad), &viewer));
6369f196a02SMartin Diehl   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
6379566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushTab(viewer));
6389f196a02SMartin Diehl   if (isascii) PetscCall(PetscQuadratureView_Ascii(quad, viewer));
6399566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopTab(viewer));
6403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
641bfa639d9SMatthew G. Knepley }
642bfa639d9SMatthew G. Knepley 
64389710940SMatthew G. Knepley /*@C
64489710940SMatthew G. Knepley   PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement
64589710940SMatthew G. Knepley 
64620f4b53cSBarry Smith   Not Collective; No Fortran Support
64789710940SMatthew G. Knepley 
648d8d19677SJose E. Roman   Input Parameters:
649dce8aebaSBarry Smith + q              - The original `PetscQuadrature`
65089710940SMatthew G. Knepley . numSubelements - The number of subelements the original element is divided into
65189710940SMatthew G. Knepley . v0             - An array of the initial points for each subelement
65289710940SMatthew G. Knepley - jac            - An array of the Jacobian mappings from the reference to each subelement
65389710940SMatthew G. Knepley 
6542fe279fdSBarry Smith   Output Parameter:
65560225df5SJacob Faibussowitsch . qref - The dimension
65689710940SMatthew G. Knepley 
65720f4b53cSBarry Smith   Level: intermediate
65820f4b53cSBarry Smith 
659dce8aebaSBarry Smith   Note:
6601d27aa22SBarry Smith   Together `v0` and `jac` define an affine mapping from the original reference element to each subelement
66189710940SMatthew G. Knepley 
662dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscFECreate()`, `PetscSpaceGetDimension()`, `PetscDualSpaceGetDimension()`
66389710940SMatthew G. Knepley @*/
PetscQuadratureExpandComposite(PetscQuadrature q,PetscInt numSubelements,const PetscReal v0[],const PetscReal jac[],PetscQuadrature * qref)664d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref)
665d71ae5a4SJacob Faibussowitsch {
6664366bac7SMatthew G. Knepley   DMPolytopeType   ct;
66789710940SMatthew G. Knepley   const PetscReal *points, *weights;
66889710940SMatthew G. Knepley   PetscReal       *pointsRef, *weightsRef;
669a6b92713SMatthew G. Knepley   PetscInt         dim, Nc, order, npoints, npointsRef, c, p, cp, d, e;
67089710940SMatthew G. Knepley 
67189710940SMatthew G. Knepley   PetscFunctionBegin;
6722cd22861SMatthew G. Knepley   PetscValidHeaderSpecific(q, PETSCQUADRATURE_CLASSID, 1);
6734f572ea9SToby Isaac   PetscAssertPointer(v0, 3);
6744f572ea9SToby Isaac   PetscAssertPointer(jac, 4);
6754f572ea9SToby Isaac   PetscAssertPointer(qref, 5);
6769566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, qref));
6774366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureGetCellType(q, &ct));
6789566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetOrder(q, &order));
6799566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights));
68089710940SMatthew G. Knepley   npointsRef = npoints * numSubelements;
6819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npointsRef * dim, &pointsRef));
6829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npointsRef * Nc, &weightsRef));
68389710940SMatthew G. Knepley   for (c = 0; c < numSubelements; ++c) {
68489710940SMatthew G. Knepley     for (p = 0; p < npoints; ++p) {
68589710940SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
68689710940SMatthew G. Knepley         pointsRef[(c * npoints + p) * dim + d] = v0[c * dim + d];
687ad540459SPierre Jolivet         for (e = 0; e < dim; ++e) pointsRef[(c * npoints + p) * dim + d] += jac[(c * dim + d) * dim + e] * (points[p * dim + e] + 1.0);
68889710940SMatthew G. Knepley       }
68989710940SMatthew G. Knepley       /* Could also use detJ here */
690a6b92713SMatthew G. Knepley       for (cp = 0; cp < Nc; ++cp) weightsRef[(c * npoints + p) * Nc + cp] = weights[p * Nc + cp] / numSubelements;
69189710940SMatthew G. Knepley     }
69289710940SMatthew G. Knepley   }
6934366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureSetCellType(*qref, ct));
6949566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetOrder(*qref, order));
6959566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef));
6963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
69789710940SMatthew G. Knepley }
69889710940SMatthew G. Knepley 
69994e21283SToby Isaac /* Compute the coefficients for the Jacobi polynomial recurrence,
7001d27aa22SBarry Smith 
7011d27aa22SBarry Smith    J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x).
70294e21283SToby Isaac  */
70394e21283SToby Isaac #define PetscDTJacobiRecurrence_Internal(n, a, b, cnm1, cnm1x, cnm2) \
70494e21283SToby Isaac   do { \
70594e21283SToby Isaac     PetscReal _a = (a); \
70694e21283SToby Isaac     PetscReal _b = (b); \
70794e21283SToby Isaac     PetscReal _n = (n); \
70894e21283SToby Isaac     if (n == 1) { \
70994e21283SToby Isaac       (cnm1)  = (_a - _b) * 0.5; \
71094e21283SToby Isaac       (cnm1x) = (_a + _b + 2.) * 0.5; \
71194e21283SToby Isaac       (cnm2)  = 0.; \
71294e21283SToby Isaac     } else { \
71394e21283SToby Isaac       PetscReal _2n  = _n + _n; \
71494e21283SToby Isaac       PetscReal _d   = (_2n * (_n + _a + _b) * (_2n + _a + _b - 2)); \
71594e21283SToby Isaac       PetscReal _n1  = (_2n + _a + _b - 1.) * (_a * _a - _b * _b); \
71694e21283SToby Isaac       PetscReal _n1x = (_2n + _a + _b - 1.) * (_2n + _a + _b) * (_2n + _a + _b - 2); \
71794e21283SToby Isaac       PetscReal _n2  = 2. * ((_n + _a - 1.) * (_n + _b - 1.) * (_2n + _a + _b)); \
71894e21283SToby Isaac       (cnm1)         = _n1 / _d; \
71994e21283SToby Isaac       (cnm1x)        = _n1x / _d; \
72094e21283SToby Isaac       (cnm2)         = _n2 / _d; \
72194e21283SToby Isaac     } \
72294e21283SToby Isaac   } while (0)
72394e21283SToby Isaac 
724fbdc3dfeSToby Isaac /*@
725fbdc3dfeSToby Isaac   PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial.
726fbdc3dfeSToby Isaac 
727fbdc3dfeSToby Isaac   $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$
728fbdc3dfeSToby Isaac 
7294165533cSJose E. Roman   Input Parameters:
73060225df5SJacob Faibussowitsch + alpha - the left exponent > -1
731fbdc3dfeSToby Isaac . beta  - the right exponent > -1
73260225df5SJacob Faibussowitsch - n     - the polynomial degree
733fbdc3dfeSToby Isaac 
7344165533cSJose E. Roman   Output Parameter:
735fbdc3dfeSToby Isaac . norm - the weighted L2 norm
736fbdc3dfeSToby Isaac 
737fbdc3dfeSToby Isaac   Level: beginner
738fbdc3dfeSToby Isaac 
739dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscDTJacobiEval()`
740fbdc3dfeSToby Isaac @*/
PetscDTJacobiNorm(PetscReal alpha,PetscReal beta,PetscInt n,PetscReal * norm)741d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm)
742d71ae5a4SJacob Faibussowitsch {
743fbdc3dfeSToby Isaac   PetscReal twoab1;
744fbdc3dfeSToby Isaac   PetscReal gr;
745fbdc3dfeSToby Isaac 
746fbdc3dfeSToby Isaac   PetscFunctionBegin;
74708401ef6SPierre Jolivet   PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid", (double)alpha);
74808401ef6SPierre Jolivet   PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid", (double)beta);
74963a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %" PetscInt_FMT " < 0 invalid", n);
750fbdc3dfeSToby Isaac   twoab1 = PetscPowReal(2., alpha + beta + 1.);
751fbdc3dfeSToby Isaac #if defined(PETSC_HAVE_LGAMMA)
752fbdc3dfeSToby Isaac   if (!n) {
753fbdc3dfeSToby Isaac     gr = PetscExpReal(PetscLGamma(alpha + 1.) + PetscLGamma(beta + 1.) - PetscLGamma(alpha + beta + 2.));
754fbdc3dfeSToby Isaac   } else {
755fbdc3dfeSToby Isaac     gr = PetscExpReal(PetscLGamma(n + alpha + 1.) + PetscLGamma(n + beta + 1.) - (PetscLGamma(n + 1.) + PetscLGamma(n + alpha + beta + 1.))) / (n + n + alpha + beta + 1.);
756fbdc3dfeSToby Isaac   }
757fbdc3dfeSToby Isaac #else
758fbdc3dfeSToby Isaac   {
759fbdc3dfeSToby Isaac     PetscInt alphai = (PetscInt)alpha;
760fbdc3dfeSToby Isaac     PetscInt betai  = (PetscInt)beta;
761fbdc3dfeSToby Isaac     PetscInt i;
762fbdc3dfeSToby Isaac 
763fbdc3dfeSToby Isaac     gr = n ? (1. / (n + n + alpha + beta + 1.)) : 1.;
764fbdc3dfeSToby Isaac     if ((PetscReal)alphai == alpha) {
765fbdc3dfeSToby Isaac       if (!n) {
766fbdc3dfeSToby Isaac         for (i = 0; i < alphai; i++) gr *= (i + 1.) / (beta + i + 1.);
767fbdc3dfeSToby Isaac         gr /= (alpha + beta + 1.);
768fbdc3dfeSToby Isaac       } else {
769fbdc3dfeSToby Isaac         for (i = 0; i < alphai; i++) gr *= (n + i + 1.) / (n + beta + i + 1.);
770fbdc3dfeSToby Isaac       }
771fbdc3dfeSToby Isaac     } else if ((PetscReal)betai == beta) {
772fbdc3dfeSToby Isaac       if (!n) {
773fbdc3dfeSToby Isaac         for (i = 0; i < betai; i++) gr *= (i + 1.) / (alpha + i + 2.);
774fbdc3dfeSToby Isaac         gr /= (alpha + beta + 1.);
775fbdc3dfeSToby Isaac       } else {
776fbdc3dfeSToby Isaac         for (i = 0; i < betai; i++) gr *= (n + i + 1.) / (n + alpha + i + 1.);
777fbdc3dfeSToby Isaac       }
778fbdc3dfeSToby Isaac     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "lgamma() - math routine is unavailable.");
779fbdc3dfeSToby Isaac   }
780fbdc3dfeSToby Isaac #endif
781fbdc3dfeSToby Isaac   *norm = PetscSqrtReal(twoab1 * gr);
7823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
783fbdc3dfeSToby Isaac }
784fbdc3dfeSToby Isaac 
PetscDTJacobiEval_Internal(PetscInt npoints,PetscReal a,PetscReal b,PetscInt k,const PetscReal * points,PetscInt ndegree,const PetscInt * degrees,PetscReal * p)785d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTJacobiEval_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscInt k, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PetscReal *p)
786d71ae5a4SJacob Faibussowitsch {
78794e21283SToby Isaac   PetscReal ak, bk;
78894e21283SToby Isaac   PetscReal abk1;
78994e21283SToby Isaac   PetscInt  i, l, maxdegree;
79094e21283SToby Isaac 
79194e21283SToby Isaac   PetscFunctionBegin;
79294e21283SToby Isaac   maxdegree = degrees[ndegree - 1] - k;
79394e21283SToby Isaac   ak        = a + k;
79494e21283SToby Isaac   bk        = b + k;
79594e21283SToby Isaac   abk1      = a + b + k + 1.;
79694e21283SToby Isaac   if (maxdegree < 0) {
7979371c9d4SSatish Balay     for (i = 0; i < npoints; i++)
7989371c9d4SSatish Balay       for (l = 0; l < ndegree; l++) p[i * ndegree + l] = 0.;
7993ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
80094e21283SToby Isaac   }
80194e21283SToby Isaac   for (i = 0; i < npoints; i++) {
80294e21283SToby Isaac     PetscReal pm1, pm2, x;
80394e21283SToby Isaac     PetscReal cnm1, cnm1x, cnm2;
80494e21283SToby Isaac     PetscInt  j, m;
80594e21283SToby Isaac 
80694e21283SToby Isaac     x   = points[i];
80794e21283SToby Isaac     pm2 = 1.;
80894e21283SToby Isaac     PetscDTJacobiRecurrence_Internal(1, ak, bk, cnm1, cnm1x, cnm2);
80994e21283SToby Isaac     pm1 = (cnm1 + cnm1x * x);
81094e21283SToby Isaac     l   = 0;
811ad540459SPierre Jolivet     while (l < ndegree && degrees[l] - k < 0) p[l++] = 0.;
81294e21283SToby Isaac     while (l < ndegree && degrees[l] - k == 0) {
81394e21283SToby Isaac       p[l] = pm2;
81494e21283SToby Isaac       for (m = 0; m < k; m++) p[l] *= (abk1 + m) * 0.5;
81594e21283SToby Isaac       l++;
81694e21283SToby Isaac     }
81794e21283SToby Isaac     while (l < ndegree && degrees[l] - k == 1) {
81894e21283SToby Isaac       p[l] = pm1;
81994e21283SToby Isaac       for (m = 0; m < k; m++) p[l] *= (abk1 + 1 + m) * 0.5;
82094e21283SToby Isaac       l++;
82194e21283SToby Isaac     }
82294e21283SToby Isaac     for (j = 2; j <= maxdegree; j++) {
82394e21283SToby Isaac       PetscReal pp;
82494e21283SToby Isaac 
82594e21283SToby Isaac       PetscDTJacobiRecurrence_Internal(j, ak, bk, cnm1, cnm1x, cnm2);
82694e21283SToby Isaac       pp  = (cnm1 + cnm1x * x) * pm1 - cnm2 * pm2;
82794e21283SToby Isaac       pm2 = pm1;
82894e21283SToby Isaac       pm1 = pp;
82994e21283SToby Isaac       while (l < ndegree && degrees[l] - k == j) {
83094e21283SToby Isaac         p[l] = pp;
83194e21283SToby Isaac         for (m = 0; m < k; m++) p[l] *= (abk1 + j + m) * 0.5;
83294e21283SToby Isaac         l++;
83394e21283SToby Isaac       }
83494e21283SToby Isaac     }
83594e21283SToby Isaac     p += ndegree;
83694e21283SToby Isaac   }
8373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
83894e21283SToby Isaac }
83994e21283SToby Isaac 
84037045ce4SJed Brown /*@
841dce8aebaSBarry Smith   PetscDTJacobiEvalJet - Evaluate the jet (function and derivatives) of the Jacobi polynomials basis up to a given degree.
842fbdc3dfeSToby Isaac 
8434165533cSJose E. Roman   Input Parameters:
844fbdc3dfeSToby Isaac + alpha   - the left exponent of the weight
845fbdc3dfeSToby Isaac . beta    - the right exponetn of the weight
846fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at
847fbdc3dfeSToby Isaac . points  - [npoints] array of point coordinates
848fbdc3dfeSToby Isaac . degree  - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total.
849fbdc3dfeSToby Isaac - k       - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total.
850fbdc3dfeSToby Isaac 
8512fe279fdSBarry Smith   Output Parameter:
8522fe279fdSBarry Smith . p - an array containing the evaluations of the Jacobi polynomials's jets on the points.  the size is (degree + 1) x
853fbdc3dfeSToby Isaac       (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first
854fbdc3dfeSToby Isaac       (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest
855fbdc3dfeSToby Isaac       varying) dimension is the index of the evaluation point.
856fbdc3dfeSToby Isaac 
857fbdc3dfeSToby Isaac   Level: advanced
858fbdc3dfeSToby Isaac 
859a4e35b19SJacob Faibussowitsch   Notes:
860a4e35b19SJacob Faibussowitsch   The Jacobi polynomials with indices $\alpha$ and $\beta$ are orthogonal with respect to the
861a4e35b19SJacob Faibussowitsch   weighted inner product $\langle f, g \rangle = \int_{-1}^1 (1+x)^{\alpha} (1-x)^{\beta} f(x)
862a4e35b19SJacob Faibussowitsch   g(x) dx$.
863a4e35b19SJacob Faibussowitsch 
864db781477SPatrick Sanan .seealso: `PetscDTJacobiEval()`, `PetscDTPKDEvalJet()`
865fbdc3dfeSToby Isaac @*/
PetscDTJacobiEvalJet(PetscReal alpha,PetscReal beta,PetscInt npoints,const PetscReal points[],PetscInt degree,PetscInt k,PetscReal p[])866d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[])
867d71ae5a4SJacob Faibussowitsch {
868fbdc3dfeSToby Isaac   PetscInt   i, j, l;
869fbdc3dfeSToby Isaac   PetscInt  *degrees;
870fbdc3dfeSToby Isaac   PetscReal *psingle;
871fbdc3dfeSToby Isaac 
872fbdc3dfeSToby Isaac   PetscFunctionBegin;
873fbdc3dfeSToby Isaac   if (degree == 0) {
874fbdc3dfeSToby Isaac     PetscInt zero = 0;
875fbdc3dfeSToby Isaac 
87648a46eb9SPierre Jolivet     for (i = 0; i <= k; i++) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i * npoints]));
8773ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
878fbdc3dfeSToby Isaac   }
8799566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(degree + 1, &degrees));
8809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((degree + 1) * npoints, &psingle));
881fbdc3dfeSToby Isaac   for (i = 0; i <= degree; i++) degrees[i] = i;
882fbdc3dfeSToby Isaac   for (i = 0; i <= k; i++) {
8839566063dSJacob Faibussowitsch     PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle));
884fbdc3dfeSToby Isaac     for (j = 0; j <= degree; j++) {
885ad540459SPierre Jolivet       for (l = 0; l < npoints; l++) p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j];
886fbdc3dfeSToby Isaac     }
887fbdc3dfeSToby Isaac   }
8889566063dSJacob Faibussowitsch   PetscCall(PetscFree(psingle));
8899566063dSJacob Faibussowitsch   PetscCall(PetscFree(degrees));
8903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
891fbdc3dfeSToby Isaac }
892fbdc3dfeSToby Isaac 
893fbdc3dfeSToby Isaac /*@
894dce8aebaSBarry Smith   PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$ at a set of points
89594e21283SToby Isaac   at points
89694e21283SToby Isaac 
89794e21283SToby Isaac   Not Collective
89894e21283SToby Isaac 
8994165533cSJose E. Roman   Input Parameters:
90094e21283SToby Isaac + npoints - number of spatial points to evaluate at
90194e21283SToby Isaac . alpha   - the left exponent > -1
90294e21283SToby Isaac . beta    - the right exponent > -1
90394e21283SToby Isaac . points  - array of locations to evaluate at
90494e21283SToby Isaac . ndegree - number of basis degrees to evaluate
90594e21283SToby Isaac - degrees - sorted array of degrees to evaluate
90694e21283SToby Isaac 
9074165533cSJose E. Roman   Output Parameters:
9081d27aa22SBarry Smith + B  - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or `NULL`)
9091d27aa22SBarry Smith . D  - row-oriented derivative evaluation matrix (or `NULL`)
9101d27aa22SBarry Smith - D2 - row-oriented second derivative evaluation matrix (or `NULL`)
91194e21283SToby Isaac 
91294e21283SToby Isaac   Level: intermediate
91394e21283SToby Isaac 
914dce8aebaSBarry Smith .seealso: `PetscDTGaussQuadrature()`, `PetscDTLegendreEval()`
91594e21283SToby Isaac @*/
PetscDTJacobiEval(PetscInt npoints,PetscReal alpha,PetscReal beta,const PetscReal * points,PetscInt ndegree,const PetscInt * degrees,PeOp PetscReal B[],PeOp PetscReal D[],PeOp PetscReal D2[])916ce78bad3SBarry Smith PetscErrorCode PetscDTJacobiEval(PetscInt npoints, PetscReal alpha, PetscReal beta, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PeOp PetscReal B[], PeOp PetscReal D[], PeOp PetscReal D2[])
917d71ae5a4SJacob Faibussowitsch {
91894e21283SToby Isaac   PetscFunctionBegin;
91908401ef6SPierre Jolivet   PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "alpha must be > -1.");
92008401ef6SPierre Jolivet   PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "beta must be > -1.");
9213ba16761SJacob Faibussowitsch   if (!npoints || !ndegree) PetscFunctionReturn(PETSC_SUCCESS);
9229566063dSJacob Faibussowitsch   if (B) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B));
9239566063dSJacob Faibussowitsch   if (D) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D));
9249566063dSJacob Faibussowitsch   if (D2) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2));
9253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
92694e21283SToby Isaac }
92794e21283SToby Isaac 
92894e21283SToby Isaac /*@
92994e21283SToby Isaac   PetscDTLegendreEval - evaluate Legendre polynomials at points
93037045ce4SJed Brown 
93137045ce4SJed Brown   Not Collective
93237045ce4SJed Brown 
9334165533cSJose E. Roman   Input Parameters:
93437045ce4SJed Brown + npoints - number of spatial points to evaluate at
93537045ce4SJed Brown . points  - array of locations to evaluate at
93637045ce4SJed Brown . ndegree - number of basis degrees to evaluate
93737045ce4SJed Brown - degrees - sorted array of degrees to evaluate
93837045ce4SJed Brown 
9394165533cSJose E. Roman   Output Parameters:
9401d27aa22SBarry Smith + B  - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension `npoints`*`ndegrees`, allocated by caller) (or `NULL`)
9411d27aa22SBarry Smith . D  - row-oriented derivative evaluation matrix (or `NULL`)
9421d27aa22SBarry Smith - D2 - row-oriented second derivative evaluation matrix (or `NULL`)
94337045ce4SJed Brown 
94437045ce4SJed Brown   Level: intermediate
94537045ce4SJed Brown 
946db781477SPatrick Sanan .seealso: `PetscDTGaussQuadrature()`
94737045ce4SJed Brown @*/
PetscDTLegendreEval(PetscInt npoints,const PetscReal * points,PetscInt ndegree,const PetscInt * degrees,PeOp PetscReal B[],PeOp PetscReal D[],PeOp PetscReal D2[])948ce78bad3SBarry Smith PetscErrorCode PetscDTLegendreEval(PetscInt npoints, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PeOp PetscReal B[], PeOp PetscReal D[], PeOp PetscReal D2[])
949d71ae5a4SJacob Faibussowitsch {
95037045ce4SJed Brown   PetscFunctionBegin;
9519566063dSJacob Faibussowitsch   PetscCall(PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2));
9523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
95337045ce4SJed Brown }
95437045ce4SJed Brown 
955fbdc3dfeSToby Isaac /*@
9561d27aa22SBarry Smith   PetscDTIndexToGradedOrder - convert an index into a tuple of monomial degrees in a graded order (that is, if the degree sum of tuple x is less than the degree sum of tuple y,
9571d27aa22SBarry Smith   then the index of x is smaller than the index of y)
958fbdc3dfeSToby Isaac 
959fbdc3dfeSToby Isaac   Input Parameters:
960fbdc3dfeSToby Isaac + len   - the desired length of the degree tuple
961fbdc3dfeSToby Isaac - index - the index to convert: should be >= 0
962fbdc3dfeSToby Isaac 
963fbdc3dfeSToby Isaac   Output Parameter:
964ce78bad3SBarry Smith . degtup - filled with a tuple of degrees
965fbdc3dfeSToby Isaac 
966fbdc3dfeSToby Isaac   Level: beginner
967fbdc3dfeSToby Isaac 
968dce8aebaSBarry Smith   Note:
969dce8aebaSBarry Smith   For two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples
970fbdc3dfeSToby Isaac   acts as a tiebreaker.  For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the
971fbdc3dfeSToby Isaac   last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1).
972fbdc3dfeSToby Isaac 
973db781477SPatrick Sanan .seealso: `PetscDTGradedOrderToIndex()`
974fbdc3dfeSToby Isaac @*/
PetscDTIndexToGradedOrder(PetscInt len,PetscInt index,PetscInt degtup[])975d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[])
976d71ae5a4SJacob Faibussowitsch {
977fbdc3dfeSToby Isaac   PetscInt i, total;
978fbdc3dfeSToby Isaac   PetscInt sum;
979fbdc3dfeSToby Isaac 
980fbdc3dfeSToby Isaac   PetscFunctionBeginHot;
98108401ef6SPierre Jolivet   PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
98208401ef6SPierre Jolivet   PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative");
983fbdc3dfeSToby Isaac   total = 1;
984fbdc3dfeSToby Isaac   sum   = 0;
985fbdc3dfeSToby Isaac   while (index >= total) {
986fbdc3dfeSToby Isaac     index -= total;
987fbdc3dfeSToby Isaac     total = (total * (len + sum)) / (sum + 1);
988fbdc3dfeSToby Isaac     sum++;
989fbdc3dfeSToby Isaac   }
990fbdc3dfeSToby Isaac   for (i = 0; i < len; i++) {
991fbdc3dfeSToby Isaac     PetscInt c;
992fbdc3dfeSToby Isaac 
993fbdc3dfeSToby Isaac     degtup[i] = sum;
994fbdc3dfeSToby Isaac     for (c = 0, total = 1; c < sum; c++) {
995fbdc3dfeSToby Isaac       /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */
996fbdc3dfeSToby Isaac       if (index < total) break;
997fbdc3dfeSToby Isaac       index -= total;
998fbdc3dfeSToby Isaac       total = (total * (len - 1 - i + c)) / (c + 1);
999fbdc3dfeSToby Isaac       degtup[i]--;
1000fbdc3dfeSToby Isaac     }
1001fbdc3dfeSToby Isaac     sum -= degtup[i];
1002fbdc3dfeSToby Isaac   }
10033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1004fbdc3dfeSToby Isaac }
1005fbdc3dfeSToby Isaac 
1006fbdc3dfeSToby Isaac /*@
1007dce8aebaSBarry Smith   PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of `PetscDTIndexToGradedOrder()`.
1008fbdc3dfeSToby Isaac 
1009fbdc3dfeSToby Isaac   Input Parameters:
1010fbdc3dfeSToby Isaac + len    - the length of the degree tuple
1011fbdc3dfeSToby Isaac - degtup - tuple with this length
1012fbdc3dfeSToby Isaac 
1013fbdc3dfeSToby Isaac   Output Parameter:
1014fbdc3dfeSToby Isaac . index - index in graded order: >= 0
1015fbdc3dfeSToby Isaac 
101660225df5SJacob Faibussowitsch   Level: beginner
1017fbdc3dfeSToby Isaac 
1018dce8aebaSBarry Smith   Note:
1019dce8aebaSBarry Smith   For two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples
1020fbdc3dfeSToby Isaac   acts as a tiebreaker.  For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the
1021fbdc3dfeSToby Isaac   last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1).
1022fbdc3dfeSToby Isaac 
1023db781477SPatrick Sanan .seealso: `PetscDTIndexToGradedOrder()`
1024fbdc3dfeSToby Isaac @*/
PetscDTGradedOrderToIndex(PetscInt len,const PetscInt degtup[],PetscInt * index)1025d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index)
1026d71ae5a4SJacob Faibussowitsch {
1027fbdc3dfeSToby Isaac   PetscInt i, idx, sum, total;
1028fbdc3dfeSToby Isaac 
1029fbdc3dfeSToby Isaac   PetscFunctionBeginHot;
103008401ef6SPierre Jolivet   PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
1031fbdc3dfeSToby Isaac   for (i = 0, sum = 0; i < len; i++) sum += degtup[i];
1032fbdc3dfeSToby Isaac   idx   = 0;
1033fbdc3dfeSToby Isaac   total = 1;
1034fbdc3dfeSToby Isaac   for (i = 0; i < sum; i++) {
1035fbdc3dfeSToby Isaac     idx += total;
1036fbdc3dfeSToby Isaac     total = (total * (len + i)) / (i + 1);
1037fbdc3dfeSToby Isaac   }
1038fbdc3dfeSToby Isaac   for (i = 0; i < len - 1; i++) {
1039fbdc3dfeSToby Isaac     PetscInt c;
1040fbdc3dfeSToby Isaac 
1041fbdc3dfeSToby Isaac     total = 1;
1042fbdc3dfeSToby Isaac     sum -= degtup[i];
1043fbdc3dfeSToby Isaac     for (c = 0; c < sum; c++) {
1044fbdc3dfeSToby Isaac       idx += total;
1045fbdc3dfeSToby Isaac       total = (total * (len - 1 - i + c)) / (c + 1);
1046fbdc3dfeSToby Isaac     }
1047fbdc3dfeSToby Isaac   }
1048fbdc3dfeSToby Isaac   *index = idx;
10493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1050fbdc3dfeSToby Isaac }
1051fbdc3dfeSToby Isaac 
1052e3aa2e09SToby Isaac static PetscBool PKDCite       = PETSC_FALSE;
1053e3aa2e09SToby Isaac const char       PKDCitation[] = "@article{Kirby2010,\n"
1054e3aa2e09SToby Isaac                                  "  title={Singularity-free evaluation of collapsed-coordinate orthogonal polynomials},\n"
1055e3aa2e09SToby Isaac                                  "  author={Kirby, Robert C},\n"
1056e3aa2e09SToby Isaac                                  "  journal={ACM Transactions on Mathematical Software (TOMS)},\n"
1057e3aa2e09SToby Isaac                                  "  volume={37},\n"
1058e3aa2e09SToby Isaac                                  "  number={1},\n"
1059e3aa2e09SToby Isaac                                  "  pages={1--16},\n"
1060e3aa2e09SToby Isaac                                  "  year={2010},\n"
1061e3aa2e09SToby Isaac                                  "  publisher={ACM New York, NY, USA}\n}\n";
1062e3aa2e09SToby Isaac 
1063fbdc3dfeSToby Isaac /*@
1064d8f25ad8SToby Isaac   PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Proriol-Koornwinder-Dubiner (PKD) basis for
1065a4e35b19SJacob Faibussowitsch   the space of polynomials up to a given degree.
1066fbdc3dfeSToby Isaac 
10674165533cSJose E. Roman   Input Parameters:
1068fbdc3dfeSToby Isaac + dim     - the number of variables in the multivariate polynomials
1069fbdc3dfeSToby Isaac . npoints - the number of points to evaluate the polynomials at
1070fbdc3dfeSToby Isaac . points  - [npoints x dim] array of point coordinates
1071fbdc3dfeSToby Isaac . degree  - the degree (sum of degrees on the variables in a monomial) of the polynomial space to evaluate.  There are ((dim + degree) choose dim) polynomials in this space.
1072fbdc3dfeSToby Isaac - k       - the maximum order partial derivative to evaluate in the jet.  There are (dim + k choose dim) partial derivatives
1073fbdc3dfeSToby Isaac             in the jet.  Choosing k = 0 means to evaluate just the function and no derivatives
1074fbdc3dfeSToby Isaac 
10752fe279fdSBarry Smith   Output Parameter:
10762fe279fdSBarry Smith . p - an array containing the evaluations of the PKD polynomials' jets on the points.  The size is ((dim + degree)
1077fbdc3dfeSToby Isaac       choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this
1078fbdc3dfeSToby Isaac       three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet
1079fbdc3dfeSToby Isaac       index; the third (fastest varying) dimension is the index of the evaluation point.
1080fbdc3dfeSToby Isaac 
1081fbdc3dfeSToby Isaac   Level: advanced
1082fbdc3dfeSToby Isaac 
1083dce8aebaSBarry Smith   Notes:
1084a4e35b19SJacob Faibussowitsch   The PKD basis is L2-orthonormal on the biunit simplex (which is used as the reference element
1085a4e35b19SJacob Faibussowitsch   for finite elements in PETSc), which makes it a stable basis to use for evaluating
1086a4e35b19SJacob Faibussowitsch   polynomials in that domain.
1087a4e35b19SJacob Faibussowitsch 
1088dce8aebaSBarry Smith   The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded
1089dce8aebaSBarry Smith   ordering of `PetscDTIndexToGradedOrder()` and `PetscDTGradedOrderToIndex()`.  For example, in 3D, the polynomial with
1090dce8aebaSBarry Smith   leading monomial x^2,y^0,z^1, which has degree tuple (2,0,1), which by `PetscDTGradedOrderToIndex()` has index 12 (it is the 13th basis function in the space);
1091fbdc3dfeSToby Isaac   the partial derivative $\partial_x \partial_z$ has order tuple (1,0,1), appears at index 6 in the jet (it is the 7th partial derivative in the jet).
1092fbdc3dfeSToby Isaac 
10931d27aa22SBarry Smith   The implementation uses Kirby's singularity-free evaluation algorithm, <https://doi.org/10.1145/1644001.1644006>.
1094e3aa2e09SToby Isaac 
1095db781477SPatrick Sanan .seealso: `PetscDTGradedOrderToIndex()`, `PetscDTIndexToGradedOrder()`, `PetscDTJacobiEvalJet()`
1096fbdc3dfeSToby Isaac @*/
PetscDTPKDEvalJet(PetscInt dim,PetscInt npoints,const PetscReal points[],PetscInt degree,PetscInt k,PetscReal p[])1097d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[])
1098d71ae5a4SJacob Faibussowitsch {
1099fbdc3dfeSToby Isaac   PetscInt   degidx, kidx, d, pt;
1100fbdc3dfeSToby Isaac   PetscInt   Nk, Ndeg;
1101fbdc3dfeSToby Isaac   PetscInt  *ktup, *degtup;
1102fbdc3dfeSToby Isaac   PetscReal *scales, initscale, scaleexp;
1103fbdc3dfeSToby Isaac 
1104fbdc3dfeSToby Isaac   PetscFunctionBegin;
11059566063dSJacob Faibussowitsch   PetscCall(PetscCitationsRegister(PKDCitation, &PKDCite));
11069566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(dim + k, k, &Nk));
11079566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(degree + dim, degree, &Ndeg));
11089566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim, &degtup, dim, &ktup));
11099566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Ndeg, &scales));
1110fbdc3dfeSToby Isaac   initscale = 1.;
1111fbdc3dfeSToby Isaac   if (dim > 1) {
11129566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomial(dim, 2, &scaleexp));
11132f613bf5SBarry Smith     initscale = PetscPowReal(2., scaleexp * 0.5);
1114fbdc3dfeSToby Isaac   }
1115fbdc3dfeSToby Isaac   for (degidx = 0; degidx < Ndeg; degidx++) {
1116fbdc3dfeSToby Isaac     PetscInt  e, i;
1117fbdc3dfeSToby Isaac     PetscInt  m1idx = -1, m2idx = -1;
1118fbdc3dfeSToby Isaac     PetscInt  n;
1119fbdc3dfeSToby Isaac     PetscInt  degsum;
1120fbdc3dfeSToby Isaac     PetscReal alpha;
1121fbdc3dfeSToby Isaac     PetscReal cnm1, cnm1x, cnm2;
1122fbdc3dfeSToby Isaac     PetscReal norm;
1123fbdc3dfeSToby Isaac 
11249566063dSJacob Faibussowitsch     PetscCall(PetscDTIndexToGradedOrder(dim, degidx, degtup));
11259371c9d4SSatish Balay     for (d = dim - 1; d >= 0; d--)
11269371c9d4SSatish Balay       if (degtup[d]) break;
1127fbdc3dfeSToby Isaac     if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */
1128fbdc3dfeSToby Isaac       scales[degidx] = initscale;
1129fbdc3dfeSToby Isaac       for (e = 0; e < dim; e++) {
11309566063dSJacob Faibussowitsch         PetscCall(PetscDTJacobiNorm(e, 0., 0, &norm));
1131fbdc3dfeSToby Isaac         scales[degidx] /= norm;
1132fbdc3dfeSToby Isaac       }
1133fbdc3dfeSToby Isaac       for (i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.;
1134fbdc3dfeSToby Isaac       for (i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.;
1135fbdc3dfeSToby Isaac       continue;
1136fbdc3dfeSToby Isaac     }
1137fbdc3dfeSToby Isaac     n = degtup[d];
1138fbdc3dfeSToby Isaac     degtup[d]--;
11399566063dSJacob Faibussowitsch     PetscCall(PetscDTGradedOrderToIndex(dim, degtup, &m1idx));
1140fbdc3dfeSToby Isaac     if (degtup[d] > 0) {
1141fbdc3dfeSToby Isaac       degtup[d]--;
11429566063dSJacob Faibussowitsch       PetscCall(PetscDTGradedOrderToIndex(dim, degtup, &m2idx));
1143fbdc3dfeSToby Isaac       degtup[d]++;
1144fbdc3dfeSToby Isaac     }
1145fbdc3dfeSToby Isaac     degtup[d]++;
1146fbdc3dfeSToby Isaac     for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e];
1147fbdc3dfeSToby Isaac     alpha = 2 * degsum + d;
1148fbdc3dfeSToby Isaac     PetscDTJacobiRecurrence_Internal(n, alpha, 0., cnm1, cnm1x, cnm2);
1149fbdc3dfeSToby Isaac 
1150fbdc3dfeSToby Isaac     scales[degidx] = initscale;
1151fbdc3dfeSToby Isaac     for (e = 0, degsum = 0; e < dim; e++) {
1152fbdc3dfeSToby Isaac       PetscInt  f;
1153fbdc3dfeSToby Isaac       PetscReal ealpha;
1154fbdc3dfeSToby Isaac       PetscReal enorm;
1155fbdc3dfeSToby Isaac 
1156fbdc3dfeSToby Isaac       ealpha = 2 * degsum + e;
1157fbdc3dfeSToby Isaac       for (f = 0; f < degsum; f++) scales[degidx] *= 2.;
11589566063dSJacob Faibussowitsch       PetscCall(PetscDTJacobiNorm(ealpha, 0., degtup[e], &enorm));
1159fbdc3dfeSToby Isaac       scales[degidx] /= enorm;
1160fbdc3dfeSToby Isaac       degsum += degtup[e];
1161fbdc3dfeSToby Isaac     }
1162fbdc3dfeSToby Isaac 
1163fbdc3dfeSToby Isaac     for (pt = 0; pt < npoints; pt++) {
1164fbdc3dfeSToby Isaac       /* compute the multipliers */
1165fbdc3dfeSToby Isaac       PetscReal thetanm1, thetanm1x, thetanm2;
1166fbdc3dfeSToby Isaac 
1167fbdc3dfeSToby Isaac       thetanm1x = dim - (d + 1) + 2. * points[pt * dim + d];
1168fbdc3dfeSToby Isaac       for (e = d + 1; e < dim; e++) thetanm1x += points[pt * dim + e];
1169fbdc3dfeSToby Isaac       thetanm1x *= 0.5;
1170fbdc3dfeSToby Isaac       thetanm1 = (2. - (dim - (d + 1)));
1171fbdc3dfeSToby Isaac       for (e = d + 1; e < dim; e++) thetanm1 -= points[pt * dim + e];
1172fbdc3dfeSToby Isaac       thetanm1 *= 0.5;
1173fbdc3dfeSToby Isaac       thetanm2 = thetanm1 * thetanm1;
1174fbdc3dfeSToby Isaac 
1175fbdc3dfeSToby Isaac       for (kidx = 0; kidx < Nk; kidx++) {
1176fbdc3dfeSToby Isaac         PetscInt f;
1177fbdc3dfeSToby Isaac 
11789566063dSJacob Faibussowitsch         PetscCall(PetscDTIndexToGradedOrder(dim, kidx, ktup));
1179fbdc3dfeSToby Isaac         /* first sum in the same derivative terms */
1180fbdc3dfeSToby Isaac         p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt];
1181ad540459SPierre Jolivet         if (m2idx >= 0) p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt];
1182fbdc3dfeSToby Isaac 
1183fbdc3dfeSToby Isaac         for (f = d; f < dim; f++) {
1184fbdc3dfeSToby Isaac           PetscInt km1idx, mplty = ktup[f];
1185fbdc3dfeSToby Isaac 
1186fbdc3dfeSToby Isaac           if (!mplty) continue;
1187fbdc3dfeSToby Isaac           ktup[f]--;
11889566063dSJacob Faibussowitsch           PetscCall(PetscDTGradedOrderToIndex(dim, ktup, &km1idx));
1189fbdc3dfeSToby Isaac 
1190fbdc3dfeSToby Isaac           /* the derivative of  cnm1x * thetanm1x  wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */
1191fbdc3dfeSToby Isaac           /* the derivative of  cnm1  * thetanm1   wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */
1192fbdc3dfeSToby Isaac           /* the derivative of -cnm2  * thetanm2   wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */
1193fbdc3dfeSToby Isaac           if (f > d) {
1194fbdc3dfeSToby Isaac             PetscInt f2;
1195fbdc3dfeSToby Isaac 
1196fbdc3dfeSToby Isaac             p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt];
1197fbdc3dfeSToby Isaac             if (m2idx >= 0) {
1198fbdc3dfeSToby Isaac               p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt];
1199fbdc3dfeSToby Isaac               /* second derivatives of -cnm2  * thetanm2   wrt x variable f,f2 is like - 0.5 * cnm2 */
1200fbdc3dfeSToby Isaac               for (f2 = f; f2 < dim; f2++) {
1201fbdc3dfeSToby Isaac                 PetscInt km2idx, mplty2 = ktup[f2];
1202fbdc3dfeSToby Isaac                 PetscInt factor;
1203fbdc3dfeSToby Isaac 
1204fbdc3dfeSToby Isaac                 if (!mplty2) continue;
1205fbdc3dfeSToby Isaac                 ktup[f2]--;
12069566063dSJacob Faibussowitsch                 PetscCall(PetscDTGradedOrderToIndex(dim, ktup, &km2idx));
1207fbdc3dfeSToby Isaac 
1208fbdc3dfeSToby Isaac                 factor = mplty * mplty2;
1209fbdc3dfeSToby Isaac                 if (f == f2) factor /= 2;
1210fbdc3dfeSToby Isaac                 p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt];
1211fbdc3dfeSToby Isaac                 ktup[f2]++;
1212fbdc3dfeSToby Isaac               }
12133034baaeSToby Isaac             }
1214fbdc3dfeSToby Isaac           } else {
1215fbdc3dfeSToby Isaac             p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt];
1216fbdc3dfeSToby Isaac           }
1217fbdc3dfeSToby Isaac           ktup[f]++;
1218fbdc3dfeSToby Isaac         }
1219fbdc3dfeSToby Isaac       }
1220fbdc3dfeSToby Isaac     }
1221fbdc3dfeSToby Isaac   }
1222fbdc3dfeSToby Isaac   for (degidx = 0; degidx < Ndeg; degidx++) {
1223fbdc3dfeSToby Isaac     PetscReal scale = scales[degidx];
1224fbdc3dfeSToby Isaac     PetscInt  i;
1225fbdc3dfeSToby Isaac 
1226fbdc3dfeSToby Isaac     for (i = 0; i < Nk * npoints; i++) p[degidx * Nk * npoints + i] *= scale;
1227fbdc3dfeSToby Isaac   }
12289566063dSJacob Faibussowitsch   PetscCall(PetscFree(scales));
12299566063dSJacob Faibussowitsch   PetscCall(PetscFree2(degtup, ktup));
12303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1231fbdc3dfeSToby Isaac }
1232fbdc3dfeSToby Isaac 
1233d8f25ad8SToby Isaac /*@
1234d8f25ad8SToby Isaac   PetscDTPTrimmedSize - The size of the trimmed polynomial space of k-forms with a given degree and form degree,
1235dce8aebaSBarry Smith   which can be evaluated in `PetscDTPTrimmedEvalJet()`.
1236d8f25ad8SToby Isaac 
1237d8f25ad8SToby Isaac   Input Parameters:
1238d8f25ad8SToby Isaac + dim        - the number of variables in the multivariate polynomials
1239d8f25ad8SToby Isaac . degree     - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space.
1240d8f25ad8SToby Isaac - formDegree - the degree of the form
1241d8f25ad8SToby Isaac 
12422fe279fdSBarry Smith   Output Parameter:
124360225df5SJacob Faibussowitsch . size - The number ((`dim` + `degree`) choose (`dim` + `formDegree`)) x ((`degree` + `formDegree` - 1) choose (`formDegree`))
1244d8f25ad8SToby Isaac 
1245d8f25ad8SToby Isaac   Level: advanced
1246d8f25ad8SToby Isaac 
1247db781477SPatrick Sanan .seealso: `PetscDTPTrimmedEvalJet()`
1248d8f25ad8SToby Isaac @*/
PetscDTPTrimmedSize(PetscInt dim,PetscInt degree,PetscInt formDegree,PetscInt * size)1249d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTPTrimmedSize(PetscInt dim, PetscInt degree, PetscInt formDegree, PetscInt *size)
1250d71ae5a4SJacob Faibussowitsch {
1251d8f25ad8SToby Isaac   PetscInt Nrk, Nbpt; // number of trimmed polynomials
1252d8f25ad8SToby Isaac 
1253d8f25ad8SToby Isaac   PetscFunctionBegin;
1254d8f25ad8SToby Isaac   formDegree = PetscAbsInt(formDegree);
12559566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(degree + dim, degree + formDegree, &Nbpt));
12569566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(degree + formDegree - 1, formDegree, &Nrk));
1257d8f25ad8SToby Isaac   Nbpt *= Nrk;
1258d8f25ad8SToby Isaac   *size = Nbpt;
12593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1260d8f25ad8SToby Isaac }
1261d8f25ad8SToby Isaac 
1262d8f25ad8SToby Isaac /* there was a reference implementation based on section 4.4 of Arnold, Falk & Winther (acta numerica, 2006), but it
1263d8f25ad8SToby Isaac  * was inferior to this implementation */
PetscDTPTrimmedEvalJet_Internal(PetscInt dim,PetscInt npoints,const PetscReal points[],PetscInt degree,PetscInt formDegree,PetscInt jetDegree,PetscReal p[])1264d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTPTrimmedEvalJet_Internal(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[])
1265d71ae5a4SJacob Faibussowitsch {
1266d8f25ad8SToby Isaac   PetscInt  formDegreeOrig = formDegree;
1267d8f25ad8SToby Isaac   PetscBool formNegative   = (formDegreeOrig < 0) ? PETSC_TRUE : PETSC_FALSE;
1268d8f25ad8SToby Isaac 
1269d8f25ad8SToby Isaac   PetscFunctionBegin;
1270d8f25ad8SToby Isaac   formDegree = PetscAbsInt(formDegreeOrig);
1271d8f25ad8SToby Isaac   if (formDegree == 0) {
12729566063dSJacob Faibussowitsch     PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree, jetDegree, p));
12733ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1274d8f25ad8SToby Isaac   }
1275d8f25ad8SToby Isaac   if (formDegree == dim) {
12769566063dSJacob Faibussowitsch     PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p));
12773ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1278d8f25ad8SToby Isaac   }
1279d8f25ad8SToby Isaac   PetscInt Nbpt;
12809566063dSJacob Faibussowitsch   PetscCall(PetscDTPTrimmedSize(dim, degree, formDegree, &Nbpt));
1281d8f25ad8SToby Isaac   PetscInt Nf;
12829566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(dim, formDegree, &Nf));
1283d8f25ad8SToby Isaac   PetscInt Nk;
12849566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(dim + jetDegree, dim, &Nk));
12859566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(p, Nbpt * Nf * Nk * npoints));
1286d8f25ad8SToby Isaac 
1287d8f25ad8SToby Isaac   PetscInt Nbpm1; // number of scalar polynomials up to degree - 1;
12889566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(dim + degree - 1, dim, &Nbpm1));
1289d8f25ad8SToby Isaac   PetscReal *p_scalar;
12909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nbpm1 * Nk * npoints, &p_scalar));
12919566063dSJacob Faibussowitsch   PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p_scalar));
1292d8f25ad8SToby Isaac   PetscInt total = 0;
1293d8f25ad8SToby Isaac   // First add the full polynomials up to degree - 1 into the basis: take the scalar
1294d8f25ad8SToby Isaac   // and copy one for each form component
1295d8f25ad8SToby Isaac   for (PetscInt i = 0; i < Nbpm1; i++) {
1296d8f25ad8SToby Isaac     const PetscReal *src = &p_scalar[i * Nk * npoints];
1297d8f25ad8SToby Isaac     for (PetscInt f = 0; f < Nf; f++) {
1298d8f25ad8SToby Isaac       PetscReal *dest = &p[(total++ * Nf + f) * Nk * npoints];
12999566063dSJacob Faibussowitsch       PetscCall(PetscArraycpy(dest, src, Nk * npoints));
1300d8f25ad8SToby Isaac     }
1301d8f25ad8SToby Isaac   }
1302d8f25ad8SToby Isaac   PetscInt *form_atoms;
13039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(formDegree + 1, &form_atoms));
1304d8f25ad8SToby Isaac   // construct the interior product pattern
1305d8f25ad8SToby Isaac   PetscInt (*pattern)[3];
1306d8f25ad8SToby Isaac   PetscInt Nf1; // number of formDegree + 1 forms
13079566063dSJacob Faibussowitsch   PetscCall(PetscDTBinomialInt(dim, formDegree + 1, &Nf1));
1308d8f25ad8SToby Isaac   PetscInt nnz = Nf1 * (formDegree + 1);
13099566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Nf1 * (formDegree + 1), &pattern));
13109566063dSJacob Faibussowitsch   PetscCall(PetscDTAltVInteriorPattern(dim, formDegree + 1, pattern));
1311d8f25ad8SToby Isaac   PetscReal centroid = (1. - dim) / (dim + 1.);
1312d8f25ad8SToby Isaac   PetscInt *deriv;
13139566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(dim, &deriv));
1314d8f25ad8SToby Isaac   for (PetscInt d = dim; d >= formDegree + 1; d--) {
1315d8f25ad8SToby Isaac     PetscInt Nfd1; // number of formDegree + 1 forms in dimension d that include dx_0
1316d8f25ad8SToby Isaac                    // (equal to the number of formDegree forms in dimension d-1)
13179566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomialInt(d - 1, formDegree, &Nfd1));
1318d8f25ad8SToby Isaac     // The number of homogeneous (degree-1) scalar polynomials in d variables
1319d8f25ad8SToby Isaac     PetscInt Nh;
13209566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomialInt(d - 1 + degree - 1, d - 1, &Nh));
1321d8f25ad8SToby Isaac     const PetscReal *h_scalar = &p_scalar[(Nbpm1 - Nh) * Nk * npoints];
1322d8f25ad8SToby Isaac     for (PetscInt b = 0; b < Nh; b++) {
1323d8f25ad8SToby Isaac       const PetscReal *h_s = &h_scalar[b * Nk * npoints];
1324d8f25ad8SToby Isaac       for (PetscInt f = 0; f < Nfd1; f++) {
1325d8f25ad8SToby Isaac         // construct all formDegree+1 forms that start with dx_(dim - d) /\ ...
1326d8f25ad8SToby Isaac         form_atoms[0] = dim - d;
13279566063dSJacob Faibussowitsch         PetscCall(PetscDTEnumSubset(d - 1, formDegree, f, &form_atoms[1]));
1328ad540459SPierre Jolivet         for (PetscInt i = 0; i < formDegree; i++) form_atoms[1 + i] += form_atoms[0] + 1;
1329d8f25ad8SToby Isaac         PetscInt f_ind; // index of the resulting form
13309566063dSJacob Faibussowitsch         PetscCall(PetscDTSubsetIndex(dim, formDegree + 1, form_atoms, &f_ind));
1331d8f25ad8SToby Isaac         PetscReal *p_f = &p[total++ * Nf * Nk * npoints];
1332d8f25ad8SToby Isaac         for (PetscInt nz = 0; nz < nnz; nz++) {
1333d8f25ad8SToby Isaac           PetscInt  i     = pattern[nz][0]; // formDegree component
1334d8f25ad8SToby Isaac           PetscInt  j     = pattern[nz][1]; // (formDegree + 1) component
1335d8f25ad8SToby Isaac           PetscInt  v     = pattern[nz][2]; // coordinate component
1336d8f25ad8SToby Isaac           PetscReal scale = v < 0 ? -1. : 1.;
1337d8f25ad8SToby Isaac 
1338d8f25ad8SToby Isaac           i     = formNegative ? (Nf - 1 - i) : i;
1339d8f25ad8SToby Isaac           scale = (formNegative && (i & 1)) ? -scale : scale;
1340d8f25ad8SToby Isaac           v     = v < 0 ? -(v + 1) : v;
1341ad540459SPierre Jolivet           if (j != f_ind) continue;
1342d8f25ad8SToby Isaac           PetscReal *p_i = &p_f[i * Nk * npoints];
1343d8f25ad8SToby Isaac           for (PetscInt jet = 0; jet < Nk; jet++) {
1344d8f25ad8SToby Isaac             const PetscReal *h_jet = &h_s[jet * npoints];
1345d8f25ad8SToby Isaac             PetscReal       *p_jet = &p_i[jet * npoints];
1346d8f25ad8SToby Isaac 
1347ad540459SPierre Jolivet             for (PetscInt pt = 0; pt < npoints; pt++) p_jet[pt] += scale * h_jet[pt] * (points[pt * dim + v] - centroid);
13489566063dSJacob Faibussowitsch             PetscCall(PetscDTIndexToGradedOrder(dim, jet, deriv));
1349d8f25ad8SToby Isaac             deriv[v]++;
1350d8f25ad8SToby Isaac             PetscReal mult = deriv[v];
1351d8f25ad8SToby Isaac             PetscInt  l;
13529566063dSJacob Faibussowitsch             PetscCall(PetscDTGradedOrderToIndex(dim, deriv, &l));
1353ad540459SPierre Jolivet             if (l >= Nk) continue;
1354d8f25ad8SToby Isaac             p_jet = &p_i[l * npoints];
1355ad540459SPierre Jolivet             for (PetscInt pt = 0; pt < npoints; pt++) p_jet[pt] += scale * mult * h_jet[pt];
1356d8f25ad8SToby Isaac             deriv[v]--;
1357d8f25ad8SToby Isaac           }
1358d8f25ad8SToby Isaac         }
1359d8f25ad8SToby Isaac       }
1360d8f25ad8SToby Isaac     }
1361d8f25ad8SToby Isaac   }
136208401ef6SPierre Jolivet   PetscCheck(total == Nbpt, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrectly counted P trimmed polynomials");
13639566063dSJacob Faibussowitsch   PetscCall(PetscFree(deriv));
13649566063dSJacob Faibussowitsch   PetscCall(PetscFree(pattern));
13659566063dSJacob Faibussowitsch   PetscCall(PetscFree(form_atoms));
13669566063dSJacob Faibussowitsch   PetscCall(PetscFree(p_scalar));
13673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1368d8f25ad8SToby Isaac }
1369d8f25ad8SToby Isaac 
1370d8f25ad8SToby Isaac /*@
1371d8f25ad8SToby Isaac   PetscDTPTrimmedEvalJet - Evaluate the jet (function and derivatives) of a basis of the trimmed polynomial k-forms up to
1372d8f25ad8SToby Isaac   a given degree.
1373d8f25ad8SToby Isaac 
1374d8f25ad8SToby Isaac   Input Parameters:
1375d8f25ad8SToby Isaac + dim        - the number of variables in the multivariate polynomials
1376d8f25ad8SToby Isaac . npoints    - the number of points to evaluate the polynomials at
1377d8f25ad8SToby Isaac . points     - [npoints x dim] array of point coordinates
1378d8f25ad8SToby Isaac . degree     - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space to evaluate.
1379d8f25ad8SToby Isaac            There are ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) polynomials in this space.
1380dce8aebaSBarry Smith            (You can use `PetscDTPTrimmedSize()` to compute this size.)
1381d8f25ad8SToby Isaac . formDegree - the degree of the form
1382d8f25ad8SToby Isaac - jetDegree  - the maximum order partial derivative to evaluate in the jet.  There are ((dim + jetDegree) choose dim) partial derivatives
1383d8f25ad8SToby Isaac               in the jet.  Choosing jetDegree = 0 means to evaluate just the function and no derivatives
1384d8f25ad8SToby Isaac 
138520f4b53cSBarry Smith   Output Parameter:
1386a4e35b19SJacob Faibussowitsch . p - an array containing the evaluations of the PKD polynomials' jets on the points.
138760225df5SJacob Faibussowitsch 
1388a4e35b19SJacob Faibussowitsch   Level: advanced
1389a4e35b19SJacob Faibussowitsch 
1390a4e35b19SJacob Faibussowitsch   Notes:
1391a4e35b19SJacob Faibussowitsch   The size of `p` is `PetscDTPTrimmedSize()` x ((dim + formDegree) choose dim) x ((dim + k)
1392a4e35b19SJacob Faibussowitsch   choose dim) x npoints,which also describes the order of the dimensions of this
1393a4e35b19SJacob Faibussowitsch   four-dimensional array\:
1394a4e35b19SJacob Faibussowitsch 
1395d8f25ad8SToby Isaac   the first (slowest varying) dimension is basis function index;
1396d8f25ad8SToby Isaac   the second dimension is component of the form;
1397d8f25ad8SToby Isaac   the third dimension is jet index;
1398d8f25ad8SToby Isaac   the fourth (fastest varying) dimension is the index of the evaluation point.
1399d8f25ad8SToby Isaac 
1400dce8aebaSBarry Smith   The ordering of the basis functions is not graded, so the basis functions are not nested by degree like `PetscDTPKDEvalJet()`.
1401d8f25ad8SToby Isaac   The basis functions are not an L2-orthonormal basis on any particular domain.
1402d8f25ad8SToby Isaac 
1403d8f25ad8SToby Isaac   The implementation is based on the description of the trimmed polynomials up to degree r as
1404d8f25ad8SToby Isaac   the direct sum of polynomials up to degree (r-1) and the Koszul differential applied to
1405d8f25ad8SToby Isaac   homogeneous polynomials of degree (r-1).
1406d8f25ad8SToby Isaac 
1407db781477SPatrick Sanan .seealso: `PetscDTPKDEvalJet()`, `PetscDTPTrimmedSize()`
1408d8f25ad8SToby Isaac @*/
PetscDTPTrimmedEvalJet(PetscInt dim,PetscInt npoints,const PetscReal points[],PetscInt degree,PetscInt formDegree,PetscInt jetDegree,PetscReal p[])1409d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTPTrimmedEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[])
1410d71ae5a4SJacob Faibussowitsch {
1411d8f25ad8SToby Isaac   PetscFunctionBegin;
14129566063dSJacob Faibussowitsch   PetscCall(PetscDTPTrimmedEvalJet_Internal(dim, npoints, points, degree, formDegree, jetDegree, p));
14133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1414d8f25ad8SToby Isaac }
1415d8f25ad8SToby Isaac 
1416e6a796c3SToby Isaac /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V
1417e6a796c3SToby Isaac  * with lds n; diag and subdiag are overwritten */
PetscDTSymmetricTridiagonalEigensolve(PetscInt n,PetscReal diag[],PetscReal subdiag[],PetscReal eigs[],PetscScalar V[])1418d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[], PetscReal eigs[], PetscScalar V[])
1419d71ae5a4SJacob Faibussowitsch {
1420e6a796c3SToby Isaac   char          jobz   = 'V'; /* eigenvalues and eigenvectors */
1421e6a796c3SToby Isaac   char          range  = 'A'; /* all eigenvalues will be found */
1422e6a796c3SToby Isaac   PetscReal     VL     = 0.;  /* ignored because range is 'A' */
1423e6a796c3SToby Isaac   PetscReal     VU     = 0.;  /* ignored because range is 'A' */
1424e6a796c3SToby Isaac   PetscBLASInt  IL     = 0;   /* ignored because range is 'A' */
1425e6a796c3SToby Isaac   PetscBLASInt  IU     = 0;   /* ignored because range is 'A' */
1426e6a796c3SToby Isaac   PetscReal     abstol = 0.;  /* unused */
1427e6a796c3SToby Isaac   PetscBLASInt  bn, bm, ldz;  /* bm will equal bn on exit */
1428e6a796c3SToby Isaac   PetscBLASInt *isuppz;
1429e6a796c3SToby Isaac   PetscBLASInt  lwork, liwork;
1430e6a796c3SToby Isaac   PetscReal     workquery;
1431e6a796c3SToby Isaac   PetscBLASInt  iworkquery;
1432e6a796c3SToby Isaac   PetscBLASInt *iwork;
1433e6a796c3SToby Isaac   PetscBLASInt  info;
1434e6a796c3SToby Isaac   PetscReal    *work = NULL;
1435e6a796c3SToby Isaac 
1436e6a796c3SToby Isaac   PetscFunctionBegin;
1437e6a796c3SToby Isaac #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG)
1438e6a796c3SToby Isaac   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found");
1439e6a796c3SToby Isaac #endif
14409566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(n, &bn));
14419566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(n, &ldz));
1442e6a796c3SToby Isaac #if !defined(PETSC_MISSING_LAPACK_STEGR)
14439566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * n, &isuppz));
1444e6a796c3SToby Isaac   lwork  = -1;
1445e6a796c3SToby Isaac   liwork = -1;
1446792fecdfSBarry Smith   PetscCallBLAS("LAPACKstegr", LAPACKstegr_(&jobz, &range, &bn, diag, subdiag, &VL, &VU, &IL, &IU, &abstol, &bm, eigs, V, &ldz, isuppz, &workquery, &lwork, &iworkquery, &liwork, &info));
144728b400f6SJacob Faibussowitsch   PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xSTEGR error");
1448e6a796c3SToby Isaac   lwork  = (PetscBLASInt)workquery;
1449835f2295SStefano Zampini   liwork = iworkquery;
14509566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(lwork, &work, liwork, &iwork));
14519566063dSJacob Faibussowitsch   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
1452792fecdfSBarry Smith   PetscCallBLAS("LAPACKstegr", LAPACKstegr_(&jobz, &range, &bn, diag, subdiag, &VL, &VU, &IL, &IU, &abstol, &bm, eigs, V, &ldz, isuppz, work, &lwork, iwork, &liwork, &info));
14539566063dSJacob Faibussowitsch   PetscCall(PetscFPTrapPop());
145428b400f6SJacob Faibussowitsch   PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xSTEGR error");
14559566063dSJacob Faibussowitsch   PetscCall(PetscFree2(work, iwork));
14569566063dSJacob Faibussowitsch   PetscCall(PetscFree(isuppz));
1457e6a796c3SToby Isaac #elif !defined(PETSC_MISSING_LAPACK_STEQR)
1458e6a796c3SToby Isaac   jobz = 'I'; /* Compute eigenvalues and eigenvectors of the
1459e6a796c3SToby Isaac                  tridiagonal matrix.  Z is initialized to the identity
1460e6a796c3SToby Isaac                  matrix. */
14619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(1, 2 * n - 2), &work));
1462792fecdfSBarry Smith   PetscCallBLAS("LAPACKsteqr", LAPACKsteqr_("I", &bn, diag, subdiag, V, &ldz, work, &info));
14639566063dSJacob Faibussowitsch   PetscCall(PetscFPTrapPop());
146428b400f6SJacob Faibussowitsch   PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xSTEQR error");
14659566063dSJacob Faibussowitsch   PetscCall(PetscFree(work));
14669566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(eigs, diag, n));
1467e6a796c3SToby Isaac #endif
14683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1469e6a796c3SToby Isaac }
1470e6a796c3SToby Isaac 
1471e6a796c3SToby Isaac /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi
1472e6a796c3SToby Isaac  * quadrature rules on the interval [-1, 1] */
PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n,PetscReal alpha,PetscReal beta,PetscReal * leftw,PetscReal * rightw)1473d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw)
1474d71ae5a4SJacob Faibussowitsch {
1475e6a796c3SToby Isaac   PetscReal twoab1;
1476e6a796c3SToby Isaac   PetscInt  m = n - 2;
1477e6a796c3SToby Isaac   PetscReal a = alpha + 1.;
1478e6a796c3SToby Isaac   PetscReal b = beta + 1.;
1479e6a796c3SToby Isaac   PetscReal gra, grb;
1480e6a796c3SToby Isaac 
1481e6a796c3SToby Isaac   PetscFunctionBegin;
1482e6a796c3SToby Isaac   twoab1 = PetscPowReal(2., a + b - 1.);
1483e6a796c3SToby Isaac #if defined(PETSC_HAVE_LGAMMA)
14849371c9d4SSatish Balay   grb = PetscExpReal(2. * PetscLGamma(b + 1.) + PetscLGamma(m + 1.) + PetscLGamma(m + a + 1.) - (PetscLGamma(m + b + 1) + PetscLGamma(m + a + b + 1.)));
14859371c9d4SSatish Balay   gra = PetscExpReal(2. * PetscLGamma(a + 1.) + PetscLGamma(m + 1.) + PetscLGamma(m + b + 1.) - (PetscLGamma(m + a + 1) + PetscLGamma(m + a + b + 1.)));
1486e6a796c3SToby Isaac #else
1487e6a796c3SToby Isaac   {
1488966bd95aSPierre Jolivet     PetscReal binom1, binom2;
1489e6a796c3SToby Isaac     PetscInt  alphai = (PetscInt)alpha;
1490e6a796c3SToby Isaac     PetscInt  betai  = (PetscInt)beta;
1491e6a796c3SToby Isaac 
1492966bd95aSPierre Jolivet     PetscCheck((PetscReal)alphai == alpha && (PetscReal)betai == beta, PETSC_COMM_SELF, PETSC_ERR_SUP, "lgamma() - math routine is unavailable.");
14939566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomial(m + b, b, &binom1));
14949566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomial(m + a + b, b, &binom2));
1495e6a796c3SToby Isaac     grb = 1. / (binom1 * binom2);
14969566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomial(m + a, a, &binom1));
14979566063dSJacob Faibussowitsch     PetscCall(PetscDTBinomial(m + a + b, a, &binom2));
1498e6a796c3SToby Isaac     gra = 1. / (binom1 * binom2);
1499e6a796c3SToby Isaac   }
1500e6a796c3SToby Isaac #endif
1501e6a796c3SToby Isaac   *leftw  = twoab1 * grb / b;
1502e6a796c3SToby Isaac   *rightw = twoab1 * gra / a;
15033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1504e6a796c3SToby Isaac }
1505e6a796c3SToby Isaac 
1506e6a796c3SToby Isaac /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x.
1507e6a796c3SToby Isaac    Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */
PetscDTComputeJacobi(PetscReal a,PetscReal b,PetscInt n,PetscReal x,PetscReal * P)1508d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P)
1509d71ae5a4SJacob Faibussowitsch {
151094e21283SToby Isaac   PetscReal pn1, pn2;
151194e21283SToby Isaac   PetscReal cnm1, cnm1x, cnm2;
1512e6a796c3SToby Isaac   PetscInt  k;
1513e6a796c3SToby Isaac 
1514e6a796c3SToby Isaac   PetscFunctionBegin;
15159371c9d4SSatish Balay   if (!n) {
15169371c9d4SSatish Balay     *P = 1.0;
15173ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
15189371c9d4SSatish Balay   }
151994e21283SToby Isaac   PetscDTJacobiRecurrence_Internal(1, a, b, cnm1, cnm1x, cnm2);
152094e21283SToby Isaac   pn2 = 1.;
152194e21283SToby Isaac   pn1 = cnm1 + cnm1x * x;
15229371c9d4SSatish Balay   if (n == 1) {
15239371c9d4SSatish Balay     *P = pn1;
15243ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
15259371c9d4SSatish Balay   }
1526e6a796c3SToby Isaac   *P = 0.0;
1527e6a796c3SToby Isaac   for (k = 2; k < n + 1; ++k) {
152894e21283SToby Isaac     PetscDTJacobiRecurrence_Internal(k, a, b, cnm1, cnm1x, cnm2);
1529e6a796c3SToby Isaac 
153094e21283SToby Isaac     *P  = (cnm1 + cnm1x * x) * pn1 - cnm2 * pn2;
1531e6a796c3SToby Isaac     pn2 = pn1;
1532e6a796c3SToby Isaac     pn1 = *P;
1533e6a796c3SToby Isaac   }
15343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1535e6a796c3SToby Isaac }
1536e6a796c3SToby Isaac 
1537e6a796c3SToby Isaac /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */
PetscDTComputeJacobiDerivative(PetscReal a,PetscReal b,PetscInt n,PetscReal x,PetscInt k,PetscReal * P)1538d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P)
1539d71ae5a4SJacob Faibussowitsch {
1540e6a796c3SToby Isaac   PetscReal nP;
1541e6a796c3SToby Isaac   PetscInt  i;
1542e6a796c3SToby Isaac 
1543e6a796c3SToby Isaac   PetscFunctionBegin;
154417a42bb7SSatish Balay   *P = 0.0;
15453ba16761SJacob Faibussowitsch   if (k > n) PetscFunctionReturn(PETSC_SUCCESS);
15469566063dSJacob Faibussowitsch   PetscCall(PetscDTComputeJacobi(a + k, b + k, n - k, x, &nP));
1547e6a796c3SToby Isaac   for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5;
1548e6a796c3SToby Isaac   *P = nP;
15493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1550e6a796c3SToby Isaac }
1551e6a796c3SToby Isaac 
PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints,PetscReal a,PetscReal b,PetscReal x[],PetscReal w[])1552d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[])
1553d71ae5a4SJacob Faibussowitsch {
1554e6a796c3SToby Isaac   PetscInt  maxIter = 100;
155594e21283SToby Isaac   PetscReal eps     = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON));
1556200b5abcSJed Brown   PetscReal a1, a6, gf;
1557e6a796c3SToby Isaac   PetscInt  k;
1558e6a796c3SToby Isaac 
1559e6a796c3SToby Isaac   PetscFunctionBegin;
1560e6a796c3SToby Isaac   a1 = PetscPowReal(2.0, a + b + 1);
156194e21283SToby Isaac #if defined(PETSC_HAVE_LGAMMA)
1562200b5abcSJed Brown   {
1563200b5abcSJed Brown     PetscReal a2, a3, a4, a5;
156494e21283SToby Isaac     a2 = PetscLGamma(a + npoints + 1);
156594e21283SToby Isaac     a3 = PetscLGamma(b + npoints + 1);
156694e21283SToby Isaac     a4 = PetscLGamma(a + b + npoints + 1);
156794e21283SToby Isaac     a5 = PetscLGamma(npoints + 1);
156894e21283SToby Isaac     gf = PetscExpReal(a2 + a3 - (a4 + a5));
1569200b5abcSJed Brown   }
1570e6a796c3SToby Isaac #else
1571e6a796c3SToby Isaac   {
1572e6a796c3SToby Isaac     PetscInt ia, ib;
1573e6a796c3SToby Isaac 
1574e6a796c3SToby Isaac     ia = (PetscInt)a;
1575e6a796c3SToby Isaac     ib = (PetscInt)b;
157694e21283SToby Isaac     gf = 1.;
157794e21283SToby Isaac     if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */
157894e21283SToby Isaac       for (k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k);
157994e21283SToby Isaac     } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */
158094e21283SToby Isaac       for (k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k);
158194e21283SToby Isaac     } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "lgamma() - math routine is unavailable.");
1582e6a796c3SToby Isaac   }
1583e6a796c3SToby Isaac #endif
1584e6a796c3SToby Isaac 
158594e21283SToby Isaac   a6 = a1 * gf;
1586e6a796c3SToby Isaac   /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses.
1587e6a796c3SToby Isaac    Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */
1588e6a796c3SToby Isaac   for (k = 0; k < npoints; ++k) {
158994e21283SToby Isaac     PetscReal r = PetscCosReal(PETSC_PI * (1. - (4. * k + 3. + 2. * b) / (4. * npoints + 2. * (a + b + 1.)))), dP;
1590e6a796c3SToby Isaac     PetscInt  j;
1591e6a796c3SToby Isaac 
1592e6a796c3SToby Isaac     if (k > 0) r = 0.5 * (r + x[k - 1]);
1593e6a796c3SToby Isaac     for (j = 0; j < maxIter; ++j) {
1594e6a796c3SToby Isaac       PetscReal s = 0.0, delta, f, fp;
1595e6a796c3SToby Isaac       PetscInt  i;
1596e6a796c3SToby Isaac 
1597e6a796c3SToby Isaac       for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]);
15989566063dSJacob Faibussowitsch       PetscCall(PetscDTComputeJacobi(a, b, npoints, r, &f));
15999566063dSJacob Faibussowitsch       PetscCall(PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp));
1600e6a796c3SToby Isaac       delta = f / (fp - f * s);
1601e6a796c3SToby Isaac       r     = r - delta;
1602e6a796c3SToby Isaac       if (PetscAbsReal(delta) < eps) break;
1603e6a796c3SToby Isaac     }
1604e6a796c3SToby Isaac     x[k] = r;
16059566063dSJacob Faibussowitsch     PetscCall(PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP));
1606e6a796c3SToby Isaac     w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP);
1607e6a796c3SToby Isaac   }
16083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1609e6a796c3SToby Isaac }
1610e6a796c3SToby Isaac 
161194e21283SToby Isaac /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi
1612e6a796c3SToby Isaac  * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */
PetscDTJacobiMatrix_Internal(PetscInt nPoints,PetscReal a,PetscReal b,PetscReal * d,PetscReal * s)1613d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s)
1614d71ae5a4SJacob Faibussowitsch {
1615e6a796c3SToby Isaac   PetscInt i;
1616e6a796c3SToby Isaac 
1617e6a796c3SToby Isaac   PetscFunctionBegin;
1618e6a796c3SToby Isaac   for (i = 0; i < nPoints; i++) {
161994e21283SToby Isaac     PetscReal A, B, C;
1620e6a796c3SToby Isaac 
162194e21283SToby Isaac     PetscDTJacobiRecurrence_Internal(i + 1, a, b, A, B, C);
162294e21283SToby Isaac     d[i] = -A / B;
162394e21283SToby Isaac     if (i) s[i - 1] *= C / B;
162494e21283SToby Isaac     if (i < nPoints - 1) s[i] = 1. / B;
1625e6a796c3SToby Isaac   }
16263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1627e6a796c3SToby Isaac }
1628e6a796c3SToby Isaac 
PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints,PetscReal a,PetscReal b,PetscReal * x,PetscReal * w)1629d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w)
1630d71ae5a4SJacob Faibussowitsch {
1631e6a796c3SToby Isaac   PetscReal mu0;
1632e6a796c3SToby Isaac   PetscReal ga, gb, gab;
1633e6a796c3SToby Isaac   PetscInt  i;
1634e6a796c3SToby Isaac 
1635e6a796c3SToby Isaac   PetscFunctionBegin;
16369566063dSJacob Faibussowitsch   PetscCall(PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite));
1637e6a796c3SToby Isaac 
1638e6a796c3SToby Isaac #if defined(PETSC_HAVE_TGAMMA)
1639e6a796c3SToby Isaac   ga  = PetscTGamma(a + 1);
1640e6a796c3SToby Isaac   gb  = PetscTGamma(b + 1);
1641e6a796c3SToby Isaac   gab = PetscTGamma(a + b + 2);
1642e6a796c3SToby Isaac #else
1643e6a796c3SToby Isaac   {
1644966bd95aSPierre Jolivet     PetscInt ia = (PetscInt)a, ib = (PetscInt)b;
1645e6a796c3SToby Isaac 
1646966bd95aSPierre Jolivet     PetscCheck(ia == a && ib == b && ia + 1 > 0 && ib + 1 > 0 && ia + ib + 2 > 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "tgamma() - math routine is unavailable.");
1647966bd95aSPierre Jolivet     /* All gamma(x) terms are (x-1)! terms */
16489566063dSJacob Faibussowitsch     PetscCall(PetscDTFactorial(ia, &ga));
16499566063dSJacob Faibussowitsch     PetscCall(PetscDTFactorial(ib, &gb));
16506205a86aSPierre Jolivet     PetscCall(PetscDTFactorial(ia + ib + 1, &gab));
1651e6a796c3SToby Isaac   }
1652e6a796c3SToby Isaac #endif
1653e6a796c3SToby Isaac   mu0 = PetscPowReal(2., a + b + 1.) * ga * gb / gab;
1654e6a796c3SToby Isaac 
1655e6a796c3SToby Isaac #if defined(PETSCDTGAUSSIANQUADRATURE_EIG)
1656e6a796c3SToby Isaac   {
1657e6a796c3SToby Isaac     PetscReal   *diag, *subdiag;
1658e6a796c3SToby Isaac     PetscScalar *V;
1659e6a796c3SToby Isaac 
16609566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(npoints, &diag, npoints, &subdiag));
16619566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(npoints * npoints, &V));
16629566063dSJacob Faibussowitsch     PetscCall(PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag));
1663e6a796c3SToby Isaac     for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]);
16649566063dSJacob Faibussowitsch     PetscCall(PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V));
166594e21283SToby Isaac     for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0;
16669566063dSJacob Faibussowitsch     PetscCall(PetscFree(V));
16679566063dSJacob Faibussowitsch     PetscCall(PetscFree2(diag, subdiag));
1668e6a796c3SToby Isaac   }
1669e6a796c3SToby Isaac #else
1670e6a796c3SToby Isaac   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found");
1671e6a796c3SToby Isaac #endif
167294e21283SToby Isaac   { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the
167394e21283SToby Isaac        eigenvalues are not guaranteed to be in ascending order.  So we heave a passive aggressive sigh and check that
167494e21283SToby Isaac        the eigenvalues are sorted */
167594e21283SToby Isaac     PetscBool sorted;
167694e21283SToby Isaac 
16779566063dSJacob Faibussowitsch     PetscCall(PetscSortedReal(npoints, x, &sorted));
167894e21283SToby Isaac     if (!sorted) {
167994e21283SToby Isaac       PetscInt  *order, i;
168094e21283SToby Isaac       PetscReal *tmp;
168194e21283SToby Isaac 
16829566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(npoints, &order, npoints, &tmp));
168394e21283SToby Isaac       for (i = 0; i < npoints; i++) order[i] = i;
16849566063dSJacob Faibussowitsch       PetscCall(PetscSortRealWithPermutation(npoints, x, order));
16859566063dSJacob Faibussowitsch       PetscCall(PetscArraycpy(tmp, x, npoints));
168694e21283SToby Isaac       for (i = 0; i < npoints; i++) x[i] = tmp[order[i]];
16879566063dSJacob Faibussowitsch       PetscCall(PetscArraycpy(tmp, w, npoints));
168894e21283SToby Isaac       for (i = 0; i < npoints; i++) w[i] = tmp[order[i]];
16899566063dSJacob Faibussowitsch       PetscCall(PetscFree2(order, tmp));
169094e21283SToby Isaac     }
169194e21283SToby Isaac   }
16923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1693e6a796c3SToby Isaac }
1694e6a796c3SToby Isaac 
PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha,PetscReal beta,PetscReal x[],PetscReal w[],PetscBool newton)1695d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton)
1696d71ae5a4SJacob Faibussowitsch {
1697e6a796c3SToby Isaac   PetscFunctionBegin;
169808401ef6SPierre Jolivet   PetscCheck(npoints >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of points must be positive");
1699e6a796c3SToby Isaac   /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */
170008401ef6SPierre Jolivet   PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "alpha must be > -1.");
170108401ef6SPierre Jolivet   PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "beta must be > -1.");
1702e6a796c3SToby Isaac 
17031baa6e33SBarry Smith   if (newton) PetscCall(PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w));
17041baa6e33SBarry Smith   else PetscCall(PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w));
1705e6a796c3SToby Isaac   if (alpha == beta) { /* symmetrize */
1706e6a796c3SToby Isaac     PetscInt i;
1707e6a796c3SToby Isaac     for (i = 0; i < (npoints + 1) / 2; i++) {
1708e6a796c3SToby Isaac       PetscInt  j  = npoints - 1 - i;
1709e6a796c3SToby Isaac       PetscReal xi = x[i];
1710e6a796c3SToby Isaac       PetscReal xj = x[j];
1711e6a796c3SToby Isaac       PetscReal wi = w[i];
1712e6a796c3SToby Isaac       PetscReal wj = w[j];
1713e6a796c3SToby Isaac 
1714e6a796c3SToby Isaac       x[i] = (xi - xj) / 2.;
1715e6a796c3SToby Isaac       x[j] = (xj - xi) / 2.;
1716e6a796c3SToby Isaac       w[i] = w[j] = (wi + wj) / 2.;
1717e6a796c3SToby Isaac     }
1718e6a796c3SToby Isaac   }
17193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1720e6a796c3SToby Isaac }
1721e6a796c3SToby Isaac 
172294e21283SToby Isaac /*@
17231d27aa22SBarry Smith   PetscDTGaussJacobiQuadrature - quadrature for the interval $[a, b]$ with the weight function
172494e21283SToby Isaac   $(x-a)^\alpha (x-b)^\beta$.
172594e21283SToby Isaac 
172620f4b53cSBarry Smith   Not Collective
172794e21283SToby Isaac 
172894e21283SToby Isaac   Input Parameters:
172994e21283SToby Isaac + npoints - the number of points in the quadrature rule
173094e21283SToby Isaac . a       - the left endpoint of the interval
173194e21283SToby Isaac . b       - the right endpoint of the interval
173294e21283SToby Isaac . alpha   - the left exponent
173394e21283SToby Isaac - beta    - the right exponent
173494e21283SToby Isaac 
173594e21283SToby Isaac   Output Parameters:
173620f4b53cSBarry Smith + x - array of length `npoints`, the locations of the quadrature points
173720f4b53cSBarry Smith - w - array of length `npoints`, the weights of the quadrature points
173894e21283SToby Isaac 
173994e21283SToby Isaac   Level: intermediate
174094e21283SToby Isaac 
1741dce8aebaSBarry Smith   Note:
17421d27aa22SBarry Smith   This quadrature rule is exact for polynomials up to degree 2*`npoints` - 1.
1743dce8aebaSBarry Smith 
1744dce8aebaSBarry Smith .seealso: `PetscDTGaussQuadrature()`
174594e21283SToby Isaac @*/
PetscDTGaussJacobiQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal alpha,PetscReal beta,PetscReal x[],PetscReal w[])1746d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt npoints, PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[])
1747d71ae5a4SJacob Faibussowitsch {
174894e21283SToby Isaac   PetscInt i;
1749e6a796c3SToby Isaac 
1750e6a796c3SToby Isaac   PetscFunctionBegin;
17519566063dSJacob Faibussowitsch   PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal));
175294e21283SToby Isaac   if (a != -1. || b != 1.) { /* shift */
175394e21283SToby Isaac     for (i = 0; i < npoints; i++) {
175494e21283SToby Isaac       x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
175594e21283SToby Isaac       w[i] *= (b - a) / 2.;
175694e21283SToby Isaac     }
175794e21283SToby Isaac   }
17583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1759e6a796c3SToby Isaac }
1760e6a796c3SToby Isaac 
PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints,PetscReal alpha,PetscReal beta,PetscReal x[],PetscReal w[],PetscBool newton)1761d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton)
1762d71ae5a4SJacob Faibussowitsch {
1763e6a796c3SToby Isaac   PetscInt i;
1764e6a796c3SToby Isaac 
1765e6a796c3SToby Isaac   PetscFunctionBegin;
176608401ef6SPierre Jolivet   PetscCheck(npoints >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of points must be positive");
1767e6a796c3SToby Isaac   /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */
176808401ef6SPierre Jolivet   PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "alpha must be > -1.");
176908401ef6SPierre Jolivet   PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "beta must be > -1.");
1770e6a796c3SToby Isaac 
1771e6a796c3SToby Isaac   x[0]           = -1.;
1772e6a796c3SToby Isaac   x[npoints - 1] = 1.;
177348a46eb9SPierre Jolivet   if (npoints > 2) PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints - 2, alpha + 1., beta + 1., &x[1], &w[1], newton));
1774ad540459SPierre Jolivet   for (i = 1; i < npoints - 1; i++) w[i] /= (1. - x[i] * x[i]);
17759566063dSJacob Faibussowitsch   PetscCall(PetscDTGaussLobattoJacobiEndweights_Internal(npoints, alpha, beta, &w[0], &w[npoints - 1]));
17763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1777e6a796c3SToby Isaac }
1778e6a796c3SToby Isaac 
177937045ce4SJed Brown /*@
17801d27aa22SBarry Smith   PetscDTGaussLobattoJacobiQuadrature - quadrature for the interval $[a, b]$ with the weight function
17811d27aa22SBarry Smith   $(x-a)^\alpha (x-b)^\beta$, with endpoints `a` and `b` included as quadrature points.
178294e21283SToby Isaac 
178320f4b53cSBarry Smith   Not Collective
178494e21283SToby Isaac 
178594e21283SToby Isaac   Input Parameters:
178694e21283SToby Isaac + npoints - the number of points in the quadrature rule
178794e21283SToby Isaac . a       - the left endpoint of the interval
178894e21283SToby Isaac . b       - the right endpoint of the interval
178994e21283SToby Isaac . alpha   - the left exponent
179094e21283SToby Isaac - beta    - the right exponent
179194e21283SToby Isaac 
179294e21283SToby Isaac   Output Parameters:
179320f4b53cSBarry Smith + x - array of length `npoints`, the locations of the quadrature points
179420f4b53cSBarry Smith - w - array of length `npoints`, the weights of the quadrature points
179594e21283SToby Isaac 
179694e21283SToby Isaac   Level: intermediate
179794e21283SToby Isaac 
1798dce8aebaSBarry Smith   Note:
17991d27aa22SBarry Smith   This quadrature rule is exact for polynomials up to degree 2*`npoints` - 3.
1800dce8aebaSBarry Smith 
1801dce8aebaSBarry Smith .seealso: `PetscDTGaussJacobiQuadrature()`
180294e21283SToby Isaac @*/
PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal alpha,PetscReal beta,PetscReal x[],PetscReal w[])1803d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints, PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[])
1804d71ae5a4SJacob Faibussowitsch {
180594e21283SToby Isaac   PetscInt i;
180694e21283SToby Isaac 
180794e21283SToby Isaac   PetscFunctionBegin;
18089566063dSJacob Faibussowitsch   PetscCall(PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal));
180994e21283SToby Isaac   if (a != -1. || b != 1.) { /* shift */
181094e21283SToby Isaac     for (i = 0; i < npoints; i++) {
181194e21283SToby Isaac       x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
181294e21283SToby Isaac       w[i] *= (b - a) / 2.;
181394e21283SToby Isaac     }
181494e21283SToby Isaac   }
18153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
181694e21283SToby Isaac }
181794e21283SToby Isaac 
181894e21283SToby Isaac /*@
1819e6a796c3SToby Isaac   PetscDTGaussQuadrature - create Gauss-Legendre quadrature
182037045ce4SJed Brown 
182137045ce4SJed Brown   Not Collective
182237045ce4SJed Brown 
18234165533cSJose E. Roman   Input Parameters:
182437045ce4SJed Brown + npoints - number of points
182537045ce4SJed Brown . a       - left end of interval (often-1)
182637045ce4SJed Brown - b       - right end of interval (often +1)
182737045ce4SJed Brown 
18284165533cSJose E. Roman   Output Parameters:
182937045ce4SJed Brown + x - quadrature points
183037045ce4SJed Brown - w - quadrature weights
183137045ce4SJed Brown 
183237045ce4SJed Brown   Level: intermediate
183337045ce4SJed Brown 
18341d27aa22SBarry Smith   Note:
18351d27aa22SBarry Smith   See {cite}`golub1969calculation`
183637045ce4SJed Brown 
1837dce8aebaSBarry Smith .seealso: `PetscDTLegendreEval()`, `PetscDTGaussJacobiQuadrature()`
183837045ce4SJed Brown @*/
PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal x[],PetscReal w[])1839ce78bad3SBarry Smith PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[])
1840d71ae5a4SJacob Faibussowitsch {
184137045ce4SJed Brown   PetscInt i;
184237045ce4SJed Brown 
184337045ce4SJed Brown   PetscFunctionBegin;
18449566063dSJacob Faibussowitsch   PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints, 0., 0., x, w, PetscDTGaussQuadratureNewton_Internal));
184594e21283SToby Isaac   if (a != -1. || b != 1.) { /* shift */
184637045ce4SJed Brown     for (i = 0; i < npoints; i++) {
1847e6a796c3SToby Isaac       x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
1848e6a796c3SToby Isaac       w[i] *= (b - a) / 2.;
184937045ce4SJed Brown     }
185037045ce4SJed Brown   }
18513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
185237045ce4SJed Brown }
1853194825f6SJed Brown 
18545d83a8b1SBarry Smith /*@
18558272889dSSatish Balay   PetscDTGaussLobattoLegendreQuadrature - creates a set of the locations and weights of the Gauss-Lobatto-Legendre
18561d27aa22SBarry Smith   nodes of a given size on the domain $[-1,1]$
18578272889dSSatish Balay 
18588272889dSSatish Balay   Not Collective
18598272889dSSatish Balay 
1860d8d19677SJose E. Roman   Input Parameters:
186160225df5SJacob Faibussowitsch + npoints - number of grid nodes
1862dce8aebaSBarry Smith - type    - `PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA` or `PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON`
18638272889dSSatish Balay 
18644165533cSJose E. Roman   Output Parameters:
1865f13dfd9eSBarry Smith + x - quadrature points, pass in an array of length `npoints`
1866f13dfd9eSBarry Smith - w - quadrature weights, pass in an array of length `npoints`
18678272889dSSatish Balay 
1868dce8aebaSBarry Smith   Level: intermediate
1869dce8aebaSBarry Smith 
18708272889dSSatish Balay   Notes:
18718272889dSSatish Balay   For n > 30  the Newton approach computes duplicate (incorrect) values for some nodes because the initial guess is apparently not
18728272889dSSatish Balay   close enough to the desired solution
18738272889dSSatish Balay 
18748272889dSSatish Balay   These are useful for implementing spectral methods based on Gauss-Lobatto-Legendre (GLL) nodes
18758272889dSSatish Balay 
18761d27aa22SBarry Smith   See <https://epubs.siam.org/doi/abs/10.1137/110855442>  <https://epubs.siam.org/doi/abs/10.1137/120889873> for better ways to compute GLL nodes
18778272889dSSatish Balay 
1878dce8aebaSBarry Smith .seealso: `PetscDTGaussQuadrature()`, `PetscGaussLobattoLegendreCreateType`
18798272889dSSatish Balay 
18808272889dSSatish Balay @*/
PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints,PetscGaussLobattoLegendreCreateType type,PetscReal x[],PetscReal w[])1881cc4c1da9SBarry Smith PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints, PetscGaussLobattoLegendreCreateType type, PetscReal x[], PetscReal w[])
1882d71ae5a4SJacob Faibussowitsch {
1883e6a796c3SToby Isaac   PetscBool newton;
18848272889dSSatish Balay 
18858272889dSSatish Balay   PetscFunctionBegin;
188608401ef6SPierre Jolivet   PetscCheck(npoints >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must provide at least 2 grid points per element");
188794e21283SToby Isaac   newton = (PetscBool)(type == PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON);
18889566063dSJacob Faibussowitsch   PetscCall(PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, 0., 0., x, w, newton));
18893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18908272889dSSatish Balay }
18918272889dSSatish Balay 
1892744bafbcSMatthew G. Knepley /*@
1893744bafbcSMatthew G. Knepley   PetscDTGaussTensorQuadrature - creates a tensor-product Gauss quadrature
1894744bafbcSMatthew G. Knepley 
1895744bafbcSMatthew G. Knepley   Not Collective
1896744bafbcSMatthew G. Knepley 
18974165533cSJose E. Roman   Input Parameters:
1898744bafbcSMatthew G. Knepley + dim     - The spatial dimension
1899a6b92713SMatthew G. Knepley . Nc      - The number of components
1900744bafbcSMatthew G. Knepley . npoints - number of points in one dimension
1901744bafbcSMatthew G. Knepley . a       - left end of interval (often-1)
1902744bafbcSMatthew G. Knepley - b       - right end of interval (often +1)
1903744bafbcSMatthew G. Knepley 
19044165533cSJose E. Roman   Output Parameter:
1905dce8aebaSBarry Smith . q - A `PetscQuadrature` object
1906744bafbcSMatthew G. Knepley 
1907744bafbcSMatthew G. Knepley   Level: intermediate
1908744bafbcSMatthew G. Knepley 
1909db781477SPatrick Sanan .seealso: `PetscDTGaussQuadrature()`, `PetscDTLegendreEval()`
1910744bafbcSMatthew G. Knepley @*/
PetscDTGaussTensorQuadrature(PetscInt dim,PetscInt Nc,PetscInt npoints,PetscReal a,PetscReal b,PetscQuadrature * q)1911d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q)
1912d71ae5a4SJacob Faibussowitsch {
19134366bac7SMatthew G. Knepley   DMPolytopeType ct;
19144366bac7SMatthew G. Knepley   PetscInt       totpoints = dim > 1 ? dim > 2 ? npoints * PetscSqr(npoints) : PetscSqr(npoints) : npoints;
1915744bafbcSMatthew G. Knepley   PetscReal     *x, *w, *xw, *ww;
1916744bafbcSMatthew G. Knepley 
1917744bafbcSMatthew G. Knepley   PetscFunctionBegin;
19189566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(totpoints * dim, &x));
19199566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(totpoints * Nc, &w));
1920744bafbcSMatthew G. Knepley   /* Set up the Golub-Welsch system */
1921744bafbcSMatthew G. Knepley   switch (dim) {
1922744bafbcSMatthew G. Knepley   case 0:
19234366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_POINT;
19249566063dSJacob Faibussowitsch     PetscCall(PetscFree(x));
19259566063dSJacob Faibussowitsch     PetscCall(PetscFree(w));
19269566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(1, &x));
19279566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(Nc, &w));
19283c1919fdSMatthew G. Knepley     totpoints = 1;
1929744bafbcSMatthew G. Knepley     x[0]      = 0.0;
19304366bac7SMatthew G. Knepley     for (PetscInt c = 0; c < Nc; ++c) w[c] = 1.0;
1931744bafbcSMatthew G. Knepley     break;
1932744bafbcSMatthew G. Knepley   case 1:
19334366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_SEGMENT;
19349566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(npoints, &ww));
19359566063dSJacob Faibussowitsch     PetscCall(PetscDTGaussQuadrature(npoints, a, b, x, ww));
19364366bac7SMatthew G. Knepley     for (PetscInt i = 0; i < npoints; ++i)
19374366bac7SMatthew G. Knepley       for (PetscInt c = 0; c < Nc; ++c) w[i * Nc + c] = ww[i];
19389566063dSJacob Faibussowitsch     PetscCall(PetscFree(ww));
1939744bafbcSMatthew G. Knepley     break;
1940744bafbcSMatthew G. Knepley   case 2:
19414366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_QUADRILATERAL;
19429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(npoints, &xw, npoints, &ww));
19439566063dSJacob Faibussowitsch     PetscCall(PetscDTGaussQuadrature(npoints, a, b, xw, ww));
19444366bac7SMatthew G. Knepley     for (PetscInt i = 0; i < npoints; ++i) {
19454366bac7SMatthew G. Knepley       for (PetscInt j = 0; j < npoints; ++j) {
1946744bafbcSMatthew G. Knepley         x[(i * npoints + j) * dim + 0] = xw[i];
1947744bafbcSMatthew G. Knepley         x[(i * npoints + j) * dim + 1] = xw[j];
19484366bac7SMatthew G. Knepley         for (PetscInt c = 0; c < Nc; ++c) w[(i * npoints + j) * Nc + c] = ww[i] * ww[j];
1949744bafbcSMatthew G. Knepley       }
1950744bafbcSMatthew G. Knepley     }
19519566063dSJacob Faibussowitsch     PetscCall(PetscFree2(xw, ww));
1952744bafbcSMatthew G. Knepley     break;
1953744bafbcSMatthew G. Knepley   case 3:
19544366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_HEXAHEDRON;
19559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(npoints, &xw, npoints, &ww));
19569566063dSJacob Faibussowitsch     PetscCall(PetscDTGaussQuadrature(npoints, a, b, xw, ww));
19574366bac7SMatthew G. Knepley     for (PetscInt i = 0; i < npoints; ++i) {
19584366bac7SMatthew G. Knepley       for (PetscInt j = 0; j < npoints; ++j) {
19594366bac7SMatthew G. Knepley         for (PetscInt k = 0; k < npoints; ++k) {
1960744bafbcSMatthew G. Knepley           x[((i * npoints + j) * npoints + k) * dim + 0] = xw[i];
1961744bafbcSMatthew G. Knepley           x[((i * npoints + j) * npoints + k) * dim + 1] = xw[j];
1962744bafbcSMatthew G. Knepley           x[((i * npoints + j) * npoints + k) * dim + 2] = xw[k];
19634366bac7SMatthew G. Knepley           for (PetscInt c = 0; c < Nc; ++c) w[((i * npoints + j) * npoints + k) * Nc + c] = ww[i] * ww[j] * ww[k];
1964744bafbcSMatthew G. Knepley         }
1965744bafbcSMatthew G. Knepley       }
1966744bafbcSMatthew G. Knepley     }
19679566063dSJacob Faibussowitsch     PetscCall(PetscFree2(xw, ww));
1968744bafbcSMatthew G. Knepley     break;
1969d71ae5a4SJacob Faibussowitsch   default:
1970d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %" PetscInt_FMT, dim);
1971744bafbcSMatthew G. Knepley   }
19729566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
19734366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureSetCellType(*q, ct));
19749566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetOrder(*q, 2 * npoints - 1));
19759566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w));
19769566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*q, "GaussTensor"));
19773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1978744bafbcSMatthew G. Knepley }
1979744bafbcSMatthew G. Knepley 
1980f5f57ec0SBarry Smith /*@
19811d27aa22SBarry Smith   PetscDTStroudConicalQuadrature - create Stroud conical quadrature for a simplex {cite}`karniadakis2005spectral`
1982494e7359SMatthew G. Knepley 
1983494e7359SMatthew G. Knepley   Not Collective
1984494e7359SMatthew G. Knepley 
19854165533cSJose E. Roman   Input Parameters:
1986494e7359SMatthew G. Knepley + dim     - The simplex dimension
1987a6b92713SMatthew G. Knepley . Nc      - The number of components
1988dcce0ee2SMatthew G. Knepley . npoints - The number of points in one dimension
1989494e7359SMatthew G. Knepley . a       - left end of interval (often-1)
1990494e7359SMatthew G. Knepley - b       - right end of interval (often +1)
1991494e7359SMatthew G. Knepley 
19924165533cSJose E. Roman   Output Parameter:
199320f4b53cSBarry Smith . q - A `PetscQuadrature` object
1994494e7359SMatthew G. Knepley 
1995494e7359SMatthew G. Knepley   Level: intermediate
1996494e7359SMatthew G. Knepley 
1997dce8aebaSBarry Smith   Note:
199820f4b53cSBarry Smith   For `dim` == 1, this is Gauss-Legendre quadrature
1999dce8aebaSBarry Smith 
2000db781477SPatrick Sanan .seealso: `PetscDTGaussTensorQuadrature()`, `PetscDTGaussQuadrature()`
2001494e7359SMatthew G. Knepley @*/
PetscDTStroudConicalQuadrature(PetscInt dim,PetscInt Nc,PetscInt npoints,PetscReal a,PetscReal b,PetscQuadrature * q)2002d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q)
2003d71ae5a4SJacob Faibussowitsch {
20044366bac7SMatthew G. Knepley   DMPolytopeType ct;
2005fbdc3dfeSToby Isaac   PetscInt       totpoints;
2006fbdc3dfeSToby Isaac   PetscReal     *p1, *w1;
2007fbdc3dfeSToby Isaac   PetscReal     *x, *w;
2008494e7359SMatthew G. Knepley 
2009494e7359SMatthew G. Knepley   PetscFunctionBegin;
201008401ef6SPierre Jolivet   PetscCheck(!(a != -1.0) && !(b != 1.0), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now");
20114366bac7SMatthew G. Knepley   switch (dim) {
20124366bac7SMatthew G. Knepley   case 0:
20134366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_POINT;
20144366bac7SMatthew G. Knepley     break;
20154366bac7SMatthew G. Knepley   case 1:
20164366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_SEGMENT;
20174366bac7SMatthew G. Knepley     break;
20184366bac7SMatthew G. Knepley   case 2:
20194366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_TRIANGLE;
20204366bac7SMatthew G. Knepley     break;
20214366bac7SMatthew G. Knepley   case 3:
20224366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_TETRAHEDRON;
20234366bac7SMatthew G. Knepley     break;
20244366bac7SMatthew G. Knepley   default:
20254366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_UNKNOWN;
20264366bac7SMatthew G. Knepley   }
2027fbdc3dfeSToby Isaac   totpoints = 1;
20284366bac7SMatthew G. Knepley   for (PetscInt i = 0; i < dim; ++i) totpoints *= npoints;
20299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(totpoints * dim, &x));
20309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(totpoints * Nc, &w));
20319566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(npoints, &p1, npoints, &w1));
20324366bac7SMatthew G. Knepley   for (PetscInt i = 0; i < totpoints * Nc; ++i) w[i] = 1.;
20334366bac7SMatthew G. Knepley   for (PetscInt i = 0, totprev = 1, totrem = totpoints / npoints; i < dim; ++i) {
2034fbdc3dfeSToby Isaac     PetscReal mul;
2035fbdc3dfeSToby Isaac 
2036fbdc3dfeSToby Isaac     mul = PetscPowReal(2., -i);
20379566063dSJacob Faibussowitsch     PetscCall(PetscDTGaussJacobiQuadrature(npoints, -1., 1., i, 0.0, p1, w1));
20384366bac7SMatthew G. Knepley     for (PetscInt pt = 0, l = 0; l < totprev; l++) {
20394366bac7SMatthew G. Knepley       for (PetscInt j = 0; j < npoints; j++) {
20404366bac7SMatthew G. Knepley         for (PetscInt m = 0; m < totrem; m++, pt++) {
20414366bac7SMatthew G. Knepley           for (PetscInt k = 0; k < i; k++) x[pt * dim + k] = (x[pt * dim + k] + 1.) * (1. - p1[j]) * 0.5 - 1.;
2042fbdc3dfeSToby Isaac           x[pt * dim + i] = p1[j];
20434366bac7SMatthew G. Knepley           for (PetscInt c = 0; c < Nc; c++) w[pt * Nc + c] *= mul * w1[j];
2044494e7359SMatthew G. Knepley         }
2045494e7359SMatthew G. Knepley       }
2046494e7359SMatthew G. Knepley     }
2047fbdc3dfeSToby Isaac     totprev *= npoints;
2048fbdc3dfeSToby Isaac     totrem /= npoints;
2049494e7359SMatthew G. Knepley   }
20509566063dSJacob Faibussowitsch   PetscCall(PetscFree2(p1, w1));
20519566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
20524366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureSetCellType(*q, ct));
20539566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetOrder(*q, 2 * npoints - 1));
20549566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w));
20559566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*q, "StroudConical"));
20563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2057494e7359SMatthew G. Knepley }
2058494e7359SMatthew G. Knepley 
2059d3c69ad0SToby Isaac static PetscBool MinSymTriQuadCite       = PETSC_FALSE;
20609371c9d4SSatish Balay const char       MinSymTriQuadCitation[] = "@article{WitherdenVincent2015,\n"
2061d3c69ad0SToby Isaac                                            "  title = {On the identification of symmetric quadrature rules for finite element methods},\n"
2062d3c69ad0SToby Isaac                                            "  journal = {Computers & Mathematics with Applications},\n"
2063d3c69ad0SToby Isaac                                            "  volume = {69},\n"
2064d3c69ad0SToby Isaac                                            "  number = {10},\n"
2065d3c69ad0SToby Isaac                                            "  pages = {1232-1241},\n"
2066d3c69ad0SToby Isaac                                            "  year = {2015},\n"
2067d3c69ad0SToby Isaac                                            "  issn = {0898-1221},\n"
2068d3c69ad0SToby Isaac                                            "  doi = {10.1016/j.camwa.2015.03.017},\n"
2069d3c69ad0SToby Isaac                                            "  url = {https://www.sciencedirect.com/science/article/pii/S0898122115001224},\n"
2070d3c69ad0SToby Isaac                                            "  author = {F.D. Witherden and P.E. Vincent},\n"
2071d3c69ad0SToby Isaac                                            "}\n";
2072d3c69ad0SToby Isaac 
2073d3c69ad0SToby Isaac #include "petscdttriquadrules.h"
2074d3c69ad0SToby Isaac 
2075d3c69ad0SToby Isaac static PetscBool MinSymTetQuadCite       = PETSC_FALSE;
20769371c9d4SSatish Balay const char       MinSymTetQuadCitation[] = "@article{JaskowiecSukumar2021\n"
2077d3c69ad0SToby Isaac                                            "  author = {Jaskowiec, Jan and Sukumar, N.},\n"
2078d3c69ad0SToby Isaac                                            "  title = {High-order symmetric cubature rules for tetrahedra and pyramids},\n"
2079d3c69ad0SToby Isaac                                            "  journal = {International Journal for Numerical Methods in Engineering},\n"
2080d3c69ad0SToby Isaac                                            "  volume = {122},\n"
2081d3c69ad0SToby Isaac                                            "  number = {1},\n"
2082d3c69ad0SToby Isaac                                            "  pages = {148-171},\n"
2083d3c69ad0SToby Isaac                                            "  doi = {10.1002/nme.6528},\n"
2084d3c69ad0SToby Isaac                                            "  url = {https://onlinelibrary.wiley.com/doi/abs/10.1002/nme.6528},\n"
2085d3c69ad0SToby Isaac                                            "  eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1002/nme.6528},\n"
2086d3c69ad0SToby Isaac                                            "  year = {2021}\n"
2087d3c69ad0SToby Isaac                                            "}\n";
2088d3c69ad0SToby Isaac 
2089d3c69ad0SToby Isaac #include "petscdttetquadrules.h"
2090d3c69ad0SToby Isaac 
2091f2c64c88SMatthew G. Knepley static PetscBool DiagSymTriQuadCite       = PETSC_FALSE;
2092f2c64c88SMatthew G. Knepley const char       DiagSymTriQuadCitation[] = "@article{KongMulderVeldhuizen1999,\n"
2093f2c64c88SMatthew G. Knepley                                             "  title = {Higher-order triangular and tetrahedral finite elements with mass lumping for solving the wave equation},\n"
2094f2c64c88SMatthew G. Knepley                                             "  journal = {Journal of Engineering Mathematics},\n"
2095f2c64c88SMatthew G. Knepley                                             "  volume = {35},\n"
2096f2c64c88SMatthew G. Knepley                                             "  number = {4},\n"
2097f2c64c88SMatthew G. Knepley                                             "  pages = {405--426},\n"
2098f2c64c88SMatthew G. Knepley                                             "  year = {1999},\n"
2099f2c64c88SMatthew G. Knepley                                             "  doi = {10.1023/A:1004420829610},\n"
2100f2c64c88SMatthew G. Knepley                                             "  url = {https://link.springer.com/article/10.1023/A:1004420829610},\n"
2101f2c64c88SMatthew G. Knepley                                             "  author = {MJS Chin-Joe-Kong and Wim A Mulder and Marinus Van Veldhuizen},\n"
2102f2c64c88SMatthew G. Knepley                                             "}\n";
2103f2c64c88SMatthew G. Knepley 
2104f2c64c88SMatthew G. Knepley #include "petscdttridiagquadrules.h"
2105f2c64c88SMatthew G. Knepley 
2106d3c69ad0SToby Isaac // https://en.wikipedia.org/wiki/Partition_(number_theory)
PetscDTPartitionNumber(PetscInt n,PetscInt * p)2107d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTPartitionNumber(PetscInt n, PetscInt *p)
2108d71ae5a4SJacob Faibussowitsch {
2109d3c69ad0SToby Isaac   // sequence A000041 in the OEIS
2110d3c69ad0SToby Isaac   const PetscInt partition[]   = {1, 1, 2, 3, 5, 7, 11, 15, 22, 30, 42, 56, 77, 101, 135, 176, 231, 297, 385, 490, 627, 792, 1002, 1255, 1575, 1958, 2436, 3010, 3718, 4565, 5604};
2111d3c69ad0SToby Isaac   PetscInt       tabulated_max = PETSC_STATIC_ARRAY_LENGTH(partition) - 1;
2112d3c69ad0SToby Isaac 
2113d3c69ad0SToby Isaac   PetscFunctionBegin;
2114d3c69ad0SToby Isaac   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Partition number not defined for negative number %" PetscInt_FMT, n);
2115d3c69ad0SToby Isaac   // not implementing the pentagonal number recurrence, we don't need partition numbers for n that high
2116d3c69ad0SToby Isaac   PetscCheck(n <= tabulated_max, PETSC_COMM_SELF, PETSC_ERR_SUP, "Partition numbers only tabulated up to %" PetscInt_FMT ", not computed for %" PetscInt_FMT, tabulated_max, n);
2117d3c69ad0SToby Isaac   *p = partition[n];
21183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2119d3c69ad0SToby Isaac }
2120d3c69ad0SToby Isaac 
2121d3c69ad0SToby Isaac /*@
2122d3c69ad0SToby Isaac   PetscDTSimplexQuadrature - Create a quadrature rule for a simplex that exactly integrates polynomials up to a given degree.
2123d3c69ad0SToby Isaac 
2124d3c69ad0SToby Isaac   Not Collective
2125d3c69ad0SToby Isaac 
2126d3c69ad0SToby Isaac   Input Parameters:
2127d3c69ad0SToby Isaac + dim    - The spatial dimension of the simplex (1 = segment, 2 = triangle, 3 = tetrahedron)
2128d3c69ad0SToby Isaac . degree - The largest polynomial degree that is required to be integrated exactly
2129f2c64c88SMatthew G. Knepley - type   - `PetscDTSimplexQuadratureType` indicating the type of quadrature rule
2130d3c69ad0SToby Isaac 
2131d3c69ad0SToby Isaac   Output Parameter:
2132dce8aebaSBarry Smith . quad - A `PetscQuadrature` object for integration over the biunit simplex
2133d3c69ad0SToby Isaac             (defined by the bounds $x_i >= -1$ and $\sum_i x_i <= 2 - d$) that is exact for
2134d3c69ad0SToby Isaac             polynomials up to the given degree
2135d3c69ad0SToby Isaac 
2136d3c69ad0SToby Isaac   Level: intermediate
2137d3c69ad0SToby Isaac 
2138dce8aebaSBarry Smith .seealso: `PetscDTSimplexQuadratureType`, `PetscDTGaussQuadrature()`, `PetscDTStroudCononicalQuadrature()`, `PetscQuadrature`
2139d3c69ad0SToby Isaac @*/
PetscDTSimplexQuadrature(PetscInt dim,PetscInt degree,PetscDTSimplexQuadratureType type,PetscQuadrature * quad)2140d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTSimplexQuadrature(PetscInt dim, PetscInt degree, PetscDTSimplexQuadratureType type, PetscQuadrature *quad)
2141d71ae5a4SJacob Faibussowitsch {
2142d3c69ad0SToby Isaac   PetscDTSimplexQuadratureType orig_type = type;
2143d3c69ad0SToby Isaac 
2144d3c69ad0SToby Isaac   PetscFunctionBegin;
2145d3c69ad0SToby Isaac   PetscCheck(dim >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative dimension %" PetscInt_FMT, dim);
2146d3c69ad0SToby Isaac   PetscCheck(degree >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative degree %" PetscInt_FMT, degree);
2147ad540459SPierre Jolivet   if (type == PETSCDTSIMPLEXQUAD_DEFAULT) type = PETSCDTSIMPLEXQUAD_MINSYM;
2148d3c69ad0SToby Isaac   if (type == PETSCDTSIMPLEXQUAD_CONIC || dim < 2) {
2149d3c69ad0SToby Isaac     PetscInt points_per_dim = (degree + 2) / 2; // ceil((degree + 1) / 2);
2150d3c69ad0SToby Isaac     PetscCall(PetscDTStroudConicalQuadrature(dim, 1, points_per_dim, -1, 1, quad));
2151d3c69ad0SToby Isaac   } else {
21524366bac7SMatthew G. Knepley     DMPolytopeType    ct;
2153d3c69ad0SToby Isaac     PetscInt          n    = dim + 1;
2154d3c69ad0SToby Isaac     PetscInt          fact = 1;
2155d3c69ad0SToby Isaac     PetscInt         *part, *perm;
2156d3c69ad0SToby Isaac     PetscInt          p = 0;
2157d3c69ad0SToby Isaac     PetscInt          max_degree;
2158d3c69ad0SToby Isaac     const PetscInt   *nodes_per_type     = NULL;
2159d3c69ad0SToby Isaac     const PetscInt   *all_num_full_nodes = NULL;
2160d3c69ad0SToby Isaac     const PetscReal **weights_list       = NULL;
2161d3c69ad0SToby Isaac     const PetscReal **compact_nodes_list = NULL;
2162d3c69ad0SToby Isaac     const char       *citation           = NULL;
2163d3c69ad0SToby Isaac     PetscBool        *cited              = NULL;
2164d3c69ad0SToby Isaac 
2165d3c69ad0SToby Isaac     switch (dim) {
21664366bac7SMatthew G. Knepley     case 0:
21674366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_POINT;
21684366bac7SMatthew G. Knepley       break;
21694366bac7SMatthew G. Knepley     case 1:
21704366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_SEGMENT;
21714366bac7SMatthew G. Knepley       break;
21724366bac7SMatthew G. Knepley     case 2:
21734366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_TRIANGLE;
21744366bac7SMatthew G. Knepley       break;
21754366bac7SMatthew G. Knepley     case 3:
21764366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_TETRAHEDRON;
21774366bac7SMatthew G. Knepley       break;
21784366bac7SMatthew G. Knepley     default:
21794366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
21804366bac7SMatthew G. Knepley     }
2181f2c64c88SMatthew G. Knepley     if (type == PETSCDTSIMPLEXQUAD_MINSYM) {
21824366bac7SMatthew G. Knepley       switch (dim) {
2183d3c69ad0SToby Isaac       case 2:
2184d3c69ad0SToby Isaac         cited              = &MinSymTriQuadCite;
2185d3c69ad0SToby Isaac         citation           = MinSymTriQuadCitation;
2186d3c69ad0SToby Isaac         max_degree         = PetscDTWVTriQuad_max_degree;
2187d3c69ad0SToby Isaac         nodes_per_type     = PetscDTWVTriQuad_num_orbits;
2188d3c69ad0SToby Isaac         all_num_full_nodes = PetscDTWVTriQuad_num_nodes;
2189d3c69ad0SToby Isaac         weights_list       = PetscDTWVTriQuad_weights;
2190d3c69ad0SToby Isaac         compact_nodes_list = PetscDTWVTriQuad_orbits;
2191d3c69ad0SToby Isaac         break;
2192d3c69ad0SToby Isaac       case 3:
2193d3c69ad0SToby Isaac         cited              = &MinSymTetQuadCite;
2194d3c69ad0SToby Isaac         citation           = MinSymTetQuadCitation;
2195d3c69ad0SToby Isaac         max_degree         = PetscDTJSTetQuad_max_degree;
2196d3c69ad0SToby Isaac         nodes_per_type     = PetscDTJSTetQuad_num_orbits;
2197d3c69ad0SToby Isaac         all_num_full_nodes = PetscDTJSTetQuad_num_nodes;
2198d3c69ad0SToby Isaac         weights_list       = PetscDTJSTetQuad_weights;
2199d3c69ad0SToby Isaac         compact_nodes_list = PetscDTJSTetQuad_orbits;
2200d3c69ad0SToby Isaac         break;
2201d71ae5a4SJacob Faibussowitsch       default:
2202d71ae5a4SJacob Faibussowitsch         max_degree = -1;
2203d71ae5a4SJacob Faibussowitsch         break;
2204d3c69ad0SToby Isaac       }
2205f2c64c88SMatthew G. Knepley     } else {
2206f2c64c88SMatthew G. Knepley       switch (dim) {
2207f2c64c88SMatthew G. Knepley       case 2:
2208f2c64c88SMatthew G. Knepley         cited              = &DiagSymTriQuadCite;
2209f2c64c88SMatthew G. Knepley         citation           = DiagSymTriQuadCitation;
2210f2c64c88SMatthew G. Knepley         max_degree         = PetscDTKMVTriQuad_max_degree;
2211f2c64c88SMatthew G. Knepley         nodes_per_type     = PetscDTKMVTriQuad_num_orbits;
2212f2c64c88SMatthew G. Knepley         all_num_full_nodes = PetscDTKMVTriQuad_num_nodes;
2213f2c64c88SMatthew G. Knepley         weights_list       = PetscDTKMVTriQuad_weights;
2214f2c64c88SMatthew G. Knepley         compact_nodes_list = PetscDTKMVTriQuad_orbits;
2215f2c64c88SMatthew G. Knepley         break;
2216f2c64c88SMatthew G. Knepley       default:
2217f2c64c88SMatthew G. Knepley         max_degree = -1;
2218f2c64c88SMatthew G. Knepley         break;
2219f2c64c88SMatthew G. Knepley       }
2220f2c64c88SMatthew G. Knepley     }
2221d3c69ad0SToby Isaac 
2222d3c69ad0SToby Isaac     if (degree > max_degree) {
2223966bd95aSPierre Jolivet       PetscCheck(orig_type == PETSCDTSIMPLEXQUAD_DEFAULT, PETSC_COMM_SELF, PETSC_ERR_SUP, "%s symmetric quadrature for dim %" PetscInt_FMT ", degree %" PetscInt_FMT " unsupported", orig_type == PETSCDTSIMPLEXQUAD_MINSYM ? "Minimal" : "Diagonal", dim, degree);
2224d3c69ad0SToby Isaac       // fall back to conic
2225d3c69ad0SToby Isaac       PetscCall(PetscDTSimplexQuadrature(dim, degree, PETSCDTSIMPLEXQUAD_CONIC, quad));
22263ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
2227d3c69ad0SToby Isaac     }
2228d3c69ad0SToby Isaac 
2229d3c69ad0SToby Isaac     PetscCall(PetscCitationsRegister(citation, cited));
2230d3c69ad0SToby Isaac 
2231d3c69ad0SToby Isaac     PetscCall(PetscDTPartitionNumber(n, &p));
2232d3c69ad0SToby Isaac     for (PetscInt d = 2; d <= n; d++) fact *= d;
2233d3c69ad0SToby Isaac 
2234d3c69ad0SToby Isaac     PetscInt         num_full_nodes      = all_num_full_nodes[degree];
2235d3c69ad0SToby Isaac     const PetscReal *all_compact_nodes   = compact_nodes_list[degree];
2236d3c69ad0SToby Isaac     const PetscReal *all_compact_weights = weights_list[degree];
2237d3c69ad0SToby Isaac     nodes_per_type                       = &nodes_per_type[p * degree];
2238d3c69ad0SToby Isaac 
2239d3c69ad0SToby Isaac     PetscReal      *points;
2240d3c69ad0SToby Isaac     PetscReal      *counts;
2241d3c69ad0SToby Isaac     PetscReal      *weights;
2242d3c69ad0SToby Isaac     PetscReal      *bary_to_biunit; // row-major transformation of barycentric coordinate to biunit
2243d3c69ad0SToby Isaac     PetscQuadrature q;
2244d3c69ad0SToby Isaac 
2245d3c69ad0SToby Isaac     // compute the transformation
2246d3c69ad0SToby Isaac     PetscCall(PetscMalloc1(n * dim, &bary_to_biunit));
2247d3c69ad0SToby Isaac     for (PetscInt d = 0; d < dim; d++) {
2248ad540459SPierre Jolivet       for (PetscInt b = 0; b < n; b++) bary_to_biunit[d * n + b] = (d == b) ? 1.0 : -1.0;
2249d3c69ad0SToby Isaac     }
2250d3c69ad0SToby Isaac 
2251d3c69ad0SToby Isaac     PetscCall(PetscMalloc3(n, &part, n, &perm, n, &counts));
2252d3c69ad0SToby Isaac     PetscCall(PetscCalloc1(num_full_nodes * dim, &points));
2253d3c69ad0SToby Isaac     PetscCall(PetscMalloc1(num_full_nodes, &weights));
2254d3c69ad0SToby Isaac 
2255d3c69ad0SToby Isaac     // (0, 0, ...) is the first partition lexicographically
2256d3c69ad0SToby Isaac     PetscCall(PetscArrayzero(part, n));
2257d3c69ad0SToby Isaac     PetscCall(PetscArrayzero(counts, n));
2258d3c69ad0SToby Isaac     counts[0] = n;
2259d3c69ad0SToby Isaac 
2260d3c69ad0SToby Isaac     // for each partition
2261d3c69ad0SToby Isaac     for (PetscInt s = 0, node_offset = 0; s < p; s++) {
2262d3c69ad0SToby Isaac       PetscInt num_compact_coords = part[n - 1] + 1;
2263d3c69ad0SToby Isaac 
2264d3c69ad0SToby Isaac       const PetscReal *compact_nodes   = all_compact_nodes;
2265d3c69ad0SToby Isaac       const PetscReal *compact_weights = all_compact_weights;
2266d3c69ad0SToby Isaac       all_compact_nodes += num_compact_coords * nodes_per_type[s];
2267d3c69ad0SToby Isaac       all_compact_weights += nodes_per_type[s];
2268d3c69ad0SToby Isaac 
2269d3c69ad0SToby Isaac       // for every permutation of the vertices
2270d3c69ad0SToby Isaac       for (PetscInt f = 0; f < fact; f++) {
2271d3c69ad0SToby Isaac         PetscCall(PetscDTEnumPerm(n, f, perm, NULL));
2272d3c69ad0SToby Isaac 
2273d3c69ad0SToby Isaac         // check if it is a valid permutation
2274d3c69ad0SToby Isaac         PetscInt digit;
2275d3c69ad0SToby Isaac         for (digit = 1; digit < n; digit++) {
2276d3c69ad0SToby Isaac           // skip permutations that would duplicate a node because it has a smaller symmetry group
2277d3c69ad0SToby Isaac           if (part[digit - 1] == part[digit] && perm[digit - 1] > perm[digit]) break;
2278d3c69ad0SToby Isaac         }
2279d3c69ad0SToby Isaac         if (digit < n) continue;
2280d3c69ad0SToby Isaac 
2281d3c69ad0SToby Isaac         // create full nodes from this permutation of the compact nodes
2282d3c69ad0SToby Isaac         PetscReal *full_nodes   = &points[node_offset * dim];
2283d3c69ad0SToby Isaac         PetscReal *full_weights = &weights[node_offset];
2284d3c69ad0SToby Isaac 
2285d3c69ad0SToby Isaac         PetscCall(PetscArraycpy(full_weights, compact_weights, nodes_per_type[s]));
2286d3c69ad0SToby Isaac         for (PetscInt b = 0; b < n; b++) {
2287d3c69ad0SToby Isaac           for (PetscInt d = 0; d < dim; d++) {
2288ad540459SPierre Jolivet             for (PetscInt node = 0; node < nodes_per_type[s]; node++) full_nodes[node * dim + d] += bary_to_biunit[d * n + perm[b]] * compact_nodes[node * num_compact_coords + part[b]];
2289d3c69ad0SToby Isaac           }
2290d3c69ad0SToby Isaac         }
2291d3c69ad0SToby Isaac         node_offset += nodes_per_type[s];
2292d3c69ad0SToby Isaac       }
2293d3c69ad0SToby Isaac 
2294d3c69ad0SToby Isaac       if (s < p - 1) { // Generate the next partition
2295d3c69ad0SToby Isaac         /* A partition is described by the number of coordinates that are in
2296d3c69ad0SToby Isaac          * each set of duplicates (counts) and redundantly by mapping each
2297d3c69ad0SToby Isaac          * index to its set of duplicates (part)
2298d3c69ad0SToby Isaac          *
2299d3c69ad0SToby Isaac          * Counts should always be in nonincreasing order
2300d3c69ad0SToby Isaac          *
2301d3c69ad0SToby Isaac          * We want to generate the partitions lexically by part, which means
2302d3c69ad0SToby Isaac          * finding the last index where count > 1 and reducing by 1.
2303d3c69ad0SToby Isaac          *
2304d3c69ad0SToby Isaac          * For the new counts beyond that index, we eagerly assign the remaining
2305d3c69ad0SToby Isaac          * capacity of the partition to smaller indices (ensures lexical ordering),
2306d3c69ad0SToby Isaac          * while respecting the nonincreasing invariant of the counts
2307d3c69ad0SToby Isaac          */
2308d3c69ad0SToby Isaac         PetscInt last_digit            = part[n - 1];
2309d3c69ad0SToby Isaac         PetscInt last_digit_with_extra = last_digit;
2310d3c69ad0SToby Isaac         while (counts[last_digit_with_extra] == 1) last_digit_with_extra--;
2311d3c69ad0SToby Isaac         PetscInt limit               = --counts[last_digit_with_extra];
2312d3c69ad0SToby Isaac         PetscInt total_to_distribute = last_digit - last_digit_with_extra + 1;
2313d3c69ad0SToby Isaac         for (PetscInt digit = last_digit_with_extra + 1; digit < n; digit++) {
2314d3c69ad0SToby Isaac           counts[digit] = PetscMin(limit, total_to_distribute);
2315d3c69ad0SToby Isaac           total_to_distribute -= PetscMin(limit, total_to_distribute);
2316d3c69ad0SToby Isaac         }
2317d3c69ad0SToby Isaac         for (PetscInt digit = 0, offset = 0; digit < n; digit++) {
2318d3c69ad0SToby Isaac           PetscInt count = counts[digit];
2319ad540459SPierre Jolivet           for (PetscInt c = 0; c < count; c++) part[offset++] = digit;
2320d3c69ad0SToby Isaac         }
2321d3c69ad0SToby Isaac       }
2322f2c64c88SMatthew G. Knepley       PetscCheck(node_offset <= num_full_nodes, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Node offset %" PetscInt_FMT " > %" PetscInt_FMT " number of nodes", node_offset, num_full_nodes);
2323d3c69ad0SToby Isaac     }
2324d3c69ad0SToby Isaac     PetscCall(PetscFree3(part, perm, counts));
2325d3c69ad0SToby Isaac     PetscCall(PetscFree(bary_to_biunit));
2326d3c69ad0SToby Isaac     PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &q));
23274366bac7SMatthew G. Knepley     PetscCall(PetscQuadratureSetCellType(q, ct));
2328b414c505SJed Brown     PetscCall(PetscQuadratureSetOrder(q, degree));
2329d3c69ad0SToby Isaac     PetscCall(PetscQuadratureSetData(q, dim, 1, num_full_nodes, points, weights));
2330d3c69ad0SToby Isaac     *quad = q;
2331d3c69ad0SToby Isaac   }
23323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2333d3c69ad0SToby Isaac }
2334d3c69ad0SToby Isaac 
2335f5f57ec0SBarry Smith /*@
2336b3c0f97bSTom Klotz   PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell
2337b3c0f97bSTom Klotz 
2338b3c0f97bSTom Klotz   Not Collective
2339b3c0f97bSTom Klotz 
23404165533cSJose E. Roman   Input Parameters:
2341b3c0f97bSTom Klotz + dim   - The cell dimension
23421d27aa22SBarry Smith . level - The number of points in one dimension, $2^l$
2343b3c0f97bSTom Klotz . a     - left end of interval (often-1)
2344b3c0f97bSTom Klotz - b     - right end of interval (often +1)
2345b3c0f97bSTom Klotz 
23464165533cSJose E. Roman   Output Parameter:
2347dce8aebaSBarry Smith . q - A `PetscQuadrature` object
2348b3c0f97bSTom Klotz 
2349b3c0f97bSTom Klotz   Level: intermediate
2350b3c0f97bSTom Klotz 
2351dce8aebaSBarry Smith .seealso: `PetscDTGaussTensorQuadrature()`, `PetscQuadrature`
2352b3c0f97bSTom Klotz @*/
PetscDTTanhSinhTensorQuadrature(PetscInt dim,PetscInt level,PetscReal a,PetscReal b,PetscQuadrature * q)2353d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q)
2354d71ae5a4SJacob Faibussowitsch {
23554366bac7SMatthew G. Knepley   DMPolytopeType  ct;
2356b3c0f97bSTom Klotz   const PetscInt  p     = 16;                        /* Digits of precision in the evaluation */
2357b3c0f97bSTom Klotz   const PetscReal alpha = (b - a) / 2.;              /* Half-width of the integration interval */
2358b3c0f97bSTom Klotz   const PetscReal beta  = (b + a) / 2.;              /* Center of the integration interval */
2359b3c0f97bSTom Klotz   const PetscReal h     = PetscPowReal(2.0, -level); /* Step size, length between x_k */
2360d84b4d08SMatthew G. Knepley   PetscReal       xk;                                /* Quadrature point x_k on reference domain [-1, 1] */
2361b3c0f97bSTom Klotz   PetscReal       wk = 0.5 * PETSC_PI;               /* Quadrature weight at x_k */
2362b3c0f97bSTom Klotz   PetscReal      *x, *w;
2363b3c0f97bSTom Klotz   PetscInt        K, k, npoints;
2364b3c0f97bSTom Klotz 
2365b3c0f97bSTom Klotz   PetscFunctionBegin;
236663a3b9bcSJacob Faibussowitsch   PetscCheck(dim <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not yet implemented", dim);
236728b400f6SJacob Faibussowitsch   PetscCheck(level, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits");
23684366bac7SMatthew G. Knepley   switch (dim) {
23694366bac7SMatthew G. Knepley   case 0:
23704366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_POINT;
23714366bac7SMatthew G. Knepley     break;
23724366bac7SMatthew G. Knepley   case 1:
23734366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_SEGMENT;
23744366bac7SMatthew G. Knepley     break;
23754366bac7SMatthew G. Knepley   case 2:
23764366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_QUADRILATERAL;
23774366bac7SMatthew G. Knepley     break;
23784366bac7SMatthew G. Knepley   case 3:
23794366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_HEXAHEDRON;
23804366bac7SMatthew G. Knepley     break;
23814366bac7SMatthew G. Knepley   default:
23824366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_UNKNOWN;
23834366bac7SMatthew G. Knepley   }
2384b3c0f97bSTom Klotz   /* Find K such that the weights are < 32 digits of precision */
2385ad540459SPierre Jolivet   for (K = 1; PetscAbsReal(PetscLog10Real(wk)) < 2 * p; ++K) wk = 0.5 * h * PETSC_PI * PetscCoshReal(K * h) / PetscSqr(PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(K * h)));
23869566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
23874366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureSetCellType(*q, ct));
23889566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetOrder(*q, 2 * K + 1));
2389b3c0f97bSTom Klotz   npoints = 2 * K - 1;
23909566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints * dim, &x));
23919566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(npoints, &w));
2392b3c0f97bSTom Klotz   /* Center term */
2393b3c0f97bSTom Klotz   x[0] = beta;
2394b3c0f97bSTom Klotz   w[0] = 0.5 * alpha * PETSC_PI;
2395b3c0f97bSTom Klotz   for (k = 1; k < K; ++k) {
23969add2064SThomas Klotz     wk           = 0.5 * alpha * h * PETSC_PI * PetscCoshReal(k * h) / PetscSqr(PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(k * h)));
23971118d4bcSLisandro Dalcin     xk           = PetscTanhReal(0.5 * PETSC_PI * PetscSinhReal(k * h));
2398b3c0f97bSTom Klotz     x[2 * k - 1] = -alpha * xk + beta;
2399b3c0f97bSTom Klotz     w[2 * k - 1] = wk;
2400b3c0f97bSTom Klotz     x[2 * k + 0] = alpha * xk + beta;
2401b3c0f97bSTom Klotz     w[2 * k + 0] = wk;
2402b3c0f97bSTom Klotz   }
24039566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*q, dim, 1, npoints, x, w));
24043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2405b3c0f97bSTom Klotz }
2406b3c0f97bSTom Klotz 
PetscDTTanhSinhIntegrate(void (* func)(const PetscReal[],void *,PetscReal *),PetscReal a,PetscReal b,PetscInt digits,PetscCtx ctx,PetscReal * sol)2407*2a8381b2SBarry Smith PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscCtx ctx, PetscReal *sol)
2408d71ae5a4SJacob Faibussowitsch {
2409b3c0f97bSTom Klotz   const PetscInt  p     = 16;           /* Digits of precision in the evaluation */
2410b3c0f97bSTom Klotz   const PetscReal alpha = (b - a) / 2.; /* Half-width of the integration interval */
2411b3c0f97bSTom Klotz   const PetscReal beta  = (b + a) / 2.; /* Center of the integration interval */
2412b3c0f97bSTom Klotz   PetscReal       h     = 1.0;          /* Step size, length between x_k */
2413b3c0f97bSTom Klotz   PetscInt        l     = 0;            /* Level of refinement, h = 2^{-l} */
2414b3c0f97bSTom Klotz   PetscReal       osum  = 0.0;          /* Integral on last level */
2415b3c0f97bSTom Klotz   PetscReal       psum  = 0.0;          /* Integral on the level before the last level */
2416b3c0f97bSTom Klotz   PetscReal       sum;                  /* Integral on current level */
2417446c295cSMatthew G. Knepley   PetscReal       yk;                   /* Quadrature point 1 - x_k on reference domain [-1, 1] */
2418b3c0f97bSTom Klotz   PetscReal       lx, rx;               /* Quadrature points to the left and right of 0 on the real domain [a, b] */
2419b3c0f97bSTom Klotz   PetscReal       wk;                   /* Quadrature weight at x_k */
2420b3c0f97bSTom Klotz   PetscReal       lval, rval;           /* Terms in the quadature sum to the left and right of 0 */
2421b3c0f97bSTom Klotz   PetscInt        d;                    /* Digits of precision in the integral */
2422b3c0f97bSTom Klotz 
2423b3c0f97bSTom Klotz   PetscFunctionBegin;
242408401ef6SPierre Jolivet   PetscCheck(digits > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits");
24252b6f951bSStefano Zampini   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2426b3c0f97bSTom Klotz   /* Center term */
2427d6685f55SMatthew G. Knepley   func(&beta, ctx, &lval);
2428b3c0f97bSTom Klotz   sum = 0.5 * alpha * PETSC_PI * lval;
2429b3c0f97bSTom Klotz   /* */
2430b3c0f97bSTom Klotz   do {
2431b3c0f97bSTom Klotz     PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4;
2432b3c0f97bSTom Klotz     PetscInt  k = 1;
2433b3c0f97bSTom Klotz 
2434b3c0f97bSTom Klotz     ++l;
243563a3b9bcSJacob Faibussowitsch     /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %" PetscInt_FMT " sum: %15.15f\n", l, sum); */
2436b3c0f97bSTom Klotz     /* At each level of refinement, h --> h/2 and sum --> sum/2 */
2437b3c0f97bSTom Klotz     psum = osum;
2438b3c0f97bSTom Klotz     osum = sum;
2439b3c0f97bSTom Klotz     h *= 0.5;
2440b3c0f97bSTom Klotz     sum *= 0.5;
2441b3c0f97bSTom Klotz     do {
24429add2064SThomas Klotz       wk = 0.5 * h * PETSC_PI * PetscCoshReal(k * h) / PetscSqr(PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(k * h)));
2443446c295cSMatthew G. Knepley       yk = 1.0 / (PetscExpReal(0.5 * PETSC_PI * PetscSinhReal(k * h)) * PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(k * h)));
2444446c295cSMatthew G. Knepley       lx = -alpha * (1.0 - yk) + beta;
2445446c295cSMatthew G. Knepley       rx = alpha * (1.0 - yk) + beta;
2446d6685f55SMatthew G. Knepley       func(&lx, ctx, &lval);
2447d6685f55SMatthew G. Knepley       func(&rx, ctx, &rval);
2448b3c0f97bSTom Klotz       lterm   = alpha * wk * lval;
2449b3c0f97bSTom Klotz       maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm);
2450b3c0f97bSTom Klotz       sum += lterm;
2451b3c0f97bSTom Klotz       rterm   = alpha * wk * rval;
2452b3c0f97bSTom Klotz       maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm);
2453b3c0f97bSTom Klotz       sum += rterm;
2454b3c0f97bSTom Klotz       ++k;
2455b3c0f97bSTom Klotz       /* Only need to evaluate every other point on refined levels */
2456b3c0f97bSTom Klotz       if (l != 1) ++k;
24579add2064SThomas Klotz     } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */
2458b3c0f97bSTom Klotz 
2459b3c0f97bSTom Klotz     d1 = PetscLog10Real(PetscAbsReal(sum - osum));
2460b3c0f97bSTom Klotz     d2 = PetscLog10Real(PetscAbsReal(sum - psum));
2461b3c0f97bSTom Klotz     d3 = PetscLog10Real(maxTerm) - p;
246209d48545SBarry Smith     if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0;
246309d48545SBarry Smith     else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)));
2464b3c0f97bSTom Klotz     d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1) / d2, 2 * d1), d3), d4)));
24659add2064SThomas Klotz   } while (d < digits && l < 12);
2466b3c0f97bSTom Klotz   *sol = sum;
24672b6f951bSStefano Zampini   PetscCall(PetscFPTrapPop());
24683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2469b3c0f97bSTom Klotz }
2470b3c0f97bSTom Klotz 
2471497880caSRichard Tran Mills #if defined(PETSC_HAVE_MPFR)
PetscDTTanhSinhIntegrateMPFR(void (* func)(const PetscReal[],void *,PetscReal *),PetscReal a,PetscReal b,PetscInt digits,PetscCtx ctx,PetscReal * sol)2472*2a8381b2SBarry Smith PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscCtx ctx, PetscReal *sol)
2473d71ae5a4SJacob Faibussowitsch {
247446091a0eSPierre Jolivet   const PetscInt safetyFactor = 2; /* Calculate abscissa until 2*p digits */
247529f144ccSMatthew G. Knepley   PetscInt       l            = 0; /* Level of refinement, h = 2^{-l} */
247629f144ccSMatthew G. Knepley   mpfr_t         alpha;            /* Half-width of the integration interval */
247729f144ccSMatthew G. Knepley   mpfr_t         beta;             /* Center of the integration interval */
247829f144ccSMatthew G. Knepley   mpfr_t         h;                /* Step size, length between x_k */
247929f144ccSMatthew G. Knepley   mpfr_t         osum;             /* Integral on last level */
248029f144ccSMatthew G. Knepley   mpfr_t         psum;             /* Integral on the level before the last level */
248129f144ccSMatthew G. Knepley   mpfr_t         sum;              /* Integral on current level */
248229f144ccSMatthew G. Knepley   mpfr_t         yk;               /* Quadrature point 1 - x_k on reference domain [-1, 1] */
248329f144ccSMatthew G. Knepley   mpfr_t         lx, rx;           /* Quadrature points to the left and right of 0 on the real domain [a, b] */
248429f144ccSMatthew G. Knepley   mpfr_t         wk;               /* Quadrature weight at x_k */
24851fbc92bbSMatthew G. Knepley   PetscReal      lval, rval, rtmp; /* Terms in the quadature sum to the left and right of 0 */
248629f144ccSMatthew G. Knepley   PetscInt       d;                /* Digits of precision in the integral */
248729f144ccSMatthew G. Knepley   mpfr_t         pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp;
248829f144ccSMatthew G. Knepley 
248929f144ccSMatthew G. Knepley   PetscFunctionBegin;
249008401ef6SPierre Jolivet   PetscCheck(digits > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits");
249129f144ccSMatthew G. Knepley   /* Create high precision storage */
2492c9f744b5SMatthew G. Knepley   mpfr_inits2(PetscCeilReal(safetyFactor * digits * PetscLogReal(10.) / PetscLogReal(2.)), alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL);
249329f144ccSMatthew G. Knepley   /* Initialization */
249429f144ccSMatthew G. Knepley   mpfr_set_d(alpha, 0.5 * (b - a), MPFR_RNDN);
249529f144ccSMatthew G. Knepley   mpfr_set_d(beta, 0.5 * (b + a), MPFR_RNDN);
249629f144ccSMatthew G. Knepley   mpfr_set_d(osum, 0.0, MPFR_RNDN);
249729f144ccSMatthew G. Knepley   mpfr_set_d(psum, 0.0, MPFR_RNDN);
249829f144ccSMatthew G. Knepley   mpfr_set_d(h, 1.0, MPFR_RNDN);
249929f144ccSMatthew G. Knepley   mpfr_const_pi(pi2, MPFR_RNDN);
250029f144ccSMatthew G. Knepley   mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN);
250129f144ccSMatthew G. Knepley   /* Center term */
25021fbc92bbSMatthew G. Knepley   rtmp = 0.5 * (b + a);
25031fbc92bbSMatthew G. Knepley   func(&rtmp, ctx, &lval);
250429f144ccSMatthew G. Knepley   mpfr_set(sum, pi2, MPFR_RNDN);
250529f144ccSMatthew G. Knepley   mpfr_mul(sum, sum, alpha, MPFR_RNDN);
250629f144ccSMatthew G. Knepley   mpfr_mul_d(sum, sum, lval, MPFR_RNDN);
250729f144ccSMatthew G. Knepley   /* */
250829f144ccSMatthew G. Knepley   do {
250929f144ccSMatthew G. Knepley     PetscReal d1, d2, d3, d4;
251029f144ccSMatthew G. Knepley     PetscInt  k = 1;
251129f144ccSMatthew G. Knepley 
251229f144ccSMatthew G. Knepley     ++l;
251329f144ccSMatthew G. Knepley     mpfr_set_d(maxTerm, 0.0, MPFR_RNDN);
251463a3b9bcSJacob Faibussowitsch     /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %" PetscInt_FMT " sum: %15.15f\n", l, sum); */
251529f144ccSMatthew G. Knepley     /* At each level of refinement, h --> h/2 and sum --> sum/2 */
251629f144ccSMatthew G. Knepley     mpfr_set(psum, osum, MPFR_RNDN);
251729f144ccSMatthew G. Knepley     mpfr_set(osum, sum, MPFR_RNDN);
251829f144ccSMatthew G. Knepley     mpfr_mul_d(h, h, 0.5, MPFR_RNDN);
251929f144ccSMatthew G. Knepley     mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN);
252029f144ccSMatthew G. Knepley     do {
252129f144ccSMatthew G. Knepley       mpfr_set_si(kh, k, MPFR_RNDN);
252229f144ccSMatthew G. Knepley       mpfr_mul(kh, kh, h, MPFR_RNDN);
252329f144ccSMatthew G. Knepley       /* Weight */
252429f144ccSMatthew G. Knepley       mpfr_set(wk, h, MPFR_RNDN);
252529f144ccSMatthew G. Knepley       mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN);
252629f144ccSMatthew G. Knepley       mpfr_mul(msinh, msinh, pi2, MPFR_RNDN);
252729f144ccSMatthew G. Knepley       mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN);
252829f144ccSMatthew G. Knepley       mpfr_cosh(tmp, msinh, MPFR_RNDN);
252929f144ccSMatthew G. Knepley       mpfr_sqr(tmp, tmp, MPFR_RNDN);
253029f144ccSMatthew G. Knepley       mpfr_mul(wk, wk, mcosh, MPFR_RNDN);
253129f144ccSMatthew G. Knepley       mpfr_div(wk, wk, tmp, MPFR_RNDN);
253229f144ccSMatthew G. Knepley       /* Abscissa */
253329f144ccSMatthew G. Knepley       mpfr_set_d(yk, 1.0, MPFR_RNDZ);
253429f144ccSMatthew G. Knepley       mpfr_cosh(tmp, msinh, MPFR_RNDN);
253529f144ccSMatthew G. Knepley       mpfr_div(yk, yk, tmp, MPFR_RNDZ);
253629f144ccSMatthew G. Knepley       mpfr_exp(tmp, msinh, MPFR_RNDN);
253729f144ccSMatthew G. Knepley       mpfr_div(yk, yk, tmp, MPFR_RNDZ);
253829f144ccSMatthew G. Knepley       /* Quadrature points */
253929f144ccSMatthew G. Knepley       mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ);
254029f144ccSMatthew G. Knepley       mpfr_mul(lx, lx, alpha, MPFR_RNDU);
254129f144ccSMatthew G. Knepley       mpfr_add(lx, lx, beta, MPFR_RNDU);
254229f144ccSMatthew G. Knepley       mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ);
254329f144ccSMatthew G. Knepley       mpfr_mul(rx, rx, alpha, MPFR_RNDD);
254429f144ccSMatthew G. Knepley       mpfr_add(rx, rx, beta, MPFR_RNDD);
254529f144ccSMatthew G. Knepley       /* Evaluation */
25461fbc92bbSMatthew G. Knepley       rtmp = mpfr_get_d(lx, MPFR_RNDU);
25471fbc92bbSMatthew G. Knepley       func(&rtmp, ctx, &lval);
25481fbc92bbSMatthew G. Knepley       rtmp = mpfr_get_d(rx, MPFR_RNDD);
25491fbc92bbSMatthew G. Knepley       func(&rtmp, ctx, &rval);
255029f144ccSMatthew G. Knepley       /* Update */
255129f144ccSMatthew G. Knepley       mpfr_mul(tmp, wk, alpha, MPFR_RNDN);
255229f144ccSMatthew G. Knepley       mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN);
255329f144ccSMatthew G. Knepley       mpfr_add(sum, sum, tmp, MPFR_RNDN);
255429f144ccSMatthew G. Knepley       mpfr_abs(tmp, tmp, MPFR_RNDN);
255529f144ccSMatthew G. Knepley       mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN);
255629f144ccSMatthew G. Knepley       mpfr_set(curTerm, tmp, MPFR_RNDN);
255729f144ccSMatthew G. Knepley       mpfr_mul(tmp, wk, alpha, MPFR_RNDN);
255829f144ccSMatthew G. Knepley       mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN);
255929f144ccSMatthew G. Knepley       mpfr_add(sum, sum, tmp, MPFR_RNDN);
256029f144ccSMatthew G. Knepley       mpfr_abs(tmp, tmp, MPFR_RNDN);
256129f144ccSMatthew G. Knepley       mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN);
256229f144ccSMatthew G. Knepley       mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN);
256329f144ccSMatthew G. Knepley       ++k;
256429f144ccSMatthew G. Knepley       /* Only need to evaluate every other point on refined levels */
256529f144ccSMatthew G. Knepley       if (l != 1) ++k;
256629f144ccSMatthew G. Knepley       mpfr_log10(tmp, wk, MPFR_RNDN);
256729f144ccSMatthew G. Knepley       mpfr_abs(tmp, tmp, MPFR_RNDN);
2568c9f744b5SMatthew G. Knepley     } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor * digits); /* Only need to evaluate sum until weights are < 32 digits of precision */
256929f144ccSMatthew G. Knepley     mpfr_sub(tmp, sum, osum, MPFR_RNDN);
257029f144ccSMatthew G. Knepley     mpfr_abs(tmp, tmp, MPFR_RNDN);
257129f144ccSMatthew G. Knepley     mpfr_log10(tmp, tmp, MPFR_RNDN);
257229f144ccSMatthew G. Knepley     d1 = mpfr_get_d(tmp, MPFR_RNDN);
257329f144ccSMatthew G. Knepley     mpfr_sub(tmp, sum, psum, MPFR_RNDN);
257429f144ccSMatthew G. Knepley     mpfr_abs(tmp, tmp, MPFR_RNDN);
257529f144ccSMatthew G. Knepley     mpfr_log10(tmp, tmp, MPFR_RNDN);
257629f144ccSMatthew G. Knepley     d2 = mpfr_get_d(tmp, MPFR_RNDN);
257729f144ccSMatthew G. Knepley     mpfr_log10(tmp, maxTerm, MPFR_RNDN);
2578c9f744b5SMatthew G. Knepley     d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits;
257929f144ccSMatthew G. Knepley     mpfr_log10(tmp, curTerm, MPFR_RNDN);
258029f144ccSMatthew G. Knepley     d4 = mpfr_get_d(tmp, MPFR_RNDN);
258129f144ccSMatthew G. Knepley     d  = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1) / d2, 2 * d1), d3), d4)));
2582b0649871SThomas Klotz   } while (d < digits && l < 8);
258329f144ccSMatthew G. Knepley   *sol = mpfr_get_d(sum, MPFR_RNDN);
258429f144ccSMatthew G. Knepley   /* Cleanup */
258529f144ccSMatthew G. Knepley   mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL);
25863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
258729f144ccSMatthew G. Knepley }
2588d525116cSMatthew G. Knepley #else
2589fbfcfee5SBarry Smith 
PetscDTTanhSinhIntegrateMPFR(void (* func)(const PetscReal[],void *,PetscReal *),PetscReal a,PetscReal b,PetscInt digits,PetscCtx ctx,PetscReal * sol)2590*2a8381b2SBarry Smith PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscCtx ctx, PetscReal *sol)
2591d71ae5a4SJacob Faibussowitsch {
2592d525116cSMatthew G. Knepley   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp");
2593d525116cSMatthew G. Knepley }
259429f144ccSMatthew G. Knepley #endif
259529f144ccSMatthew G. Knepley 
25962df84da0SMatthew G. Knepley /*@
25972df84da0SMatthew G. Knepley   PetscDTTensorQuadratureCreate - create the tensor product quadrature from two lower-dimensional quadratures
25982df84da0SMatthew G. Knepley 
25992df84da0SMatthew G. Knepley   Not Collective
26002df84da0SMatthew G. Knepley 
26012df84da0SMatthew G. Knepley   Input Parameters:
26022df84da0SMatthew G. Knepley + q1 - The first quadrature
26032df84da0SMatthew G. Knepley - q2 - The second quadrature
26042df84da0SMatthew G. Knepley 
26052df84da0SMatthew G. Knepley   Output Parameter:
2606dce8aebaSBarry Smith . q - A `PetscQuadrature` object
26072df84da0SMatthew G. Knepley 
26082df84da0SMatthew G. Knepley   Level: intermediate
26092df84da0SMatthew G. Knepley 
2610dce8aebaSBarry Smith .seealso: `PetscQuadrature`, `PetscDTGaussTensorQuadrature()`
26112df84da0SMatthew G. Knepley @*/
PetscDTTensorQuadratureCreate(PetscQuadrature q1,PetscQuadrature q2,PetscQuadrature * q)2612d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTTensorQuadratureCreate(PetscQuadrature q1, PetscQuadrature q2, PetscQuadrature *q)
2613d71ae5a4SJacob Faibussowitsch {
26144366bac7SMatthew G. Knepley   DMPolytopeType   ct1, ct2, ct;
26152df84da0SMatthew G. Knepley   const PetscReal *x1, *w1, *x2, *w2;
26162df84da0SMatthew G. Knepley   PetscReal       *x, *w;
26172df84da0SMatthew G. Knepley   PetscInt         dim1, Nc1, Np1, order1, qa, d1;
26182df84da0SMatthew G. Knepley   PetscInt         dim2, Nc2, Np2, order2, qb, d2;
26192df84da0SMatthew G. Knepley   PetscInt         dim, Nc, Np, order, qc, d;
26202df84da0SMatthew G. Knepley 
26212df84da0SMatthew G. Knepley   PetscFunctionBegin;
26222df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(q1, PETSCQUADRATURE_CLASSID, 1);
26232df84da0SMatthew G. Knepley   PetscValidHeaderSpecific(q2, PETSCQUADRATURE_CLASSID, 2);
26244f572ea9SToby Isaac   PetscAssertPointer(q, 3);
2625377f809aSBarry Smith 
26269566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetOrder(q1, &order1));
26279566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetOrder(q2, &order2));
26282df84da0SMatthew G. Knepley   PetscCheck(order1 == order2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Order1 %" PetscInt_FMT " != %" PetscInt_FMT " Order2", order1, order2);
26299566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(q1, &dim1, &Nc1, &Np1, &x1, &w1));
26304366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureGetCellType(q1, &ct1));
26319566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureGetData(q2, &dim2, &Nc2, &Np2, &x2, &w2));
26324366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureGetCellType(q2, &ct2));
26332df84da0SMatthew G. Knepley   PetscCheck(Nc1 == Nc2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "NumComp1 %" PetscInt_FMT " != %" PetscInt_FMT " NumComp2", Nc1, Nc2);
26342df84da0SMatthew G. Knepley 
26354366bac7SMatthew G. Knepley   switch (ct1) {
26364366bac7SMatthew G. Knepley   case DM_POLYTOPE_POINT:
26374366bac7SMatthew G. Knepley     ct = ct2;
26384366bac7SMatthew G. Knepley     break;
26394366bac7SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
26404366bac7SMatthew G. Knepley     switch (ct2) {
26414366bac7SMatthew G. Knepley     case DM_POLYTOPE_POINT:
26424366bac7SMatthew G. Knepley       ct = ct1;
26434366bac7SMatthew G. Knepley       break;
26444366bac7SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
26454366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_QUADRILATERAL;
26464366bac7SMatthew G. Knepley       break;
26474366bac7SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
26484366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_TRI_PRISM;
26494366bac7SMatthew G. Knepley       break;
26504366bac7SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
26514366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_HEXAHEDRON;
26524366bac7SMatthew G. Knepley       break;
26534366bac7SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
26544366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26554366bac7SMatthew G. Knepley       break;
26564366bac7SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
26574366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26584366bac7SMatthew G. Knepley       break;
26594366bac7SMatthew G. Knepley     default:
26604366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26614366bac7SMatthew G. Knepley     }
26624366bac7SMatthew G. Knepley     break;
26634366bac7SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
26644366bac7SMatthew G. Knepley     switch (ct2) {
26654366bac7SMatthew G. Knepley     case DM_POLYTOPE_POINT:
26664366bac7SMatthew G. Knepley       ct = ct1;
26674366bac7SMatthew G. Knepley       break;
26684366bac7SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
26694366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_TRI_PRISM;
26704366bac7SMatthew G. Knepley       break;
26714366bac7SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
26724366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26734366bac7SMatthew G. Knepley       break;
26744366bac7SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
26754366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26764366bac7SMatthew G. Knepley       break;
26774366bac7SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
26784366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26794366bac7SMatthew G. Knepley       break;
26804366bac7SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
26814366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26824366bac7SMatthew G. Knepley       break;
26834366bac7SMatthew G. Knepley     default:
26844366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26854366bac7SMatthew G. Knepley     }
26864366bac7SMatthew G. Knepley     break;
26874366bac7SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
26884366bac7SMatthew G. Knepley     switch (ct2) {
26894366bac7SMatthew G. Knepley     case DM_POLYTOPE_POINT:
26904366bac7SMatthew G. Knepley       ct = ct1;
26914366bac7SMatthew G. Knepley       break;
26924366bac7SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
26934366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_HEXAHEDRON;
26944366bac7SMatthew G. Knepley       break;
26954366bac7SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
26964366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
26974366bac7SMatthew G. Knepley       break;
26984366bac7SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
26994366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27004366bac7SMatthew G. Knepley       break;
27014366bac7SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
27024366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27034366bac7SMatthew G. Knepley       break;
27044366bac7SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
27054366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27064366bac7SMatthew G. Knepley       break;
27074366bac7SMatthew G. Knepley     default:
27084366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27094366bac7SMatthew G. Knepley     }
27104366bac7SMatthew G. Knepley     break;
27114366bac7SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
27124366bac7SMatthew G. Knepley     switch (ct2) {
27134366bac7SMatthew G. Knepley     case DM_POLYTOPE_POINT:
27144366bac7SMatthew G. Knepley       ct = ct1;
27154366bac7SMatthew G. Knepley       break;
27164366bac7SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
27174366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27184366bac7SMatthew G. Knepley       break;
27194366bac7SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
27204366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27214366bac7SMatthew G. Knepley       break;
27224366bac7SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
27234366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27244366bac7SMatthew G. Knepley       break;
27254366bac7SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
27264366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27274366bac7SMatthew G. Knepley       break;
27284366bac7SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
27294366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27304366bac7SMatthew G. Knepley       break;
27314366bac7SMatthew G. Knepley     default:
27324366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27334366bac7SMatthew G. Knepley     }
27344366bac7SMatthew G. Knepley     break;
27354366bac7SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
27364366bac7SMatthew G. Knepley     switch (ct2) {
27374366bac7SMatthew G. Knepley     case DM_POLYTOPE_POINT:
27384366bac7SMatthew G. Knepley       ct = ct1;
27394366bac7SMatthew G. Knepley       break;
27404366bac7SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
27414366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27424366bac7SMatthew G. Knepley       break;
27434366bac7SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
27444366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27454366bac7SMatthew G. Knepley       break;
27464366bac7SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
27474366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27484366bac7SMatthew G. Knepley       break;
27494366bac7SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
27504366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27514366bac7SMatthew G. Knepley       break;
27524366bac7SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
27534366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27544366bac7SMatthew G. Knepley       break;
27554366bac7SMatthew G. Knepley     default:
27564366bac7SMatthew G. Knepley       ct = DM_POLYTOPE_UNKNOWN;
27574366bac7SMatthew G. Knepley     }
27584366bac7SMatthew G. Knepley     break;
27594366bac7SMatthew G. Knepley   default:
27604366bac7SMatthew G. Knepley     ct = DM_POLYTOPE_UNKNOWN;
27614366bac7SMatthew G. Knepley   }
27622df84da0SMatthew G. Knepley   dim   = dim1 + dim2;
27632df84da0SMatthew G. Knepley   Nc    = Nc1;
27642df84da0SMatthew G. Knepley   Np    = Np1 * Np2;
27652df84da0SMatthew G. Knepley   order = order1;
27669566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
27674366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureSetCellType(*q, ct));
27689566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetOrder(*q, order));
27699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Np * dim, &x));
27709566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(Np, &w));
27712df84da0SMatthew G. Knepley   for (qa = 0, qc = 0; qa < Np1; ++qa) {
27722df84da0SMatthew G. Knepley     for (qb = 0; qb < Np2; ++qb, ++qc) {
2773ad540459SPierre Jolivet       for (d1 = 0, d = 0; d1 < dim1; ++d1, ++d) x[qc * dim + d] = x1[qa * dim1 + d1];
2774ad540459SPierre Jolivet       for (d2 = 0; d2 < dim2; ++d2, ++d) x[qc * dim + d] = x2[qb * dim2 + d2];
27752df84da0SMatthew G. Knepley       w[qc] = w1[qa] * w2[qb];
27762df84da0SMatthew G. Knepley     }
27772df84da0SMatthew G. Knepley   }
27789566063dSJacob Faibussowitsch   PetscCall(PetscQuadratureSetData(*q, dim, Nc, Np, x, w));
27793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27802df84da0SMatthew G. Knepley }
27812df84da0SMatthew G. Knepley 
2782194825f6SJed Brown /* Overwrites A. Can only handle full-rank problems with m>=n
2783dce8aebaSBarry Smith    A in column-major format
2784dce8aebaSBarry Smith    Ainv in row-major format
2785dce8aebaSBarry Smith    tau has length m
2786dce8aebaSBarry Smith    worksize must be >= max(1,n)
2787194825f6SJed Brown  */
PetscDTPseudoInverseQR(PetscInt m,PetscInt mstride,PetscInt n,PetscReal * A_in,PetscReal * Ainv_out,PetscScalar * tau,PetscInt worksize,PetscScalar * work)2788d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m, PetscInt mstride, PetscInt n, PetscReal *A_in, PetscReal *Ainv_out, PetscScalar *tau, PetscInt worksize, PetscScalar *work)
2789d71ae5a4SJacob Faibussowitsch {
2790194825f6SJed Brown   PetscBLASInt M, N, K, lda, ldb, ldwork, info;
2791194825f6SJed Brown   PetscScalar *A, *Ainv, *R, *Q, Alpha;
2792194825f6SJed Brown 
2793194825f6SJed Brown   PetscFunctionBegin;
2794194825f6SJed Brown #if defined(PETSC_USE_COMPLEX)
2795194825f6SJed Brown   {
2796194825f6SJed Brown     PetscInt i, j;
27979566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(m * n, &A, m * n, &Ainv));
2798194825f6SJed Brown     for (j = 0; j < n; j++) {
2799194825f6SJed Brown       for (i = 0; i < m; i++) A[i + m * j] = A_in[i + mstride * j];
2800194825f6SJed Brown     }
2801194825f6SJed Brown     mstride = m;
2802194825f6SJed Brown   }
2803194825f6SJed Brown #else
2804194825f6SJed Brown   A    = A_in;
2805194825f6SJed Brown   Ainv = Ainv_out;
2806194825f6SJed Brown #endif
2807194825f6SJed Brown 
28089566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(m, &M));
28099566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(n, &N));
28109566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(mstride, &lda));
28119566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(worksize, &ldwork));
28129566063dSJacob Faibussowitsch   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2813792fecdfSBarry Smith   PetscCallBLAS("LAPACKgeqrf", LAPACKgeqrf_(&M, &N, A, &lda, tau, work, &ldwork, &info));
28149566063dSJacob Faibussowitsch   PetscCall(PetscFPTrapPop());
281528b400f6SJacob Faibussowitsch   PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "xGEQRF error");
2816194825f6SJed Brown   R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */
2817194825f6SJed Brown 
2818194825f6SJed Brown   /* Extract an explicit representation of Q */
2819194825f6SJed Brown   Q = Ainv;
28209566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(Q, A, mstride * n));
2821194825f6SJed Brown   K = N; /* full rank */
2822792fecdfSBarry Smith   PetscCallBLAS("LAPACKorgqr", LAPACKorgqr_(&M, &N, &K, Q, &lda, tau, work, &ldwork, &info));
282328b400f6SJacob Faibussowitsch   PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "xORGQR/xUNGQR error");
2824194825f6SJed Brown 
2825194825f6SJed Brown   /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */
2826194825f6SJed Brown   Alpha = 1.0;
2827194825f6SJed Brown   ldb   = lda;
2828792fecdfSBarry Smith   PetscCallBLAS("BLAStrsm", BLAStrsm_("Right", "Upper", "ConjugateTranspose", "NotUnitTriangular", &M, &N, &Alpha, R, &lda, Q, &ldb));
2829194825f6SJed Brown   /* Ainv is Q, overwritten with inverse */
2830194825f6SJed Brown 
2831194825f6SJed Brown #if defined(PETSC_USE_COMPLEX)
2832194825f6SJed Brown   {
2833194825f6SJed Brown     PetscInt i;
2834194825f6SJed Brown     for (i = 0; i < m * n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]);
28359566063dSJacob Faibussowitsch     PetscCall(PetscFree2(A, Ainv));
2836194825f6SJed Brown   }
2837194825f6SJed Brown #endif
28383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2839194825f6SJed Brown }
2840194825f6SJed Brown 
2841194825f6SJed Brown /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */
PetscDTLegendreIntegrate(PetscInt ninterval,const PetscReal * x,PetscInt ndegree,const PetscInt * degrees,PetscBool Transpose,PetscReal * B)2842d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval, const PetscReal *x, PetscInt ndegree, const PetscInt *degrees, PetscBool Transpose, PetscReal *B)
2843d71ae5a4SJacob Faibussowitsch {
2844194825f6SJed Brown   PetscReal *Bv;
2845194825f6SJed Brown   PetscInt   i, j;
2846194825f6SJed Brown 
2847194825f6SJed Brown   PetscFunctionBegin;
28489566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1((ninterval + 1) * ndegree, &Bv));
2849194825f6SJed Brown   /* Point evaluation of L_p on all the source vertices */
28509566063dSJacob Faibussowitsch   PetscCall(PetscDTLegendreEval(ninterval + 1, x, ndegree, degrees, Bv, NULL, NULL));
2851194825f6SJed Brown   /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */
2852194825f6SJed Brown   for (i = 0; i < ninterval; i++) {
2853194825f6SJed Brown     for (j = 0; j < ndegree; j++) {
2854194825f6SJed Brown       if (Transpose) B[i + ninterval * j] = Bv[(i + 1) * ndegree + j] - Bv[i * ndegree + j];
2855194825f6SJed Brown       else B[i * ndegree + j] = Bv[(i + 1) * ndegree + j] - Bv[i * ndegree + j];
2856194825f6SJed Brown     }
2857194825f6SJed Brown   }
28589566063dSJacob Faibussowitsch   PetscCall(PetscFree(Bv));
28593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2860194825f6SJed Brown }
2861194825f6SJed Brown 
2862194825f6SJed Brown /*@
2863194825f6SJed Brown   PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals
2864194825f6SJed Brown 
2865194825f6SJed Brown   Not Collective
2866194825f6SJed Brown 
28674165533cSJose E. Roman   Input Parameters:
2868194825f6SJed Brown + degree  - degree of reconstruction polynomial
2869194825f6SJed Brown . nsource - number of source intervals
28701d27aa22SBarry Smith . sourcex - sorted coordinates of source cell boundaries (length `nsource`+1)
2871194825f6SJed Brown . ntarget - number of target intervals
28721d27aa22SBarry Smith - targetx - sorted coordinates of target cell boundaries (length `ntarget`+1)
2873194825f6SJed Brown 
28744165533cSJose E. Roman   Output Parameter:
2875194825f6SJed Brown . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s]
2876194825f6SJed Brown 
2877194825f6SJed Brown   Level: advanced
2878194825f6SJed Brown 
2879db781477SPatrick Sanan .seealso: `PetscDTLegendreEval()`
2880194825f6SJed Brown @*/
PetscDTReconstructPoly(PetscInt degree,PetscInt nsource,const PetscReal sourcex[],PetscInt ntarget,const PetscReal targetx[],PetscReal R[])2881cc4c1da9SBarry Smith PetscErrorCode PetscDTReconstructPoly(PetscInt degree, PetscInt nsource, const PetscReal sourcex[], PetscInt ntarget, const PetscReal targetx[], PetscReal R[])
2882d71ae5a4SJacob Faibussowitsch {
2883194825f6SJed Brown   PetscInt     i, j, k, *bdegrees, worksize;
2884194825f6SJed Brown   PetscReal    xmin, xmax, center, hscale, *sourcey, *targety, *Bsource, *Bsinv, *Btarget;
2885194825f6SJed Brown   PetscScalar *tau, *work;
2886194825f6SJed Brown 
2887194825f6SJed Brown   PetscFunctionBegin;
28884f572ea9SToby Isaac   PetscAssertPointer(sourcex, 3);
28894f572ea9SToby Isaac   PetscAssertPointer(targetx, 5);
28904f572ea9SToby Isaac   PetscAssertPointer(R, 6);
289163a3b9bcSJacob Faibussowitsch   PetscCheck(degree < nsource, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Reconstruction degree %" PetscInt_FMT " must be less than number of source intervals %" PetscInt_FMT, degree, nsource);
289276bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
2893ad540459SPierre Jolivet     for (i = 0; i < nsource; i++) PetscCheck(sourcex[i] < sourcex[i + 1], PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Source interval %" PetscInt_FMT " has negative orientation (%g,%g)", i, (double)sourcex[i], (double)sourcex[i + 1]);
2894ad540459SPierre Jolivet     for (i = 0; i < ntarget; i++) PetscCheck(targetx[i] < targetx[i + 1], PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Target interval %" PetscInt_FMT " has negative orientation (%g,%g)", i, (double)targetx[i], (double)targetx[i + 1]);
289576bd3646SJed Brown   }
2896194825f6SJed Brown   xmin     = PetscMin(sourcex[0], targetx[0]);
2897194825f6SJed Brown   xmax     = PetscMax(sourcex[nsource], targetx[ntarget]);
2898194825f6SJed Brown   center   = (xmin + xmax) / 2;
2899194825f6SJed Brown   hscale   = (xmax - xmin) / 2;
2900194825f6SJed Brown   worksize = nsource;
29019566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(degree + 1, &bdegrees, nsource + 1, &sourcey, nsource * (degree + 1), &Bsource, worksize, &work));
29029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(nsource, &tau, nsource * (degree + 1), &Bsinv, ntarget + 1, &targety, ntarget * (degree + 1), &Btarget));
2903194825f6SJed Brown   for (i = 0; i <= nsource; i++) sourcey[i] = (sourcex[i] - center) / hscale;
2904194825f6SJed Brown   for (i = 0; i <= degree; i++) bdegrees[i] = i + 1;
29059566063dSJacob Faibussowitsch   PetscCall(PetscDTLegendreIntegrate(nsource, sourcey, degree + 1, bdegrees, PETSC_TRUE, Bsource));
29069566063dSJacob Faibussowitsch   PetscCall(PetscDTPseudoInverseQR(nsource, nsource, degree + 1, Bsource, Bsinv, tau, nsource, work));
2907194825f6SJed Brown   for (i = 0; i <= ntarget; i++) targety[i] = (targetx[i] - center) / hscale;
29089566063dSJacob Faibussowitsch   PetscCall(PetscDTLegendreIntegrate(ntarget, targety, degree + 1, bdegrees, PETSC_FALSE, Btarget));
2909194825f6SJed Brown   for (i = 0; i < ntarget; i++) {
2910194825f6SJed Brown     PetscReal rowsum = 0;
2911194825f6SJed Brown     for (j = 0; j < nsource; j++) {
2912194825f6SJed Brown       PetscReal sum = 0;
2913ad540459SPierre Jolivet       for (k = 0; k < degree + 1; k++) sum += Btarget[i * (degree + 1) + k] * Bsinv[k * nsource + j];
2914194825f6SJed Brown       R[i * nsource + j] = sum;
2915194825f6SJed Brown       rowsum += sum;
2916194825f6SJed Brown     }
2917194825f6SJed Brown     for (j = 0; j < nsource; j++) R[i * nsource + j] /= rowsum; /* normalize each row */
2918194825f6SJed Brown   }
29199566063dSJacob Faibussowitsch   PetscCall(PetscFree4(bdegrees, sourcey, Bsource, work));
29209566063dSJacob Faibussowitsch   PetscCall(PetscFree4(tau, Bsinv, targety, Btarget));
29213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2922194825f6SJed Brown }
2923916e780bShannah_mairs 
2924cc4c1da9SBarry Smith /*@
2925916e780bShannah_mairs   PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points
2926916e780bShannah_mairs 
2927916e780bShannah_mairs   Not Collective
2928916e780bShannah_mairs 
2929d8d19677SJose E. Roman   Input Parameters:
2930916e780bShannah_mairs + n       - the number of GLL nodes
2931916e780bShannah_mairs . nodes   - the GLL nodes
2932916e780bShannah_mairs . weights - the GLL weights
2933f0fc11ceSJed Brown - f       - the function values at the nodes
2934916e780bShannah_mairs 
2935916e780bShannah_mairs   Output Parameter:
2936916e780bShannah_mairs . in - the value of the integral
2937916e780bShannah_mairs 
2938916e780bShannah_mairs   Level: beginner
2939916e780bShannah_mairs 
2940db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`
2941916e780bShannah_mairs @*/
PetscGaussLobattoLegendreIntegrate(PetscInt n,PetscReal nodes[],PetscReal weights[],const PetscReal f[],PetscReal * in)2942cc4c1da9SBarry Smith PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n, PetscReal nodes[], PetscReal weights[], const PetscReal f[], PetscReal *in)
2943d71ae5a4SJacob Faibussowitsch {
2944916e780bShannah_mairs   PetscInt i;
2945916e780bShannah_mairs 
2946916e780bShannah_mairs   PetscFunctionBegin;
2947916e780bShannah_mairs   *in = 0.;
2948ad540459SPierre Jolivet   for (i = 0; i < n; i++) *in += f[i] * f[i] * weights[i];
29493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2950916e780bShannah_mairs }
2951916e780bShannah_mairs 
2952916e780bShannah_mairs /*@C
2953916e780bShannah_mairs   PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element
2954916e780bShannah_mairs 
2955916e780bShannah_mairs   Not Collective
2956916e780bShannah_mairs 
2957d8d19677SJose E. Roman   Input Parameters:
2958916e780bShannah_mairs + n       - the number of GLL nodes
2959f13dfd9eSBarry Smith . nodes   - the GLL nodes, of length `n`
2960f13dfd9eSBarry Smith - weights - the GLL weights, of length `n`
2961916e780bShannah_mairs 
2962916e780bShannah_mairs   Output Parameter:
2963f13dfd9eSBarry Smith . AA - the stiffness element, of size `n` by `n`
2964916e780bShannah_mairs 
2965916e780bShannah_mairs   Level: beginner
2966916e780bShannah_mairs 
2967916e780bShannah_mairs   Notes:
2968dce8aebaSBarry Smith   Destroy this with `PetscGaussLobattoLegendreElementLaplacianDestroy()`
2969916e780bShannah_mairs 
29705e116b59SBarry Smith   You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row-oriented (the array is symmetric)
2971916e780bShannah_mairs 
2972db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianDestroy()`
2973916e780bShannah_mairs @*/
PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n,PetscReal nodes[],PetscReal weights[],PetscReal *** AA)2974cc4c1da9SBarry Smith PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
2975d71ae5a4SJacob Faibussowitsch {
2976916e780bShannah_mairs   PetscReal      **A;
2977916e780bShannah_mairs   const PetscReal *gllnodes = nodes;
2978916e780bShannah_mairs   const PetscInt   p        = n - 1;
2979916e780bShannah_mairs   PetscReal        z0, z1, z2 = -1, x, Lpj, Lpr;
2980916e780bShannah_mairs   PetscInt         i, j, nn, r;
2981916e780bShannah_mairs 
2982916e780bShannah_mairs   PetscFunctionBegin;
29839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &A));
29849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n * n, &A[0]));
2985916e780bShannah_mairs   for (i = 1; i < n; i++) A[i] = A[i - 1] + n;
2986916e780bShannah_mairs 
2987916e780bShannah_mairs   for (j = 1; j < p; j++) {
2988916e780bShannah_mairs     x  = gllnodes[j];
2989916e780bShannah_mairs     z0 = 1.;
2990916e780bShannah_mairs     z1 = x;
2991916e780bShannah_mairs     for (nn = 1; nn < p; nn++) {
2992916e780bShannah_mairs       z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
2993916e780bShannah_mairs       z0 = z1;
2994916e780bShannah_mairs       z1 = z2;
2995916e780bShannah_mairs     }
2996916e780bShannah_mairs     Lpj = z2;
2997916e780bShannah_mairs     for (r = 1; r < p; r++) {
2998916e780bShannah_mairs       if (r == j) {
2999916e780bShannah_mairs         A[j][j] = 2. / (3. * (1. - gllnodes[j] * gllnodes[j]) * Lpj * Lpj);
3000916e780bShannah_mairs       } else {
3001916e780bShannah_mairs         x  = gllnodes[r];
3002916e780bShannah_mairs         z0 = 1.;
3003916e780bShannah_mairs         z1 = x;
3004916e780bShannah_mairs         for (nn = 1; nn < p; nn++) {
3005916e780bShannah_mairs           z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3006916e780bShannah_mairs           z0 = z1;
3007916e780bShannah_mairs           z1 = z2;
3008916e780bShannah_mairs         }
3009916e780bShannah_mairs         Lpr     = z2;
3010916e780bShannah_mairs         A[r][j] = 4. / (((PetscReal)p) * (((PetscReal)p) + 1.) * Lpj * Lpr * (gllnodes[j] - gllnodes[r]) * (gllnodes[j] - gllnodes[r]));
3011916e780bShannah_mairs       }
3012916e780bShannah_mairs     }
3013916e780bShannah_mairs   }
3014916e780bShannah_mairs   for (j = 1; j < p + 1; j++) {
3015916e780bShannah_mairs     x  = gllnodes[j];
3016916e780bShannah_mairs     z0 = 1.;
3017916e780bShannah_mairs     z1 = x;
3018916e780bShannah_mairs     for (nn = 1; nn < p; nn++) {
3019916e780bShannah_mairs       z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3020916e780bShannah_mairs       z0 = z1;
3021916e780bShannah_mairs       z1 = z2;
3022916e780bShannah_mairs     }
3023916e780bShannah_mairs     Lpj     = z2;
3024916e780bShannah_mairs     A[j][0] = 4. * PetscPowRealInt(-1., p) / (((PetscReal)p) * (((PetscReal)p) + 1.) * Lpj * (1. + gllnodes[j]) * (1. + gllnodes[j]));
3025916e780bShannah_mairs     A[0][j] = A[j][0];
3026916e780bShannah_mairs   }
3027916e780bShannah_mairs   for (j = 0; j < p; j++) {
3028916e780bShannah_mairs     x  = gllnodes[j];
3029916e780bShannah_mairs     z0 = 1.;
3030916e780bShannah_mairs     z1 = x;
3031916e780bShannah_mairs     for (nn = 1; nn < p; nn++) {
3032916e780bShannah_mairs       z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3033916e780bShannah_mairs       z0 = z1;
3034916e780bShannah_mairs       z1 = z2;
3035916e780bShannah_mairs     }
3036916e780bShannah_mairs     Lpj = z2;
3037916e780bShannah_mairs 
3038916e780bShannah_mairs     A[p][j] = 4. / (((PetscReal)p) * (((PetscReal)p) + 1.) * Lpj * (1. - gllnodes[j]) * (1. - gllnodes[j]));
3039916e780bShannah_mairs     A[j][p] = A[p][j];
3040916e780bShannah_mairs   }
3041916e780bShannah_mairs   A[0][0] = 0.5 + (((PetscReal)p) * (((PetscReal)p) + 1.) - 2.) / 6.;
3042916e780bShannah_mairs   A[p][p] = A[0][0];
3043916e780bShannah_mairs   *AA     = A;
30443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3045916e780bShannah_mairs }
3046916e780bShannah_mairs 
3047916e780bShannah_mairs /*@C
3048dce8aebaSBarry Smith   PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element created with `PetscGaussLobattoLegendreElementLaplacianCreate()`
3049916e780bShannah_mairs 
3050916e780bShannah_mairs   Not Collective
3051916e780bShannah_mairs 
3052d8d19677SJose E. Roman   Input Parameters:
3053916e780bShannah_mairs + n       - the number of GLL nodes
3054f13dfd9eSBarry Smith . nodes   - the GLL nodes, ignored
3055f13dfd9eSBarry Smith . weights - the GLL weightss, ignored
3056f13dfd9eSBarry Smith - AA      - the stiffness element from `PetscGaussLobattoLegendreElementLaplacianCreate()`
3057916e780bShannah_mairs 
3058916e780bShannah_mairs   Level: beginner
3059916e780bShannah_mairs 
3060db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`
3061916e780bShannah_mairs @*/
PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n,PetscReal nodes[],PetscReal weights[],PetscReal *** AA)3062cc4c1da9SBarry Smith PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3063d71ae5a4SJacob Faibussowitsch {
3064916e780bShannah_mairs   PetscFunctionBegin;
30659566063dSJacob Faibussowitsch   PetscCall(PetscFree((*AA)[0]));
30669566063dSJacob Faibussowitsch   PetscCall(PetscFree(*AA));
3067916e780bShannah_mairs   *AA = NULL;
30683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3069916e780bShannah_mairs }
3070916e780bShannah_mairs 
3071916e780bShannah_mairs /*@C
3072916e780bShannah_mairs   PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element
3073916e780bShannah_mairs 
3074916e780bShannah_mairs   Not Collective
3075916e780bShannah_mairs 
307660225df5SJacob Faibussowitsch   Input Parameters:
3077916e780bShannah_mairs + n       - the number of GLL nodes
3078f13dfd9eSBarry Smith . nodes   - the GLL nodes, of length `n`
3079f13dfd9eSBarry Smith - weights - the GLL weights, of length `n`
3080916e780bShannah_mairs 
3081d8d19677SJose E. Roman   Output Parameters:
3082f13dfd9eSBarry Smith + AA  - the stiffness element, of dimension `n` by `n`
3083f13dfd9eSBarry Smith - AAT - the transpose of AA (pass in `NULL` if you do not need this array), of dimension `n` by `n`
3084916e780bShannah_mairs 
3085916e780bShannah_mairs   Level: beginner
3086916e780bShannah_mairs 
3087916e780bShannah_mairs   Notes:
3088dce8aebaSBarry Smith   Destroy this with `PetscGaussLobattoLegendreElementGradientDestroy()`
3089916e780bShannah_mairs 
30905e116b59SBarry Smith   You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row-oriented
3091916e780bShannah_mairs 
3092dce8aebaSBarry Smith .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianDestroy()`, `PetscGaussLobattoLegendreElementGradientDestroy()`
3093916e780bShannah_mairs @*/
PetscGaussLobattoLegendreElementGradientCreate(PetscInt n,PetscReal nodes[],PetscReal weights[],PetscReal *** AA,PetscReal *** AAT)3094cc4c1da9SBarry Smith PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA, PetscReal ***AAT)
3095d71ae5a4SJacob Faibussowitsch {
3096916e780bShannah_mairs   PetscReal      **A, **AT = NULL;
3097916e780bShannah_mairs   const PetscReal *gllnodes = nodes;
3098916e780bShannah_mairs   const PetscInt   p        = n - 1;
3099e6a796c3SToby Isaac   PetscReal        Li, Lj, d0;
3100916e780bShannah_mairs   PetscInt         i, j;
3101916e780bShannah_mairs 
3102916e780bShannah_mairs   PetscFunctionBegin;
31039566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n, &A));
31049566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(n * n, &A[0]));
3105916e780bShannah_mairs   for (i = 1; i < n; i++) A[i] = A[i - 1] + n;
3106916e780bShannah_mairs 
3107916e780bShannah_mairs   if (AAT) {
31089566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n, &AT));
31099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(n * n, &AT[0]));
3110916e780bShannah_mairs     for (i = 1; i < n; i++) AT[i] = AT[i - 1] + n;
3111916e780bShannah_mairs   }
3112916e780bShannah_mairs 
3113ad540459SPierre Jolivet   if (n == 1) A[0][0] = 0.;
3114916e780bShannah_mairs   d0 = (PetscReal)p * ((PetscReal)p + 1.) / 4.;
3115916e780bShannah_mairs   for (i = 0; i < n; i++) {
3116916e780bShannah_mairs     for (j = 0; j < n; j++) {
3117916e780bShannah_mairs       A[i][j] = 0.;
31189566063dSJacob Faibussowitsch       PetscCall(PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li));
31199566063dSJacob Faibussowitsch       PetscCall(PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj));
3120916e780bShannah_mairs       if (i != j) A[i][j] = Li / (Lj * (gllnodes[i] - gllnodes[j]));
3121916e780bShannah_mairs       if ((j == i) && (i == 0)) A[i][j] = -d0;
3122916e780bShannah_mairs       if (j == i && i == p) A[i][j] = d0;
3123916e780bShannah_mairs       if (AT) AT[j][i] = A[i][j];
3124916e780bShannah_mairs     }
3125916e780bShannah_mairs   }
3126916e780bShannah_mairs   if (AAT) *AAT = AT;
3127916e780bShannah_mairs   *AA = A;
31283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3129916e780bShannah_mairs }
3130916e780bShannah_mairs 
3131916e780bShannah_mairs /*@C
3132dce8aebaSBarry Smith   PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with `PetscGaussLobattoLegendreElementGradientCreate()`
3133916e780bShannah_mairs 
3134916e780bShannah_mairs   Not Collective
3135916e780bShannah_mairs 
3136d8d19677SJose E. Roman   Input Parameters:
3137916e780bShannah_mairs + n       - the number of GLL nodes
3138f13dfd9eSBarry Smith . nodes   - the GLL nodes, ignored
3139f13dfd9eSBarry Smith . weights - the GLL weights, ignored
3140f13dfd9eSBarry Smith . AA      - the stiffness element obtained with `PetscGaussLobattoLegendreElementGradientCreate()`
3141f13dfd9eSBarry Smith - AAT     - the transpose of the element obtained with `PetscGaussLobattoLegendreElementGradientCreate()`
3142916e780bShannah_mairs 
3143916e780bShannah_mairs   Level: beginner
3144916e780bShannah_mairs 
3145db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionCreate()`
3146916e780bShannah_mairs @*/
PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n,PetscReal nodes[],PetscReal weights[],PetscReal *** AA,PetscReal *** AAT)3147f13dfd9eSBarry Smith PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA, PetscReal ***AAT)
3148d71ae5a4SJacob Faibussowitsch {
3149916e780bShannah_mairs   PetscFunctionBegin;
31509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*AA)[0]));
31519566063dSJacob Faibussowitsch   PetscCall(PetscFree(*AA));
3152916e780bShannah_mairs   *AA = NULL;
31539ea709c2SMatthew G. Knepley   if (AAT) {
31549566063dSJacob Faibussowitsch     PetscCall(PetscFree((*AAT)[0]));
31559566063dSJacob Faibussowitsch     PetscCall(PetscFree(*AAT));
3156916e780bShannah_mairs     *AAT = NULL;
3157916e780bShannah_mairs   }
31583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3159916e780bShannah_mairs }
3160916e780bShannah_mairs 
3161916e780bShannah_mairs /*@C
3162916e780bShannah_mairs   PetscGaussLobattoLegendreElementAdvectionCreate - computes the advection operator for a single 1d GLL element
3163916e780bShannah_mairs 
3164916e780bShannah_mairs   Not Collective
3165916e780bShannah_mairs 
3166d8d19677SJose E. Roman   Input Parameters:
3167916e780bShannah_mairs + n       - the number of GLL nodes
3168f13dfd9eSBarry Smith . nodes   - the GLL nodes, of length `n`
3169f13dfd9eSBarry Smith - weights - the GLL weights, of length `n`
3170916e780bShannah_mairs 
3171916e780bShannah_mairs   Output Parameter:
3172f13dfd9eSBarry Smith . AA - the stiffness element, of dimension `n` by `n`
3173916e780bShannah_mairs 
3174916e780bShannah_mairs   Level: beginner
3175916e780bShannah_mairs 
3176916e780bShannah_mairs   Notes:
3177dce8aebaSBarry Smith   Destroy this with `PetscGaussLobattoLegendreElementAdvectionDestroy()`
3178916e780bShannah_mairs 
3179916e780bShannah_mairs   This is the same as the Gradient operator multiplied by the diagonal mass matrix
3180916e780bShannah_mairs 
31815e116b59SBarry Smith   You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row-oriented
3182916e780bShannah_mairs 
3183db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionDestroy()`
3184916e780bShannah_mairs @*/
PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n,PetscReal nodes[],PetscReal weights[],PetscReal *** AA)3185cc4c1da9SBarry Smith PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3186d71ae5a4SJacob Faibussowitsch {
3187916e780bShannah_mairs   PetscReal      **D;
3188916e780bShannah_mairs   const PetscReal *gllweights = weights;
3189916e780bShannah_mairs   const PetscInt   glln       = n;
3190916e780bShannah_mairs   PetscInt         i, j;
3191916e780bShannah_mairs 
3192916e780bShannah_mairs   PetscFunctionBegin;
31939566063dSJacob Faibussowitsch   PetscCall(PetscGaussLobattoLegendreElementGradientCreate(n, nodes, weights, &D, NULL));
3194916e780bShannah_mairs   for (i = 0; i < glln; i++) {
3195ad540459SPierre Jolivet     for (j = 0; j < glln; j++) D[i][j] = gllweights[i] * D[i][j];
3196916e780bShannah_mairs   }
3197916e780bShannah_mairs   *AA = D;
31983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3199916e780bShannah_mairs }
3200916e780bShannah_mairs 
3201916e780bShannah_mairs /*@C
3202dce8aebaSBarry Smith   PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element created with `PetscGaussLobattoLegendreElementAdvectionCreate()`
3203916e780bShannah_mairs 
3204916e780bShannah_mairs   Not Collective
3205916e780bShannah_mairs 
3206d8d19677SJose E. Roman   Input Parameters:
3207916e780bShannah_mairs + n       - the number of GLL nodes
3208f13dfd9eSBarry Smith . nodes   - the GLL nodes, ignored
3209f13dfd9eSBarry Smith . weights - the GLL weights, ignored
3210f13dfd9eSBarry Smith - AA      - advection obtained with `PetscGaussLobattoLegendreElementAdvectionCreate()`
3211916e780bShannah_mairs 
3212916e780bShannah_mairs   Level: beginner
3213916e780bShannah_mairs 
3214db781477SPatrick Sanan .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementAdvectionCreate()`
3215916e780bShannah_mairs @*/
PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n,PetscReal nodes[],PetscReal weights[],PetscReal *** AA)3216f13dfd9eSBarry Smith PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3217d71ae5a4SJacob Faibussowitsch {
3218916e780bShannah_mairs   PetscFunctionBegin;
32199566063dSJacob Faibussowitsch   PetscCall(PetscFree((*AA)[0]));
32209566063dSJacob Faibussowitsch   PetscCall(PetscFree(*AA));
3221916e780bShannah_mairs   *AA = NULL;
32223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3223916e780bShannah_mairs }
3224916e780bShannah_mairs 
PetscGaussLobattoLegendreElementMassCreate(PetscInt n,PetscReal * nodes,PetscReal * weights,PetscReal *** AA)3225d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n, PetscReal *nodes, PetscReal *weights, PetscReal ***AA)
3226d71ae5a4SJacob Faibussowitsch {
3227916e780bShannah_mairs   PetscReal      **A;
3228916e780bShannah_mairs   const PetscReal *gllweights = weights;
3229916e780bShannah_mairs   const PetscInt   glln       = n;
3230916e780bShannah_mairs   PetscInt         i, j;
3231916e780bShannah_mairs 
3232916e780bShannah_mairs   PetscFunctionBegin;
32339566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(glln, &A));
32349566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(glln * glln, &A[0]));
3235916e780bShannah_mairs   for (i = 1; i < glln; i++) A[i] = A[i - 1] + glln;
3236ad540459SPierre Jolivet   if (glln == 1) A[0][0] = 0.;
3237916e780bShannah_mairs   for (i = 0; i < glln; i++) {
3238916e780bShannah_mairs     for (j = 0; j < glln; j++) {
3239916e780bShannah_mairs       A[i][j] = 0.;
3240916e780bShannah_mairs       if (j == i) A[i][j] = gllweights[i];
3241916e780bShannah_mairs     }
3242916e780bShannah_mairs   }
3243916e780bShannah_mairs   *AA = A;
32443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3245916e780bShannah_mairs }
3246916e780bShannah_mairs 
PetscGaussLobattoLegendreElementMassDestroy(PetscInt n,PetscReal * nodes,PetscReal * weights,PetscReal *** AA)3247d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n, PetscReal *nodes, PetscReal *weights, PetscReal ***AA)
3248d71ae5a4SJacob Faibussowitsch {
3249916e780bShannah_mairs   PetscFunctionBegin;
32509566063dSJacob Faibussowitsch   PetscCall(PetscFree((*AA)[0]));
32519566063dSJacob Faibussowitsch   PetscCall(PetscFree(*AA));
3252916e780bShannah_mairs   *AA = NULL;
32533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3254916e780bShannah_mairs }
3255d4afb720SToby Isaac 
3256d4afb720SToby Isaac /*@
3257d4afb720SToby Isaac   PetscDTIndexToBary - convert an index into a barycentric coordinate.
3258d4afb720SToby Isaac 
3259d4afb720SToby Isaac   Input Parameters:
3260d4afb720SToby Isaac + len   - the desired length of the barycentric tuple (usually 1 more than the dimension it represents, so a barycentric coordinate in a triangle has length 3)
3261d4afb720SToby Isaac . sum   - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to
3262d4afb720SToby Isaac - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum)
3263d4afb720SToby Isaac 
3264d4afb720SToby Isaac   Output Parameter:
3265f13dfd9eSBarry Smith . coord - will be filled with the barycentric coordinate, of length `n`
3266d4afb720SToby Isaac 
3267d4afb720SToby Isaac   Level: beginner
3268d4afb720SToby Isaac 
3269dce8aebaSBarry Smith   Note:
3270dce8aebaSBarry Smith   The indices map to barycentric coordinates in lexicographic order, where the first index is the
3271d4afb720SToby Isaac   least significant and the last index is the most significant.
3272d4afb720SToby Isaac 
3273db781477SPatrick Sanan .seealso: `PetscDTBaryToIndex()`
3274d4afb720SToby Isaac @*/
PetscDTIndexToBary(PetscInt len,PetscInt sum,PetscInt index,PetscInt coord[])3275d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[])
3276d71ae5a4SJacob Faibussowitsch {
3277d4afb720SToby Isaac   PetscInt c, d, s, total, subtotal, nexttotal;
3278d4afb720SToby Isaac 
3279d4afb720SToby Isaac   PetscFunctionBeginHot;
328008401ef6SPierre Jolivet   PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
328108401ef6SPierre Jolivet   PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative");
3282d4afb720SToby Isaac   if (!len) {
3283966bd95aSPierre Jolivet     PetscCheck(!sum && !index, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate");
3284966bd95aSPierre Jolivet     PetscFunctionReturn(PETSC_SUCCESS);
3285d4afb720SToby Isaac   }
3286d4afb720SToby Isaac   for (c = 1, total = 1; c <= len; c++) {
3287d4afb720SToby Isaac     /* total is the number of ways to have a tuple of length c with sum */
3288d4afb720SToby Isaac     if (index < total) break;
3289d4afb720SToby Isaac     total = (total * (sum + c)) / c;
3290d4afb720SToby Isaac   }
329108401ef6SPierre Jolivet   PetscCheck(c <= len, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range");
3292d4afb720SToby Isaac   for (d = c; d < len; d++) coord[d] = 0;
3293d4afb720SToby Isaac   for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) {
3294d4afb720SToby Isaac     /* subtotal is the number of ways to have a tuple of length c with sum s */
3295d4afb720SToby Isaac     /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */
3296d4afb720SToby Isaac     if ((index + subtotal) >= total) {
3297d4afb720SToby Isaac       coord[--c] = sum - s;
3298d4afb720SToby Isaac       index -= (total - subtotal);
3299d4afb720SToby Isaac       sum       = s;
3300d4afb720SToby Isaac       total     = nexttotal;
3301d4afb720SToby Isaac       subtotal  = 1;
3302d4afb720SToby Isaac       nexttotal = 1;
3303d4afb720SToby Isaac       s         = 0;
3304d4afb720SToby Isaac     } else {
3305d4afb720SToby Isaac       subtotal  = (subtotal * (c + s)) / (s + 1);
3306d4afb720SToby Isaac       nexttotal = (nexttotal * (c - 1 + s)) / (s + 1);
3307d4afb720SToby Isaac       s++;
3308d4afb720SToby Isaac     }
3309d4afb720SToby Isaac   }
33103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3311d4afb720SToby Isaac }
3312d4afb720SToby Isaac 
3313d4afb720SToby Isaac /*@
3314d4afb720SToby Isaac   PetscDTBaryToIndex - convert a barycentric coordinate to an index
3315d4afb720SToby Isaac 
3316d4afb720SToby Isaac   Input Parameters:
3317d4afb720SToby Isaac + len   - the desired length of the barycentric tuple (usually 1 more than the dimension it represents, so a barycentric coordinate in a triangle has length 3)
3318d4afb720SToby Isaac . sum   - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to
3319f13dfd9eSBarry Smith - coord - a barycentric coordinate with the given length `len` and `sum`
3320d4afb720SToby Isaac 
3321d4afb720SToby Isaac   Output Parameter:
3322d4afb720SToby Isaac . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum)
3323d4afb720SToby Isaac 
3324d4afb720SToby Isaac   Level: beginner
3325d4afb720SToby Isaac 
3326dce8aebaSBarry Smith   Note:
3327dce8aebaSBarry Smith   The indices map to barycentric coordinates in lexicographic order, where the first index is the
3328d4afb720SToby Isaac   least significant and the last index is the most significant.
3329d4afb720SToby Isaac 
3330db781477SPatrick Sanan .seealso: `PetscDTIndexToBary`
3331d4afb720SToby Isaac @*/
PetscDTBaryToIndex(PetscInt len,PetscInt sum,const PetscInt coord[],PetscInt * index)3332d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index)
3333d71ae5a4SJacob Faibussowitsch {
3334d4afb720SToby Isaac   PetscInt c;
3335d4afb720SToby Isaac   PetscInt i;
3336d4afb720SToby Isaac   PetscInt total;
3337d4afb720SToby Isaac 
3338d4afb720SToby Isaac   PetscFunctionBeginHot;
333908401ef6SPierre Jolivet   PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
3340d4afb720SToby Isaac   if (!len) {
3341966bd95aSPierre Jolivet     PetscCheck(!sum, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate");
3342d4afb720SToby Isaac     *index = 0;
33433ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
3344d4afb720SToby Isaac   }
3345d4afb720SToby Isaac   for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c;
3346d4afb720SToby Isaac   i = total - 1;
3347d4afb720SToby Isaac   c = len - 1;
3348d4afb720SToby Isaac   sum -= coord[c];
3349d4afb720SToby Isaac   while (sum > 0) {
3350d4afb720SToby Isaac     PetscInt subtotal;
3351d4afb720SToby Isaac     PetscInt s;
3352d4afb720SToby Isaac 
3353d4afb720SToby Isaac     for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s;
3354d4afb720SToby Isaac     i -= subtotal;
3355d4afb720SToby Isaac     sum -= coord[--c];
3356d4afb720SToby Isaac   }
3357d4afb720SToby Isaac   *index = i;
33583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3359d4afb720SToby Isaac }
336007218a29SMatthew G. Knepley 
33614366bac7SMatthew G. Knepley /*@
33624366bac7SMatthew G. Knepley   PetscQuadratureComputePermutations - Compute permutations of quadrature points corresponding to domain orientations
33634366bac7SMatthew G. Knepley 
33644366bac7SMatthew G. Knepley   Input Parameter:
33654366bac7SMatthew G. Knepley . quad - The `PetscQuadrature`
33664366bac7SMatthew G. Knepley 
33674366bac7SMatthew G. Knepley   Output Parameters:
33684366bac7SMatthew G. Knepley + Np   - The number of domain orientations
33694366bac7SMatthew G. Knepley - perm - An array of `IS` permutations, one for ech orientation,
33704366bac7SMatthew G. Knepley 
337160820804SBarry Smith   Level: developer
33724366bac7SMatthew G. Knepley 
33734366bac7SMatthew G. Knepley .seealso: `PetscQuadratureSetCellType()`, `PetscQuadrature`
33744366bac7SMatthew G. Knepley @*/
PetscQuadratureComputePermutations(PetscQuadrature quad,PeOp PetscInt * Np,IS * perm[])3375ce78bad3SBarry Smith PetscErrorCode PetscQuadratureComputePermutations(PetscQuadrature quad, PeOp PetscInt *Np, IS *perm[])
337607218a29SMatthew G. Knepley {
33774366bac7SMatthew G. Knepley   DMPolytopeType   ct;
337807218a29SMatthew G. Knepley   const PetscReal *xq, *wq;
337907218a29SMatthew G. Knepley   PetscInt         dim, qdim, d, Na, o, Nq, q, qp;
338007218a29SMatthew G. Knepley 
338107218a29SMatthew G. Knepley   PetscFunctionBegin;
33824366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &xq, &wq));
33834366bac7SMatthew G. Knepley   PetscCall(PetscQuadratureGetCellType(quad, &ct));
338407218a29SMatthew G. Knepley   dim = DMPolytopeTypeGetDim(ct);
338585036b15SMatthew G. Knepley   Na  = DMPolytopeTypeGetNumArrangements(ct);
338607218a29SMatthew G. Knepley   PetscCall(PetscMalloc1(Na, perm));
33874366bac7SMatthew G. Knepley   if (Np) *Np = Na;
33884366bac7SMatthew G. Knepley   Na /= 2;
33894366bac7SMatthew G. Knepley   for (o = -Na; o < Na; ++o) {
339007218a29SMatthew G. Knepley     DM        refdm;
339107218a29SMatthew G. Knepley     PetscInt *idx;
339207218a29SMatthew G. Knepley     PetscReal xi0[3] = {-1., -1., -1.}, v0[3], J[9], detJ, txq[3];
339307218a29SMatthew G. Knepley     PetscBool flg;
339407218a29SMatthew G. Knepley 
339507218a29SMatthew G. Knepley     PetscCall(DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &refdm));
339607218a29SMatthew G. Knepley     PetscCall(DMPlexOrientPoint(refdm, 0, o));
339707218a29SMatthew G. Knepley     PetscCall(DMPlexComputeCellGeometryFEM(refdm, 0, NULL, v0, J, NULL, &detJ));
339807218a29SMatthew G. Knepley     PetscCall(PetscMalloc1(Nq, &idx));
339907218a29SMatthew G. Knepley     for (q = 0; q < Nq; ++q) {
340007218a29SMatthew G. Knepley       CoordinatesRefToReal(dim, dim, xi0, v0, J, &xq[q * dim], txq);
340107218a29SMatthew G. Knepley       for (qp = 0; qp < Nq; ++qp) {
340207218a29SMatthew G. Knepley         PetscReal diff = 0.;
340307218a29SMatthew G. Knepley 
340407218a29SMatthew G. Knepley         for (d = 0; d < dim; ++d) diff += PetscAbsReal(txq[d] - xq[qp * dim + d]);
340507218a29SMatthew G. Knepley         if (diff < PETSC_SMALL) break;
340607218a29SMatthew G. Knepley       }
340707218a29SMatthew G. Knepley       PetscCheck(qp < Nq, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Transformed quad point %" PetscInt_FMT " does not match another quad point", q);
340807218a29SMatthew G. Knepley       idx[q] = qp;
340907218a29SMatthew G. Knepley     }
341007218a29SMatthew G. Knepley     PetscCall(DMDestroy(&refdm));
34114366bac7SMatthew G. Knepley     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, Nq, idx, PETSC_OWN_POINTER, &(*perm)[o + Na]));
34124366bac7SMatthew G. Knepley     PetscCall(ISGetInfo((*perm)[o + Na], IS_PERMUTATION, IS_LOCAL, PETSC_TRUE, &flg));
341307218a29SMatthew G. Knepley     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ordering for orientation %" PetscInt_FMT " was not a permutation", o);
34144366bac7SMatthew G. Knepley     PetscCall(ISSetPermutation((*perm)[o + Na]));
34154366bac7SMatthew G. Knepley   }
34164366bac7SMatthew G. Knepley   if (!Na) (*perm)[0] = NULL;
34174366bac7SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
34184366bac7SMatthew G. Knepley }
34194366bac7SMatthew G. Knepley 
34204366bac7SMatthew G. Knepley /*@
3421f2c64c88SMatthew G. Knepley   PetscDTCreateQuadratureByCell - Create default quadrature for a given cell
34224366bac7SMatthew G. Knepley 
34234366bac7SMatthew G. Knepley   Not collective
34244366bac7SMatthew G. Knepley 
34254366bac7SMatthew G. Knepley   Input Parameters:
34264366bac7SMatthew G. Knepley + ct     - The integration domain
3427f2c64c88SMatthew G. Knepley . qorder - The desired quadrature order
3428f2c64c88SMatthew G. Knepley - qtype  - The type of simplex quadrature, or PETSCDTSIMPLEXQUAD_DEFAULT
34294366bac7SMatthew G. Knepley 
34304366bac7SMatthew G. Knepley   Output Parameters:
34314366bac7SMatthew G. Knepley + q  - The cell quadrature
34324366bac7SMatthew G. Knepley - fq - The face quadrature
34334366bac7SMatthew G. Knepley 
34344366bac7SMatthew G. Knepley   Level: developer
34354366bac7SMatthew G. Knepley 
3436f2c64c88SMatthew G. Knepley .seealso: `PetscDTCreateDefaultQuadrature()`, `PetscFECreateDefault()`, `PetscDTGaussTensorQuadrature()`, `PetscDTSimplexQuadrature()`, `PetscDTTensorQuadratureCreate()`
34374366bac7SMatthew G. Knepley @*/
PetscDTCreateQuadratureByCell(DMPolytopeType ct,PetscInt qorder,PetscDTSimplexQuadratureType qtype,PetscQuadrature * q,PetscQuadrature * fq)3438f2c64c88SMatthew G. Knepley PetscErrorCode PetscDTCreateQuadratureByCell(DMPolytopeType ct, PetscInt qorder, PetscDTSimplexQuadratureType qtype, PetscQuadrature *q, PetscQuadrature *fq)
34394366bac7SMatthew G. Knepley {
34404366bac7SMatthew G. Knepley   const PetscInt quadPointsPerEdge = PetscMax(qorder + 1, 1);
34414366bac7SMatthew G. Knepley   const PetscInt dim               = DMPolytopeTypeGetDim(ct);
34424366bac7SMatthew G. Knepley 
34434366bac7SMatthew G. Knepley   PetscFunctionBegin;
34444366bac7SMatthew G. Knepley   switch (ct) {
34454366bac7SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
34464366bac7SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
34474366bac7SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
34484366bac7SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
34494366bac7SMatthew G. Knepley   case DM_POLYTOPE_HEXAHEDRON:
34504366bac7SMatthew G. Knepley   case DM_POLYTOPE_QUAD_PRISM_TENSOR:
34514366bac7SMatthew G. Knepley     PetscCall(PetscDTGaussTensorQuadrature(dim, 1, quadPointsPerEdge, -1.0, 1.0, q));
34524366bac7SMatthew G. Knepley     PetscCall(PetscDTGaussTensorQuadrature(dim - 1, 1, quadPointsPerEdge, -1.0, 1.0, fq));
34534366bac7SMatthew G. Knepley     break;
34544366bac7SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
34554366bac7SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
3456f2c64c88SMatthew G. Knepley     PetscCall(PetscDTSimplexQuadrature(dim, 2 * qorder, qtype, q));
3457f2c64c88SMatthew G. Knepley     PetscCall(PetscDTSimplexQuadrature(dim - 1, 2 * qorder, qtype, fq));
34584366bac7SMatthew G. Knepley     break;
34594366bac7SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM:
34604366bac7SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
34614366bac7SMatthew G. Knepley     PetscQuadrature q1, q2;
34624366bac7SMatthew G. Knepley 
34634366bac7SMatthew G. Knepley     // TODO: this should be able to use symmetric rules, but doing so causes tests to fail
34644366bac7SMatthew G. Knepley     PetscCall(PetscDTSimplexQuadrature(2, 2 * qorder, PETSCDTSIMPLEXQUAD_CONIC, &q1));
34654366bac7SMatthew G. Knepley     PetscCall(PetscDTGaussTensorQuadrature(1, 1, quadPointsPerEdge, -1.0, 1.0, &q2));
34664366bac7SMatthew G. Knepley     PetscCall(PetscDTTensorQuadratureCreate(q1, q2, q));
34674366bac7SMatthew G. Knepley     PetscCall(PetscQuadratureDestroy(&q2));
34684366bac7SMatthew G. Knepley     *fq = q1;
34694366bac7SMatthew G. Knepley     /* TODO Need separate quadratures for each face */
34704366bac7SMatthew G. Knepley   } break;
34714366bac7SMatthew G. Knepley   default:
34724366bac7SMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No quadrature for celltype %s", DMPolytopeTypes[PetscMin(ct, DM_POLYTOPE_UNKNOWN)]);
347307218a29SMatthew G. Knepley   }
347407218a29SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
347507218a29SMatthew G. Knepley }
3476f2c64c88SMatthew G. Knepley 
3477f2c64c88SMatthew G. Knepley /*@
3478f2c64c88SMatthew G. Knepley   PetscDTCreateDefaultQuadrature - Create default quadrature for a given cell
3479f2c64c88SMatthew G. Knepley 
3480f2c64c88SMatthew G. Knepley   Not collective
3481f2c64c88SMatthew G. Knepley 
3482f2c64c88SMatthew G. Knepley   Input Parameters:
3483f2c64c88SMatthew G. Knepley + ct     - The integration domain
3484f2c64c88SMatthew G. Knepley - qorder - The desired quadrature order
3485f2c64c88SMatthew G. Knepley 
3486f2c64c88SMatthew G. Knepley   Output Parameters:
3487f2c64c88SMatthew G. Knepley + q  - The cell quadrature
3488f2c64c88SMatthew G. Knepley - fq - The face quadrature
3489f2c64c88SMatthew G. Knepley 
3490f2c64c88SMatthew G. Knepley   Level: developer
3491f2c64c88SMatthew G. Knepley 
3492f2c64c88SMatthew G. Knepley .seealso: `PetscDTCreateQuadratureByCell()`, `PetscFECreateDefault()`, `PetscDTGaussTensorQuadrature()`, `PetscDTSimplexQuadrature()`, `PetscDTTensorQuadratureCreate()`
3493f2c64c88SMatthew G. Knepley @*/
PetscDTCreateDefaultQuadrature(DMPolytopeType ct,PetscInt qorder,PetscQuadrature * q,PetscQuadrature * fq)3494f2c64c88SMatthew G. Knepley PetscErrorCode PetscDTCreateDefaultQuadrature(DMPolytopeType ct, PetscInt qorder, PetscQuadrature *q, PetscQuadrature *fq)
3495f2c64c88SMatthew G. Knepley {
3496f2c64c88SMatthew G. Knepley   PetscFunctionBegin;
3497f2c64c88SMatthew G. Knepley   PetscCall(PetscDTCreateQuadratureByCell(ct, qorder, PETSCDTSIMPLEXQUAD_DEFAULT, q, fq));
3498f2c64c88SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
3499f2c64c88SMatthew G. Knepley }
3500