Actual source code: dt.c
1: /* Discretization tools */
3: #include <petscdt.h>
4: #include <petscblaslapack.h>
5: #include <petsc/private/petscimpl.h>
6: #include <petsc/private/dtimpl.h>
7: #include <petsc/private/petscfeimpl.h>
8: #include <petscviewer.h>
9: #include <petscdmplex.h>
10: #include <petscdmshell.h>
12: #if defined(PETSC_HAVE_MPFR)
13: #include <mpfr.h>
14: #endif
16: const char *const PetscDTNodeTypes_shifted[] = {"default", "gaussjacobi", "equispaced", "tanhsinh", "PetscDTNodeType", "PETSCDTNODES_", NULL};
17: const char *const *const PetscDTNodeTypes = PetscDTNodeTypes_shifted + 1;
19: const char *const PetscDTSimplexQuadratureTypes_shifted[] = {"default", "conic", "minsym", "diagsym", "PetscDTSimplexQuadratureType", "PETSCDTSIMPLEXQUAD_", NULL};
20: const char *const *const PetscDTSimplexQuadratureTypes = PetscDTSimplexQuadratureTypes_shifted + 1;
22: static PetscBool GolubWelschCite = PETSC_FALSE;
23: const char GolubWelschCitation[] = "@article{GolubWelsch1969,\n"
24: " author = {Golub and Welsch},\n"
25: " title = {Calculation of Quadrature Rules},\n"
26: " journal = {Math. Comp.},\n"
27: " volume = {23},\n"
28: " number = {106},\n"
29: " pages = {221--230},\n"
30: " year = {1969}\n}\n";
32: /* Numerical tests in src/dm/dt/tests/ex1.c show that when computing the nodes and weights of Gauss-Jacobi
33: quadrature rules:
35: - in double precision, Newton's method and Golub & Welsch both work for moderate degrees (< 100),
36: - in single precision, Newton's method starts producing incorrect roots around n = 15, but
37: the weights from Golub & Welsch become a problem before then: they produces errors
38: in computing the Jacobi-polynomial Gram matrix around n = 6.
40: So we default to Newton's method (required fewer dependencies) */
41: PetscBool PetscDTGaussQuadratureNewton_Internal = PETSC_TRUE;
43: PetscClassId PETSCQUADRATURE_CLASSID = 0;
45: /*@
46: PetscQuadratureCreate - Create a `PetscQuadrature` object
48: Collective
50: Input Parameter:
51: . comm - The communicator for the `PetscQuadrature` object
53: Output Parameter:
54: . q - The `PetscQuadrature` object
56: Level: beginner
58: .seealso: `PetscQuadrature`, `Petscquadraturedestroy()`, `PetscQuadratureGetData()`
59: @*/
60: PetscErrorCode PetscQuadratureCreate(MPI_Comm comm, PetscQuadrature *q)
61: {
62: PetscFunctionBegin;
63: PetscAssertPointer(q, 2);
64: PetscCall(DMInitializePackage());
65: PetscCall(PetscHeaderCreate(*q, PETSCQUADRATURE_CLASSID, "PetscQuadrature", "Quadrature", "DT", comm, PetscQuadratureDestroy, PetscQuadratureView));
66: (*q)->ct = DM_POLYTOPE_UNKNOWN;
67: (*q)->dim = -1;
68: (*q)->Nc = 1;
69: (*q)->order = -1;
70: (*q)->numPoints = 0;
71: (*q)->points = NULL;
72: (*q)->weights = NULL;
73: PetscFunctionReturn(PETSC_SUCCESS);
74: }
76: /*@
77: PetscQuadratureDuplicate - Create a deep copy of the `PetscQuadrature` object
79: Collective
81: Input Parameter:
82: . q - The `PetscQuadrature` object
84: Output Parameter:
85: . r - The new `PetscQuadrature` object
87: Level: beginner
89: .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureDestroy()`, `PetscQuadratureGetData()`
90: @*/
91: PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature q, PetscQuadrature *r)
92: {
93: DMPolytopeType ct;
94: PetscInt order, dim, Nc, Nq;
95: const PetscReal *points, *weights;
96: PetscReal *p, *w;
98: PetscFunctionBegin;
99: PetscAssertPointer(q, 1);
100: PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject)q), r));
101: PetscCall(PetscQuadratureGetCellType(q, &ct));
102: PetscCall(PetscQuadratureSetCellType(*r, ct));
103: PetscCall(PetscQuadratureGetOrder(q, &order));
104: PetscCall(PetscQuadratureSetOrder(*r, order));
105: PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &Nq, &points, &weights));
106: PetscCall(PetscMalloc1(Nq * dim, &p));
107: PetscCall(PetscMalloc1(Nq * Nc, &w));
108: PetscCall(PetscArraycpy(p, points, Nq * dim));
109: PetscCall(PetscArraycpy(w, weights, Nc * Nq));
110: PetscCall(PetscQuadratureSetData(*r, dim, Nc, Nq, p, w));
111: PetscFunctionReturn(PETSC_SUCCESS);
112: }
114: /*@
115: PetscQuadratureDestroy - Destroys a `PetscQuadrature` object
117: Collective
119: Input Parameter:
120: . q - The `PetscQuadrature` object
122: Level: beginner
124: .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureGetData()`
125: @*/
126: PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *q)
127: {
128: PetscFunctionBegin;
129: if (!*q) PetscFunctionReturn(PETSC_SUCCESS);
131: if (--((PetscObject)*q)->refct > 0) {
132: *q = NULL;
133: PetscFunctionReturn(PETSC_SUCCESS);
134: }
135: PetscCall(PetscFree((*q)->points));
136: PetscCall(PetscFree((*q)->weights));
137: PetscCall(PetscHeaderDestroy(q));
138: PetscFunctionReturn(PETSC_SUCCESS);
139: }
141: /*@
142: PetscQuadratureGetCellType - Return the cell type of the integration domain
144: Not Collective
146: Input Parameter:
147: . q - The `PetscQuadrature` object
149: Output Parameter:
150: . ct - The cell type of the integration domain
152: Level: intermediate
154: .seealso: `PetscQuadrature`, `PetscQuadratureSetCellType()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
155: @*/
156: PetscErrorCode PetscQuadratureGetCellType(PetscQuadrature q, DMPolytopeType *ct)
157: {
158: PetscFunctionBegin;
160: PetscAssertPointer(ct, 2);
161: *ct = q->ct;
162: PetscFunctionReturn(PETSC_SUCCESS);
163: }
165: /*@
166: PetscQuadratureSetCellType - Set the cell type of the integration domain
168: Not Collective
170: Input Parameters:
171: + q - The `PetscQuadrature` object
172: - ct - The cell type of the integration domain
174: Level: intermediate
176: .seealso: `PetscQuadrature`, `PetscQuadratureGetCellType()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
177: @*/
178: PetscErrorCode PetscQuadratureSetCellType(PetscQuadrature q, DMPolytopeType ct)
179: {
180: PetscFunctionBegin;
182: q->ct = ct;
183: PetscFunctionReturn(PETSC_SUCCESS);
184: }
186: /*@
187: PetscQuadratureGetOrder - Return the order of the method in the `PetscQuadrature`
189: Not Collective
191: Input Parameter:
192: . q - The `PetscQuadrature` object
194: Output Parameter:
195: . order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated
197: Level: intermediate
199: .seealso: `PetscQuadrature`, `PetscQuadratureSetOrder()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
200: @*/
201: PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature q, PetscInt *order)
202: {
203: PetscFunctionBegin;
205: PetscAssertPointer(order, 2);
206: *order = q->order;
207: PetscFunctionReturn(PETSC_SUCCESS);
208: }
210: /*@
211: PetscQuadratureSetOrder - Set the order of the method in the `PetscQuadrature`
213: Not Collective
215: Input Parameters:
216: + q - The `PetscQuadrature` object
217: - order - The order of the quadrature, i.e. the highest degree polynomial that is exactly integrated
219: Level: intermediate
221: .seealso: `PetscQuadrature`, `PetscQuadratureGetOrder()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
222: @*/
223: PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature q, PetscInt order)
224: {
225: PetscFunctionBegin;
227: q->order = order;
228: PetscFunctionReturn(PETSC_SUCCESS);
229: }
231: /*@
232: PetscQuadratureGetNumComponents - Return the number of components for functions to be integrated
234: Not Collective
236: Input Parameter:
237: . q - The `PetscQuadrature` object
239: Output Parameter:
240: . Nc - The number of components
242: Level: intermediate
244: Note:
245: We are performing an integral $\int f(x) w(x) dx$, where both $f$ and $w$ (the weight) have `Nc` components.
247: .seealso: `PetscQuadrature`, `PetscQuadratureSetNumComponents()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
248: @*/
249: PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature q, PetscInt *Nc)
250: {
251: PetscFunctionBegin;
253: PetscAssertPointer(Nc, 2);
254: *Nc = q->Nc;
255: PetscFunctionReturn(PETSC_SUCCESS);
256: }
258: /*@
259: PetscQuadratureSetNumComponents - Sets the number of components for functions to be integrated
261: Not Collective
263: Input Parameters:
264: + q - The `PetscQuadrature` object
265: - Nc - The number of components
267: Level: intermediate
269: Note:
270: We are performing an integral $\int f(x) w(x) dx$, where both $f$ and $w$ (the weight) have `Nc` components.
272: .seealso: `PetscQuadrature`, `PetscQuadratureGetNumComponents()`, `PetscQuadratureGetData()`, `PetscQuadratureSetData()`
273: @*/
274: PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature q, PetscInt Nc)
275: {
276: PetscFunctionBegin;
278: q->Nc = Nc;
279: PetscFunctionReturn(PETSC_SUCCESS);
280: }
282: /*@C
283: PetscQuadratureGetData - Returns the data defining the `PetscQuadrature`
285: Not Collective
287: Input Parameter:
288: . q - The `PetscQuadrature` object
290: Output Parameters:
291: + dim - The spatial dimension
292: . Nc - The number of components
293: . npoints - The number of quadrature points
294: . points - The coordinates of each quadrature point
295: - weights - The weight of each quadrature point
297: Level: intermediate
299: Note:
300: All output arguments are optional, pass `NULL` for any argument not required
302: Fortran Note:
303: Call `PetscQuadratureRestoreData()` when you are done with the data
305: .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureSetData()`
306: @*/
307: PetscErrorCode PetscQuadratureGetData(PetscQuadrature q, PeOp PetscInt *dim, PeOp PetscInt *Nc, PeOp PetscInt *npoints, PeOp const PetscReal *points[], PeOp const PetscReal *weights[])
308: {
309: PetscFunctionBegin;
311: if (dim) {
312: PetscAssertPointer(dim, 2);
313: *dim = q->dim;
314: }
315: if (Nc) {
316: PetscAssertPointer(Nc, 3);
317: *Nc = q->Nc;
318: }
319: if (npoints) {
320: PetscAssertPointer(npoints, 4);
321: *npoints = q->numPoints;
322: }
323: if (points) {
324: PetscAssertPointer(points, 5);
325: *points = q->points;
326: }
327: if (weights) {
328: PetscAssertPointer(weights, 6);
329: *weights = q->weights;
330: }
331: PetscFunctionReturn(PETSC_SUCCESS);
332: }
334: /*@
335: PetscQuadratureEqual - determine whether two quadratures are equivalent
337: Input Parameters:
338: + A - A `PetscQuadrature` object
339: - B - Another `PetscQuadrature` object
341: Output Parameter:
342: . equal - `PETSC_TRUE` if the quadratures are the same
344: Level: intermediate
346: .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`
347: @*/
348: PetscErrorCode PetscQuadratureEqual(PetscQuadrature A, PetscQuadrature B, PetscBool *equal)
349: {
350: PetscFunctionBegin;
353: PetscAssertPointer(equal, 3);
354: *equal = PETSC_FALSE;
355: if (A->ct != B->ct || A->dim != B->dim || A->Nc != B->Nc || A->order != B->order || A->numPoints != B->numPoints) PetscFunctionReturn(PETSC_SUCCESS);
356: for (PetscInt i = 0; i < A->numPoints * A->dim; i++) {
357: if (PetscAbsReal(A->points[i] - B->points[i]) > PETSC_SMALL) PetscFunctionReturn(PETSC_SUCCESS);
358: }
359: if (!A->weights && !B->weights) {
360: *equal = PETSC_TRUE;
361: PetscFunctionReturn(PETSC_SUCCESS);
362: }
363: if (A->weights && B->weights) {
364: for (PetscInt i = 0; i < A->numPoints; i++) {
365: if (PetscAbsReal(A->weights[i] - B->weights[i]) > PETSC_SMALL) PetscFunctionReturn(PETSC_SUCCESS);
366: }
367: *equal = PETSC_TRUE;
368: }
369: PetscFunctionReturn(PETSC_SUCCESS);
370: }
372: static PetscErrorCode PetscDTJacobianInverse_Internal(PetscInt m, PetscInt n, const PetscReal J[], PetscReal Jinv[])
373: {
374: PetscScalar *Js, *Jinvs;
375: PetscInt i, j, k;
376: PetscBLASInt bm, bn, info;
378: PetscFunctionBegin;
379: if (!m || !n) PetscFunctionReturn(PETSC_SUCCESS);
380: PetscCall(PetscBLASIntCast(m, &bm));
381: PetscCall(PetscBLASIntCast(n, &bn));
382: #if defined(PETSC_USE_COMPLEX)
383: PetscCall(PetscMalloc2(m * n, &Js, m * n, &Jinvs));
384: for (i = 0; i < m * n; i++) Js[i] = J[i];
385: #else
386: Js = (PetscReal *)J;
387: Jinvs = Jinv;
388: #endif
389: if (m == n) {
390: PetscBLASInt *pivots;
391: PetscScalar *W;
393: PetscCall(PetscMalloc2(m, &pivots, m, &W));
395: PetscCall(PetscArraycpy(Jinvs, Js, m * m));
396: PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, Jinvs, &bm, pivots, &info));
397: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetrf %" PetscBLASInt_FMT, info);
398: PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, Jinvs, &bm, pivots, W, &bm, &info));
399: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetri %" PetscBLASInt_FMT, info);
400: PetscCall(PetscFree2(pivots, W));
401: } else if (m < n) {
402: PetscScalar *JJT;
403: PetscBLASInt *pivots;
404: PetscScalar *W;
406: PetscCall(PetscMalloc1(m * m, &JJT));
407: PetscCall(PetscMalloc2(m, &pivots, m, &W));
408: for (i = 0; i < m; i++) {
409: for (j = 0; j < m; j++) {
410: PetscScalar val = 0.;
412: for (k = 0; k < n; k++) val += Js[i * n + k] * Js[j * n + k];
413: JJT[i * m + j] = val;
414: }
415: }
417: PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bm, &bm, JJT, &bm, pivots, &info));
418: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetrf %" PetscBLASInt_FMT, info);
419: PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bm, JJT, &bm, pivots, W, &bm, &info));
420: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetri %" PetscBLASInt_FMT, info);
421: for (i = 0; i < n; i++) {
422: for (j = 0; j < m; j++) {
423: PetscScalar val = 0.;
425: for (k = 0; k < m; k++) val += Js[k * n + i] * JJT[k * m + j];
426: Jinvs[i * m + j] = val;
427: }
428: }
429: PetscCall(PetscFree2(pivots, W));
430: PetscCall(PetscFree(JJT));
431: } else {
432: PetscScalar *JTJ;
433: PetscBLASInt *pivots;
434: PetscScalar *W;
436: PetscCall(PetscMalloc1(n * n, &JTJ));
437: PetscCall(PetscMalloc2(n, &pivots, n, &W));
438: for (i = 0; i < n; i++) {
439: for (j = 0; j < n; j++) {
440: PetscScalar val = 0.;
442: for (k = 0; k < m; k++) val += Js[k * n + i] * Js[k * n + j];
443: JTJ[i * n + j] = val;
444: }
445: }
447: PetscCallBLAS("LAPACKgetrf", LAPACKgetrf_(&bn, &bn, JTJ, &bn, pivots, &info));
448: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetrf %" PetscBLASInt_FMT, info);
449: PetscCallBLAS("LAPACKgetri", LAPACKgetri_(&bn, JTJ, &bn, pivots, W, &bn, &info));
450: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error returned from LAPACKgetri %" PetscBLASInt_FMT, info);
451: for (i = 0; i < n; i++) {
452: for (j = 0; j < m; j++) {
453: PetscScalar val = 0.;
455: for (k = 0; k < n; k++) val += JTJ[i * n + k] * Js[j * n + k];
456: Jinvs[i * m + j] = val;
457: }
458: }
459: PetscCall(PetscFree2(pivots, W));
460: PetscCall(PetscFree(JTJ));
461: }
462: #if defined(PETSC_USE_COMPLEX)
463: for (i = 0; i < m * n; i++) Jinv[i] = PetscRealPart(Jinvs[i]);
464: PetscCall(PetscFree2(Js, Jinvs));
465: #endif
466: PetscFunctionReturn(PETSC_SUCCESS);
467: }
469: /*@
470: PetscQuadraturePushForward - Push forward a quadrature functional under an affine transformation.
472: Collective
474: Input Parameters:
475: + q - the quadrature functional
476: . imageDim - the dimension of the image of the transformation
477: . origin - a point in the original space
478: . originImage - the image of the origin under the transformation
479: . J - the Jacobian of the image: an [imageDim x dim] matrix in row major order
480: - formDegree - transform the quadrature weights as k-forms of this form degree (if the number of components is a multiple of (dim choose `formDegree`),
481: it is assumed that they represent multiple k-forms) [see `PetscDTAltVPullback()` for interpretation of `formDegree`]
483: Output Parameter:
484: . 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
485: been pulled-back by the pseudoinverse of `J` to the k-form weights in the image space.
487: Level: intermediate
489: Note:
490: The new quadrature rule will have a different number of components if spaces have different dimensions.
491: 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.
493: .seealso: `PetscQuadrature`, `PetscDTAltVPullback()`, `PetscDTAltVPullbackMatrix()`
494: @*/
495: PetscErrorCode PetscQuadraturePushForward(PetscQuadrature q, PetscInt imageDim, const PetscReal origin[], const PetscReal originImage[], const PetscReal J[], PetscInt formDegree, PetscQuadrature *Jinvstarq)
496: {
497: PetscInt dim, Nc, imageNc, formSize, Ncopies, imageFormSize, Npoints, pt, i, j, c;
498: const PetscReal *points;
499: const PetscReal *weights;
500: PetscReal *imagePoints, *imageWeights;
501: PetscReal *Jinv;
502: PetscReal *Jinvstar;
504: PetscFunctionBegin;
506: PetscCheck(imageDim >= PetscAbsInt(formDegree), PetscObjectComm((PetscObject)q), PETSC_ERR_ARG_INCOMP, "Cannot represent a %" PetscInt_FMT "-form in %" PetscInt_FMT " dimensions", PetscAbsInt(formDegree), imageDim);
507: PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &Npoints, &points, &weights));
508: PetscCall(PetscDTBinomialInt(dim, PetscAbsInt(formDegree), &formSize));
509: 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);
510: Ncopies = Nc / formSize;
511: PetscCall(PetscDTBinomialInt(imageDim, PetscAbsInt(formDegree), &imageFormSize));
512: imageNc = Ncopies * imageFormSize;
513: PetscCall(PetscMalloc1(Npoints * imageDim, &imagePoints));
514: PetscCall(PetscMalloc1(Npoints * imageNc, &imageWeights));
515: PetscCall(PetscMalloc2(imageDim * dim, &Jinv, formSize * imageFormSize, &Jinvstar));
516: PetscCall(PetscDTJacobianInverse_Internal(imageDim, dim, J, Jinv));
517: PetscCall(PetscDTAltVPullbackMatrix(imageDim, dim, Jinv, formDegree, Jinvstar));
518: for (pt = 0; pt < Npoints; pt++) {
519: const PetscReal *point = PetscSafePointerPlusOffset(points, pt * dim);
520: PetscReal *imagePoint = &imagePoints[pt * imageDim];
522: for (i = 0; i < imageDim; i++) {
523: PetscReal val = originImage[i];
525: for (j = 0; j < dim; j++) val += J[i * dim + j] * (point[j] - origin[j]);
526: imagePoint[i] = val;
527: }
528: for (c = 0; c < Ncopies; c++) {
529: const PetscReal *form = &weights[pt * Nc + c * formSize];
530: PetscReal *imageForm = &imageWeights[pt * imageNc + c * imageFormSize];
532: for (i = 0; i < imageFormSize; i++) {
533: PetscReal val = 0.;
535: for (j = 0; j < formSize; j++) val += Jinvstar[i * formSize + j] * form[j];
536: imageForm[i] = val;
537: }
538: }
539: }
540: PetscCall(PetscQuadratureCreate(PetscObjectComm((PetscObject)q), Jinvstarq));
541: PetscCall(PetscQuadratureSetData(*Jinvstarq, imageDim, imageNc, Npoints, imagePoints, imageWeights));
542: PetscCall(PetscFree2(Jinv, Jinvstar));
543: PetscFunctionReturn(PETSC_SUCCESS);
544: }
546: /*@C
547: PetscQuadratureSetData - Sets the data defining the quadrature
549: Not Collective
551: Input Parameters:
552: + q - The `PetscQuadrature` object
553: . dim - The spatial dimension
554: . Nc - The number of components
555: . npoints - The number of quadrature points
556: . points - The coordinates of each quadrature point
557: - weights - The weight of each quadrature point
559: Level: intermediate
561: Note:
562: `q` owns the references to points and weights, so they must be allocated using `PetscMalloc()` and the user should not free them.
564: .seealso: `PetscQuadrature`, `PetscQuadratureCreate()`, `PetscQuadratureGetData()`
565: @*/
566: PetscErrorCode PetscQuadratureSetData(PetscQuadrature q, PetscInt dim, PetscInt Nc, PetscInt npoints, const PetscReal points[], const PetscReal weights[]) PeNSS
567: {
568: PetscFunctionBegin;
570: if (dim >= 0) q->dim = dim;
571: if (Nc >= 0) q->Nc = Nc;
572: if (npoints >= 0) q->numPoints = npoints;
573: if (points) {
574: PetscAssertPointer(points, 5);
575: q->points = points;
576: }
577: if (weights) {
578: PetscAssertPointer(weights, 6);
579: q->weights = weights;
580: }
581: PetscFunctionReturn(PETSC_SUCCESS);
582: }
584: static PetscErrorCode PetscQuadratureView_Ascii(PetscQuadrature quad, PetscViewer v)
585: {
586: PetscInt q, d, c;
587: PetscViewerFormat format;
589: PetscFunctionBegin;
590: if (quad->Nc > 1)
591: 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));
592: 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));
593: PetscCall(PetscViewerGetFormat(v, &format));
594: if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
595: for (q = 0; q < quad->numPoints; ++q) {
596: PetscCall(PetscViewerASCIIPrintf(v, "p%" PetscInt_FMT " (", q));
597: PetscCall(PetscViewerASCIIUseTabs(v, PETSC_FALSE));
598: for (d = 0; d < quad->dim; ++d) {
599: if (d) PetscCall(PetscViewerASCIIPrintf(v, ", "));
600: PetscCall(PetscViewerASCIIPrintf(v, "%+g", (double)quad->points[q * quad->dim + d]));
601: }
602: PetscCall(PetscViewerASCIIPrintf(v, ") "));
603: if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, "w%" PetscInt_FMT " (", q));
604: for (c = 0; c < quad->Nc; ++c) {
605: if (c) PetscCall(PetscViewerASCIIPrintf(v, ", "));
606: PetscCall(PetscViewerASCIIPrintf(v, "%+g", (double)quad->weights[q * quad->Nc + c]));
607: }
608: if (quad->Nc > 1) PetscCall(PetscViewerASCIIPrintf(v, ")"));
609: PetscCall(PetscViewerASCIIPrintf(v, "\n"));
610: PetscCall(PetscViewerASCIIUseTabs(v, PETSC_TRUE));
611: }
612: PetscFunctionReturn(PETSC_SUCCESS);
613: }
615: /*@
616: PetscQuadratureView - View a `PetscQuadrature` object
618: Collective
620: Input Parameters:
621: + quad - The `PetscQuadrature` object
622: - viewer - The `PetscViewer` object
624: Level: beginner
626: .seealso: `PetscQuadrature`, `PetscViewer`, `PetscQuadratureCreate()`, `PetscQuadratureGetData()`
627: @*/
628: PetscErrorCode PetscQuadratureView(PetscQuadrature quad, PetscViewer viewer)
629: {
630: PetscBool isascii;
632: PetscFunctionBegin;
635: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)quad), &viewer));
636: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
637: PetscCall(PetscViewerASCIIPushTab(viewer));
638: if (isascii) PetscCall(PetscQuadratureView_Ascii(quad, viewer));
639: PetscCall(PetscViewerASCIIPopTab(viewer));
640: PetscFunctionReturn(PETSC_SUCCESS);
641: }
643: /*@C
644: PetscQuadratureExpandComposite - Return a quadrature over the composite element, which has the original quadrature in each subelement
646: Not Collective; No Fortran Support
648: Input Parameters:
649: + q - The original `PetscQuadrature`
650: . numSubelements - The number of subelements the original element is divided into
651: . v0 - An array of the initial points for each subelement
652: - jac - An array of the Jacobian mappings from the reference to each subelement
654: Output Parameter:
655: . qref - The dimension
657: Level: intermediate
659: Note:
660: Together `v0` and `jac` define an affine mapping from the original reference element to each subelement
662: .seealso: `PetscQuadrature`, `PetscFECreate()`, `PetscSpaceGetDimension()`, `PetscDualSpaceGetDimension()`
663: @*/
664: PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature q, PetscInt numSubelements, const PetscReal v0[], const PetscReal jac[], PetscQuadrature *qref)
665: {
666: DMPolytopeType ct;
667: const PetscReal *points, *weights;
668: PetscReal *pointsRef, *weightsRef;
669: PetscInt dim, Nc, order, npoints, npointsRef, c, p, cp, d, e;
671: PetscFunctionBegin;
673: PetscAssertPointer(v0, 3);
674: PetscAssertPointer(jac, 4);
675: PetscAssertPointer(qref, 5);
676: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, qref));
677: PetscCall(PetscQuadratureGetCellType(q, &ct));
678: PetscCall(PetscQuadratureGetOrder(q, &order));
679: PetscCall(PetscQuadratureGetData(q, &dim, &Nc, &npoints, &points, &weights));
680: npointsRef = npoints * numSubelements;
681: PetscCall(PetscMalloc1(npointsRef * dim, &pointsRef));
682: PetscCall(PetscMalloc1(npointsRef * Nc, &weightsRef));
683: for (c = 0; c < numSubelements; ++c) {
684: for (p = 0; p < npoints; ++p) {
685: for (d = 0; d < dim; ++d) {
686: pointsRef[(c * npoints + p) * dim + d] = v0[c * dim + d];
687: for (e = 0; e < dim; ++e) pointsRef[(c * npoints + p) * dim + d] += jac[(c * dim + d) * dim + e] * (points[p * dim + e] + 1.0);
688: }
689: /* Could also use detJ here */
690: for (cp = 0; cp < Nc; ++cp) weightsRef[(c * npoints + p) * Nc + cp] = weights[p * Nc + cp] / numSubelements;
691: }
692: }
693: PetscCall(PetscQuadratureSetCellType(*qref, ct));
694: PetscCall(PetscQuadratureSetOrder(*qref, order));
695: PetscCall(PetscQuadratureSetData(*qref, dim, Nc, npointsRef, pointsRef, weightsRef));
696: PetscFunctionReturn(PETSC_SUCCESS);
697: }
699: /* Compute the coefficients for the Jacobi polynomial recurrence,
701: J^{a,b}_n(x) = (cnm1 + cnm1x * x) * J^{a,b}_{n-1}(x) - cnm2 * J^{a,b}_{n-2}(x).
702: */
703: #define PetscDTJacobiRecurrence_Internal(n, a, b, cnm1, cnm1x, cnm2) \
704: do { \
705: PetscReal _a = (a); \
706: PetscReal _b = (b); \
707: PetscReal _n = (n); \
708: if (n == 1) { \
709: (cnm1) = (_a - _b) * 0.5; \
710: (cnm1x) = (_a + _b + 2.) * 0.5; \
711: (cnm2) = 0.; \
712: } else { \
713: PetscReal _2n = _n + _n; \
714: PetscReal _d = (_2n * (_n + _a + _b) * (_2n + _a + _b - 2)); \
715: PetscReal _n1 = (_2n + _a + _b - 1.) * (_a * _a - _b * _b); \
716: PetscReal _n1x = (_2n + _a + _b - 1.) * (_2n + _a + _b) * (_2n + _a + _b - 2); \
717: PetscReal _n2 = 2. * ((_n + _a - 1.) * (_n + _b - 1.) * (_2n + _a + _b)); \
718: (cnm1) = _n1 / _d; \
719: (cnm1x) = _n1x / _d; \
720: (cnm2) = _n2 / _d; \
721: } \
722: } while (0)
724: /*@
725: PetscDTJacobiNorm - Compute the weighted L2 norm of a Jacobi polynomial.
727: $\| P^{\alpha,\beta}_n \|_{\alpha,\beta}^2 = \int_{-1}^1 (1 + x)^{\alpha} (1 - x)^{\beta} P^{\alpha,\beta}_n (x)^2 dx.$
729: Input Parameters:
730: + alpha - the left exponent > -1
731: . beta - the right exponent > -1
732: - n - the polynomial degree
734: Output Parameter:
735: . norm - the weighted L2 norm
737: Level: beginner
739: .seealso: `PetscQuadrature`, `PetscDTJacobiEval()`
740: @*/
741: PetscErrorCode PetscDTJacobiNorm(PetscReal alpha, PetscReal beta, PetscInt n, PetscReal *norm)
742: {
743: PetscReal twoab1;
744: PetscReal gr;
746: PetscFunctionBegin;
747: PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent alpha %g <= -1. invalid", (double)alpha);
748: PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Exponent beta %g <= -1. invalid", (double)beta);
749: PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "n %" PetscInt_FMT " < 0 invalid", n);
750: twoab1 = PetscPowReal(2., alpha + beta + 1.);
751: #if defined(PETSC_HAVE_LGAMMA)
752: if (!n) {
753: gr = PetscExpReal(PetscLGamma(alpha + 1.) + PetscLGamma(beta + 1.) - PetscLGamma(alpha + beta + 2.));
754: } else {
755: gr = PetscExpReal(PetscLGamma(n + alpha + 1.) + PetscLGamma(n + beta + 1.) - (PetscLGamma(n + 1.) + PetscLGamma(n + alpha + beta + 1.))) / (n + n + alpha + beta + 1.);
756: }
757: #else
758: {
759: PetscInt alphai = (PetscInt)alpha;
760: PetscInt betai = (PetscInt)beta;
761: PetscInt i;
763: gr = n ? (1. / (n + n + alpha + beta + 1.)) : 1.;
764: if ((PetscReal)alphai == alpha) {
765: if (!n) {
766: for (i = 0; i < alphai; i++) gr *= (i + 1.) / (beta + i + 1.);
767: gr /= (alpha + beta + 1.);
768: } else {
769: for (i = 0; i < alphai; i++) gr *= (n + i + 1.) / (n + beta + i + 1.);
770: }
771: } else if ((PetscReal)betai == beta) {
772: if (!n) {
773: for (i = 0; i < betai; i++) gr *= (i + 1.) / (alpha + i + 2.);
774: gr /= (alpha + beta + 1.);
775: } else {
776: for (i = 0; i < betai; i++) gr *= (n + i + 1.) / (n + alpha + i + 1.);
777: }
778: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "lgamma() - math routine is unavailable.");
779: }
780: #endif
781: *norm = PetscSqrtReal(twoab1 * gr);
782: PetscFunctionReturn(PETSC_SUCCESS);
783: }
785: static PetscErrorCode PetscDTJacobiEval_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscInt k, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PetscReal *p)
786: {
787: PetscReal ak, bk;
788: PetscReal abk1;
789: PetscInt i, l, maxdegree;
791: PetscFunctionBegin;
792: maxdegree = degrees[ndegree - 1] - k;
793: ak = a + k;
794: bk = b + k;
795: abk1 = a + b + k + 1.;
796: if (maxdegree < 0) {
797: for (i = 0; i < npoints; i++)
798: for (l = 0; l < ndegree; l++) p[i * ndegree + l] = 0.;
799: PetscFunctionReturn(PETSC_SUCCESS);
800: }
801: for (i = 0; i < npoints; i++) {
802: PetscReal pm1, pm2, x;
803: PetscReal cnm1, cnm1x, cnm2;
804: PetscInt j;
806: x = points[i];
807: pm2 = 1.;
808: PetscDTJacobiRecurrence_Internal(1, ak, bk, cnm1, cnm1x, cnm2);
809: pm1 = (cnm1 + cnm1x * x);
810: l = 0;
811: while (l < ndegree && degrees[l] - k < 0) p[l++] = 0.;
812: while (l < ndegree && degrees[l] - k == 0) {
813: p[l] = pm2;
814: for (PetscInt m = 0; m < k; m++) p[l] *= (abk1 + m) * 0.5;
815: l++;
816: }
817: while (l < ndegree && degrees[l] - k == 1) {
818: p[l] = pm1;
819: for (PetscInt m = 0; m < k; m++) p[l] *= (abk1 + 1 + m) * 0.5;
820: l++;
821: }
822: for (j = 2; j <= maxdegree; j++) {
823: PetscReal pp;
825: PetscDTJacobiRecurrence_Internal(j, ak, bk, cnm1, cnm1x, cnm2);
826: pp = (cnm1 + cnm1x * x) * pm1 - cnm2 * pm2;
827: pm2 = pm1;
828: pm1 = pp;
829: while (l < ndegree && degrees[l] - k == j) {
830: p[l] = pp;
831: for (PetscInt m = 0; m < k; m++) p[l] *= (abk1 + j + m) * 0.5;
832: l++;
833: }
834: }
835: p += ndegree;
836: }
837: PetscFunctionReturn(PETSC_SUCCESS);
838: }
840: /*@
841: PetscDTJacobiEvalJet - Evaluate the jet (function and derivatives) of the Jacobi polynomials basis up to a given degree.
843: Input Parameters:
844: + alpha - the left exponent of the weight
845: . beta - the right exponetn of the weight
846: . npoints - the number of points to evaluate the polynomials at
847: . points - [npoints] array of point coordinates
848: . degree - the maximm degree polynomial space to evaluate, (degree + 1) will be evaluated total.
849: - k - the maximum derivative to evaluate in the jet, (k + 1) will be evaluated total.
851: Output Parameter:
852: . p - an array containing the evaluations of the Jacobi polynomials's jets on the points. the size is (degree + 1) x
853: (k + 1) x npoints, which also describes the order of the dimensions of this three-dimensional array: the first
854: (slowest varying) dimension is polynomial degree; the second dimension is derivative order; the third (fastest
855: varying) dimension is the index of the evaluation point.
857: Level: advanced
859: Notes:
860: The Jacobi polynomials with indices $\alpha$ and $\beta$ are orthogonal with respect to the
861: weighted inner product $\langle f, g \rangle = \int_{-1}^1 (1+x)^{\alpha} (1-x)^{\beta} f(x)
862: g(x) dx$.
864: .seealso: `PetscDTJacobiEval()`, `PetscDTPKDEvalJet()`
865: @*/
866: PetscErrorCode PetscDTJacobiEvalJet(PetscReal alpha, PetscReal beta, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[])
867: {
868: PetscInt i, j, l;
869: PetscInt *degrees;
870: PetscReal *psingle;
872: PetscFunctionBegin;
873: if (degree == 0) {
874: PetscInt zero = 0;
876: for (i = 0; i <= k; i++) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, 1, &zero, &p[i * npoints]));
877: PetscFunctionReturn(PETSC_SUCCESS);
878: }
879: PetscCall(PetscMalloc1(degree + 1, °rees));
880: PetscCall(PetscMalloc1((degree + 1) * npoints, &psingle));
881: for (i = 0; i <= degree; i++) degrees[i] = i;
882: for (i = 0; i <= k; i++) {
883: PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, i, points, degree + 1, degrees, psingle));
884: for (j = 0; j <= degree; j++) {
885: for (l = 0; l < npoints; l++) p[(j * (k + 1) + i) * npoints + l] = psingle[l * (degree + 1) + j];
886: }
887: }
888: PetscCall(PetscFree(psingle));
889: PetscCall(PetscFree(degrees));
890: PetscFunctionReturn(PETSC_SUCCESS);
891: }
893: /*@
894: PetscDTJacobiEval - evaluate Jacobi polynomials for the weight function $(1.+x)^{\alpha} (1.-x)^{\beta}$ at a set of points
895: at points
897: Not Collective
899: Input Parameters:
900: + npoints - number of spatial points to evaluate at
901: . alpha - the left exponent > -1
902: . beta - the right exponent > -1
903: . points - array of locations to evaluate at
904: . ndegree - number of basis degrees to evaluate
905: - degrees - sorted array of degrees to evaluate
907: Output Parameters:
908: + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension npoints*ndegrees, allocated by caller) (or `NULL`)
909: . D - row-oriented derivative evaluation matrix (or `NULL`)
910: - D2 - row-oriented second derivative evaluation matrix (or `NULL`)
912: Level: intermediate
914: .seealso: `PetscDTGaussQuadrature()`, `PetscDTLegendreEval()`
915: @*/
916: 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[])
917: {
918: PetscFunctionBegin;
919: PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "alpha must be > -1.");
920: PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "beta must be > -1.");
921: if (!npoints || !ndegree) PetscFunctionReturn(PETSC_SUCCESS);
922: if (B) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 0, points, ndegree, degrees, B));
923: if (D) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 1, points, ndegree, degrees, D));
924: if (D2) PetscCall(PetscDTJacobiEval_Internal(npoints, alpha, beta, 2, points, ndegree, degrees, D2));
925: PetscFunctionReturn(PETSC_SUCCESS);
926: }
928: /*@
929: PetscDTLegendreEval - evaluate Legendre polynomials at points
931: Not Collective
933: Input Parameters:
934: + npoints - number of spatial points to evaluate at
935: . points - array of locations to evaluate at
936: . ndegree - number of basis degrees to evaluate
937: - degrees - sorted array of degrees to evaluate
939: Output Parameters:
940: + B - row-oriented basis evaluation matrix B[point*ndegree + degree] (dimension `npoints`*`ndegrees`, allocated by caller) (or `NULL`)
941: . D - row-oriented derivative evaluation matrix (or `NULL`)
942: - D2 - row-oriented second derivative evaluation matrix (or `NULL`)
944: Level: intermediate
946: .seealso: `PetscDTGaussQuadrature()`
947: @*/
948: PetscErrorCode PetscDTLegendreEval(PetscInt npoints, const PetscReal *points, PetscInt ndegree, const PetscInt *degrees, PeOp PetscReal B[], PeOp PetscReal D[], PeOp PetscReal D2[])
949: {
950: PetscFunctionBegin;
951: PetscCall(PetscDTJacobiEval(npoints, 0., 0., points, ndegree, degrees, B, D, D2));
952: PetscFunctionReturn(PETSC_SUCCESS);
953: }
955: /*@
956: 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,
957: then the index of x is smaller than the index of y)
959: Input Parameters:
960: + len - the desired length of the degree tuple
961: - index - the index to convert: should be >= 0
963: Output Parameter:
964: . degtup - filled with a tuple of degrees
966: Level: beginner
968: Note:
969: For two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples
970: acts as a tiebreaker. For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the
971: last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1).
973: .seealso: `PetscDTGradedOrderToIndex()`
974: @*/
975: PetscErrorCode PetscDTIndexToGradedOrder(PetscInt len, PetscInt index, PetscInt degtup[])
976: {
977: PetscInt i, total;
978: PetscInt sum;
980: PetscFunctionBeginHot;
981: PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
982: PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative");
983: total = 1;
984: sum = 0;
985: while (index >= total) {
986: index -= total;
987: total = (total * (len + sum)) / (sum + 1);
988: sum++;
989: }
990: for (i = 0; i < len; i++) {
991: PetscInt c;
993: degtup[i] = sum;
994: for (c = 0, total = 1; c < sum; c++) {
995: /* going into the loop, total is the number of way to have a tuple of sum exactly c with length len - 1 - i */
996: if (index < total) break;
997: index -= total;
998: total = (total * (len - 1 - i + c)) / (c + 1);
999: degtup[i]--;
1000: }
1001: sum -= degtup[i];
1002: }
1003: PetscFunctionReturn(PETSC_SUCCESS);
1004: }
1006: /*@
1007: PetscDTGradedOrderToIndex - convert a tuple into an index in a graded order, the inverse of `PetscDTIndexToGradedOrder()`.
1009: Input Parameters:
1010: + len - the length of the degree tuple
1011: - degtup - tuple with this length
1013: Output Parameter:
1014: . index - index in graded order: >= 0
1016: Level: beginner
1018: Note:
1019: For two tuples x and y with the same degree sum, partial degree sums over the final elements of the tuples
1020: acts as a tiebreaker. For example, (2, 1, 1) and (1, 2, 1) have the same degree sum, but the degree sum over the
1021: last two elements is smaller for the former, so (2, 1, 1) < (1, 2, 1).
1023: .seealso: `PetscDTIndexToGradedOrder()`
1024: @*/
1025: PetscErrorCode PetscDTGradedOrderToIndex(PetscInt len, const PetscInt degtup[], PetscInt *index)
1026: {
1027: PetscInt i, idx, sum, total;
1029: PetscFunctionBeginHot;
1030: PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
1031: for (i = 0, sum = 0; i < len; i++) sum += degtup[i];
1032: idx = 0;
1033: total = 1;
1034: for (i = 0; i < sum; i++) {
1035: idx += total;
1036: total = (total * (len + i)) / (i + 1);
1037: }
1038: for (i = 0; i < len - 1; i++) {
1039: total = 1;
1040: sum -= degtup[i];
1041: for (PetscInt c = 0; c < sum; c++) {
1042: idx += total;
1043: total = (total * (len - 1 - i + c)) / (c + 1);
1044: }
1045: }
1046: *index = idx;
1047: PetscFunctionReturn(PETSC_SUCCESS);
1048: }
1050: static PetscBool PKDCite = PETSC_FALSE;
1051: const char PKDCitation[] = "@article{Kirby2010,\n"
1052: " title={Singularity-free evaluation of collapsed-coordinate orthogonal polynomials},\n"
1053: " author={Kirby, Robert C},\n"
1054: " journal={ACM Transactions on Mathematical Software (TOMS)},\n"
1055: " volume={37},\n"
1056: " number={1},\n"
1057: " pages={1--16},\n"
1058: " year={2010},\n"
1059: " publisher={ACM New York, NY, USA}\n}\n";
1061: /*@
1062: PetscDTPKDEvalJet - Evaluate the jet (function and derivatives) of the Proriol-Koornwinder-Dubiner (PKD) basis for
1063: the space of polynomials up to a given degree.
1065: Input Parameters:
1066: + dim - the number of variables in the multivariate polynomials
1067: . npoints - the number of points to evaluate the polynomials at
1068: . points - [npoints x dim] array of point coordinates
1069: . 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.
1070: - k - the maximum order partial derivative to evaluate in the jet. There are (dim + k choose dim) partial derivatives
1071: in the jet. Choosing k = 0 means to evaluate just the function and no derivatives
1073: Output Parameter:
1074: . p - an array containing the evaluations of the PKD polynomials' jets on the points. The size is ((dim + degree)
1075: choose dim) x ((dim + k) choose dim) x npoints, which also describes the order of the dimensions of this
1076: three-dimensional array: the first (slowest varying) dimension is basis function index; the second dimension is jet
1077: index; the third (fastest varying) dimension is the index of the evaluation point.
1079: Level: advanced
1081: Notes:
1082: The PKD basis is L2-orthonormal on the biunit simplex (which is used as the reference element
1083: for finite elements in PETSc), which makes it a stable basis to use for evaluating
1084: polynomials in that domain.
1086: The ordering of the basis functions, and the ordering of the derivatives in the jet, both follow the graded
1087: ordering of `PetscDTIndexToGradedOrder()` and `PetscDTGradedOrderToIndex()`. For example, in 3D, the polynomial with
1088: 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);
1089: 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).
1091: The implementation uses Kirby's singularity-free evaluation algorithm, <https://doi.org/10.1145/1644001.1644006>.
1093: .seealso: `PetscDTGradedOrderToIndex()`, `PetscDTIndexToGradedOrder()`, `PetscDTJacobiEvalJet()`
1094: @*/
1095: PetscErrorCode PetscDTPKDEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt k, PetscReal p[])
1096: {
1097: PetscInt degidx, kidx, d, pt;
1098: PetscInt Nk, Ndeg;
1099: PetscInt *ktup, *degtup;
1100: PetscReal *scales, initscale, scaleexp;
1102: PetscFunctionBegin;
1103: PetscCall(PetscCitationsRegister(PKDCitation, &PKDCite));
1104: PetscCall(PetscDTBinomialInt(dim + k, k, &Nk));
1105: PetscCall(PetscDTBinomialInt(degree + dim, degree, &Ndeg));
1106: PetscCall(PetscMalloc2(dim, °tup, dim, &ktup));
1107: PetscCall(PetscMalloc1(Ndeg, &scales));
1108: initscale = 1.;
1109: if (dim > 1) {
1110: PetscCall(PetscDTBinomial(dim, 2, &scaleexp));
1111: initscale = PetscPowReal(2., scaleexp * 0.5);
1112: }
1113: for (degidx = 0; degidx < Ndeg; degidx++) {
1114: PetscInt e;
1115: PetscInt m1idx = -1, m2idx = -1;
1116: PetscInt n;
1117: PetscInt degsum;
1118: PetscReal alpha;
1119: PetscReal cnm1, cnm1x, cnm2;
1120: PetscReal norm;
1122: PetscCall(PetscDTIndexToGradedOrder(dim, degidx, degtup));
1123: for (d = dim - 1; d >= 0; d--)
1124: if (degtup[d]) break;
1125: if (d < 0) { /* constant is 1 everywhere, all derivatives are zero */
1126: scales[degidx] = initscale;
1127: for (e = 0; e < dim; e++) {
1128: PetscCall(PetscDTJacobiNorm(e, 0., 0, &norm));
1129: scales[degidx] /= norm;
1130: }
1131: for (PetscInt i = 0; i < npoints; i++) p[degidx * Nk * npoints + i] = 1.;
1132: for (PetscInt i = 0; i < (Nk - 1) * npoints; i++) p[(degidx * Nk + 1) * npoints + i] = 0.;
1133: continue;
1134: }
1135: n = degtup[d];
1136: degtup[d]--;
1137: PetscCall(PetscDTGradedOrderToIndex(dim, degtup, &m1idx));
1138: if (degtup[d] > 0) {
1139: degtup[d]--;
1140: PetscCall(PetscDTGradedOrderToIndex(dim, degtup, &m2idx));
1141: degtup[d]++;
1142: }
1143: degtup[d]++;
1144: for (e = 0, degsum = 0; e < d; e++) degsum += degtup[e];
1145: alpha = 2 * degsum + d;
1146: PetscDTJacobiRecurrence_Internal(n, alpha, 0., cnm1, cnm1x, cnm2);
1148: scales[degidx] = initscale;
1149: for (e = 0, degsum = 0; e < dim; e++) {
1150: PetscReal ealpha;
1151: PetscReal enorm;
1153: ealpha = 2 * degsum + e;
1154: for (PetscInt f = 0; f < degsum; f++) scales[degidx] *= 2.;
1155: PetscCall(PetscDTJacobiNorm(ealpha, 0., degtup[e], &enorm));
1156: scales[degidx] /= enorm;
1157: degsum += degtup[e];
1158: }
1160: for (pt = 0; pt < npoints; pt++) {
1161: /* compute the multipliers */
1162: PetscReal thetanm1, thetanm1x, thetanm2;
1164: thetanm1x = dim - (d + 1) + 2. * points[pt * dim + d];
1165: for (e = d + 1; e < dim; e++) thetanm1x += points[pt * dim + e];
1166: thetanm1x *= 0.5;
1167: thetanm1 = (2. - (dim - (d + 1)));
1168: for (e = d + 1; e < dim; e++) thetanm1 -= points[pt * dim + e];
1169: thetanm1 *= 0.5;
1170: thetanm2 = thetanm1 * thetanm1;
1172: for (kidx = 0; kidx < Nk; kidx++) {
1173: PetscCall(PetscDTIndexToGradedOrder(dim, kidx, ktup));
1174: /* first sum in the same derivative terms */
1175: p[(degidx * Nk + kidx) * npoints + pt] = (cnm1 * thetanm1 + cnm1x * thetanm1x) * p[(m1idx * Nk + kidx) * npoints + pt];
1176: if (m2idx >= 0) p[(degidx * Nk + kidx) * npoints + pt] -= cnm2 * thetanm2 * p[(m2idx * Nk + kidx) * npoints + pt];
1178: for (PetscInt f = d; f < dim; f++) {
1179: PetscInt km1idx, mplty = ktup[f];
1181: if (!mplty) continue;
1182: ktup[f]--;
1183: PetscCall(PetscDTGradedOrderToIndex(dim, ktup, &km1idx));
1185: /* the derivative of cnm1x * thetanm1x wrt x variable f is 0.5 * cnm1x if f > d otherwise it is cnm1x */
1186: /* the derivative of cnm1 * thetanm1 wrt x variable f is 0 if f == d, otherwise it is -0.5 * cnm1 */
1187: /* the derivative of -cnm2 * thetanm2 wrt x variable f is 0 if f == d, otherwise it is cnm2 * thetanm1 */
1188: if (f > d) {
1189: p[(degidx * Nk + kidx) * npoints + pt] += mplty * 0.5 * (cnm1x - cnm1) * p[(m1idx * Nk + km1idx) * npoints + pt];
1190: if (m2idx >= 0) {
1191: p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm2 * thetanm1 * p[(m2idx * Nk + km1idx) * npoints + pt];
1192: /* second derivatives of -cnm2 * thetanm2 wrt x variable f,f2 is like - 0.5 * cnm2 */
1193: for (PetscInt f2 = f; f2 < dim; f2++) {
1194: PetscInt km2idx, mplty2 = ktup[f2];
1195: PetscInt factor;
1197: if (!mplty2) continue;
1198: ktup[f2]--;
1199: PetscCall(PetscDTGradedOrderToIndex(dim, ktup, &km2idx));
1201: factor = mplty * mplty2;
1202: if (f == f2) factor /= 2;
1203: p[(degidx * Nk + kidx) * npoints + pt] -= 0.5 * factor * cnm2 * p[(m2idx * Nk + km2idx) * npoints + pt];
1204: ktup[f2]++;
1205: }
1206: }
1207: } else {
1208: p[(degidx * Nk + kidx) * npoints + pt] += mplty * cnm1x * p[(m1idx * Nk + km1idx) * npoints + pt];
1209: }
1210: ktup[f]++;
1211: }
1212: }
1213: }
1214: }
1215: for (degidx = 0; degidx < Ndeg; degidx++) {
1216: PetscReal scale = scales[degidx];
1218: for (PetscInt i = 0; i < Nk * npoints; i++) p[degidx * Nk * npoints + i] *= scale;
1219: }
1220: PetscCall(PetscFree(scales));
1221: PetscCall(PetscFree2(degtup, ktup));
1222: PetscFunctionReturn(PETSC_SUCCESS);
1223: }
1225: /*@
1226: PetscDTPTrimmedSize - The size of the trimmed polynomial space of k-forms with a given degree and form degree,
1227: which can be evaluated in `PetscDTPTrimmedEvalJet()`.
1229: Input Parameters:
1230: + dim - the number of variables in the multivariate polynomials
1231: . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space.
1232: - formDegree - the degree of the form
1234: Output Parameter:
1235: . size - The number ((`dim` + `degree`) choose (`dim` + `formDegree`)) x ((`degree` + `formDegree` - 1) choose (`formDegree`))
1237: Level: advanced
1239: .seealso: `PetscDTPTrimmedEvalJet()`
1240: @*/
1241: PetscErrorCode PetscDTPTrimmedSize(PetscInt dim, PetscInt degree, PetscInt formDegree, PetscInt *size)
1242: {
1243: PetscInt Nrk, Nbpt; // number of trimmed polynomials
1245: PetscFunctionBegin;
1246: formDegree = PetscAbsInt(formDegree);
1247: PetscCall(PetscDTBinomialInt(degree + dim, degree + formDegree, &Nbpt));
1248: PetscCall(PetscDTBinomialInt(degree + formDegree - 1, formDegree, &Nrk));
1249: Nbpt *= Nrk;
1250: *size = Nbpt;
1251: PetscFunctionReturn(PETSC_SUCCESS);
1252: }
1254: /* there was a reference implementation based on section 4.4 of Arnold, Falk & Winther (acta numerica, 2006), but it
1255: * was inferior to this implementation */
1256: static PetscErrorCode PetscDTPTrimmedEvalJet_Internal(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[])
1257: {
1258: PetscInt formDegreeOrig = formDegree;
1259: PetscBool formNegative = (formDegreeOrig < 0) ? PETSC_TRUE : PETSC_FALSE;
1261: PetscFunctionBegin;
1262: formDegree = PetscAbsInt(formDegreeOrig);
1263: if (formDegree == 0) {
1264: PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree, jetDegree, p));
1265: PetscFunctionReturn(PETSC_SUCCESS);
1266: }
1267: if (formDegree == dim) {
1268: PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p));
1269: PetscFunctionReturn(PETSC_SUCCESS);
1270: }
1271: PetscInt Nbpt;
1272: PetscCall(PetscDTPTrimmedSize(dim, degree, formDegree, &Nbpt));
1273: PetscInt Nf;
1274: PetscCall(PetscDTBinomialInt(dim, formDegree, &Nf));
1275: PetscInt Nk;
1276: PetscCall(PetscDTBinomialInt(dim + jetDegree, dim, &Nk));
1277: PetscCall(PetscArrayzero(p, Nbpt * Nf * Nk * npoints));
1279: PetscInt Nbpm1; // number of scalar polynomials up to degree - 1;
1280: PetscCall(PetscDTBinomialInt(dim + degree - 1, dim, &Nbpm1));
1281: PetscReal *p_scalar;
1282: PetscCall(PetscMalloc1(Nbpm1 * Nk * npoints, &p_scalar));
1283: PetscCall(PetscDTPKDEvalJet(dim, npoints, points, degree - 1, jetDegree, p_scalar));
1284: PetscInt total = 0;
1285: // First add the full polynomials up to degree - 1 into the basis: take the scalar
1286: // and copy one for each form component
1287: for (PetscInt i = 0; i < Nbpm1; i++) {
1288: const PetscReal *src = &p_scalar[i * Nk * npoints];
1289: for (PetscInt f = 0; f < Nf; f++) {
1290: PetscReal *dest = &p[(total++ * Nf + f) * Nk * npoints];
1291: PetscCall(PetscArraycpy(dest, src, Nk * npoints));
1292: }
1293: }
1294: PetscInt *form_atoms;
1295: PetscCall(PetscMalloc1(formDegree + 1, &form_atoms));
1296: // construct the interior product pattern
1297: PetscInt (*pattern)[3];
1298: PetscInt Nf1; // number of formDegree + 1 forms
1299: PetscCall(PetscDTBinomialInt(dim, formDegree + 1, &Nf1));
1300: PetscInt nnz = Nf1 * (formDegree + 1);
1301: PetscCall(PetscMalloc1(Nf1 * (formDegree + 1), &pattern));
1302: PetscCall(PetscDTAltVInteriorPattern(dim, formDegree + 1, pattern));
1303: PetscReal centroid = (1. - dim) / (dim + 1.);
1304: PetscInt *deriv;
1305: PetscCall(PetscMalloc1(dim, &deriv));
1306: for (PetscInt d = dim; d >= formDegree + 1; d--) {
1307: PetscInt Nfd1; // number of formDegree + 1 forms in dimension d that include dx_0
1308: // (equal to the number of formDegree forms in dimension d-1)
1309: PetscCall(PetscDTBinomialInt(d - 1, formDegree, &Nfd1));
1310: // The number of homogeneous (degree-1) scalar polynomials in d variables
1311: PetscInt Nh;
1312: PetscCall(PetscDTBinomialInt(d - 1 + degree - 1, d - 1, &Nh));
1313: const PetscReal *h_scalar = &p_scalar[(Nbpm1 - Nh) * Nk * npoints];
1314: for (PetscInt b = 0; b < Nh; b++) {
1315: const PetscReal *h_s = &h_scalar[b * Nk * npoints];
1316: for (PetscInt f = 0; f < Nfd1; f++) {
1317: // construct all formDegree+1 forms that start with dx_(dim - d) /\ ...
1318: form_atoms[0] = dim - d;
1319: PetscCall(PetscDTEnumSubset(d - 1, formDegree, f, &form_atoms[1]));
1320: for (PetscInt i = 0; i < formDegree; i++) form_atoms[1 + i] += form_atoms[0] + 1;
1321: PetscInt f_ind; // index of the resulting form
1322: PetscCall(PetscDTSubsetIndex(dim, formDegree + 1, form_atoms, &f_ind));
1323: PetscReal *p_f = &p[total++ * Nf * Nk * npoints];
1324: for (PetscInt nz = 0; nz < nnz; nz++) {
1325: PetscInt i = pattern[nz][0]; // formDegree component
1326: PetscInt j = pattern[nz][1]; // (formDegree + 1) component
1327: PetscInt v = pattern[nz][2]; // coordinate component
1328: PetscReal scale = v < 0 ? -1. : 1.;
1330: i = formNegative ? (Nf - 1 - i) : i;
1331: scale = (formNegative && (i & 1)) ? -scale : scale;
1332: v = v < 0 ? -(v + 1) : v;
1333: if (j != f_ind) continue;
1334: PetscReal *p_i = &p_f[i * Nk * npoints];
1335: for (PetscInt jet = 0; jet < Nk; jet++) {
1336: const PetscReal *h_jet = &h_s[jet * npoints];
1337: PetscReal *p_jet = &p_i[jet * npoints];
1339: for (PetscInt pt = 0; pt < npoints; pt++) p_jet[pt] += scale * h_jet[pt] * (points[pt * dim + v] - centroid);
1340: PetscCall(PetscDTIndexToGradedOrder(dim, jet, deriv));
1341: deriv[v]++;
1342: PetscReal mult = deriv[v];
1343: PetscInt l;
1344: PetscCall(PetscDTGradedOrderToIndex(dim, deriv, &l));
1345: if (l >= Nk) continue;
1346: p_jet = &p_i[l * npoints];
1347: for (PetscInt pt = 0; pt < npoints; pt++) p_jet[pt] += scale * mult * h_jet[pt];
1348: deriv[v]--;
1349: }
1350: }
1351: }
1352: }
1353: }
1354: PetscCheck(total == Nbpt, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrectly counted P trimmed polynomials");
1355: PetscCall(PetscFree(deriv));
1356: PetscCall(PetscFree(pattern));
1357: PetscCall(PetscFree(form_atoms));
1358: PetscCall(PetscFree(p_scalar));
1359: PetscFunctionReturn(PETSC_SUCCESS);
1360: }
1362: /*@
1363: PetscDTPTrimmedEvalJet - Evaluate the jet (function and derivatives) of a basis of the trimmed polynomial k-forms up to
1364: a given degree.
1366: Input Parameters:
1367: + dim - the number of variables in the multivariate polynomials
1368: . npoints - the number of points to evaluate the polynomials at
1369: . points - [npoints x dim] array of point coordinates
1370: . degree - the degree (sum of degrees on the variables in a monomial) of the trimmed polynomial space to evaluate.
1371: There are ((dim + degree) choose (dim + formDegree)) x ((degree + formDegree - 1) choose (formDegree)) polynomials in this space.
1372: (You can use `PetscDTPTrimmedSize()` to compute this size.)
1373: . formDegree - the degree of the form
1374: - jetDegree - the maximum order partial derivative to evaluate in the jet. There are ((dim + jetDegree) choose dim) partial derivatives
1375: in the jet. Choosing jetDegree = 0 means to evaluate just the function and no derivatives
1377: Output Parameter:
1378: . p - an array containing the evaluations of the PKD polynomials' jets on the points.
1380: Level: advanced
1382: Notes:
1383: The size of `p` is `PetscDTPTrimmedSize()` x ((dim + formDegree) choose dim) x ((dim + k)
1384: choose dim) x npoints,which also describes the order of the dimensions of this
1385: four-dimensional array\:
1387: the first (slowest varying) dimension is basis function index;
1388: the second dimension is component of the form;
1389: the third dimension is jet index;
1390: the fourth (fastest varying) dimension is the index of the evaluation point.
1392: The ordering of the basis functions is not graded, so the basis functions are not nested by degree like `PetscDTPKDEvalJet()`.
1393: The basis functions are not an L2-orthonormal basis on any particular domain.
1395: The implementation is based on the description of the trimmed polynomials up to degree r as
1396: the direct sum of polynomials up to degree (r-1) and the Koszul differential applied to
1397: homogeneous polynomials of degree (r-1).
1399: .seealso: `PetscDTPKDEvalJet()`, `PetscDTPTrimmedSize()`
1400: @*/
1401: PetscErrorCode PetscDTPTrimmedEvalJet(PetscInt dim, PetscInt npoints, const PetscReal points[], PetscInt degree, PetscInt formDegree, PetscInt jetDegree, PetscReal p[])
1402: {
1403: PetscFunctionBegin;
1404: PetscCall(PetscDTPTrimmedEvalJet_Internal(dim, npoints, points, degree, formDegree, jetDegree, p));
1405: PetscFunctionReturn(PETSC_SUCCESS);
1406: }
1408: /* solve the symmetric tridiagonal eigenvalue system, writing the eigenvalues into eigs and the eigenvectors into V
1409: * with lds n; diag and subdiag are overwritten */
1410: static PetscErrorCode PetscDTSymmetricTridiagonalEigensolve(PetscInt n, PetscReal diag[], PetscReal subdiag[], PetscReal eigs[], PetscScalar V[])
1411: {
1412: char jobz = 'V'; /* eigenvalues and eigenvectors */
1413: char range = 'A'; /* all eigenvalues will be found */
1414: PetscReal VL = 0.; /* ignored because range is 'A' */
1415: PetscReal VU = 0.; /* ignored because range is 'A' */
1416: PetscBLASInt IL = 0; /* ignored because range is 'A' */
1417: PetscBLASInt IU = 0; /* ignored because range is 'A' */
1418: PetscReal abstol = 0.; /* unused */
1419: PetscBLASInt bn, bm, ldz; /* bm will equal bn on exit */
1420: PetscBLASInt *isuppz;
1421: PetscBLASInt lwork, liwork;
1422: PetscReal workquery;
1423: PetscBLASInt iworkquery;
1424: PetscBLASInt *iwork;
1425: PetscBLASInt info;
1426: PetscReal *work = NULL;
1428: PetscFunctionBegin;
1429: #if !defined(PETSCDTGAUSSIANQUADRATURE_EIG)
1430: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found");
1431: #endif
1432: PetscCall(PetscBLASIntCast(n, &bn));
1433: PetscCall(PetscBLASIntCast(n, &ldz));
1434: #if !defined(PETSC_MISSING_LAPACK_STEGR)
1435: PetscCall(PetscMalloc1(2 * n, &isuppz));
1436: lwork = -1;
1437: liwork = -1;
1438: PetscCallBLAS("LAPACKstegr", LAPACKstegr_(&jobz, &range, &bn, diag, subdiag, &VL, &VU, &IL, &IU, &abstol, &bm, eigs, V, &ldz, isuppz, &workquery, &lwork, &iworkquery, &liwork, &info));
1439: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xSTEGR error");
1440: lwork = (PetscBLASInt)workquery;
1441: liwork = iworkquery;
1442: PetscCall(PetscMalloc2(lwork, &work, liwork, &iwork));
1443: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
1444: PetscCallBLAS("LAPACKstegr", LAPACKstegr_(&jobz, &range, &bn, diag, subdiag, &VL, &VU, &IL, &IU, &abstol, &bm, eigs, V, &ldz, isuppz, work, &lwork, iwork, &liwork, &info));
1445: PetscCall(PetscFPTrapPop());
1446: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xSTEGR error");
1447: PetscCall(PetscFree2(work, iwork));
1448: PetscCall(PetscFree(isuppz));
1449: #elif !defined(PETSC_MISSING_LAPACK_STEQR)
1450: jobz = 'I'; /* Compute eigenvalues and eigenvectors of the
1451: tridiagonal matrix. Z is initialized to the identity
1452: matrix. */
1453: PetscCall(PetscMalloc1(PetscMax(1, 2 * n - 2), &work));
1454: PetscCallBLAS("LAPACKsteqr", LAPACKsteqr_("I", &bn, diag, subdiag, V, &ldz, work, &info));
1455: PetscCall(PetscFPTrapPop());
1456: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "xSTEQR error");
1457: PetscCall(PetscFree(work));
1458: PetscCall(PetscArraycpy(eigs, diag, n));
1459: #endif
1460: PetscFunctionReturn(PETSC_SUCCESS);
1461: }
1463: /* Formula for the weights at the endpoints (-1 and 1) of Gauss-Lobatto-Jacobi
1464: * quadrature rules on the interval [-1, 1] */
1465: static PetscErrorCode PetscDTGaussLobattoJacobiEndweights_Internal(PetscInt n, PetscReal alpha, PetscReal beta, PetscReal *leftw, PetscReal *rightw)
1466: {
1467: PetscReal twoab1;
1468: PetscInt m = n - 2;
1469: PetscReal a = alpha + 1.;
1470: PetscReal b = beta + 1.;
1471: PetscReal gra, grb;
1473: PetscFunctionBegin;
1474: twoab1 = PetscPowReal(2., a + b - 1.);
1475: #if defined(PETSC_HAVE_LGAMMA)
1476: grb = PetscExpReal(2. * PetscLGamma(b + 1.) + PetscLGamma(m + 1.) + PetscLGamma(m + a + 1.) - (PetscLGamma(m + b + 1) + PetscLGamma(m + a + b + 1.)));
1477: gra = PetscExpReal(2. * PetscLGamma(a + 1.) + PetscLGamma(m + 1.) + PetscLGamma(m + b + 1.) - (PetscLGamma(m + a + 1) + PetscLGamma(m + a + b + 1.)));
1478: #else
1479: {
1480: PetscReal binom1, binom2;
1481: PetscInt alphai = (PetscInt)alpha;
1482: PetscInt betai = (PetscInt)beta;
1484: PetscCheck((PetscReal)alphai == alpha && (PetscReal)betai == beta, PETSC_COMM_SELF, PETSC_ERR_SUP, "lgamma() - math routine is unavailable.");
1485: PetscCall(PetscDTBinomial(m + b, b, &binom1));
1486: PetscCall(PetscDTBinomial(m + a + b, b, &binom2));
1487: grb = 1. / (binom1 * binom2);
1488: PetscCall(PetscDTBinomial(m + a, a, &binom1));
1489: PetscCall(PetscDTBinomial(m + a + b, a, &binom2));
1490: gra = 1. / (binom1 * binom2);
1491: }
1492: #endif
1493: *leftw = twoab1 * grb / b;
1494: *rightw = twoab1 * gra / a;
1495: PetscFunctionReturn(PETSC_SUCCESS);
1496: }
1498: /* Evaluates the nth jacobi polynomial with weight parameters a,b at a point x.
1499: Recurrence relations implemented from the pseudocode given in Karniadakis and Sherwin, Appendix B */
1500: static inline PetscErrorCode PetscDTComputeJacobi(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscReal *P)
1501: {
1502: PetscReal pn1, pn2;
1503: PetscReal cnm1, cnm1x, cnm2;
1505: PetscFunctionBegin;
1506: if (!n) {
1507: *P = 1.0;
1508: PetscFunctionReturn(PETSC_SUCCESS);
1509: }
1510: PetscDTJacobiRecurrence_Internal(1, a, b, cnm1, cnm1x, cnm2);
1511: pn2 = 1.;
1512: pn1 = cnm1 + cnm1x * x;
1513: if (n == 1) {
1514: *P = pn1;
1515: PetscFunctionReturn(PETSC_SUCCESS);
1516: }
1517: *P = 0.0;
1518: for (PetscInt k = 2; k < n + 1; ++k) {
1519: PetscDTJacobiRecurrence_Internal(k, a, b, cnm1, cnm1x, cnm2);
1521: *P = (cnm1 + cnm1x * x) * pn1 - cnm2 * pn2;
1522: pn2 = pn1;
1523: pn1 = *P;
1524: }
1525: PetscFunctionReturn(PETSC_SUCCESS);
1526: }
1528: /* Evaluates the first derivative of P_{n}^{a,b} at a point x. */
1529: static inline PetscErrorCode PetscDTComputeJacobiDerivative(PetscReal a, PetscReal b, PetscInt n, PetscReal x, PetscInt k, PetscReal *P)
1530: {
1531: PetscReal nP;
1532: PetscInt i;
1534: PetscFunctionBegin;
1535: *P = 0.0;
1536: if (k > n) PetscFunctionReturn(PETSC_SUCCESS);
1537: PetscCall(PetscDTComputeJacobi(a + k, b + k, n - k, x, &nP));
1538: for (i = 0; i < k; i++) nP *= (a + b + n + 1. + i) * 0.5;
1539: *P = nP;
1540: PetscFunctionReturn(PETSC_SUCCESS);
1541: }
1543: static PetscErrorCode PetscDTGaussJacobiQuadrature_Newton_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[])
1544: {
1545: PetscInt maxIter = 100;
1546: PetscReal eps = PetscExpReal(0.75 * PetscLogReal(PETSC_MACHINE_EPSILON));
1547: PetscReal a1, a6, gf;
1549: PetscFunctionBegin;
1550: a1 = PetscPowReal(2.0, a + b + 1);
1551: #if defined(PETSC_HAVE_LGAMMA)
1552: {
1553: PetscReal a2, a3, a4, a5;
1554: a2 = PetscLGamma(a + npoints + 1);
1555: a3 = PetscLGamma(b + npoints + 1);
1556: a4 = PetscLGamma(a + b + npoints + 1);
1557: a5 = PetscLGamma(npoints + 1);
1558: gf = PetscExpReal(a2 + a3 - (a4 + a5));
1559: }
1560: #else
1561: {
1562: PetscInt ia, ib;
1564: ia = (PetscInt)a;
1565: ib = (PetscInt)b;
1566: gf = 1.;
1567: if (ia == a && ia >= 0) { /* compute ratio of rising factorals wrt a */
1568: for (PetscInt k = 0; k < ia; k++) gf *= (npoints + 1. + k) / (npoints + b + 1. + k);
1569: } else if (b == b && ib >= 0) { /* compute ratio of rising factorials wrt b */
1570: for (PetscInt k = 0; k < ib; k++) gf *= (npoints + 1. + k) / (npoints + a + 1. + k);
1571: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "lgamma() - math routine is unavailable.");
1572: }
1573: #endif
1575: a6 = a1 * gf;
1576: /* Computes the m roots of P_{m}^{a,b} on [-1,1] by Newton's method with Chebyshev points as initial guesses.
1577: Algorithm implemented from the pseudocode given by Karniadakis and Sherwin and Python in FIAT */
1578: for (PetscInt k = 0; k < npoints; ++k) {
1579: PetscReal r = PetscCosReal(PETSC_PI * (1. - (4. * k + 3. + 2. * b) / (4. * npoints + 2. * (a + b + 1.)))), dP;
1581: if (k > 0) r = 0.5 * (r + x[k - 1]);
1582: for (PetscInt j = 0; j < maxIter; ++j) {
1583: PetscReal s = 0.0, delta, f, fp;
1584: PetscInt i;
1586: for (i = 0; i < k; ++i) s = s + 1.0 / (r - x[i]);
1587: PetscCall(PetscDTComputeJacobi(a, b, npoints, r, &f));
1588: PetscCall(PetscDTComputeJacobiDerivative(a, b, npoints, r, 1, &fp));
1589: delta = f / (fp - f * s);
1590: r = r - delta;
1591: if (PetscAbsReal(delta) < eps) break;
1592: }
1593: x[k] = r;
1594: PetscCall(PetscDTComputeJacobiDerivative(a, b, npoints, x[k], 1, &dP));
1595: w[k] = a6 / (1.0 - PetscSqr(x[k])) / PetscSqr(dP);
1596: }
1597: PetscFunctionReturn(PETSC_SUCCESS);
1598: }
1600: /* Compute the diagonals of the Jacobi matrix used in Golub & Welsch algorithms for Gauss-Jacobi
1601: * quadrature weight calculations on [-1,1] for exponents (1. + x)^a (1.-x)^b */
1602: static PetscErrorCode PetscDTJacobiMatrix_Internal(PetscInt nPoints, PetscReal a, PetscReal b, PetscReal *d, PetscReal *s)
1603: {
1604: PetscInt i;
1606: PetscFunctionBegin;
1607: for (i = 0; i < nPoints; i++) {
1608: PetscReal A, B, C;
1610: PetscDTJacobiRecurrence_Internal(i + 1, a, b, A, B, C);
1611: d[i] = -A / B;
1612: if (i) s[i - 1] *= C / B;
1613: if (i < nPoints - 1) s[i] = 1. / B;
1614: }
1615: PetscFunctionReturn(PETSC_SUCCESS);
1616: }
1618: static PetscErrorCode PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(PetscInt npoints, PetscReal a, PetscReal b, PetscReal *x, PetscReal *w)
1619: {
1620: PetscReal mu0;
1621: PetscReal ga, gb, gab;
1622: PetscInt i;
1624: PetscFunctionBegin;
1625: PetscCall(PetscCitationsRegister(GolubWelschCitation, &GolubWelschCite));
1627: #if defined(PETSC_HAVE_TGAMMA)
1628: ga = PetscTGamma(a + 1);
1629: gb = PetscTGamma(b + 1);
1630: gab = PetscTGamma(a + b + 2);
1631: #else
1632: {
1633: PetscInt ia = (PetscInt)a, ib = (PetscInt)b;
1635: 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.");
1636: /* All gamma(x) terms are (x-1)! terms */
1637: PetscCall(PetscDTFactorial(ia, &ga));
1638: PetscCall(PetscDTFactorial(ib, &gb));
1639: PetscCall(PetscDTFactorial(ia + ib + 1, &gab));
1640: }
1641: #endif
1642: mu0 = PetscPowReal(2., a + b + 1.) * ga * gb / gab;
1644: #if defined(PETSCDTGAUSSIANQUADRATURE_EIG)
1645: {
1646: PetscReal *diag, *subdiag;
1647: PetscScalar *V;
1649: PetscCall(PetscMalloc2(npoints, &diag, npoints, &subdiag));
1650: PetscCall(PetscMalloc1(npoints * npoints, &V));
1651: PetscCall(PetscDTJacobiMatrix_Internal(npoints, a, b, diag, subdiag));
1652: for (i = 0; i < npoints - 1; i++) subdiag[i] = PetscSqrtReal(subdiag[i]);
1653: PetscCall(PetscDTSymmetricTridiagonalEigensolve(npoints, diag, subdiag, x, V));
1654: for (i = 0; i < npoints; i++) w[i] = PetscSqr(PetscRealPart(V[i * npoints])) * mu0;
1655: PetscCall(PetscFree(V));
1656: PetscCall(PetscFree2(diag, subdiag));
1657: }
1658: #else
1659: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "A LAPACK symmetric tridiagonal eigensolver could not be found");
1660: #endif
1661: { /* As of March 2, 2020, The Sun Performance Library breaks the LAPACK contract for xstegr and xsteqr: the
1662: eigenvalues are not guaranteed to be in ascending order. So we heave a passive aggressive sigh and check that
1663: the eigenvalues are sorted */
1664: PetscBool sorted;
1666: PetscCall(PetscSortedReal(npoints, x, &sorted));
1667: if (!sorted) {
1668: PetscInt *order, i;
1669: PetscReal *tmp;
1671: PetscCall(PetscMalloc2(npoints, &order, npoints, &tmp));
1672: for (i = 0; i < npoints; i++) order[i] = i;
1673: PetscCall(PetscSortRealWithPermutation(npoints, x, order));
1674: PetscCall(PetscArraycpy(tmp, x, npoints));
1675: for (i = 0; i < npoints; i++) x[i] = tmp[order[i]];
1676: PetscCall(PetscArraycpy(tmp, w, npoints));
1677: for (i = 0; i < npoints; i++) w[i] = tmp[order[i]];
1678: PetscCall(PetscFree2(order, tmp));
1679: }
1680: }
1681: PetscFunctionReturn(PETSC_SUCCESS);
1682: }
1684: static PetscErrorCode PetscDTGaussJacobiQuadrature_Internal(PetscInt npoints, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton)
1685: {
1686: PetscFunctionBegin;
1687: PetscCheck(npoints >= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of points must be positive");
1688: /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */
1689: PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "alpha must be > -1.");
1690: PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "beta must be > -1.");
1692: if (newton) PetscCall(PetscDTGaussJacobiQuadrature_Newton_Internal(npoints, alpha, beta, x, w));
1693: else PetscCall(PetscDTGaussJacobiQuadrature_GolubWelsch_Internal(npoints, alpha, beta, x, w));
1694: if (alpha == beta) { /* symmetrize */
1695: PetscInt i;
1696: for (i = 0; i < (npoints + 1) / 2; i++) {
1697: PetscInt j = npoints - 1 - i;
1698: PetscReal xi = x[i];
1699: PetscReal xj = x[j];
1700: PetscReal wi = w[i];
1701: PetscReal wj = w[j];
1703: x[i] = (xi - xj) / 2.;
1704: x[j] = (xj - xi) / 2.;
1705: w[i] = w[j] = (wi + wj) / 2.;
1706: }
1707: }
1708: PetscFunctionReturn(PETSC_SUCCESS);
1709: }
1711: /*@
1712: PetscDTGaussJacobiQuadrature - quadrature for the interval $[a, b]$ with the weight function
1713: $(x-a)^\alpha (x-b)^\beta$.
1715: Not Collective
1717: Input Parameters:
1718: + npoints - the number of points in the quadrature rule
1719: . a - the left endpoint of the interval
1720: . b - the right endpoint of the interval
1721: . alpha - the left exponent
1722: - beta - the right exponent
1724: Output Parameters:
1725: + x - array of length `npoints`, the locations of the quadrature points
1726: - w - array of length `npoints`, the weights of the quadrature points
1728: Level: intermediate
1730: Note:
1731: This quadrature rule is exact for polynomials up to degree 2*`npoints` - 1.
1733: .seealso: `PetscDTGaussQuadrature()`
1734: @*/
1735: PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt npoints, PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[])
1736: {
1737: PetscInt i;
1739: PetscFunctionBegin;
1740: PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal));
1741: if (a != -1. || b != 1.) { /* shift */
1742: for (i = 0; i < npoints; i++) {
1743: x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
1744: w[i] *= (b - a) / 2.;
1745: }
1746: }
1747: PetscFunctionReturn(PETSC_SUCCESS);
1748: }
1750: static PetscErrorCode PetscDTGaussLobattoJacobiQuadrature_Internal(PetscInt npoints, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[], PetscBool newton)
1751: {
1752: PetscFunctionBegin;
1753: PetscCheck(npoints >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of points must be positive");
1754: /* If asking for a 1D Lobatto point, just return the non-Lobatto 1D point */
1755: PetscCheck(alpha > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "alpha must be > -1.");
1756: PetscCheck(beta > -1., PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "beta must be > -1.");
1758: x[0] = -1.;
1759: x[npoints - 1] = 1.;
1760: if (npoints > 2) PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints - 2, alpha + 1., beta + 1., &x[1], &w[1], newton));
1761: for (PetscInt i = 1; i < npoints - 1; i++) w[i] /= (1. - x[i] * x[i]);
1762: PetscCall(PetscDTGaussLobattoJacobiEndweights_Internal(npoints, alpha, beta, &w[0], &w[npoints - 1]));
1763: PetscFunctionReturn(PETSC_SUCCESS);
1764: }
1766: /*@
1767: PetscDTGaussLobattoJacobiQuadrature - quadrature for the interval $[a, b]$ with the weight function
1768: $(x-a)^\alpha (x-b)^\beta$, with endpoints `a` and `b` included as quadrature points.
1770: Not Collective
1772: Input Parameters:
1773: + npoints - the number of points in the quadrature rule
1774: . a - the left endpoint of the interval
1775: . b - the right endpoint of the interval
1776: . alpha - the left exponent
1777: - beta - the right exponent
1779: Output Parameters:
1780: + x - array of length `npoints`, the locations of the quadrature points
1781: - w - array of length `npoints`, the weights of the quadrature points
1783: Level: intermediate
1785: Note:
1786: This quadrature rule is exact for polynomials up to degree 2*`npoints` - 3.
1788: .seealso: `PetscDTGaussJacobiQuadrature()`
1789: @*/
1790: PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt npoints, PetscReal a, PetscReal b, PetscReal alpha, PetscReal beta, PetscReal x[], PetscReal w[])
1791: {
1792: PetscFunctionBegin;
1793: PetscCall(PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, alpha, beta, x, w, PetscDTGaussQuadratureNewton_Internal));
1794: if (a != -1. || b != 1.) { /* shift */
1795: for (PetscInt i = 0; i < npoints; i++) {
1796: x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
1797: w[i] *= (b - a) / 2.;
1798: }
1799: }
1800: PetscFunctionReturn(PETSC_SUCCESS);
1801: }
1803: /*@
1804: PetscDTGaussQuadrature - create Gauss-Legendre quadrature
1806: Not Collective
1808: Input Parameters:
1809: + npoints - number of points
1810: . a - left end of interval (often-1)
1811: - b - right end of interval (often +1)
1813: Output Parameters:
1814: + x - quadrature points
1815: - w - quadrature weights
1817: Level: intermediate
1819: Note:
1820: See {cite}`golub1969calculation`
1822: .seealso: `PetscDTLegendreEval()`, `PetscDTGaussJacobiQuadrature()`
1823: @*/
1824: PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints, PetscReal a, PetscReal b, PetscReal x[], PetscReal w[])
1825: {
1826: PetscFunctionBegin;
1827: PetscCall(PetscDTGaussJacobiQuadrature_Internal(npoints, 0., 0., x, w, PetscDTGaussQuadratureNewton_Internal));
1828: if (a != -1. || b != 1.) { /* shift */
1829: for (PetscInt i = 0; i < npoints; i++) {
1830: x[i] = (x[i] + 1.) * ((b - a) / 2.) + a;
1831: w[i] *= (b - a) / 2.;
1832: }
1833: }
1834: PetscFunctionReturn(PETSC_SUCCESS);
1835: }
1837: /*@
1838: PetscDTGaussLobattoLegendreQuadrature - creates a set of the locations and weights of the Gauss-Lobatto-Legendre
1839: nodes of a given size on the domain $[-1,1]$
1841: Not Collective
1843: Input Parameters:
1844: + npoints - number of grid nodes
1845: - type - `PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA` or `PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON`
1847: Output Parameters:
1848: + x - quadrature points, pass in an array of length `npoints`
1849: - w - quadrature weights, pass in an array of length `npoints`
1851: Level: intermediate
1853: Notes:
1854: For n > 30 the Newton approach computes duplicate (incorrect) values for some nodes because the initial guess is apparently not
1855: close enough to the desired solution
1857: These are useful for implementing spectral methods based on Gauss-Lobatto-Legendre (GLL) nodes
1859: 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
1861: .seealso: `PetscDTGaussQuadrature()`, `PetscGaussLobattoLegendreCreateType`
1862: @*/
1863: PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt npoints, PetscGaussLobattoLegendreCreateType type, PetscReal x[], PetscReal w[])
1864: {
1865: PetscBool newton;
1867: PetscFunctionBegin;
1868: PetscCheck(npoints >= 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must provide at least 2 grid points per element");
1869: newton = (PetscBool)(type == PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON);
1870: PetscCall(PetscDTGaussLobattoJacobiQuadrature_Internal(npoints, 0., 0., x, w, newton));
1871: PetscFunctionReturn(PETSC_SUCCESS);
1872: }
1874: /*@
1875: PetscDTGaussTensorQuadrature - creates a tensor-product Gauss quadrature
1877: Not Collective
1879: Input Parameters:
1880: + dim - The spatial dimension
1881: . Nc - The number of components
1882: . npoints - number of points in one dimension
1883: . a - left end of interval (often-1)
1884: - b - right end of interval (often +1)
1886: Output Parameter:
1887: . q - A `PetscQuadrature` object
1889: Level: intermediate
1891: .seealso: `PetscDTGaussQuadrature()`, `PetscDTLegendreEval()`
1892: @*/
1893: PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q)
1894: {
1895: DMPolytopeType ct;
1896: PetscInt totpoints = dim > 1 ? dim > 2 ? npoints * PetscSqr(npoints) : PetscSqr(npoints) : npoints;
1897: PetscReal *x, *w, *xw, *ww;
1899: PetscFunctionBegin;
1900: PetscCall(PetscMalloc1(totpoints * dim, &x));
1901: PetscCall(PetscMalloc1(totpoints * Nc, &w));
1902: /* Set up the Golub-Welsch system */
1903: switch (dim) {
1904: case 0:
1905: ct = DM_POLYTOPE_POINT;
1906: PetscCall(PetscFree(x));
1907: PetscCall(PetscFree(w));
1908: PetscCall(PetscMalloc1(1, &x));
1909: PetscCall(PetscMalloc1(Nc, &w));
1910: totpoints = 1;
1911: x[0] = 0.0;
1912: for (PetscInt c = 0; c < Nc; ++c) w[c] = 1.0;
1913: break;
1914: case 1:
1915: ct = DM_POLYTOPE_SEGMENT;
1916: PetscCall(PetscMalloc1(npoints, &ww));
1917: PetscCall(PetscDTGaussQuadrature(npoints, a, b, x, ww));
1918: for (PetscInt i = 0; i < npoints; ++i)
1919: for (PetscInt c = 0; c < Nc; ++c) w[i * Nc + c] = ww[i];
1920: PetscCall(PetscFree(ww));
1921: break;
1922: case 2:
1923: ct = DM_POLYTOPE_QUADRILATERAL;
1924: PetscCall(PetscMalloc2(npoints, &xw, npoints, &ww));
1925: PetscCall(PetscDTGaussQuadrature(npoints, a, b, xw, ww));
1926: for (PetscInt i = 0; i < npoints; ++i) {
1927: for (PetscInt j = 0; j < npoints; ++j) {
1928: x[(i * npoints + j) * dim + 0] = xw[i];
1929: x[(i * npoints + j) * dim + 1] = xw[j];
1930: for (PetscInt c = 0; c < Nc; ++c) w[(i * npoints + j) * Nc + c] = ww[i] * ww[j];
1931: }
1932: }
1933: PetscCall(PetscFree2(xw, ww));
1934: break;
1935: case 3:
1936: ct = DM_POLYTOPE_HEXAHEDRON;
1937: PetscCall(PetscMalloc2(npoints, &xw, npoints, &ww));
1938: PetscCall(PetscDTGaussQuadrature(npoints, a, b, xw, ww));
1939: for (PetscInt i = 0; i < npoints; ++i) {
1940: for (PetscInt j = 0; j < npoints; ++j) {
1941: for (PetscInt k = 0; k < npoints; ++k) {
1942: x[((i * npoints + j) * npoints + k) * dim + 0] = xw[i];
1943: x[((i * npoints + j) * npoints + k) * dim + 1] = xw[j];
1944: x[((i * npoints + j) * npoints + k) * dim + 2] = xw[k];
1945: for (PetscInt c = 0; c < Nc; ++c) w[((i * npoints + j) * npoints + k) * Nc + c] = ww[i] * ww[j] * ww[k];
1946: }
1947: }
1948: }
1949: PetscCall(PetscFree2(xw, ww));
1950: break;
1951: default:
1952: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct quadrature rule for dimension %" PetscInt_FMT, dim);
1953: }
1954: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
1955: PetscCall(PetscQuadratureSetCellType(*q, ct));
1956: PetscCall(PetscQuadratureSetOrder(*q, 2 * npoints - 1));
1957: PetscCall(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w));
1958: PetscCall(PetscObjectChangeTypeName((PetscObject)*q, "GaussTensor"));
1959: PetscFunctionReturn(PETSC_SUCCESS);
1960: }
1962: /*@
1963: PetscDTStroudConicalQuadrature - create Stroud conical quadrature for a simplex {cite}`karniadakis2005spectral`
1965: Not Collective
1967: Input Parameters:
1968: + dim - The simplex dimension
1969: . Nc - The number of components
1970: . npoints - The number of points in one dimension
1971: . a - left end of interval (often-1)
1972: - b - right end of interval (often +1)
1974: Output Parameter:
1975: . q - A `PetscQuadrature` object
1977: Level: intermediate
1979: Note:
1980: For `dim` == 1, this is Gauss-Legendre quadrature
1982: .seealso: `PetscDTGaussTensorQuadrature()`, `PetscDTGaussQuadrature()`
1983: @*/
1984: PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt dim, PetscInt Nc, PetscInt npoints, PetscReal a, PetscReal b, PetscQuadrature *q)
1985: {
1986: DMPolytopeType ct;
1987: PetscInt totpoints;
1988: PetscReal *p1, *w1;
1989: PetscReal *x, *w;
1991: PetscFunctionBegin;
1992: PetscCheck(!(a != -1.0) && !(b != 1.0), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must use default internal right now");
1993: switch (dim) {
1994: case 0:
1995: ct = DM_POLYTOPE_POINT;
1996: break;
1997: case 1:
1998: ct = DM_POLYTOPE_SEGMENT;
1999: break;
2000: case 2:
2001: ct = DM_POLYTOPE_TRIANGLE;
2002: break;
2003: case 3:
2004: ct = DM_POLYTOPE_TETRAHEDRON;
2005: break;
2006: default:
2007: ct = DM_POLYTOPE_UNKNOWN;
2008: }
2009: totpoints = 1;
2010: for (PetscInt i = 0; i < dim; ++i) totpoints *= npoints;
2011: PetscCall(PetscMalloc1(totpoints * dim, &x));
2012: PetscCall(PetscMalloc1(totpoints * Nc, &w));
2013: PetscCall(PetscMalloc2(npoints, &p1, npoints, &w1));
2014: for (PetscInt i = 0; i < totpoints * Nc; ++i) w[i] = 1.;
2015: for (PetscInt i = 0, totprev = 1, totrem = totpoints / npoints; i < dim; ++i) {
2016: PetscReal mul;
2018: mul = PetscPowReal(2., -i);
2019: PetscCall(PetscDTGaussJacobiQuadrature(npoints, -1., 1., i, 0.0, p1, w1));
2020: for (PetscInt pt = 0, l = 0; l < totprev; l++) {
2021: for (PetscInt j = 0; j < npoints; j++) {
2022: for (PetscInt m = 0; m < totrem; m++, pt++) {
2023: for (PetscInt k = 0; k < i; k++) x[pt * dim + k] = (x[pt * dim + k] + 1.) * (1. - p1[j]) * 0.5 - 1.;
2024: x[pt * dim + i] = p1[j];
2025: for (PetscInt c = 0; c < Nc; c++) w[pt * Nc + c] *= mul * w1[j];
2026: }
2027: }
2028: }
2029: totprev *= npoints;
2030: totrem /= npoints;
2031: }
2032: PetscCall(PetscFree2(p1, w1));
2033: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
2034: PetscCall(PetscQuadratureSetCellType(*q, ct));
2035: PetscCall(PetscQuadratureSetOrder(*q, 2 * npoints - 1));
2036: PetscCall(PetscQuadratureSetData(*q, dim, Nc, totpoints, x, w));
2037: PetscCall(PetscObjectChangeTypeName((PetscObject)*q, "StroudConical"));
2038: PetscFunctionReturn(PETSC_SUCCESS);
2039: }
2041: static PetscBool MinSymTriQuadCite = PETSC_FALSE;
2042: const char MinSymTriQuadCitation[] = "@article{WitherdenVincent2015,\n"
2043: " title = {On the identification of symmetric quadrature rules for finite element methods},\n"
2044: " journal = {Computers & Mathematics with Applications},\n"
2045: " volume = {69},\n"
2046: " number = {10},\n"
2047: " pages = {1232-1241},\n"
2048: " year = {2015},\n"
2049: " issn = {0898-1221},\n"
2050: " doi = {10.1016/j.camwa.2015.03.017},\n"
2051: " url = {https://www.sciencedirect.com/science/article/pii/S0898122115001224},\n"
2052: " author = {F.D. Witherden and P.E. Vincent},\n"
2053: "}\n";
2055: #include "petscdttriquadrules.h"
2057: static PetscBool MinSymTetQuadCite = PETSC_FALSE;
2058: const char MinSymTetQuadCitation[] = "@article{JaskowiecSukumar2021\n"
2059: " author = {Jaskowiec, Jan and Sukumar, N.},\n"
2060: " title = {High-order symmetric cubature rules for tetrahedra and pyramids},\n"
2061: " journal = {International Journal for Numerical Methods in Engineering},\n"
2062: " volume = {122},\n"
2063: " number = {1},\n"
2064: " pages = {148-171},\n"
2065: " doi = {10.1002/nme.6528},\n"
2066: " url = {https://onlinelibrary.wiley.com/doi/abs/10.1002/nme.6528},\n"
2067: " eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1002/nme.6528},\n"
2068: " year = {2021}\n"
2069: "}\n";
2071: #include "petscdttetquadrules.h"
2073: static PetscBool DiagSymTriQuadCite = PETSC_FALSE;
2074: const char DiagSymTriQuadCitation[] = "@article{KongMulderVeldhuizen1999,\n"
2075: " title = {Higher-order triangular and tetrahedral finite elements with mass lumping for solving the wave equation},\n"
2076: " journal = {Journal of Engineering Mathematics},\n"
2077: " volume = {35},\n"
2078: " number = {4},\n"
2079: " pages = {405--426},\n"
2080: " year = {1999},\n"
2081: " doi = {10.1023/A:1004420829610},\n"
2082: " url = {https://link.springer.com/article/10.1023/A:1004420829610},\n"
2083: " author = {MJS Chin-Joe-Kong and Wim A Mulder and Marinus Van Veldhuizen},\n"
2084: "}\n";
2086: #include "petscdttridiagquadrules.h"
2088: // https://en.wikipedia.org/wiki/Partition_(number_theory)
2089: static PetscErrorCode PetscDTPartitionNumber(PetscInt n, PetscInt *p)
2090: {
2091: // sequence A000041 in the OEIS
2092: 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};
2093: PetscInt tabulated_max = PETSC_STATIC_ARRAY_LENGTH(partition) - 1;
2095: PetscFunctionBegin;
2096: PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Partition number not defined for negative number %" PetscInt_FMT, n);
2097: // not implementing the pentagonal number recurrence, we don't need partition numbers for n that high
2098: 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);
2099: *p = partition[n];
2100: PetscFunctionReturn(PETSC_SUCCESS);
2101: }
2103: /*@
2104: PetscDTSimplexQuadrature - Create a quadrature rule for a simplex that exactly integrates polynomials up to a given degree.
2106: Not Collective
2108: Input Parameters:
2109: + dim - The spatial dimension of the simplex (1 = segment, 2 = triangle, 3 = tetrahedron)
2110: . degree - The largest polynomial degree that is required to be integrated exactly
2111: - type - `PetscDTSimplexQuadratureType` indicating the type of quadrature rule
2113: Output Parameter:
2114: . quad - A `PetscQuadrature` object for integration over the biunit simplex
2115: (defined by the bounds $x_i >= -1$ and $\sum_i x_i <= 2 - d$) that is exact for
2116: polynomials up to the given degree
2118: Level: intermediate
2120: .seealso: `PetscDTSimplexQuadratureType`, `PetscDTGaussQuadrature()`, `PetscDTStroudCononicalQuadrature()`, `PetscQuadrature`
2121: @*/
2122: PetscErrorCode PetscDTSimplexQuadrature(PetscInt dim, PetscInt degree, PetscDTSimplexQuadratureType type, PetscQuadrature *quad)
2123: {
2124: PetscDTSimplexQuadratureType orig_type = type;
2126: PetscFunctionBegin;
2127: PetscCheck(dim >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative dimension %" PetscInt_FMT, dim);
2128: PetscCheck(degree >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative degree %" PetscInt_FMT, degree);
2129: if (type == PETSCDTSIMPLEXQUAD_DEFAULT) type = PETSCDTSIMPLEXQUAD_MINSYM;
2130: if (type == PETSCDTSIMPLEXQUAD_CONIC || dim < 2) {
2131: PetscInt points_per_dim = (degree + 2) / 2; // ceil((degree + 1) / 2);
2132: PetscCall(PetscDTStroudConicalQuadrature(dim, 1, points_per_dim, -1, 1, quad));
2133: } else {
2134: DMPolytopeType ct;
2135: PetscInt n = dim + 1;
2136: PetscInt fact = 1;
2137: PetscInt *part, *perm;
2138: PetscInt p = 0;
2139: PetscInt max_degree;
2140: const PetscInt *nodes_per_type = NULL;
2141: const PetscInt *all_num_full_nodes = NULL;
2142: const PetscReal **weights_list = NULL;
2143: const PetscReal **compact_nodes_list = NULL;
2144: const char *citation = NULL;
2145: PetscBool *cited = NULL;
2147: switch (dim) {
2148: case 0:
2149: ct = DM_POLYTOPE_POINT;
2150: break;
2151: case 1:
2152: ct = DM_POLYTOPE_SEGMENT;
2153: break;
2154: case 2:
2155: ct = DM_POLYTOPE_TRIANGLE;
2156: break;
2157: case 3:
2158: ct = DM_POLYTOPE_TETRAHEDRON;
2159: break;
2160: default:
2161: ct = DM_POLYTOPE_UNKNOWN;
2162: }
2163: if (type == PETSCDTSIMPLEXQUAD_MINSYM) {
2164: switch (dim) {
2165: case 2:
2166: cited = &MinSymTriQuadCite;
2167: citation = MinSymTriQuadCitation;
2168: max_degree = PetscDTWVTriQuad_max_degree;
2169: nodes_per_type = PetscDTWVTriQuad_num_orbits;
2170: all_num_full_nodes = PetscDTWVTriQuad_num_nodes;
2171: weights_list = PetscDTWVTriQuad_weights;
2172: compact_nodes_list = PetscDTWVTriQuad_orbits;
2173: break;
2174: case 3:
2175: cited = &MinSymTetQuadCite;
2176: citation = MinSymTetQuadCitation;
2177: max_degree = PetscDTJSTetQuad_max_degree;
2178: nodes_per_type = PetscDTJSTetQuad_num_orbits;
2179: all_num_full_nodes = PetscDTJSTetQuad_num_nodes;
2180: weights_list = PetscDTJSTetQuad_weights;
2181: compact_nodes_list = PetscDTJSTetQuad_orbits;
2182: break;
2183: default:
2184: max_degree = -1;
2185: break;
2186: }
2187: } else {
2188: switch (dim) {
2189: case 2:
2190: cited = &DiagSymTriQuadCite;
2191: citation = DiagSymTriQuadCitation;
2192: max_degree = PetscDTKMVTriQuad_max_degree;
2193: nodes_per_type = PetscDTKMVTriQuad_num_orbits;
2194: all_num_full_nodes = PetscDTKMVTriQuad_num_nodes;
2195: weights_list = PetscDTKMVTriQuad_weights;
2196: compact_nodes_list = PetscDTKMVTriQuad_orbits;
2197: break;
2198: default:
2199: max_degree = -1;
2200: break;
2201: }
2202: }
2204: if (degree > max_degree) {
2205: 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);
2206: // fall back to conic
2207: PetscCall(PetscDTSimplexQuadrature(dim, degree, PETSCDTSIMPLEXQUAD_CONIC, quad));
2208: PetscFunctionReturn(PETSC_SUCCESS);
2209: }
2211: PetscCall(PetscCitationsRegister(citation, cited));
2213: PetscCall(PetscDTPartitionNumber(n, &p));
2214: for (PetscInt d = 2; d <= n; d++) fact *= d;
2216: PetscInt num_full_nodes = all_num_full_nodes[degree];
2217: const PetscReal *all_compact_nodes = compact_nodes_list[degree];
2218: const PetscReal *all_compact_weights = weights_list[degree];
2219: nodes_per_type = &nodes_per_type[p * degree];
2221: PetscReal *points;
2222: PetscReal *counts;
2223: PetscReal *weights;
2224: PetscReal *bary_to_biunit; // row-major transformation of barycentric coordinate to biunit
2225: PetscQuadrature q;
2227: // compute the transformation
2228: PetscCall(PetscMalloc1(n * dim, &bary_to_biunit));
2229: for (PetscInt d = 0; d < dim; d++) {
2230: for (PetscInt b = 0; b < n; b++) bary_to_biunit[d * n + b] = (d == b) ? 1.0 : -1.0;
2231: }
2233: PetscCall(PetscMalloc3(n, &part, n, &perm, n, &counts));
2234: PetscCall(PetscCalloc1(num_full_nodes * dim, &points));
2235: PetscCall(PetscMalloc1(num_full_nodes, &weights));
2237: // (0, 0, ...) is the first partition lexicographically
2238: PetscCall(PetscArrayzero(part, n));
2239: PetscCall(PetscArrayzero(counts, n));
2240: counts[0] = n;
2242: // for each partition
2243: for (PetscInt s = 0, node_offset = 0; s < p; s++) {
2244: PetscInt num_compact_coords = part[n - 1] + 1;
2246: const PetscReal *compact_nodes = all_compact_nodes;
2247: const PetscReal *compact_weights = all_compact_weights;
2248: all_compact_nodes += num_compact_coords * nodes_per_type[s];
2249: all_compact_weights += nodes_per_type[s];
2251: // for every permutation of the vertices
2252: for (PetscInt f = 0; f < fact; f++) {
2253: PetscCall(PetscDTEnumPerm(n, f, perm, NULL));
2255: // check if it is a valid permutation
2256: PetscInt digit;
2257: for (digit = 1; digit < n; digit++) {
2258: // skip permutations that would duplicate a node because it has a smaller symmetry group
2259: if (part[digit - 1] == part[digit] && perm[digit - 1] > perm[digit]) break;
2260: }
2261: if (digit < n) continue;
2263: // create full nodes from this permutation of the compact nodes
2264: PetscReal *full_nodes = &points[node_offset * dim];
2265: PetscReal *full_weights = &weights[node_offset];
2267: PetscCall(PetscArraycpy(full_weights, compact_weights, nodes_per_type[s]));
2268: for (PetscInt b = 0; b < n; b++) {
2269: for (PetscInt d = 0; d < dim; d++) {
2270: 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]];
2271: }
2272: }
2273: node_offset += nodes_per_type[s];
2274: }
2276: if (s < p - 1) { // Generate the next partition
2277: /* A partition is described by the number of coordinates that are in
2278: * each set of duplicates (counts) and redundantly by mapping each
2279: * index to its set of duplicates (part)
2280: *
2281: * Counts should always be in nonincreasing order
2282: *
2283: * We want to generate the partitions lexically by part, which means
2284: * finding the last index where count > 1 and reducing by 1.
2285: *
2286: * For the new counts beyond that index, we eagerly assign the remaining
2287: * capacity of the partition to smaller indices (ensures lexical ordering),
2288: * while respecting the nonincreasing invariant of the counts
2289: */
2290: PetscInt last_digit = part[n - 1];
2291: PetscInt last_digit_with_extra = last_digit;
2292: while (counts[last_digit_with_extra] == 1) last_digit_with_extra--;
2293: PetscInt limit = --counts[last_digit_with_extra];
2294: PetscInt total_to_distribute = last_digit - last_digit_with_extra + 1;
2295: for (PetscInt digit = last_digit_with_extra + 1; digit < n; digit++) {
2296: counts[digit] = PetscMin(limit, total_to_distribute);
2297: total_to_distribute -= PetscMin(limit, total_to_distribute);
2298: }
2299: for (PetscInt digit = 0, offset = 0; digit < n; digit++) {
2300: PetscInt count = counts[digit];
2301: for (PetscInt c = 0; c < count; c++) part[offset++] = digit;
2302: }
2303: }
2304: 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);
2305: }
2306: PetscCall(PetscFree3(part, perm, counts));
2307: PetscCall(PetscFree(bary_to_biunit));
2308: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, &q));
2309: PetscCall(PetscQuadratureSetCellType(q, ct));
2310: PetscCall(PetscQuadratureSetOrder(q, degree));
2311: PetscCall(PetscQuadratureSetData(q, dim, 1, num_full_nodes, points, weights));
2312: *quad = q;
2313: }
2314: PetscFunctionReturn(PETSC_SUCCESS);
2315: }
2317: /*@
2318: PetscDTTanhSinhTensorQuadrature - create tanh-sinh quadrature for a tensor product cell
2320: Not Collective
2322: Input Parameters:
2323: + dim - The cell dimension
2324: . level - The number of points in one dimension, $2^l$
2325: . a - left end of interval (often-1)
2326: - b - right end of interval (often +1)
2328: Output Parameter:
2329: . q - A `PetscQuadrature` object
2331: Level: intermediate
2333: .seealso: `PetscDTGaussTensorQuadrature()`, `PetscQuadrature`
2334: @*/
2335: PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt dim, PetscInt level, PetscReal a, PetscReal b, PetscQuadrature *q)
2336: {
2337: DMPolytopeType ct;
2338: const PetscInt p = 16; /* Digits of precision in the evaluation */
2339: const PetscReal alpha = (b - a) / 2.; /* Half-width of the integration interval */
2340: const PetscReal beta = (b + a) / 2.; /* Center of the integration interval */
2341: const PetscReal h = PetscPowReal(2.0, -level); /* Step size, length between x_k */
2342: PetscReal xk; /* Quadrature point x_k on reference domain [-1, 1] */
2343: PetscReal wk = 0.5 * PETSC_PI; /* Quadrature weight at x_k */
2344: PetscReal *x, *w;
2345: PetscInt K, k, npoints;
2347: PetscFunctionBegin;
2348: PetscCheck(dim <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not yet implemented", dim);
2349: PetscCheck(level, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a number of significant digits");
2350: switch (dim) {
2351: case 0:
2352: ct = DM_POLYTOPE_POINT;
2353: break;
2354: case 1:
2355: ct = DM_POLYTOPE_SEGMENT;
2356: break;
2357: case 2:
2358: ct = DM_POLYTOPE_QUADRILATERAL;
2359: break;
2360: case 3:
2361: ct = DM_POLYTOPE_HEXAHEDRON;
2362: break;
2363: default:
2364: ct = DM_POLYTOPE_UNKNOWN;
2365: }
2366: /* Find K such that the weights are < 32 digits of precision */
2367: 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)));
2368: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
2369: PetscCall(PetscQuadratureSetCellType(*q, ct));
2370: PetscCall(PetscQuadratureSetOrder(*q, 2 * K + 1));
2371: npoints = 2 * K - 1;
2372: PetscCall(PetscMalloc1(npoints * dim, &x));
2373: PetscCall(PetscMalloc1(npoints, &w));
2374: /* Center term */
2375: x[0] = beta;
2376: w[0] = 0.5 * alpha * PETSC_PI;
2377: for (k = 1; k < K; ++k) {
2378: wk = 0.5 * alpha * h * PETSC_PI * PetscCoshReal(k * h) / PetscSqr(PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(k * h)));
2379: xk = PetscTanhReal(0.5 * PETSC_PI * PetscSinhReal(k * h));
2380: x[2 * k - 1] = -alpha * xk + beta;
2381: w[2 * k - 1] = wk;
2382: x[2 * k + 0] = alpha * xk + beta;
2383: w[2 * k + 0] = wk;
2384: }
2385: PetscCall(PetscQuadratureSetData(*q, dim, 1, npoints, x, w));
2386: PetscFunctionReturn(PETSC_SUCCESS);
2387: }
2389: /*@C
2390: PetscDTTanhSinhIntegrate - Approximate $\int_a^b f(x)\,dx$ to a requested precision using adaptive tanh-sinh (double-exponential) quadrature
2392: Not Collective; No Fortran Support
2394: Input Parameters:
2395: + func - the integrand callback (`func(x, ctx, &value)` evaluates the integrand at point `x`)
2396: . a - lower limit of integration
2397: . b - upper limit of integration
2398: . digits - target number of correct decimal digits
2399: - ctx - optional user context passed to `func`
2401: Output Parameter:
2402: . sol - the approximate value of the integral
2404: Level: developer
2406: Notes:
2407: Doubles the number of quadrature points at each refinement level until the change in the
2408: integral falls below the requested tolerance. Suitable for smooth integrands and integrands
2409: with endpoint singularities.
2411: For arbitrary-precision arithmetic via MPFR, see `PetscDTTanhSinhIntegrateMPFR()`.
2413: .seealso: `PetscDTTanhSinhIntegrateMPFR()`, `PetscDTGaussQuadrature()`
2414: @*/
2415: PetscErrorCode PetscDTTanhSinhIntegrate(void (*func)(const PetscReal[], PetscCtx, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscCtx ctx, PetscReal *sol)
2416: {
2417: const PetscInt p = 16; /* Digits of precision in the evaluation */
2418: const PetscReal alpha = (b - a) / 2.; /* Half-width of the integration interval */
2419: const PetscReal beta = (b + a) / 2.; /* Center of the integration interval */
2420: PetscReal h = 1.0; /* Step size, length between x_k */
2421: PetscInt l = 0; /* Level of refinement, h = 2^{-l} */
2422: PetscReal osum = 0.0; /* Integral on last level */
2423: PetscReal psum = 0.0; /* Integral on the level before the last level */
2424: PetscReal sum; /* Integral on current level */
2425: PetscReal yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */
2426: PetscReal lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */
2427: PetscReal wk; /* Quadrature weight at x_k */
2428: PetscReal lval, rval; /* Terms in the quadature sum to the left and right of 0 */
2429: PetscInt d; /* Digits of precision in the integral */
2431: PetscFunctionBegin;
2432: PetscCheck(digits > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits");
2433: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2434: /* Center term */
2435: func(&beta, ctx, &lval);
2436: sum = 0.5 * alpha * PETSC_PI * lval;
2437: /* */
2438: do {
2439: PetscReal lterm, rterm, maxTerm = 0.0, d1, d2, d3, d4;
2440: PetscInt k = 1;
2442: ++l;
2443: /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %" PetscInt_FMT " sum: %15.15f\n", l, sum); */
2444: /* At each level of refinement, h --> h/2 and sum --> sum/2 */
2445: psum = osum;
2446: osum = sum;
2447: h *= 0.5;
2448: sum *= 0.5;
2449: do {
2450: wk = 0.5 * h * PETSC_PI * PetscCoshReal(k * h) / PetscSqr(PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(k * h)));
2451: yk = 1.0 / (PetscExpReal(0.5 * PETSC_PI * PetscSinhReal(k * h)) * PetscCoshReal(0.5 * PETSC_PI * PetscSinhReal(k * h)));
2452: lx = -alpha * (1.0 - yk) + beta;
2453: rx = alpha * (1.0 - yk) + beta;
2454: func(&lx, ctx, &lval);
2455: func(&rx, ctx, &rval);
2456: lterm = alpha * wk * lval;
2457: maxTerm = PetscMax(PetscAbsReal(lterm), maxTerm);
2458: sum += lterm;
2459: rterm = alpha * wk * rval;
2460: maxTerm = PetscMax(PetscAbsReal(rterm), maxTerm);
2461: sum += rterm;
2462: ++k;
2463: /* Only need to evaluate every other point on refined levels */
2464: if (l != 1) ++k;
2465: } while (PetscAbsReal(PetscLog10Real(wk)) < p); /* Only need to evaluate sum until weights are < 32 digits of precision */
2467: d1 = PetscLog10Real(PetscAbsReal(sum - osum));
2468: d2 = PetscLog10Real(PetscAbsReal(sum - psum));
2469: d3 = PetscLog10Real(maxTerm) - p;
2470: if (PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)) == 0.0) d4 = 0.0;
2471: else d4 = PetscLog10Real(PetscMax(PetscAbsReal(lterm), PetscAbsReal(rterm)));
2472: d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1) / d2, 2 * d1), d3), d4)));
2473: } while (d < digits && l < 12);
2474: *sol = sum;
2475: PetscCall(PetscFPTrapPop());
2476: PetscFunctionReturn(PETSC_SUCCESS);
2477: }
2479: /*@C
2480: PetscDTTanhSinhIntegrateMPFR - High-precision version of `PetscDTTanhSinhIntegrate()` that uses the MPFR arbitrary-precision library to evaluate the quadrature
2482: Not Collective; No Fortran Support
2484: Input Parameters:
2485: + func - the integrand callback (`func(x, ctx, &value)` evaluates the integrand at point `x`)
2486: . a - lower limit of integration
2487: . b - upper limit of integration
2488: . digits - target number of correct decimal digits (also drives the working MPFR precision)
2489: - ctx - optional user context passed to `func`
2491: Output Parameter:
2492: . sol - the approximate value of the integral
2494: Level: developer
2496: Note:
2497: Requires PETSc to be configured with `--with-mpfr`; otherwise an error is raised.
2499: .seealso: `PetscDTTanhSinhIntegrate()`, `PetscDTGaussQuadrature()`
2500: @*/
2501: #if defined(PETSC_HAVE_MPFR)
2502: PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], PetscCtx, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscCtx ctx, PetscReal *sol)
2503: {
2504: const PetscInt safetyFactor = 2; /* Calculate abscissa until 2*p digits */
2505: PetscInt l = 0; /* Level of refinement, h = 2^{-l} */
2506: mpfr_t alpha; /* Half-width of the integration interval */
2507: mpfr_t beta; /* Center of the integration interval */
2508: mpfr_t h; /* Step size, length between x_k */
2509: mpfr_t osum; /* Integral on last level */
2510: mpfr_t psum; /* Integral on the level before the last level */
2511: mpfr_t sum; /* Integral on current level */
2512: mpfr_t yk; /* Quadrature point 1 - x_k on reference domain [-1, 1] */
2513: mpfr_t lx, rx; /* Quadrature points to the left and right of 0 on the real domain [a, b] */
2514: mpfr_t wk; /* Quadrature weight at x_k */
2515: PetscReal lval, rval, rtmp; /* Terms in the quadature sum to the left and right of 0 */
2516: PetscInt d; /* Digits of precision in the integral */
2517: mpfr_t pi2, kh, msinh, mcosh, maxTerm, curTerm, tmp;
2519: PetscFunctionBegin;
2520: PetscCheck(digits > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Must give a positive number of significant digits");
2521: /* Create high precision storage */
2522: 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);
2523: /* Initialization */
2524: mpfr_set_d(alpha, 0.5 * (b - a), MPFR_RNDN);
2525: mpfr_set_d(beta, 0.5 * (b + a), MPFR_RNDN);
2526: mpfr_set_d(osum, 0.0, MPFR_RNDN);
2527: mpfr_set_d(psum, 0.0, MPFR_RNDN);
2528: mpfr_set_d(h, 1.0, MPFR_RNDN);
2529: mpfr_const_pi(pi2, MPFR_RNDN);
2530: mpfr_mul_d(pi2, pi2, 0.5, MPFR_RNDN);
2531: /* Center term */
2532: rtmp = 0.5 * (b + a);
2533: func(&rtmp, ctx, &lval);
2534: mpfr_set(sum, pi2, MPFR_RNDN);
2535: mpfr_mul(sum, sum, alpha, MPFR_RNDN);
2536: mpfr_mul_d(sum, sum, lval, MPFR_RNDN);
2537: /* */
2538: do {
2539: PetscReal d1, d2, d3, d4;
2540: PetscInt k = 1;
2542: ++l;
2543: mpfr_set_d(maxTerm, 0.0, MPFR_RNDN);
2544: /* PetscPrintf(PETSC_COMM_SELF, "LEVEL %" PetscInt_FMT " sum: %15.15f\n", l, sum); */
2545: /* At each level of refinement, h --> h/2 and sum --> sum/2 */
2546: mpfr_set(psum, osum, MPFR_RNDN);
2547: mpfr_set(osum, sum, MPFR_RNDN);
2548: mpfr_mul_d(h, h, 0.5, MPFR_RNDN);
2549: mpfr_mul_d(sum, sum, 0.5, MPFR_RNDN);
2550: do {
2551: mpfr_set_si(kh, k, MPFR_RNDN);
2552: mpfr_mul(kh, kh, h, MPFR_RNDN);
2553: /* Weight */
2554: mpfr_set(wk, h, MPFR_RNDN);
2555: mpfr_sinh_cosh(msinh, mcosh, kh, MPFR_RNDN);
2556: mpfr_mul(msinh, msinh, pi2, MPFR_RNDN);
2557: mpfr_mul(mcosh, mcosh, pi2, MPFR_RNDN);
2558: mpfr_cosh(tmp, msinh, MPFR_RNDN);
2559: mpfr_sqr(tmp, tmp, MPFR_RNDN);
2560: mpfr_mul(wk, wk, mcosh, MPFR_RNDN);
2561: mpfr_div(wk, wk, tmp, MPFR_RNDN);
2562: /* Abscissa */
2563: mpfr_set_d(yk, 1.0, MPFR_RNDZ);
2564: mpfr_cosh(tmp, msinh, MPFR_RNDN);
2565: mpfr_div(yk, yk, tmp, MPFR_RNDZ);
2566: mpfr_exp(tmp, msinh, MPFR_RNDN);
2567: mpfr_div(yk, yk, tmp, MPFR_RNDZ);
2568: /* Quadrature points */
2569: mpfr_sub_d(lx, yk, 1.0, MPFR_RNDZ);
2570: mpfr_mul(lx, lx, alpha, MPFR_RNDU);
2571: mpfr_add(lx, lx, beta, MPFR_RNDU);
2572: mpfr_d_sub(rx, 1.0, yk, MPFR_RNDZ);
2573: mpfr_mul(rx, rx, alpha, MPFR_RNDD);
2574: mpfr_add(rx, rx, beta, MPFR_RNDD);
2575: /* Evaluation */
2576: rtmp = mpfr_get_d(lx, MPFR_RNDU);
2577: func(&rtmp, ctx, &lval);
2578: rtmp = mpfr_get_d(rx, MPFR_RNDD);
2579: func(&rtmp, ctx, &rval);
2580: /* Update */
2581: mpfr_mul(tmp, wk, alpha, MPFR_RNDN);
2582: mpfr_mul_d(tmp, tmp, lval, MPFR_RNDN);
2583: mpfr_add(sum, sum, tmp, MPFR_RNDN);
2584: mpfr_abs(tmp, tmp, MPFR_RNDN);
2585: mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN);
2586: mpfr_set(curTerm, tmp, MPFR_RNDN);
2587: mpfr_mul(tmp, wk, alpha, MPFR_RNDN);
2588: mpfr_mul_d(tmp, tmp, rval, MPFR_RNDN);
2589: mpfr_add(sum, sum, tmp, MPFR_RNDN);
2590: mpfr_abs(tmp, tmp, MPFR_RNDN);
2591: mpfr_max(maxTerm, maxTerm, tmp, MPFR_RNDN);
2592: mpfr_max(curTerm, curTerm, tmp, MPFR_RNDN);
2593: ++k;
2594: /* Only need to evaluate every other point on refined levels */
2595: if (l != 1) ++k;
2596: mpfr_log10(tmp, wk, MPFR_RNDN);
2597: mpfr_abs(tmp, tmp, MPFR_RNDN);
2598: } while (mpfr_get_d(tmp, MPFR_RNDN) < safetyFactor * digits); /* Only need to evaluate sum until weights are < 32 digits of precision */
2599: mpfr_sub(tmp, sum, osum, MPFR_RNDN);
2600: mpfr_abs(tmp, tmp, MPFR_RNDN);
2601: mpfr_log10(tmp, tmp, MPFR_RNDN);
2602: d1 = mpfr_get_d(tmp, MPFR_RNDN);
2603: mpfr_sub(tmp, sum, psum, MPFR_RNDN);
2604: mpfr_abs(tmp, tmp, MPFR_RNDN);
2605: mpfr_log10(tmp, tmp, MPFR_RNDN);
2606: d2 = mpfr_get_d(tmp, MPFR_RNDN);
2607: mpfr_log10(tmp, maxTerm, MPFR_RNDN);
2608: d3 = mpfr_get_d(tmp, MPFR_RNDN) - digits;
2609: mpfr_log10(tmp, curTerm, MPFR_RNDN);
2610: d4 = mpfr_get_d(tmp, MPFR_RNDN);
2611: d = PetscAbsInt(PetscMin(0, PetscMax(PetscMax(PetscMax(PetscSqr(d1) / d2, 2 * d1), d3), d4)));
2612: } while (d < digits && l < 8);
2613: *sol = mpfr_get_d(sum, MPFR_RNDN);
2614: /* Cleanup */
2615: mpfr_clears(alpha, beta, h, sum, osum, psum, yk, wk, lx, rx, tmp, maxTerm, curTerm, pi2, kh, msinh, mcosh, NULL);
2616: PetscFunctionReturn(PETSC_SUCCESS);
2617: }
2618: #else
2620: PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*func)(const PetscReal[], void *, PetscReal *), PetscReal a, PetscReal b, PetscInt digits, PetscCtx ctx, PetscReal *sol)
2621: {
2622: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "This method will not work without MPFR. Reconfigure using --download-mpfr --download-gmp");
2623: }
2624: #endif
2626: /*@
2627: PetscDTTensorQuadratureCreate - create the tensor product quadrature from two lower-dimensional quadratures
2629: Not Collective
2631: Input Parameters:
2632: + q1 - The first quadrature
2633: - q2 - The second quadrature
2635: Output Parameter:
2636: . q - A `PetscQuadrature` object
2638: Level: intermediate
2640: .seealso: `PetscQuadrature`, `PetscDTGaussTensorQuadrature()`
2641: @*/
2642: PetscErrorCode PetscDTTensorQuadratureCreate(PetscQuadrature q1, PetscQuadrature q2, PetscQuadrature *q)
2643: {
2644: DMPolytopeType ct1, ct2, ct;
2645: const PetscReal *x1, *w1, *x2, *w2;
2646: PetscReal *x, *w;
2647: PetscInt dim1, Nc1, Np1, order1, qa, d1;
2648: PetscInt dim2, Nc2, Np2, order2, qb, d2;
2649: PetscInt dim, Nc, Np, order, qc, d;
2651: PetscFunctionBegin;
2654: PetscAssertPointer(q, 3);
2656: PetscCall(PetscQuadratureGetOrder(q1, &order1));
2657: PetscCall(PetscQuadratureGetOrder(q2, &order2));
2658: PetscCheck(order1 == order2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Order1 %" PetscInt_FMT " != %" PetscInt_FMT " Order2", order1, order2);
2659: PetscCall(PetscQuadratureGetData(q1, &dim1, &Nc1, &Np1, &x1, &w1));
2660: PetscCall(PetscQuadratureGetCellType(q1, &ct1));
2661: PetscCall(PetscQuadratureGetData(q2, &dim2, &Nc2, &Np2, &x2, &w2));
2662: PetscCall(PetscQuadratureGetCellType(q2, &ct2));
2663: PetscCheck(Nc1 == Nc2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "NumComp1 %" PetscInt_FMT " != %" PetscInt_FMT " NumComp2", Nc1, Nc2);
2665: switch (ct1) {
2666: case DM_POLYTOPE_POINT:
2667: ct = ct2;
2668: break;
2669: case DM_POLYTOPE_SEGMENT:
2670: switch (ct2) {
2671: case DM_POLYTOPE_POINT:
2672: ct = ct1;
2673: break;
2674: case DM_POLYTOPE_SEGMENT:
2675: ct = DM_POLYTOPE_QUADRILATERAL;
2676: break;
2677: case DM_POLYTOPE_TRIANGLE:
2678: ct = DM_POLYTOPE_TRI_PRISM;
2679: break;
2680: case DM_POLYTOPE_QUADRILATERAL:
2681: ct = DM_POLYTOPE_HEXAHEDRON;
2682: break;
2683: case DM_POLYTOPE_TETRAHEDRON:
2684: ct = DM_POLYTOPE_UNKNOWN;
2685: break;
2686: case DM_POLYTOPE_HEXAHEDRON:
2687: ct = DM_POLYTOPE_UNKNOWN;
2688: break;
2689: default:
2690: ct = DM_POLYTOPE_UNKNOWN;
2691: }
2692: break;
2693: case DM_POLYTOPE_TRIANGLE:
2694: switch (ct2) {
2695: case DM_POLYTOPE_POINT:
2696: ct = ct1;
2697: break;
2698: case DM_POLYTOPE_SEGMENT:
2699: ct = DM_POLYTOPE_TRI_PRISM;
2700: break;
2701: case DM_POLYTOPE_TRIANGLE:
2702: ct = DM_POLYTOPE_UNKNOWN;
2703: break;
2704: case DM_POLYTOPE_QUADRILATERAL:
2705: ct = DM_POLYTOPE_UNKNOWN;
2706: break;
2707: case DM_POLYTOPE_TETRAHEDRON:
2708: ct = DM_POLYTOPE_UNKNOWN;
2709: break;
2710: case DM_POLYTOPE_HEXAHEDRON:
2711: ct = DM_POLYTOPE_UNKNOWN;
2712: break;
2713: default:
2714: ct = DM_POLYTOPE_UNKNOWN;
2715: }
2716: break;
2717: case DM_POLYTOPE_QUADRILATERAL:
2718: switch (ct2) {
2719: case DM_POLYTOPE_POINT:
2720: ct = ct1;
2721: break;
2722: case DM_POLYTOPE_SEGMENT:
2723: ct = DM_POLYTOPE_HEXAHEDRON;
2724: break;
2725: case DM_POLYTOPE_TRIANGLE:
2726: ct = DM_POLYTOPE_UNKNOWN;
2727: break;
2728: case DM_POLYTOPE_QUADRILATERAL:
2729: ct = DM_POLYTOPE_UNKNOWN;
2730: break;
2731: case DM_POLYTOPE_TETRAHEDRON:
2732: ct = DM_POLYTOPE_UNKNOWN;
2733: break;
2734: case DM_POLYTOPE_HEXAHEDRON:
2735: ct = DM_POLYTOPE_UNKNOWN;
2736: break;
2737: default:
2738: ct = DM_POLYTOPE_UNKNOWN;
2739: }
2740: break;
2741: case DM_POLYTOPE_TETRAHEDRON:
2742: switch (ct2) {
2743: case DM_POLYTOPE_POINT:
2744: ct = ct1;
2745: break;
2746: case DM_POLYTOPE_SEGMENT:
2747: ct = DM_POLYTOPE_UNKNOWN;
2748: break;
2749: case DM_POLYTOPE_TRIANGLE:
2750: ct = DM_POLYTOPE_UNKNOWN;
2751: break;
2752: case DM_POLYTOPE_QUADRILATERAL:
2753: ct = DM_POLYTOPE_UNKNOWN;
2754: break;
2755: case DM_POLYTOPE_TETRAHEDRON:
2756: ct = DM_POLYTOPE_UNKNOWN;
2757: break;
2758: case DM_POLYTOPE_HEXAHEDRON:
2759: ct = DM_POLYTOPE_UNKNOWN;
2760: break;
2761: default:
2762: ct = DM_POLYTOPE_UNKNOWN;
2763: }
2764: break;
2765: case DM_POLYTOPE_HEXAHEDRON:
2766: switch (ct2) {
2767: case DM_POLYTOPE_POINT:
2768: ct = ct1;
2769: break;
2770: case DM_POLYTOPE_SEGMENT:
2771: ct = DM_POLYTOPE_UNKNOWN;
2772: break;
2773: case DM_POLYTOPE_TRIANGLE:
2774: ct = DM_POLYTOPE_UNKNOWN;
2775: break;
2776: case DM_POLYTOPE_QUADRILATERAL:
2777: ct = DM_POLYTOPE_UNKNOWN;
2778: break;
2779: case DM_POLYTOPE_TETRAHEDRON:
2780: ct = DM_POLYTOPE_UNKNOWN;
2781: break;
2782: case DM_POLYTOPE_HEXAHEDRON:
2783: ct = DM_POLYTOPE_UNKNOWN;
2784: break;
2785: default:
2786: ct = DM_POLYTOPE_UNKNOWN;
2787: }
2788: break;
2789: default:
2790: ct = DM_POLYTOPE_UNKNOWN;
2791: }
2792: dim = dim1 + dim2;
2793: Nc = Nc1;
2794: Np = Np1 * Np2;
2795: order = order1;
2796: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
2797: PetscCall(PetscQuadratureSetCellType(*q, ct));
2798: PetscCall(PetscQuadratureSetOrder(*q, order));
2799: PetscCall(PetscMalloc1(Np * dim, &x));
2800: PetscCall(PetscMalloc1(Np, &w));
2801: for (qa = 0, qc = 0; qa < Np1; ++qa) {
2802: for (qb = 0; qb < Np2; ++qb, ++qc) {
2803: for (d1 = 0, d = 0; d1 < dim1; ++d1, ++d) x[qc * dim + d] = x1[qa * dim1 + d1];
2804: for (d2 = 0; d2 < dim2; ++d2, ++d) x[qc * dim + d] = x2[qb * dim2 + d2];
2805: w[qc] = w1[qa] * w2[qb];
2806: }
2807: }
2808: PetscCall(PetscQuadratureSetData(*q, dim, Nc, Np, x, w));
2809: PetscFunctionReturn(PETSC_SUCCESS);
2810: }
2812: /* Overwrites A. Can only handle full-rank problems with m>=n
2813: A in column-major format
2814: Ainv in row-major format
2815: tau has length m
2816: worksize must be >= max(1,n)
2817: */
2818: static PetscErrorCode PetscDTPseudoInverseQR(PetscInt m, PetscInt mstride, PetscInt n, PetscReal *A_in, PetscReal *Ainv_out, PetscScalar *tau, PetscInt worksize, PetscScalar *work)
2819: {
2820: PetscBLASInt M, N, K, lda, ldb, ldwork, info;
2821: PetscScalar *A, *Ainv, *R, *Q, Alpha;
2823: PetscFunctionBegin;
2824: #if defined(PETSC_USE_COMPLEX)
2825: {
2826: PetscInt i, j;
2827: PetscCall(PetscMalloc2(m * n, &A, m * n, &Ainv));
2828: for (j = 0; j < n; j++) {
2829: for (i = 0; i < m; i++) A[i + m * j] = A_in[i + mstride * j];
2830: }
2831: mstride = m;
2832: }
2833: #else
2834: A = A_in;
2835: Ainv = Ainv_out;
2836: #endif
2838: PetscCall(PetscBLASIntCast(m, &M));
2839: PetscCall(PetscBLASIntCast(n, &N));
2840: PetscCall(PetscBLASIntCast(mstride, &lda));
2841: PetscCall(PetscBLASIntCast(worksize, &ldwork));
2842: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
2843: PetscCallBLAS("LAPACKgeqrf", LAPACKgeqrf_(&M, &N, A, &lda, tau, work, &ldwork, &info));
2844: PetscCall(PetscFPTrapPop());
2845: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "xGEQRF error");
2846: R = A; /* Upper triangular part of A now contains R, the rest contains the elementary reflectors */
2848: /* Extract an explicit representation of Q */
2849: Q = Ainv;
2850: PetscCall(PetscArraycpy(Q, A, mstride * n));
2851: K = N; /* full rank */
2852: PetscCallBLAS("LAPACKorgqr", LAPACKorgqr_(&M, &N, &K, Q, &lda, tau, work, &ldwork, &info));
2853: PetscCheck(!info, PETSC_COMM_SELF, PETSC_ERR_LIB, "xORGQR/xUNGQR error");
2855: /* Compute A^{-T} = (R^{-1} Q^T)^T = Q R^{-T} */
2856: Alpha = 1.0;
2857: ldb = lda;
2858: PetscCallBLAS("BLAStrsm", BLAStrsm_("Right", "Upper", "ConjugateTranspose", "NotUnitTriangular", &M, &N, &Alpha, R, &lda, Q, &ldb));
2859: /* Ainv is Q, overwritten with inverse */
2861: #if defined(PETSC_USE_COMPLEX)
2862: {
2863: PetscInt i;
2864: for (i = 0; i < m * n; i++) Ainv_out[i] = PetscRealPart(Ainv[i]);
2865: PetscCall(PetscFree2(A, Ainv));
2866: }
2867: #endif
2868: PetscFunctionReturn(PETSC_SUCCESS);
2869: }
2871: /* Computes integral of L_p' over intervals {(x0,x1),(x1,x2),...} */
2872: static PetscErrorCode PetscDTLegendreIntegrate(PetscInt ninterval, const PetscReal *x, PetscInt ndegree, const PetscInt *degrees, PetscBool Transpose, PetscReal *B)
2873: {
2874: PetscReal *Bv;
2875: PetscInt i, j;
2877: PetscFunctionBegin;
2878: PetscCall(PetscMalloc1((ninterval + 1) * ndegree, &Bv));
2879: /* Point evaluation of L_p on all the source vertices */
2880: PetscCall(PetscDTLegendreEval(ninterval + 1, x, ndegree, degrees, Bv, NULL, NULL));
2881: /* Integral over each interval: \int_a^b L_p' = L_p(b)-L_p(a) */
2882: for (i = 0; i < ninterval; i++) {
2883: for (j = 0; j < ndegree; j++) {
2884: if (Transpose) B[i + ninterval * j] = Bv[(i + 1) * ndegree + j] - Bv[i * ndegree + j];
2885: else B[i * ndegree + j] = Bv[(i + 1) * ndegree + j] - Bv[i * ndegree + j];
2886: }
2887: }
2888: PetscCall(PetscFree(Bv));
2889: PetscFunctionReturn(PETSC_SUCCESS);
2890: }
2892: /*@
2893: PetscDTReconstructPoly - create matrix representing polynomial reconstruction using cell intervals and evaluation at target intervals
2895: Not Collective
2897: Input Parameters:
2898: + degree - degree of reconstruction polynomial
2899: . nsource - number of source intervals
2900: . sourcex - sorted coordinates of source cell boundaries (length `nsource`+1)
2901: . ntarget - number of target intervals
2902: - targetx - sorted coordinates of target cell boundaries (length `ntarget`+1)
2904: Output Parameter:
2905: . R - reconstruction matrix, utarget = sum_s R[t*nsource+s] * usource[s]
2907: Level: advanced
2909: .seealso: `PetscDTLegendreEval()`
2910: @*/
2911: PetscErrorCode PetscDTReconstructPoly(PetscInt degree, PetscInt nsource, const PetscReal sourcex[], PetscInt ntarget, const PetscReal targetx[], PetscReal R[])
2912: {
2913: PetscInt i, j, k, *bdegrees, worksize;
2914: PetscReal xmin, xmax, center, hscale, *sourcey, *targety, *Bsource, *Bsinv, *Btarget;
2915: PetscScalar *tau, *work;
2917: PetscFunctionBegin;
2918: PetscAssertPointer(sourcex, 3);
2919: PetscAssertPointer(targetx, 5);
2920: PetscAssertPointer(R, 6);
2921: 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);
2922: if (PetscDefined(USE_DEBUG)) {
2923: 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]);
2924: 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]);
2925: }
2926: xmin = PetscMin(sourcex[0], targetx[0]);
2927: xmax = PetscMax(sourcex[nsource], targetx[ntarget]);
2928: center = (xmin + xmax) / 2;
2929: hscale = (xmax - xmin) / 2;
2930: worksize = nsource;
2931: PetscCall(PetscMalloc4(degree + 1, &bdegrees, nsource + 1, &sourcey, nsource * (degree + 1), &Bsource, worksize, &work));
2932: PetscCall(PetscMalloc4(nsource, &tau, nsource * (degree + 1), &Bsinv, ntarget + 1, &targety, ntarget * (degree + 1), &Btarget));
2933: for (i = 0; i <= nsource; i++) sourcey[i] = (sourcex[i] - center) / hscale;
2934: for (i = 0; i <= degree; i++) bdegrees[i] = i + 1;
2935: PetscCall(PetscDTLegendreIntegrate(nsource, sourcey, degree + 1, bdegrees, PETSC_TRUE, Bsource));
2936: PetscCall(PetscDTPseudoInverseQR(nsource, nsource, degree + 1, Bsource, Bsinv, tau, nsource, work));
2937: for (i = 0; i <= ntarget; i++) targety[i] = (targetx[i] - center) / hscale;
2938: PetscCall(PetscDTLegendreIntegrate(ntarget, targety, degree + 1, bdegrees, PETSC_FALSE, Btarget));
2939: for (i = 0; i < ntarget; i++) {
2940: PetscReal rowsum = 0;
2941: for (j = 0; j < nsource; j++) {
2942: PetscReal sum = 0;
2943: for (k = 0; k < degree + 1; k++) sum += Btarget[i * (degree + 1) + k] * Bsinv[k * nsource + j];
2944: R[i * nsource + j] = sum;
2945: rowsum += sum;
2946: }
2947: for (j = 0; j < nsource; j++) R[i * nsource + j] /= rowsum; /* normalize each row */
2948: }
2949: PetscCall(PetscFree4(bdegrees, sourcey, Bsource, work));
2950: PetscCall(PetscFree4(tau, Bsinv, targety, Btarget));
2951: PetscFunctionReturn(PETSC_SUCCESS);
2952: }
2954: /*@
2955: PetscGaussLobattoLegendreIntegrate - Compute the L2 integral of a function on the GLL points
2957: Not Collective
2959: Input Parameters:
2960: + n - the number of GLL nodes
2961: . nodes - the GLL nodes
2962: . weights - the GLL weights
2963: - f - the function values at the nodes
2965: Output Parameter:
2966: . in - the value of the integral
2968: Level: beginner
2970: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`
2971: @*/
2972: PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt n, PetscReal nodes[], PetscReal weights[], const PetscReal f[], PetscReal *in)
2973: {
2974: PetscInt i;
2976: PetscFunctionBegin;
2977: *in = 0.;
2978: for (i = 0; i < n; i++) *in += f[i] * f[i] * weights[i];
2979: PetscFunctionReturn(PETSC_SUCCESS);
2980: }
2982: /*@C
2983: PetscGaussLobattoLegendreElementLaplacianCreate - computes the Laplacian for a single 1d GLL element
2985: Not Collective
2987: Input Parameters:
2988: + n - the number of GLL nodes
2989: . nodes - the GLL nodes, of length `n`
2990: - weights - the GLL weights, of length `n`
2992: Output Parameter:
2993: . AA - the stiffness element, of size `n` by `n`
2995: Level: beginner
2997: Notes:
2998: Destroy this with `PetscGaussLobattoLegendreElementLaplacianDestroy()`
3000: 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)
3002: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianDestroy()`
3003: @*/
3004: PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3005: {
3006: PetscReal **A;
3007: const PetscReal *gllnodes = nodes;
3008: const PetscInt p = n - 1;
3009: PetscReal z0, z1, z2 = -1, x, Lpj, Lpr;
3010: PetscInt i, j, nn, r;
3012: PetscFunctionBegin;
3013: PetscCall(PetscMalloc1(n, &A));
3014: PetscCall(PetscMalloc1(n * n, &A[0]));
3015: for (i = 1; i < n; i++) A[i] = A[i - 1] + n;
3017: for (j = 1; j < p; j++) {
3018: x = gllnodes[j];
3019: z0 = 1.;
3020: z1 = x;
3021: for (nn = 1; nn < p; nn++) {
3022: z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3023: z0 = z1;
3024: z1 = z2;
3025: }
3026: Lpj = z2;
3027: for (r = 1; r < p; r++) {
3028: if (r == j) {
3029: A[j][j] = 2. / (3. * (1. - gllnodes[j] * gllnodes[j]) * Lpj * Lpj);
3030: } else {
3031: x = gllnodes[r];
3032: z0 = 1.;
3033: z1 = x;
3034: for (nn = 1; nn < p; nn++) {
3035: z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3036: z0 = z1;
3037: z1 = z2;
3038: }
3039: Lpr = z2;
3040: A[r][j] = 4. / (((PetscReal)p) * (((PetscReal)p) + 1.) * Lpj * Lpr * (gllnodes[j] - gllnodes[r]) * (gllnodes[j] - gllnodes[r]));
3041: }
3042: }
3043: }
3044: for (j = 1; j < p + 1; j++) {
3045: x = gllnodes[j];
3046: z0 = 1.;
3047: z1 = x;
3048: for (nn = 1; nn < p; nn++) {
3049: z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3050: z0 = z1;
3051: z1 = z2;
3052: }
3053: Lpj = z2;
3054: A[j][0] = 4. * PetscPowRealInt(-1., p) / (((PetscReal)p) * (((PetscReal)p) + 1.) * Lpj * (1. + gllnodes[j]) * (1. + gllnodes[j]));
3055: A[0][j] = A[j][0];
3056: }
3057: for (j = 0; j < p; j++) {
3058: x = gllnodes[j];
3059: z0 = 1.;
3060: z1 = x;
3061: for (nn = 1; nn < p; nn++) {
3062: z2 = x * z1 * (2. * ((PetscReal)nn) + 1.) / (((PetscReal)nn) + 1.) - z0 * (((PetscReal)nn) / (((PetscReal)nn) + 1.));
3063: z0 = z1;
3064: z1 = z2;
3065: }
3066: Lpj = z2;
3068: A[p][j] = 4. / (((PetscReal)p) * (((PetscReal)p) + 1.) * Lpj * (1. - gllnodes[j]) * (1. - gllnodes[j]));
3069: A[j][p] = A[p][j];
3070: }
3071: A[0][0] = 0.5 + (((PetscReal)p) * (((PetscReal)p) + 1.) - 2.) / 6.;
3072: A[p][p] = A[0][0];
3073: *AA = A;
3074: PetscFunctionReturn(PETSC_SUCCESS);
3075: }
3077: /*@C
3078: PetscGaussLobattoLegendreElementLaplacianDestroy - frees the Laplacian for a single 1d GLL element created with `PetscGaussLobattoLegendreElementLaplacianCreate()`
3080: Not Collective
3082: Input Parameters:
3083: + n - the number of GLL nodes
3084: . nodes - the GLL nodes, ignored
3085: . weights - the GLL weightss, ignored
3086: - AA - the stiffness element from `PetscGaussLobattoLegendreElementLaplacianCreate()`
3088: Level: beginner
3090: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`
3091: @*/
3092: PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3093: {
3094: PetscFunctionBegin;
3095: PetscCall(PetscFree((*AA)[0]));
3096: PetscCall(PetscFree(*AA));
3097: *AA = NULL;
3098: PetscFunctionReturn(PETSC_SUCCESS);
3099: }
3101: /*@C
3102: PetscGaussLobattoLegendreElementGradientCreate - computes the gradient for a single 1d GLL element
3104: Not Collective
3106: Input Parameters:
3107: + n - the number of GLL nodes
3108: . nodes - the GLL nodes, of length `n`
3109: - weights - the GLL weights, of length `n`
3111: Output Parameters:
3112: + AA - the stiffness element, of dimension `n` by `n`
3113: - AAT - the transpose of AA (pass in `NULL` if you do not need this array), of dimension `n` by `n`
3115: Level: beginner
3117: Notes:
3118: Destroy this with `PetscGaussLobattoLegendreElementGradientDestroy()`
3120: You can access entries in these arrays with AA[i][j] but in memory it is stored in contiguous memory, row-oriented
3122: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianDestroy()`, `PetscGaussLobattoLegendreElementGradientDestroy()`
3123: @*/
3124: PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA, PetscReal ***AAT)
3125: {
3126: PetscReal **A, **AT = NULL;
3127: const PetscReal *gllnodes = nodes;
3128: const PetscInt p = n - 1;
3129: PetscReal Li, Lj, d0;
3130: PetscInt i, j;
3132: PetscFunctionBegin;
3133: PetscCall(PetscMalloc1(n, &A));
3134: PetscCall(PetscMalloc1(n * n, &A[0]));
3135: for (i = 1; i < n; i++) A[i] = A[i - 1] + n;
3137: if (AAT) {
3138: PetscCall(PetscMalloc1(n, &AT));
3139: PetscCall(PetscMalloc1(n * n, &AT[0]));
3140: for (i = 1; i < n; i++) AT[i] = AT[i - 1] + n;
3141: }
3143: if (n == 1) A[0][0] = 0.;
3144: d0 = (PetscReal)p * ((PetscReal)p + 1.) / 4.;
3145: for (i = 0; i < n; i++) {
3146: for (j = 0; j < n; j++) {
3147: A[i][j] = 0.;
3148: PetscCall(PetscDTComputeJacobi(0., 0., p, gllnodes[i], &Li));
3149: PetscCall(PetscDTComputeJacobi(0., 0., p, gllnodes[j], &Lj));
3150: if (i != j) A[i][j] = Li / (Lj * (gllnodes[i] - gllnodes[j]));
3151: if ((j == i) && (i == 0)) A[i][j] = -d0;
3152: if (j == i && i == p) A[i][j] = d0;
3153: if (AT) AT[j][i] = A[i][j];
3154: }
3155: }
3156: if (AAT) *AAT = AT;
3157: *AA = A;
3158: PetscFunctionReturn(PETSC_SUCCESS);
3159: }
3161: /*@C
3162: PetscGaussLobattoLegendreElementGradientDestroy - frees the gradient for a single 1d GLL element obtained with `PetscGaussLobattoLegendreElementGradientCreate()`
3164: Not Collective
3166: Input Parameters:
3167: + n - the number of GLL nodes
3168: . nodes - the GLL nodes, ignored
3169: . weights - the GLL weights, ignored
3170: . AA - the stiffness element obtained with `PetscGaussLobattoLegendreElementGradientCreate()`
3171: - AAT - the transpose of the element obtained with `PetscGaussLobattoLegendreElementGradientCreate()`
3173: Level: beginner
3175: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionCreate()`
3176: @*/
3177: PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA, PetscReal ***AAT)
3178: {
3179: PetscFunctionBegin;
3180: PetscCall(PetscFree((*AA)[0]));
3181: PetscCall(PetscFree(*AA));
3182: *AA = NULL;
3183: if (AAT) {
3184: PetscCall(PetscFree((*AAT)[0]));
3185: PetscCall(PetscFree(*AAT));
3186: *AAT = NULL;
3187: }
3188: PetscFunctionReturn(PETSC_SUCCESS);
3189: }
3191: /*@C
3192: PetscGaussLobattoLegendreElementAdvectionCreate - computes the advection operator for a single 1d GLL element
3194: Not Collective
3196: Input Parameters:
3197: + n - the number of GLL nodes
3198: . nodes - the GLL nodes, of length `n`
3199: - weights - the GLL weights, of length `n`
3201: Output Parameter:
3202: . AA - the stiffness element, of dimension `n` by `n`
3204: Level: beginner
3206: Notes:
3207: Destroy this with `PetscGaussLobattoLegendreElementAdvectionDestroy()`
3209: This is the same as the Gradient operator multiplied by the diagonal mass matrix
3211: You can access entries in this array with AA[i][j] but in memory it is stored in contiguous memory, row-oriented
3213: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionDestroy()`
3214: @*/
3215: PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3216: {
3217: PetscReal **D;
3218: const PetscReal *gllweights = weights;
3219: const PetscInt glln = n;
3220: PetscInt j;
3222: PetscFunctionBegin;
3223: PetscCall(PetscGaussLobattoLegendreElementGradientCreate(n, nodes, weights, &D, NULL));
3224: for (PetscInt i = 0; i < glln; i++) {
3225: for (j = 0; j < glln; j++) D[i][j] = gllweights[i] * D[i][j];
3226: }
3227: *AA = D;
3228: PetscFunctionReturn(PETSC_SUCCESS);
3229: }
3231: /*@C
3232: PetscGaussLobattoLegendreElementAdvectionDestroy - frees the advection stiffness for a single 1d GLL element created with `PetscGaussLobattoLegendreElementAdvectionCreate()`
3234: Not Collective
3236: Input Parameters:
3237: + n - the number of GLL nodes
3238: . nodes - the GLL nodes, ignored
3239: . weights - the GLL weights, ignored
3240: - AA - advection obtained with `PetscGaussLobattoLegendreElementAdvectionCreate()`
3242: Level: beginner
3244: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementAdvectionCreate()`
3245: @*/
3246: PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt n, PetscReal nodes[], PetscReal weights[], PetscReal ***AA)
3247: {
3248: PetscFunctionBegin;
3249: PetscCall(PetscFree((*AA)[0]));
3250: PetscCall(PetscFree(*AA));
3251: *AA = NULL;
3252: PetscFunctionReturn(PETSC_SUCCESS);
3253: }
3255: /*@C
3256: PetscGaussLobattoLegendreElementMassCreate - Build the elemental mass matrix for a single 1D Gauss-Lobatto-Legendre (GLL) spectral element
3258: Not Collective; No Fortran Support
3260: Input Parameters:
3261: + n - number of GLL nodes
3262: . nodes - the GLL quadrature nodes
3263: - weights - the GLL quadrature weights
3265: Output Parameter:
3266: . AA - newly allocated `n` x `n` mass matrix as `PetscReal **`
3268: Level: beginner
3270: Note:
3271: Free with `PetscGaussLobattoLegendreElementMassDestroy()`.
3273: .seealso: `PetscDTGaussLobattoLegendreQuadrature()`, `PetscGaussLobattoLegendreElementMassDestroy()`, `PetscGaussLobattoLegendreElementLaplacianCreate()`, `PetscGaussLobattoLegendreElementAdvectionCreate()`
3274: @*/
3275: PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt n, PetscReal *nodes, PetscReal *weights, PetscReal ***AA)
3276: {
3277: PetscReal **A;
3278: const PetscReal *gllweights = weights;
3279: const PetscInt glln = n;
3281: PetscFunctionBegin;
3282: PetscCall(PetscMalloc1(glln, &A));
3283: PetscCall(PetscMalloc1(glln * glln, &A[0]));
3284: for (PetscInt i = 1; i < glln; i++) A[i] = A[i - 1] + glln;
3285: if (glln == 1) A[0][0] = 0.;
3286: for (PetscInt i = 0; i < glln; i++) {
3287: for (PetscInt j = 0; j < glln; j++) {
3288: A[i][j] = 0.;
3289: if (j == i) A[i][j] = gllweights[i];
3290: }
3291: }
3292: *AA = A;
3293: PetscFunctionReturn(PETSC_SUCCESS);
3294: }
3296: /*@C
3297: PetscGaussLobattoLegendreElementMassDestroy - Free a 1D GLL elemental mass matrix created with `PetscGaussLobattoLegendreElementMassCreate()`
3299: Not Collective; No Fortran Support
3301: Input Parameters:
3302: + n - number of GLL nodes (ignored)
3303: . nodes - the GLL quadrature nodes (ignored)
3304: . weights - the GLL quadrature weights (ignored)
3305: - AA - the mass matrix to free; `*AA` is set to `NULL` on return
3307: Level: beginner
3309: .seealso: `PetscGaussLobattoLegendreElementMassCreate()`, `PetscDTGaussLobattoLegendreQuadrature()`
3310: @*/
3311: PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt n, PetscReal *nodes, PetscReal *weights, PetscReal ***AA)
3312: {
3313: PetscFunctionBegin;
3314: PetscCall(PetscFree((*AA)[0]));
3315: PetscCall(PetscFree(*AA));
3316: *AA = NULL;
3317: PetscFunctionReturn(PETSC_SUCCESS);
3318: }
3320: /*@
3321: PetscDTIndexToBary - convert an index into a barycentric coordinate.
3323: Input Parameters:
3324: + 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)
3325: . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to
3326: - index - the index to convert: should be >= 0 and < Binomial(len - 1 + sum, sum)
3328: Output Parameter:
3329: . coord - will be filled with the barycentric coordinate, of length `n`
3331: Level: beginner
3333: Note:
3334: The indices map to barycentric coordinates in lexicographic order, where the first index is the
3335: least significant and the last index is the most significant.
3337: .seealso: `PetscDTBaryToIndex()`
3338: @*/
3339: PetscErrorCode PetscDTIndexToBary(PetscInt len, PetscInt sum, PetscInt index, PetscInt coord[])
3340: {
3341: PetscInt c, d, s, total, subtotal, nexttotal;
3343: PetscFunctionBeginHot;
3344: PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
3345: PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index must be non-negative");
3346: if (!len) {
3347: PetscCheck(!sum && !index, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate");
3348: PetscFunctionReturn(PETSC_SUCCESS);
3349: }
3350: for (c = 1, total = 1; c <= len; c++) {
3351: /* total is the number of ways to have a tuple of length c with sum */
3352: if (index < total) break;
3353: total = (total * (sum + c)) / c;
3354: }
3355: PetscCheck(c <= len, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "index out of range");
3356: for (d = c; d < len; d++) coord[d] = 0;
3357: for (s = 0, subtotal = 1, nexttotal = 1; c > 0;) {
3358: /* subtotal is the number of ways to have a tuple of length c with sum s */
3359: /* nexttotal is the number of ways to have a tuple of length c-1 with sum s */
3360: if ((index + subtotal) >= total) {
3361: coord[--c] = sum - s;
3362: index -= (total - subtotal);
3363: sum = s;
3364: total = nexttotal;
3365: subtotal = 1;
3366: nexttotal = 1;
3367: s = 0;
3368: } else {
3369: subtotal = (subtotal * (c + s)) / (s + 1);
3370: nexttotal = (nexttotal * (c - 1 + s)) / (s + 1);
3371: s++;
3372: }
3373: }
3374: PetscFunctionReturn(PETSC_SUCCESS);
3375: }
3377: /*@
3378: PetscDTBaryToIndex - convert a barycentric coordinate to an index
3380: Input Parameters:
3381: + 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)
3382: . sum - the value that the sum of the barycentric coordinates (which will be non-negative integers) should sum to
3383: - coord - a barycentric coordinate with the given length `len` and `sum`
3385: Output Parameter:
3386: . index - the unique index for the coordinate, >= 0 and < Binomial(len - 1 + sum, sum)
3388: Level: beginner
3390: Note:
3391: The indices map to barycentric coordinates in lexicographic order, where the first index is the
3392: least significant and the last index is the most significant.
3394: .seealso: `PetscDTIndexToBary`
3395: @*/
3396: PetscErrorCode PetscDTBaryToIndex(PetscInt len, PetscInt sum, const PetscInt coord[], PetscInt *index)
3397: {
3398: PetscInt c;
3399: PetscInt i;
3400: PetscInt total;
3402: PetscFunctionBeginHot;
3403: PetscCheck(len >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "length must be non-negative");
3404: if (!len) {
3405: PetscCheck(!sum, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid index or sum for length 0 barycentric coordinate");
3406: *index = 0;
3407: PetscFunctionReturn(PETSC_SUCCESS);
3408: }
3409: for (c = 1, total = 1; c < len; c++) total = (total * (sum + c)) / c;
3410: i = total - 1;
3411: c = len - 1;
3412: sum -= coord[c];
3413: while (sum > 0) {
3414: PetscInt subtotal;
3415: PetscInt s;
3417: for (s = 1, subtotal = 1; s < sum; s++) subtotal = (subtotal * (c + s)) / s;
3418: i -= subtotal;
3419: sum -= coord[--c];
3420: }
3421: *index = i;
3422: PetscFunctionReturn(PETSC_SUCCESS);
3423: }
3425: /*@
3426: PetscQuadratureComputePermutations - Compute permutations of quadrature points corresponding to domain orientations
3428: Input Parameter:
3429: . quad - The `PetscQuadrature`
3431: Output Parameters:
3432: + Np - The number of domain orientations
3433: - perm - An array of `IS` permutations, one for ech orientation,
3435: Level: developer
3437: .seealso: `PetscQuadratureSetCellType()`, `PetscQuadrature`
3438: @*/
3439: PetscErrorCode PetscQuadratureComputePermutations(PetscQuadrature quad, PeOp PetscInt *Np, IS *perm[])
3440: {
3441: DMPolytopeType ct;
3442: const PetscReal *xq, *wq;
3443: PetscInt dim, qdim, d, Na, o, Nq, q, qp;
3445: PetscFunctionBegin;
3446: PetscCall(PetscQuadratureGetData(quad, &qdim, NULL, &Nq, &xq, &wq));
3447: PetscCall(PetscQuadratureGetCellType(quad, &ct));
3448: dim = DMPolytopeTypeGetDim(ct);
3449: Na = DMPolytopeTypeGetNumArrangements(ct);
3450: PetscCall(PetscMalloc1(Na, perm));
3451: if (Np) *Np = Na;
3452: Na /= 2;
3453: for (o = -Na; o < Na; ++o) {
3454: DM refdm;
3455: PetscInt *idx;
3456: PetscReal xi0[3] = {-1., -1., -1.}, v0[3], J[9], detJ, txq[3];
3457: PetscBool flg;
3459: PetscCall(DMPlexCreateReferenceCell(PETSC_COMM_SELF, ct, &refdm));
3460: PetscCall(DMPlexOrientPoint(refdm, 0, o));
3461: PetscCall(DMPlexComputeCellGeometryFEM(refdm, 0, NULL, v0, J, NULL, &detJ));
3462: PetscCall(PetscMalloc1(Nq, &idx));
3463: for (q = 0; q < Nq; ++q) {
3464: CoordinatesRefToReal(dim, dim, xi0, v0, J, &xq[q * dim], txq);
3465: for (qp = 0; qp < Nq; ++qp) {
3466: PetscReal diff = 0.;
3468: for (d = 0; d < dim; ++d) diff += PetscAbsReal(txq[d] - xq[qp * dim + d]);
3469: if (diff < PETSC_SMALL) break;
3470: }
3471: PetscCheck(qp < Nq, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Transformed quad point %" PetscInt_FMT " does not match another quad point", q);
3472: idx[q] = qp;
3473: }
3474: PetscCall(DMDestroy(&refdm));
3475: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, Nq, idx, PETSC_OWN_POINTER, &(*perm)[o + Na]));
3476: PetscCall(ISGetInfo((*perm)[o + Na], IS_PERMUTATION, IS_LOCAL, PETSC_TRUE, &flg));
3477: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ordering for orientation %" PetscInt_FMT " was not a permutation", o);
3478: PetscCall(ISSetPermutation((*perm)[o + Na]));
3479: }
3480: if (!Na) (*perm)[0] = NULL;
3481: PetscFunctionReturn(PETSC_SUCCESS);
3482: }
3484: /*@
3485: PetscDTCreateQuadratureByCell - Create default quadrature for a given cell
3487: Not collective
3489: Input Parameters:
3490: + ct - The integration domain
3491: . qorder - The desired quadrature order
3492: - qtype - The type of simplex quadrature, or PETSCDTSIMPLEXQUAD_DEFAULT
3494: Output Parameters:
3495: + q - The cell quadrature
3496: - fq - The face quadrature
3498: Level: developer
3500: .seealso: `PetscDTCreateDefaultQuadrature()`, `PetscFECreateDefault()`, `PetscDTGaussTensorQuadrature()`, `PetscDTSimplexQuadrature()`, `PetscDTTensorQuadratureCreate()`
3501: @*/
3502: PetscErrorCode PetscDTCreateQuadratureByCell(DMPolytopeType ct, PetscInt qorder, PetscDTSimplexQuadratureType qtype, PetscQuadrature *q, PetscQuadrature *fq)
3503: {
3504: const PetscInt quadPointsPerEdge = PetscMax(qorder + 1, 1);
3505: const PetscInt dim = DMPolytopeTypeGetDim(ct);
3507: PetscFunctionBegin;
3508: switch (ct) {
3509: case DM_POLYTOPE_POINT: {
3510: PetscReal *x, *w;
3512: PetscCall(PetscQuadratureCreate(PETSC_COMM_SELF, q));
3513: PetscCall(PetscQuadratureSetCellType(*q, ct));
3514: PetscCall(PetscQuadratureSetOrder(*q, 0));
3515: PetscCall(PetscMalloc1(1, &x));
3516: PetscCall(PetscMalloc1(1, &w));
3517: x[0] = 0.;
3518: w[0] = 1.;
3519: PetscCall(PetscQuadratureSetData(*q, dim, 1, 1, x, w));
3520: *fq = NULL;
3521: } break;
3522: case DM_POLYTOPE_SEGMENT:
3523: case DM_POLYTOPE_POINT_PRISM_TENSOR:
3524: case DM_POLYTOPE_QUADRILATERAL:
3525: case DM_POLYTOPE_SEG_PRISM_TENSOR:
3526: case DM_POLYTOPE_HEXAHEDRON:
3527: case DM_POLYTOPE_QUAD_PRISM_TENSOR:
3528: PetscCall(PetscDTGaussTensorQuadrature(dim, 1, quadPointsPerEdge, -1.0, 1.0, q));
3529: PetscCall(PetscDTGaussTensorQuadrature(dim - 1, 1, quadPointsPerEdge, -1.0, 1.0, fq));
3530: break;
3531: case DM_POLYTOPE_TRIANGLE:
3532: case DM_POLYTOPE_TETRAHEDRON:
3533: PetscCall(PetscDTSimplexQuadrature(dim, 2 * qorder, qtype, q));
3534: PetscCall(PetscDTSimplexQuadrature(dim - 1, 2 * qorder, qtype, fq));
3535: break;
3536: case DM_POLYTOPE_TRI_PRISM:
3537: case DM_POLYTOPE_TRI_PRISM_TENSOR: {
3538: PetscQuadrature q1, q2;
3540: // TODO: this should be able to use symmetric rules, but doing so causes tests to fail
3541: PetscCall(PetscDTSimplexQuadrature(2, 2 * qorder, PETSCDTSIMPLEXQUAD_CONIC, &q1));
3542: PetscCall(PetscDTGaussTensorQuadrature(1, 1, quadPointsPerEdge, -1.0, 1.0, &q2));
3543: PetscCall(PetscDTTensorQuadratureCreate(q1, q2, q));
3544: PetscCall(PetscQuadratureDestroy(&q2));
3545: *fq = q1;
3546: /* TODO Need separate quadratures for each face */
3547: } break;
3548: default:
3549: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No quadrature for celltype %s", DMPolytopeTypes[PetscMin(ct, DM_POLYTOPE_UNKNOWN)]);
3550: }
3551: PetscFunctionReturn(PETSC_SUCCESS);
3552: }
3554: /*@
3555: PetscDTCreateDefaultQuadrature - Create default quadrature for a given cell
3557: Not collective
3559: Input Parameters:
3560: + ct - The integration domain
3561: - qorder - The desired quadrature order
3563: Output Parameters:
3564: + q - The cell quadrature
3565: - fq - The face quadrature
3567: Level: developer
3569: .seealso: `PetscDTCreateQuadratureByCell()`, `PetscFECreateDefault()`, `PetscDTGaussTensorQuadrature()`, `PetscDTSimplexQuadrature()`, `PetscDTTensorQuadratureCreate()`
3570: @*/
3571: PetscErrorCode PetscDTCreateDefaultQuadrature(DMPolytopeType ct, PetscInt qorder, PetscQuadrature *q, PetscQuadrature *fq)
3572: {
3573: PetscFunctionBegin;
3574: PetscCall(PetscDTCreateQuadratureByCell(ct, qorder, PETSCDTSIMPLEXQUAD_DEFAULT, q, fq));
3575: PetscFunctionReturn(PETSC_SUCCESS);
3576: }