Actual source code: petscdt.h

  1: /*
  2:   Common tools for constructing discretizations
  3: */
  4: #pragma once

  6: #include <petscsys.h>
  7: #include <petscdmtypes.h>
  8: #include <petscistypes.h>

 10: /* MANSEC = DM */
 11: /* SUBMANSEC = DT */

 13: PETSC_EXTERN PetscClassId PETSCQUADRATURE_CLASSID;

 15: /*S
 16:   PetscQuadrature - Quadrature rule for numerical integration.

 18:   Level: beginner

 20: .seealso: `PetscQuadratureCreate()`, `PetscQuadratureDestroy()`
 21: S*/
 22: typedef struct _p_PetscQuadrature *PetscQuadrature;

 24: /*E
 25:   PetscGaussLobattoLegendreCreateType - algorithm used to compute the Gauss-Lobatto-Legendre nodes and weights

 27:   Values:
 28: +  `PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA` - compute the nodes via linear algebra
 29: -  `PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON`         - compute the nodes by solving a nonlinear equation with Newton's method

 31:   Level: intermediate

 33: .seealso: `PetscQuadrature`
 34: E*/
 35: typedef enum {
 36:   PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA,
 37:   PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON
 38: } PetscGaussLobattoLegendreCreateType;

 40: /*E
 41:   PetscDTNodeType - A description of strategies for generating nodes (both
 42:   quadrature nodes and nodes for Lagrange polynomials)

 44:   Values:
 45: + `PETSCDTNODES_DEFAULT`     - Nodes chosen by PETSc
 46: . `PETSCDTNODES_GAUSSJACOBI` - Nodes at either Gauss-Jacobi or Gauss-Lobatto-Jacobi quadrature points
 47: . `PETSCDTNODES_EQUISPACED`  - Nodes equispaced either including the endpoints or excluding them
 48: - `PETSCDTNODES_TANHSINH`    - Nodes at Tanh-Sinh quadrature points

 50:   Level: intermediate

 52:   Note:
 53:   A `PetscDTNodeType` can be paired with a `PetscBool` to indicate whether
 54:   the nodes include endpoints or not, and in the case of `PETSCDT_GAUSSJACOBI`
 55:   with exponents for the weight function.

 57: .seealso: `PetscQuadrature`
 58: E*/
 59: typedef enum {
 60:   PETSCDTNODES_DEFAULT     = -1,
 61:   PETSCDTNODES_GAUSSJACOBI = 0,
 62:   PETSCDTNODES_EQUISPACED  = 1,
 63:   PETSCDTNODES_TANHSINH    = 2
 64: } PetscDTNodeType;

 66: PETSC_EXTERN const char *const *const PetscDTNodeTypes;

 68: /*E
 69:   PetscDTSimplexQuadratureType - A description of classes of quadrature rules for simplices

 71:   Values:
 72: +  `PETSCDTSIMPLEXQUAD_DEFAULT` - Quadrature rule chosen by PETSc
 73: .  `PETSCDTSIMPLEXQUAD_CONIC`   - Quadrature rules constructed as
 74:                                   conically-warped tensor products of 1D
 75:                                   Gauss-Jacobi quadrature rules.  These are
 76:                                   explicitly computable in any dimension for any
 77:                                   degree, and the tensor-product structure can be
 78:                                   exploited by sum-factorization methods, but
 79:                                   they are not efficient in terms of nodes per
 80:                                   polynomial degree.
 81: -  `PETSCDTSIMPLEXQUAD_MINSYM`  - Quadrature rules that are fully symmetric
 82:                                   (symmetries of the simplex preserve the nodes
 83:                                   and weights) with minimal (or near minimal)
 84:                                   number of nodes.  In dimensions higher than 1
 85:                                   these are not simple to compute, so lookup
 86:                                   tables are used.

 88:   Level: intermediate

 90: .seealso: `PetscQuadrature`, `PetscDTSimplexQuadrature()`
 91: E*/
 92: typedef enum {
 93:   PETSCDTSIMPLEXQUAD_DEFAULT = -1,
 94:   PETSCDTSIMPLEXQUAD_CONIC   = 0,
 95:   PETSCDTSIMPLEXQUAD_MINSYM  = 1
 96: } PetscDTSimplexQuadratureType;

 98: PETSC_EXTERN const char *const *const PetscDTSimplexQuadratureTypes;

