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 *[]);

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

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

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

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

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

156:   Level: advanced

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

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

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

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

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

188:   Not Collective

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

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

196:   Level: beginner

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

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

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

215:   Not Collective

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

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

223:   Level: beginner

225:   Note:
226:   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.

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

234:   PetscFunctionBegin;
235:   *factorial = -1;
236:   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);
237:   if (n <= 12) {
238:     *factorial = facLookup[n];
239:   } else {
240:     PetscInt f = facLookup[12];
241:     PetscInt i;

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

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

252:   Not Collective

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

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

261:   Level: beginner

263: .seealso: `PetscDTFactorial()`, `PetscDTFactorialInt()`, `PetscDTBinomialInt()`, `PetscDTEnumPerm()`
264: M*/
265: static inline PetscErrorCode PetscDTBinomial(PetscInt n, PetscInt k, PetscReal *binomial)
266: {
267:   PetscFunctionBeginHot;
268:   *binomial = -1.0;
269:   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);
270:   if (n <= 3) {
271:     PetscInt binomLookup[4][4] = {
272:       {1, 0, 0, 0},
273:       {1, 1, 0, 0},
274:       {1, 2, 1, 0},
275:       {1, 3, 3, 1}
276:     };

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

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

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

292:   Not Collective

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

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

301:   Level: beginner

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

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

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

314:   PetscFunctionBegin;
315:   *binomial = -1;
316:   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);
317:   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);
318:   if (n <= 3) {
319:     PetscInt binomLookup[4][4] = {
320:       {1, 0, 0, 0},
321:       {1, 1, 0, 0},
322:       {1, 2, 1, 0},
323:       {1, 3, 3, 1}
324:     };

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

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

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

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

344:   Not Collective

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

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

354:   Level: intermediate

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

363:   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.

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

374:   PetscFunctionBegin;
375:   if (isOdd) *isOdd = PETSC_FALSE;
376:   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);
377:   if (n >= 2) {
378:     w = &work[n - 2];
379:     for (i = 2; i <= n; i++) {
380:       *(w--) = k % i;
381:       k /= i;
382:     }
383:   }
384:   for (i = 0; i < n; i++) perm[i] = i;
385:   for (i = 0; i < n - 1; i++) {
386:     PetscInt s    = work[i];
387:     PetscInt swap = perm[i];

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

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

400:   Not Collective

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

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

410:   Level: beginner

412:   Note:
413:   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.

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

424:   PetscFunctionBeginHot;
425:   *k = -1;
426:   if (isOdd) *isOdd = PETSC_FALSE;
427:   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);
428:   for (i = 0; i < n; i++) work[i] = i;  /* partial permutation */
429:   for (i = 0; i < n; i++) iwork[i] = i; /* partial permutation inverse */
430:   for (idx = 0, i = 0; i < n - 1; i++) {
431:     PetscInt j    = perm[i];
432:     PetscInt icur = work[i];
433:     PetscInt jloc = iwork[j];
434:     PetscInt diff = jloc - i;

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

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

453:   Not Collective

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

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

463:   Level: beginner

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

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

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

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

491: /*MC
492:   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.
493:   This is the inverse of `PetscDTEnumSubset`.

495:   Not Collective

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

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

505:   Level: beginner

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

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

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

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

535: /*MC
536:   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.

538:   Not Collective

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

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

549:   Level: beginner

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

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

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

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

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

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

600:    Level: intermediate

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

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

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

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

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

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

624:   Level: beginner

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

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

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

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

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

644:    Level: intermediate

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

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

680: #include <petscvec.h>

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