100: PETSC_EXTERN PetscErrorCode PetscQuadratureCreate(MPI_Comm, PetscQuadrature *);
101: PETSC_EXTERN PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature, PetscQuadrature *);
102: PETSC_EXTERN PetscErrorCode PetscQuadratureGetCellType(PetscQuadrature, DMPolytopeType *);
103: PETSC_EXTERN PetscErrorCode PetscQuadratureSetCellType(PetscQuadrature, DMPolytopeType);
104: PETSC_EXTERN PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature, PetscInt *);
105: PETSC_EXTERN PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature, PetscInt);
106: PETSC_EXTERN PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature, PetscInt *);
107: PETSC_EXTERN PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature, PetscInt);
108: PETSC_EXTERN PetscErrorCode PetscQuadratureEqual(PetscQuadrature, PetscQuadrature, PetscBool *);
109: PETSC_EXTERN PetscErrorCode PetscQuadratureGetData(PetscQuadrature, PetscInt *, PetscInt *, PetscInt *, const PetscReal *[], const PetscReal *[]);
110: PETSC_EXTERN PetscErrorCode PetscQuadratureSetData(PetscQuadrature, PetscInt, PetscInt, PetscInt, const PetscReal[], const PetscReal[]);
111: PETSC_EXTERN PetscErrorCode PetscQuadratureView(PetscQuadrature, PetscViewer);
112: PETSC_EXTERN PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *);

114: PETSC_EXTERN PetscErrorCode PetscDTTensorQuadratureCreate(PetscQuadrature, PetscQuadrature, PetscQuadrature *);
115: PETSC_EXTERN PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature, PetscInt, const PetscReal[], const PetscReal[], PetscQuadrature *);
116: PETSC_EXTERN PetscErrorCode PetscQuadratureComputePermutations(PetscQuadrature, PetscInt *, IS *[]);
117: PETSC_EXTERN PetscErrorCode PetscQuadratureGetPermutations(PetscQuadrature, PetscInt *, IS *[]);

119: PETSC_EXTERN PetscErrorCode PetscQuadraturePushForward(PetscQuadrature, PetscInt, const PetscReal[], const PetscReal[], const PetscReal[], PetscInt, PetscQuadrature *);

121: PETSC_EXTERN PetscErrorCode PetscDTLegendreEval(PetscInt, const PetscReal *, PetscInt, const PetscInt *, PetscReal *, PetscReal *, PetscReal *);
122: PETSC_EXTERN PetscErrorCode PetscDTJacobiNorm(PetscReal, PetscReal, PetscInt, PetscReal *);
123: PETSC_EXTERN PetscErrorCode PetscDTJacobiEval(PetscInt, PetscReal, PetscReal, const PetscReal *, PetscInt, const PetscInt *, PetscReal *, PetscReal *, PetscReal *);
124: PETSC_EXTERN PetscErrorCode PetscDTJacobiEvalJet(PetscReal, PetscReal, PetscInt, const PetscReal[], PetscInt, PetscInt, PetscReal[]);
125: PETSC_EXTERN PetscErrorCode PetscDTPKDEvalJet(PetscInt, PetscInt, const PetscReal[], PetscInt, PetscInt, PetscReal[]);
126: PETSC_EXTERN PetscErrorCode PetscDTPTrimmedSize(PetscInt, PetscInt, PetscInt, PetscInt *);
127: PETSC_EXTERN PetscErrorCode PetscDTPTrimmedEvalJet(PetscInt, PetscInt, const PetscReal[], PetscInt, PetscInt, PetscInt, PetscReal[]);
128: PETSC_EXTERN PetscErrorCode PetscDTGaussQuadrature(PetscInt, PetscReal, PetscReal, PetscReal *, PetscReal *);
129: PETSC_EXTERN PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt, PetscReal, PetscReal, PetscReal, PetscReal, PetscReal *, PetscReal *);
130: PETSC_EXTERN PetscErrorCode PetscDTGaussLobattoJacobiQuadrature(PetscInt, PetscReal, PetscReal, PetscReal, PetscReal, PetscReal *, PetscReal *);
131: PETSC_EXTERN PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt, PetscGaussLobattoLegendreCreateType, PetscReal *, PetscReal *);
132: PETSC_EXTERN PetscErrorCode PetscDTReconstructPoly(PetscInt, PetscInt, const PetscReal *, PetscInt, const PetscReal *, PetscReal *);
133: PETSC_EXTERN PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt, PetscInt, PetscInt, PetscReal, PetscReal, PetscQuadrature *);
134: PETSC_EXTERN PetscErrorCode PetscDTStroudConicalQuadrature(PetscInt, PetscInt, PetscInt, PetscReal, PetscReal, PetscQuadrature *);
135: PETSC_EXTERN PetscErrorCode PetscDTSimplexQuadrature(PetscInt, PetscInt, PetscDTSimplexQuadratureType, PetscQuadrature *);
136: PETSC_EXTERN PetscErrorCode PetscDTCreateDefaultQuadrature(DMPolytopeType, PetscInt, PetscQuadrature *, PetscQuadrature *);
137: PETSC_EXTERN PetscErrorCode PetscDTCreateQuadratureByCell(DMPolytopeType, PetscInt, PetscDTSimplexQuadratureType, PetscQuadrature *, PetscQuadrature *);

139: PETSC_EXTERN PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt, PetscInt, PetscReal, PetscReal, PetscQuadrature *);
140: PETSC_EXTERN PetscErrorCode PetscDTTanhSinhIntegrate(void (*)(const PetscReal[], PetscCtx, PetscReal *), PetscReal, PetscReal, PetscInt, void *, PetscReal *);
141: PETSC_EXTERN PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*)(const PetscReal[], PetscCtx, PetscReal *), PetscReal, PetscReal, PetscInt, void *, PetscReal *);

143: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt, PetscReal *, PetscReal *, const PetscReal *, PetscReal *);
144: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
145: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
146: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***, PetscReal ***);
147: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***, PetscReal ***);
148: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
149: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
150: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
151: PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***);

153: /*MC
154:   PETSC_FORM_DEGREE_UNDEFINED - Indicates that a field does not have
155:   a well-defined form degree in exterior calculus.

157:   Level: advanced

159: .seealso: `PetscDTAltV`, `PetscDualSpaceGetFormDegree()`
160: M*/
161: #define PETSC_FORM_DEGREE_UNDEFINED PETSC_INT_MIN

163: PETSC_EXTERN PetscErrorCode PetscDTAltVApply(PetscInt, PetscInt, const PetscReal *, const PetscReal *, PetscReal *);
164: PETSC_EXTERN PetscErrorCode PetscDTAltVWedge(PetscInt, PetscInt, PetscInt, const PetscReal *, const PetscReal *, PetscReal *);
165: PETSC_EXTERN PetscErrorCode PetscDTAltVWedgeMatrix(PetscInt, PetscInt, PetscInt, const PetscReal *, PetscReal *);
166: PETSC_EXTERN PetscErrorCode PetscDTAltVPullback(PetscInt, PetscInt, const PetscReal *, PetscInt, const PetscReal *, PetscReal *);
167: PETSC_EXTERN PetscErrorCode PetscDTAltVPullbackMatrix(PetscInt, PetscInt, const PetscReal *, PetscInt, PetscReal *);
168: PETSC_EXTERN PetscErrorCode PetscDTAltVInterior(PetscInt, PetscInt, const PetscReal *, const PetscReal *, PetscReal *);
169: PETSC_EXTERN PetscErrorCode PetscDTAltVInteriorMatrix(PetscInt, PetscInt, const PetscReal *, PetscReal *);
170: PETSC_EXTERN PetscErrorCode PetscDTAltVInteriorPattern(PetscInt, PetscInt, PetscInt (*)[3]);
171: PETSC_EXTERN PetscErrorCode PetscDTAltVStar(PetscInt, PetscInt, PetscInt, const PetscReal *, PetscReal *);

173: PETSC_EXTERN PetscErrorCode PetscDTBaryToIndex(PetscInt, PetscInt, const PetscInt[], PetscInt *);
174: PETSC_EXTERN PetscErrorCode PetscDTIndexToBary(PetscInt, PetscInt, PetscInt, PetscInt[]);
175: PETSC_EXTERN PetscErrorCode PetscDTGradedOrderToIndex(PetscInt, const PetscInt[], PetscInt *);
176: PETSC_EXTERN PetscErrorCode PetscDTIndexToGradedOrder(PetscInt, PetscInt, PetscInt[]);

178: #if PetscDefined(USE_64BIT_INDICES)
179:   #define PETSC_FACTORIAL_MAX 20
180:   #define PETSC_BINOMIAL_MAX  61
181: #else
182:   #define PETSC_FACTORIAL_MAX 12
183:   #define PETSC_BINOMIAL_MAX  29
184: #endif

186: /*MC
187:   PetscDTFactorial - Approximate n! as a real number

189:   Not Collective

191:   Input Parameter:
192: . n - a non-negative integer

194:   Output Parameter:
195: . factorial - n!

197:   Level: beginner

199: .seealso: `PetscDTFactorialInt()`, `PetscDTBinomialInt()`, `PetscDTBinomial()`
200: M*/
201: static inline PetscErrorCode PetscDTFactorial(PetscInt n, PetscReal *factorial)
202: {
203:   PetscReal f = 1.0;

205:   PetscFunctionBegin;
206:   *factorial = -1.0;
207:   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Factorial called with negative number %" PetscInt_FMT, n);
208:   for (PetscInt i = 1; i < n + 1; ++i) f *= (PetscReal)i;
209:   *factorial = f;
210:   PetscFunctionReturn(PETSC_SUCCESS);
211: }

213: /*MC
214:   PetscDTFactorialInt - Compute n! as an integer

216:   Not Collective

218:   Input Parameter:
219: . n - a non-negative integer

221:   Output Parameter:
222: . factorial - n!

224:   Level: beginner

226:   Note:
227:   This is limited to `n` such that n! can be represented by `PetscInt`, which is 12 if `PetscInt` is a signed 32-bit integer and 20 if `PetscInt` is a signed 64-bit integer.

229: .seealso: `PetscDTFactorial()`, `PetscDTBinomialInt()`, `PetscDTBinomial()`
230: M*/
231: static inline PetscErrorCode PetscDTFactorialInt(PetscInt n, PetscInt *factorial)
232: {
233:   PetscInt facLookup[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};

235:   PetscFunctionBegin;
236:   *factorial = -1;
237:   PetscCheck(n >= 0 && n <= PETSC_FACTORIAL_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of elements %" PetscInt_FMT " is not in supported range [0,%d]", n, PETSC_FACTORIAL_MAX);
238:   if (n <= 12) {
239:     *factorial = facLookup[n];
240:   } else {
241:     PetscInt f = facLookup[12];
242:     PetscInt i;

244:     for (i = 13; i < n + 1; ++i) f *= i;
245:     *factorial = f;
246:   }
247:   PetscFunctionReturn(PETSC_SUCCESS);
248: }

250: /*MC
251:   PetscDTBinomial - Approximate the binomial coefficient `n` choose `k`

253:   Not Collective

255:   Input Parameters:
256: + n - a non-negative integer
257: - k - an integer between 0 and `n`, inclusive

259:   Output Parameter:
260: . binomial - approximation of the binomial coefficient `n` choose `k`

262:   Level: beginner

264: .seealso: `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomialInt()`, `PetscDTEnumPerm()`
265: M*/
266: static inline PetscErrorCode PetscDTBinomial(PetscInt n, PetscInt k, PetscReal *binomial)
267: {
268:   PetscFunctionBeginHot;
269:   *binomial = -1.0;
270:   PetscCheck(n >= 0 && k >= 0 && k <= n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Binomial arguments (%" PetscInt_FMT " %" PetscInt_FMT ") must be non-negative, k <= n", n, k);
271:   if (n <= 3) {
272:     PetscInt binomLookup[4][4] = {
273:       {1, 0, 0, 0},
274:       {1, 1, 0, 0},
275:       {1, 2, 1, 0},
276:       {1, 3, 3, 1}
277:     };

279:     *binomial = (PetscReal)binomLookup[n][k];
280:   } else {
281:     PetscReal binom = 1.0;

283:     k = PetscMin(k, n - k);
284:     for (PetscInt i = 0; i < k; i++) binom = (binom * (PetscReal)(n - i)) / (PetscReal)(i + 1);
285:     *binomial = binom;
286:   }
287:   PetscFunctionReturn(PETSC_SUCCESS);
288: }

290: /*MC
291:   PetscDTBinomialInt - Compute the binomial coefficient `n` choose `k`

293:   Not Collective

295:   Input Parameters:
296: + n - a non-negative integer
297: - k - an integer between 0 and `n`, inclusive

299:   Output Parameter:
300: . binomial - the binomial coefficient `n` choose `k`

302:   Level: beginner

304:   Note:
305:   This is limited by integers that can be represented by `PetscInt`.

307:   Use `PetscDTBinomial()` for real number approximations of larger values

309: .seealso: `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomial()`, `PetscDTEnumPerm()`
310: M*/
311: static inline PetscErrorCode PetscDTBinomialInt(PetscInt n, PetscInt k, PetscInt *binomial)
312: {
313:   PetscInt bin;

315:   PetscFunctionBegin;
316:   *binomial = -1;
317:   PetscCheck(n >= 0 && k >= 0 && k <= n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Binomial arguments (%" PetscInt_FMT " %" PetscInt_FMT ") must be non-negative, k <= n", n, k);
318:   PetscCheck(n <= PETSC_BINOMIAL_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Binomial elements %" PetscInt_FMT " is larger than max for PetscInt, %d", n, PETSC_BINOMIAL_MAX);
319:   if (n <= 3) {
320:     PetscInt binomLookup[4][4] = {
321:       {1, 0, 0, 0},
322:       {1, 1, 0, 0},
323:       {1, 2, 1, 0},
324:       {1, 3, 3, 1}
325:     };

327:     bin = binomLookup[n][k];
328:   } else {
329:     PetscInt binom = 1;

331:     k = PetscMin(k, n - k);
332:     for (PetscInt i = 0; i < k; i++) binom = (binom * (n - i)) / (i + 1);
333:     bin = binom;
334:   }
335:   *binomial = bin;
336:   PetscFunctionReturn(PETSC_SUCCESS);
337: }

339: /* the following inline routines should be not be inline routines and then Fortran binding can be built automatically */
340: #define PeOp

342: /*MC
343:   PetscDTEnumPerm - Get a permutation of `n` integers from its encoding into the integers [0, n!) as a sequence of swaps.

345:   Not Collective

347:   Input Parameters:
348: + n - a non-negative integer (see note about limits below)
349: - k - an integer in [0, n!)

351:   Output Parameters:
352: + perm  - the permuted list of the integers [0, ..., n-1]
353: - isOdd - if not `NULL`, returns whether the permutation used an even or odd number of swaps.

355:   Level: intermediate

357:   Notes:
358:   A permutation can be described by the operations that convert the lists [0, 1, ..., n-1] into the permutation,
359:   by a sequence of swaps, where the ith step swaps whatever number is in ith position with a number that is in
360:   some position j >= i.  This swap is encoded as the difference (j - i).  The difference d_i at step i is less than
361:   (n - i).  This sequence of n-1 differences [d_0, ..., d_{n-2}] is encoded as the number
362:   (n-1)! * d_0 + (n-2)! * d_1 + ... + 1! * d_{n-2}.

364:   Limited to `n` such that `n`! can be represented by `PetscInt`, which is 12 if `PetscInt` is a signed 32-bit integer and 20 if `PetscInt` is a signed 64-bit integer.

366: .seealso: `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomial()`, `PetscDTBinomialInt()`, `PetscDTPermIndex()`
367: M*/
368: static inline PetscErrorCode PetscDTEnumPerm(PetscInt n, PetscInt k, PetscInt *perm, PeOp PetscBool *isOdd)
369: {
370:   PetscInt  odd = 0;
371:   PetscInt  i;
372:   PetscInt  work[PETSC_FACTORIAL_MAX];
373:   PetscInt *w;

375:   PetscFunctionBegin;
376:   if (isOdd) *isOdd = PETSC_FALSE;
377:   PetscCheck(n >= 0 && n <= PETSC_FACTORIAL_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of elements %" PetscInt_FMT " is not in supported range [0,%d]", n, PETSC_FACTORIAL_MAX);
378:   if (n >= 2) {
379:     w = &work[n - 2];
380:     for (i = 2; i <= n; i++) {
381:       *(w--) = k % i;
382:       k /= i;
383:     }
384:   }
385:   for (i = 0; i < n; i++) perm[i] = i;
386:   for (i = 0; i < n - 1; i++) {
387:     PetscInt s    = work[i];
388:     PetscInt swap = perm[i];

390:     perm[i]     = perm[i + s];
391:     perm[i + s] = swap;
392:     odd ^= (!!s);
393:   }
394:   if (isOdd) *isOdd = odd ? PETSC_TRUE : PETSC_FALSE;
395:   PetscFunctionReturn(PETSC_SUCCESS);
396: }

398: /*MC
399:   PetscDTPermIndex - Encode a permutation of n into an integer in [0, n!).  This inverts `PetscDTEnumPerm()`.

401:   Not Collective

403:   Input Parameters:
404: + n    - a non-negative integer (see note about limits below)
405: - perm - the permuted list of the integers [0, ..., n-1]

407:   Output Parameters:
408: + k     - an integer in [0, n!)
409: - isOdd - if not `NULL`, returns whether the permutation used an even or odd number of swaps.

411:   Level: beginner

413:   Note:
414:   Limited to `n` such that `n`! can be represented by `PetscInt`, which is 12 if `PetscInt` is a signed 32-bit integer and 20 if `PetscInt` is a signed 64-bit integer.

416: .seealso: `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomial()`, `PetscDTBinomialInt()`, `PetscDTEnumPerm()`
417: M*/
418: static inline PetscErrorCode PetscDTPermIndex(PetscInt n, const PetscInt *perm, PetscInt *k, PeOp PetscBool *isOdd)
419: {
420:   PetscInt odd = 0;
421:   PetscInt i, idx;
422:   PetscInt work[PETSC_FACTORIAL_MAX];
423:   PetscInt iwork[PETSC_FACTORIAL_MAX];

425:   PetscFunctionBeginHot;
426:   *k = -1;
427:   if (isOdd) *isOdd = PETSC_FALSE;
428:   PetscCheck(n >= 0 && n <= PETSC_FACTORIAL_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of elements %" PetscInt_FMT " is not in supported range [0,%d]", n, PETSC_FACTORIAL_MAX);
429:   for (i = 0; i < n; i++) work[i] = i;  /* partial permutation */
430:   for (i = 0; i < n; i++) iwork[i] = i; /* partial permutation inverse */
431:   for (idx = 0, i = 0; i < n - 1; i++) {
432:     PetscInt j    = perm[i];
433:     PetscInt icur = work[i];
434:     PetscInt jloc = iwork[j];
435:     PetscInt diff = jloc - i;

437:     idx = idx * (n - i) + diff;
438:     /* swap (i, jloc) */
439:     work[i]     = j;
440:     work[jloc]  = icur;
441:     iwork[j]    = i;
442:     iwork[icur] = jloc;
443:     odd ^= (!!diff);
444:   }
445:   *k = idx;
446:   if (isOdd) *isOdd = odd ? PETSC_TRUE : PETSC_FALSE;
447:   PetscFunctionReturn(PETSC_SUCCESS);
448: }

450: /*MC
451:   PetscDTEnumSubset - Get an ordered subset of the integers [0, ..., n - 1] from its encoding as an integers in [0, n choose k).
452:   The encoding is in lexicographic order.

454:   Not Collective

456:   Input Parameters:
457: + n - a non-negative integer (see note about limits below)
458: . k - an integer in [0, n]
459: - j - an index in [0, n choose k)

461:   Output Parameter:
462: . subset - the jth subset of size k of the integers [0, ..., n - 1]

464:   Level: beginner

466:   Note:
467:   Limited by arguments such that `n` choose `k` can be represented by `PetscInt`

469: .seealso: `PetscDTSubsetIndex()`, `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomial()`, `PetscDTBinomialInt()`, `PetscDTEnumPerm()`, `PetscDTPermIndex()`
470: M*/
471: static inline PetscErrorCode PetscDTEnumSubset(PetscInt n, PetscInt k, PetscInt j, PetscInt *subset)
472: {
473:   PetscInt Nk;

475:   PetscFunctionBeginHot;
476:   PetscCall(PetscDTBinomialInt(n, k, &Nk));
477:   for (PetscInt i = 0, l = 0; i < n && l < k; i++) {
478:     PetscInt Nminuskminus = (Nk * (k - l)) / (n - i);
479:     PetscInt Nminusk      = Nk - Nminuskminus;

481:     if (j < Nminuskminus) {
482:       subset[l++] = i;
483:       Nk          = Nminuskminus;
484:     } else {
485:       j -= Nminuskminus;
486:       Nk = Nminusk;
487:     }
488:   }
489:   PetscFunctionReturn(PETSC_SUCCESS);
490: }

492: /*MC
493:   PetscDTSubsetIndex - Convert an ordered subset of k integers from the set [0, ..., n - 1] to its encoding as an integers in [0, n choose k) in lexicographic order.
494:   This is the inverse of `PetscDTEnumSubset`.

496:   Not Collective

498:   Input Parameters:
499: + n      - a non-negative integer (see note about limits below)
500: . k      - an integer in [0, n]
501: - subset - an ordered subset of the integers [0, ..., n - 1]

503:   Output Parameter:
504: . index - the rank of the subset in lexicographic order

506:   Level: beginner

508:   Note:
509:   Limited by arguments such that `n` choose `k` can be represented by `PetscInt`

511: .seealso: `PetscDTEnumSubset()`, `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomial()`, `PetscDTBinomialInt()`, `PetscDTEnumPerm()`, `PetscDTPermIndex()`
512: M*/
513: static inline PetscErrorCode PetscDTSubsetIndex(PetscInt n, PetscInt k, const PetscInt *subset, PetscInt *index)
514: {
515:   PetscInt j = 0, Nk;

517:   PetscFunctionBegin;
518:   *index = -1;
519:   PetscCall(PetscDTBinomialInt(n, k, &Nk));
520:   for (PetscInt i = 0, l = 0; i < n && l < k; i++) {
521:     PetscInt Nminuskminus = (Nk * (k - l)) / (n - i);
522:     PetscInt Nminusk      = Nk - Nminuskminus;

524:     if (subset[l] == i) {
525:       l++;
526:       Nk = Nminuskminus;
527:     } else {
528:       j += Nminuskminus;
529:       Nk = Nminusk;
530:     }
531:   }
532:   *index = j;
533:   PetscFunctionReturn(PETSC_SUCCESS);
534: }

536: /*MC
537:   PetscDTEnumSplit - Split the integers [0, ..., n - 1] into two complementary ordered subsets, the first subset of size k and being the jth subset of that size in lexicographic order.

539:   Not Collective

541:   Input Parameters:
542: + n - a non-negative integer (see note about limits below)
543: . k - an integer in [0, n]
544: - j - an index in [0, n choose k)

546:   Output Parameters:
547: + perm  - the jth subset of size k of the integers [0, ..., n - 1], followed by its complementary set.
548: - isOdd - if not `NULL`, return whether perm is an even or odd permutation.

550:   Level: beginner

552:   Note:
553:   Limited by arguments such that `n` choose `k` can be represented by `PetscInt`

555: .seealso: `PetscDTEnumSubset()`, `PetscDTSubsetIndex()`, `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomial()`, `PetscDTBinomialInt()`, `PetscDTEnumPerm()`,
556:   `PetscDTPermIndex()`
557: M*/
558: static inline PetscErrorCode PetscDTEnumSplit(PetscInt n, PetscInt k, PetscInt j, PetscInt *perm, PeOp PetscBool *isOdd)
559: {
560:   PetscInt  i, l, m, Nk, odd = 0;
561:   PetscInt *subcomp = PetscSafePointerPlusOffset(perm, k);

563:   PetscFunctionBegin;
564:   if (isOdd) *isOdd = PETSC_FALSE;
565:   PetscCall(PetscDTBinomialInt(n, k, &Nk));
566:   for (i = 0, l = 0, m = 0; i < n && l < k; i++) {
567:     PetscInt Nminuskminus = (Nk * (k - l)) / (n - i);
568:     PetscInt Nminusk      = Nk - Nminuskminus;

570:     if (j < Nminuskminus) {
571:       perm[l++] = i;
572:       Nk        = Nminuskminus;
573:     } else {
574:       subcomp[m++] = i;
575:       j -= Nminuskminus;
576:       odd ^= ((k - l) & 1);
577:       Nk = Nminusk;
578:     }
579:   }
580:   for (; i < n; i++) subcomp[m++] = i;
581:   if (isOdd) *isOdd = odd ? PETSC_TRUE : PETSC_FALSE;
582:   PetscFunctionReturn(PETSC_SUCCESS);
583: }

585: struct _n_PetscTabulation {
586:   PetscInt    K;    /* Indicates a k-jet, namely tabulated derivatives up to order k */
587:   PetscInt    Nr;   /* The number of tabulation replicas (often 1) */
588:   PetscInt    Np;   /* The number of tabulation points in a replica */
589:   PetscInt    Nb;   /* The number of functions tabulated */
590:   PetscInt    Nc;   /* The number of function components */
591:   PetscInt    cdim; /* The coordinate dimension */
592:   PetscReal **T;    /* The tabulation T[K] of functions and their derivatives
593:                        T[0] = B[Nr*Np][Nb][Nc]:             The basis function values at quadrature points
594:                        T[1] = D[Nr*Np][Nb][Nc][cdim]:       The basis function derivatives at quadrature points
595:                        T[2] = H[Nr*Np][Nb][Nc][cdim][cdim]: The basis function second derivatives at quadrature points */
596: };

598: /*S
599:    PetscTabulation - PETSc object that manages tabulations for finite element methods.

601:    Level: intermediate

603:    Note:
604:    This is a pointer to a C struct, hence the data in it may be accessed directly.

606:    Fortran Note:
607:    Use `PetscTabulationGetData()` and `PetscTabulationRestoreData()` to access the arrays in the tabulation.

609:    Developer Note:
610:    TODO: put the meaning of the struct fields in this manual page

612: .seealso: `PetscTabulationDestroy()`, `PetscFECreateTabulation()`, `PetscFEGetCellTabulation()`
613: S*/
614: typedef struct _n_PetscTabulation *PetscTabulation;

616: /*S
617:   PetscProbFn - A prototype of a PDF or CDF used with PETSc probability operations whose names begin with `PetscProb` such as
618:   `PetscProbComputeKSStatistic()`.

620:   Calling Sequence:
621: + x      - input value
622: . scale  - scale factor, I don't know what this is for
623: - result - the value of the PDF or CDF at the input value

625:   Level: beginner

627:   Developer Note:
628:   Why does this take an array argument for `result` when it seems to be able to output a single value?

630: .seealso: `PetscProbComputeKSStatistic()`, `PetscProbComputeKSStatisticWeighted()`, `PetscPDFMaxwellBoltzmann1D()`
631: S*/
632: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode PetscProbFn(const PetscReal x[], const PetscReal scale[], PetscReal result[]);

634: PETSC_EXTERN_TYPEDEF typedef PetscProbFn *PetscProbFunc PETSC_DEPRECATED_TYPEDEF(3, 24, 0, "PetscProbFn*", );

636: /*E
637:    DTProbDensityType - Names of the built-in probability density functions that PETSc can sample, evaluate, or use to test a Kolmogorov--Smirnov statistic

639:    Values:
640: +   `DTPROB_DENSITY_CONSTANT`          - uniform density
641: .   `DTPROB_DENSITY_GAUSSIAN`          - Gaussian (normal) density
642: .   `DTPROB_DENSITY_MAXWELL_BOLTZMANN` - Maxwell--Boltzmann density (1D/2D/3D variants are provided)
643: -   `DTPROB_NUM_DENSITY`               - sentinel; equals the number of meaningful entries in this enumeration

645:    Level: intermediate

647: .seealso: `PetscPDFMaxwellBoltzmann1D()`, `PetscProbComputeKSStatistic()`, `PetscProbComputeKSStatisticWeighted()`
648: E*/
649: typedef enum {
650:   DTPROB_DENSITY_CONSTANT,
651:   DTPROB_DENSITY_GAUSSIAN,
652:   DTPROB_DENSITY_MAXWELL_BOLTZMANN,
653:   DTPROB_NUM_DENSITY
654: } DTProbDensityType;
655: PETSC_EXTERN const char *const DTProbDensityTypes[];

657: PETSC_EXTERN PetscProbFn    PetscPDFMaxwellBoltzmann1D;
658: PETSC_EXTERN PetscProbFn    PetscCDFMaxwellBoltzmann1D;
659: PETSC_EXTERN PetscProbFn    PetscPDFMaxwellBoltzmann2D;
660: PETSC_EXTERN PetscProbFn    PetscCDFMaxwellBoltzmann2D;
661: PETSC_EXTERN PetscProbFn    PetscPDFMaxwellBoltzmann3D;
662: PETSC_EXTERN PetscProbFn    PetscCDFMaxwellBoltzmann3D;
663: PETSC_EXTERN PetscProbFn    PetscPDFGaussian1D;
664: PETSC_EXTERN PetscProbFn    PetscCDFGaussian1D;
665: PETSC_EXTERN PetscProbFn    PetscPDFSampleGaussian1D;
666: PETSC_EXTERN PetscProbFn    PetscPDFGaussian2D;
667: PETSC_EXTERN PetscProbFn    PetscPDFSampleGaussian2D;
668: PETSC_EXTERN PetscProbFn    PetscPDFGaussian3D;
669: PETSC_EXTERN PetscProbFn    PetscPDFSampleGaussian3D;
670: PETSC_EXTERN PetscProbFn    PetscPDFConstant1D;
671: PETSC_EXTERN PetscProbFn    PetscCDFConstant1D;
672: PETSC_EXTERN PetscProbFn    PetscPDFSampleConstant1D;
673: PETSC_EXTERN PetscProbFn    PetscPDFConstant2D;
674: PETSC_EXTERN PetscProbFn    PetscCDFConstant2D;
675: PETSC_EXTERN PetscProbFn    PetscPDFSampleConstant2D;
676: PETSC_EXTERN PetscProbFn    PetscPDFConstant3D;
677: PETSC_EXTERN PetscProbFn    PetscCDFConstant3D;
678: PETSC_EXTERN PetscProbFn    PetscPDFSampleConstant3D;
679: PETSC_EXTERN PetscErrorCode PetscProbCreateFromOptions(PetscInt, const char[], const char[], PetscProbFn **, PetscProbFn **, PetscProbFn **);

681: #include <petscvec.h>

683: PETSC_EXTERN PetscErrorCode PetscProbComputeKSStatistic(Vec, PetscProbFn *, PetscReal *);
684: PETSC_EXTERN PetscErrorCode PetscProbComputeKSStatisticWeighted(Vec, Vec, PetscProbFn *, PetscReal *);
685: PETSC_EXTERN PetscErrorCode PetscProbComputeKSStatisticMagnitude(Vec, PetscProbFn *, PetscReal *);