xref: /petsc/include/petscdt.h (revision fad4db65a981d86daeaa3759591e8b6204fd5392)
1 /*
2   Common tools for constructing discretizations
3 */
4 #if !defined(PETSCDT_H)
5 #define PETSCDT_H
6 
7 #include <petscsys.h>
8 
9 /*S
10   PetscQuadrature - Quadrature rule for integration.
11 
12   Level: beginner
13 
14 .seealso:  PetscQuadratureCreate(), PetscQuadratureDestroy()
15 S*/
16 typedef struct _p_PetscQuadrature *PetscQuadrature;
17 
18 /*E
19   PetscGaussLobattoLegendreCreateType - algorithm used to compute the Gauss-Lobatto-Legendre nodes and weights
20 
21   Level: intermediate
22 
23 $  PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA - compute the nodes via linear algebra
24 $  PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON - compute the nodes by solving a nonlinear equation with Newton's method
25 
26 E*/
27 typedef enum {PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA,PETSCGAUSSLOBATTOLEGENDRE_VIA_NEWTON} PetscGaussLobattoLegendreCreateType;
28 
29 PETSC_EXTERN PetscErrorCode PetscQuadratureCreate(MPI_Comm, PetscQuadrature *);
30 PETSC_EXTERN PetscErrorCode PetscQuadratureDuplicate(PetscQuadrature, PetscQuadrature *);
31 PETSC_EXTERN PetscErrorCode PetscQuadratureGetOrder(PetscQuadrature, PetscInt*);
32 PETSC_EXTERN PetscErrorCode PetscQuadratureSetOrder(PetscQuadrature, PetscInt);
33 PETSC_EXTERN PetscErrorCode PetscQuadratureGetNumComponents(PetscQuadrature, PetscInt*);
34 PETSC_EXTERN PetscErrorCode PetscQuadratureSetNumComponents(PetscQuadrature, PetscInt);
35 PETSC_EXTERN PetscErrorCode PetscQuadratureGetData(PetscQuadrature, PetscInt*, PetscInt*, PetscInt*, const PetscReal *[], const PetscReal *[]);
36 PETSC_EXTERN PetscErrorCode PetscQuadratureSetData(PetscQuadrature, PetscInt, PetscInt, PetscInt, const PetscReal [], const PetscReal []);
37 PETSC_EXTERN PetscErrorCode PetscQuadratureView(PetscQuadrature, PetscViewer);
38 PETSC_EXTERN PetscErrorCode PetscQuadratureDestroy(PetscQuadrature *);
39 
40 PETSC_EXTERN PetscErrorCode PetscQuadratureExpandComposite(PetscQuadrature, PetscInt, const PetscReal[], const PetscReal[], PetscQuadrature *);
41 
42 PETSC_EXTERN PetscErrorCode PetscDTLegendreEval(PetscInt,const PetscReal*,PetscInt,const PetscInt*,PetscReal*,PetscReal*,PetscReal*);
43 PETSC_EXTERN PetscErrorCode PetscDTGaussQuadrature(PetscInt,PetscReal,PetscReal,PetscReal*,PetscReal*);
44 PETSC_EXTERN PetscErrorCode PetscDTGaussLobattoLegendreQuadrature(PetscInt,PetscGaussLobattoLegendreCreateType,PetscReal*,PetscReal*);
45 PETSC_EXTERN PetscErrorCode PetscDTReconstructPoly(PetscInt,PetscInt,const PetscReal*,PetscInt,const PetscReal*,PetscReal*);
46 PETSC_EXTERN PetscErrorCode PetscDTGaussTensorQuadrature(PetscInt,PetscInt,PetscInt,PetscReal,PetscReal,PetscQuadrature*);
47 PETSC_EXTERN PetscErrorCode PetscDTGaussJacobiQuadrature(PetscInt,PetscInt,PetscInt,PetscReal,PetscReal,PetscQuadrature*);
48 
49 PETSC_EXTERN PetscErrorCode PetscDTTanhSinhTensorQuadrature(PetscInt, PetscInt, PetscReal, PetscReal, PetscQuadrature *);
50 PETSC_EXTERN PetscErrorCode PetscDTTanhSinhIntegrate(void (*)(PetscReal, PetscReal *), PetscReal, PetscReal, PetscInt, PetscReal *);
51 PETSC_EXTERN PetscErrorCode PetscDTTanhSinhIntegrateMPFR(void (*)(PetscReal, PetscReal *), PetscReal, PetscReal, PetscInt, PetscReal *);
52 
53 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreIntegrate(PetscInt, PetscReal *, PetscReal *, const PetscReal *, PetscReal *);
54 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementLaplacianCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
55 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementLaplacianDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
56 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementGradientCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***, PetscReal ***);
57 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementGradientDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***, PetscReal ***);
58 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementAdvectionCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
59 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementAdvectionDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
60 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementMassCreate(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
61 PETSC_EXTERN PetscErrorCode PetscGaussLobattoLegendreElementMassDestroy(PetscInt, PetscReal *, PetscReal *, PetscReal ***);
62 
63 PETSC_EXTERN PetscErrorCode PetscDTAltVApply(PetscInt, PetscInt, const PetscReal *, const PetscReal *, PetscReal *);
64 PETSC_EXTERN PetscErrorCode PetscDTAltVWedge(PetscInt, PetscInt, PetscInt, const PetscReal *, const PetscReal *, PetscReal *);
65 PETSC_EXTERN PetscErrorCode PetscDTAltVWedgeMatrix(PetscInt, PetscInt, PetscInt, const PetscReal *, PetscReal *);
66 PETSC_EXTERN PetscErrorCode PetscDTAltVPullback(PetscInt, PetscInt, const PetscReal *, PetscInt, const PetscReal *, PetscReal *);
67 PETSC_EXTERN PetscErrorCode PetscDTAltVPullbackMatrix(PetscInt, PetscInt, const PetscReal *, PetscInt, PetscReal *);
68 PETSC_EXTERN PetscErrorCode PetscDTAltVInterior(PetscInt, PetscInt, const PetscReal *, const PetscReal *, PetscReal *);
69 PETSC_EXTERN PetscErrorCode PetscDTAltVInteriorMatrix(PetscInt, PetscInt, const PetscReal *, PetscReal *);
70 PETSC_EXTERN PetscErrorCode PetscDTAltVInteriorPattern(PetscInt, PetscInt, PetscInt (*)[3]);
71 PETSC_EXTERN PetscErrorCode PetscDTAltVStar(PetscInt, PetscInt, PetscInt, const PetscReal *, PetscReal *);
72 
73 #if defined(PETSC_USE_64BIT_INDICES)
74 #define PETSC_FACTORIAL_MAX 20
75 #define PETSC_BINOMIAL_MAX  61
76 #else
77 #define PETSC_FACTORIAL_MAX 12
78 #define PETSC_BINOMIAL_MAX  29
79 #endif
80 
81 /*MC
82    PetscDTFactorial - Approximate n! as a real number
83 
84    Input Arguments:
85 
86 .  n - a non-negative integer
87 
88    Output Arguments;
89 
90 .  factorial - n!
91 
92    Level: beginner
93 M*/
94 PETSC_STATIC_INLINE PetscErrorCode PetscDTFactorial(PetscInt n, PetscReal *factorial)
95 {
96   PetscReal f = 1.0;
97   PetscInt  i;
98 
99   PetscFunctionBegin;
100   for (i = 1; i < n+1; ++i) f *= i;
101   *factorial = f;
102   PetscFunctionReturn(0);
103 }
104 
105 /*MC
106    PetscDTFactorialInt - Compute n! as an integer
107 
108    Input Arguments:
109 
110 .  n - a non-negative integer
111 
112    Output Arguments;
113 
114 .  factorial - n!
115 
116    Level: beginner
117 
118    Note: 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.
119 M*/
120 PETSC_STATIC_INLINE PetscErrorCode PetscDTFactorialInt(PetscInt n, PetscInt *factorial)
121 {
122   PetscInt facLookup[13] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600};
123 
124   PetscFunctionBeginHot;
125   if (n < 0 || n > PETSC_FACTORIAL_MAX) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of elements %D is not in supported range [0,%D]\n",n,PETSC_FACTORIAL_MAX);
126   if (n <= 12) {
127     *factorial = facLookup[n];
128   } else {
129     PetscInt f = facLookup[12];
130     PetscInt i;
131 
132     for (i = 13; i < n+1; ++i) f *= i;
133     *factorial = f;
134   }
135   PetscFunctionReturn(0);
136 }
137 
138 /*MC
139    PetscDTBinomial - Approximate the binomial coefficient "n choose k"
140 
141    Input Arguments:
142 
143 +  n - a non-negative integer
144 -  k - an integer between 0 and n, inclusive
145 
146    Output Arguments;
147 
148 .  binomial - approximation of the binomial coefficient n choose k
149 
150    Level: beginner
151 M*/
152 PETSC_STATIC_INLINE PetscErrorCode PetscDTBinomial(PetscInt n, PetscInt k, PetscReal *binomial)
153 {
154   PetscFunctionBeginHot;
155   if (n < 0 || k < 0 || k > n) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Binomial arguments (%D %D) must be non-negative, k <= n\n", n, k);
156   if (n <= 3) {
157     PetscInt binomLookup[4][4] = {{1, 0, 0, 0}, {1, 1, 0, 0}, {1, 2, 1, 0}, {1, 3, 3, 1}};
158 
159     *binomial = binomLookup[n][k];
160   } else {
161     PetscReal binom = 1.;
162     PetscInt  i;
163 
164     k = PetscMin(k, n - k);
165     for (i = 0; i < k; i++) binom = (binom * (n - i)) / (i + 1);
166     *binomial = binom;
167   }
168   PetscFunctionReturn(0);
169 }
170 
171 /*MC
172    PetscDTBinomialInt - Compute the binomial coefficient "n choose k"
173 
174    Input Arguments:
175 
176 +  n - a non-negative integer
177 -  k - an integer between 0 and n, inclusive
178 
179    Output Arguments;
180 
181 .  binomial - the binomial coefficient n choose k
182 
183    Note: this is limited by integers that can be represented by PetscInt
184 
185    Level: beginner
186 M*/
187 PETSC_STATIC_INLINE PetscErrorCode PetscDTBinomialInt(PetscInt n, PetscInt k, PetscInt *binomial)
188 {
189   PetscFunctionBeginHot;
190   if (n < 0 || k < 0 || k > n) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Binomial arguments (%D %D) must be non-negative, k <= n\n", n, k);
191   if (n > PETSC_BINOMIAL_MAX) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Binomial elements %D is larger than max for PetscInt, %D\n", n, PETSC_BINOMIAL_MAX);
192   if (n <= 3) {
193     PetscInt binomLookup[4][4] = {{1, 0, 0, 0}, {1, 1, 0, 0}, {1, 2, 1, 0}, {1, 3, 3, 1}};
194 
195     *binomial = binomLookup[n][k];
196   } else {
197     PetscInt  binom = 1;
198     PetscInt  i;
199 
200     k = PetscMin(k, n - k);
201     for (i = 0; i < k; i++) binom = (binom * (n - i)) / (i + 1);
202     *binomial = binom;
203   }
204   PetscFunctionReturn(0);
205 }
206 
207 /*MC
208    PetscDTEnumPerm - Get a permutation of n integers from its encoding into the integers [0, n!) as a sequence of swaps.
209 
210    A permutation can be described by the operations that convert the lists [0, 1, ..., n-1] into the permutation,
211    by a sequence of swaps, where the ith step swaps whatever number is in ith position with a number that is in
212    some position j >= i.  We encode this swap as the difference (j - i).  The difference d_i at step i is less than
213    (n - i).  We encode this sequence of n-1 differences [d_0, ..., d_{n-2}] as the number
214    (n-1)! * d_0 + (n-2)! * d_1 + ... + 1! * d_{n-2}.
215 
216    Input Arguments:
217 
218 +  n - a non-negative integer (see note about limits below)
219 .  k - an integer in [0, n!)
220 -  work - a workspace of n integers
221 
222    Output Arguments:
223 
224 +  perm - the permuted list of the integers [0, ..., n-1]
225 .  isOdd - if not NULL, returns wether the permutation used an even or odd number of swaps.
226 
227    Note: 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 
229    Level: beginner
230 M*/
231 PETSC_STATIC_INLINE PetscErrorCode PetscDTEnumPerm(PetscInt n, PetscInt k, PetscInt *perm, PetscBool *isOdd)
232 {
233   PetscInt  odd = 0;
234   PetscInt  i;
235   PetscInt  work[PETSC_FACTORIAL_MAX];
236   PetscInt *w;
237 
238   PetscFunctionBeginHot;
239   if (n < 0 || n > PETSC_FACTORIAL_MAX) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of elements %D is not in supported range [0,%D]\n",n,PETSC_FACTORIAL_MAX);
240   w = &work[n - 2];
241   for (i = 2; i <= n; i++) {
242     *(w--) = k % i;
243     k /= i;
244   }
245   for (i = 0; i < n; i++) perm[i] = i;
246   for (i = 0; i < n - 1; i++) {
247     PetscInt s = work[i];
248     PetscInt swap = perm[i];
249 
250     perm[i] = perm[i + s];
251     perm[i + s] = swap;
252     odd ^= (!!s);
253   }
254   if (isOdd) *isOdd = odd ? PETSC_TRUE : PETSC_FALSE;
255   PetscFunctionReturn(0);
256 }
257 
258 /*MC
259    PetscDTEnumSubset - Get an ordered subset of the integers [0, ..., n - 1] from its encoding as an integers in [0, n choose k).
260    The encoding is in lexicographic order.
261 
262    Input Arguments:
263 
264 +  n - a non-negative integer (see note about limits below)
265 .  k - an integer in [0, n]
266 -  j - an index in [0, n choose k)
267 
268    Output Arguments:
269 
270 .  subset - the jth subset of size k of the integers [0, ..., n - 1]
271 
272    Note: this is limited by arguments such that n choose k can be represented by PetscInt
273 
274    Level: beginner
275 
276 .seealso: PetscDTSubsetIndex()
277 M*/
278 PETSC_STATIC_INLINE PetscErrorCode PetscDTEnumSubset(PetscInt n, PetscInt k, PetscInt j, PetscInt *subset)
279 {
280   PetscInt       Nk, i, l;
281   PetscErrorCode ierr;
282 
283   PetscFunctionBeginHot;
284   ierr = PetscDTBinomialInt(n, k, &Nk);CHKERRQ(ierr);
285   for (i = 0, l = 0; i < n && l < k; i++) {
286     PetscInt Nminuskminus = (Nk * (k - l)) / (n - i);
287     PetscInt Nminusk = Nk - Nminuskminus;
288 
289     if (j < Nminuskminus) {
290       subset[l++] = i;
291       Nk = Nminuskminus;
292     } else {
293       j -= Nminuskminus;
294       Nk = Nminusk;
295     }
296   }
297   PetscFunctionReturn(0);
298 }
299 
300 /*MC
301    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.  This is the inverse of PetscDTEnumSubset.
302 
303    Input Arguments:
304 
305 +  n - a non-negative integer (see note about limits below)
306 .  k - an integer in [0, n]
307 -  subset - an ordered subset of the integers [0, ..., n - 1]
308 
309    Output Arguments:
310 
311 .  index - the rank of the subset in lexicographic order
312 
313    Note: this is limited by arguments such that n choose k can be represented by PetscInt
314 
315    Level: beginner
316 
317 .seealso: PetscDTEnumSubset()
318 M*/
319 PETSC_STATIC_INLINE PetscErrorCode PetscDTSubsetIndex(PetscInt n, PetscInt k, const PetscInt *subset, PetscInt *index)
320 {
321   PetscInt       i, j = 0, l, Nk;
322   PetscErrorCode ierr;
323 
324   PetscFunctionBeginHot;
325   ierr = PetscDTBinomialInt(n, k, &Nk);CHKERRQ(ierr);
326   for (i = 0, l = 0; i < n && l < k; i++) {
327     PetscInt Nminuskminus = (Nk * (k - l)) / (n - i);
328     PetscInt Nminusk = Nk - Nminuskminus;
329 
330     if (subset[l] == i) {
331       l++;
332       Nk = Nminuskminus;
333     } else {
334       j += Nminuskminus;
335       Nk = Nminusk;
336     }
337   }
338   *index = j;
339   PetscFunctionReturn(0);
340 }
341 
342 
343 /*MC
344    PetscDTEnumSubset - Split the integers [0, ..., n - 1] into two complementary ordered subsets, the first of size k and beingthe jth in lexicographic order.
345 
346    Input Arguments:
347 
348 +  n - a non-negative integer (see note about limits below)
349 .  k - an integer in [0, n]
350 -  j - an index in [0, n choose k)
351 
352    Output Arguments:
353 
354 +  perm - the jth subset of size k of the integers [0, ..., n - 1], followed by its complementary set.
355 -  isOdd - if not NULL, return whether the permutation is even or odd.
356 
357    Note: this is limited by arguments such that n choose k can be represented by PetscInt
358 
359    Level: beginner
360 
361 .seealso: PetscDTEnumSubset(), PetscDTSubsetIndex()
362 M*/
363 PETSC_STATIC_INLINE PetscErrorCode PetscDTEnumSplit(PetscInt n, PetscInt k, PetscInt j, PetscInt *perm, PetscBool *isOdd)
364 {
365   PetscInt       i, l, m, *subcomp, Nk;
366   PetscInt       odd;
367   PetscErrorCode ierr;
368 
369   PetscFunctionBeginHot;
370   ierr = PetscDTBinomialInt(n, k, &Nk);CHKERRQ(ierr);
371   odd = 0;
372   subcomp = &perm[k];
373   for (i = 0, l = 0, m = 0; i < n && l < k; i++) {
374     PetscInt Nminuskminus = (Nk * (k - l)) / (n - i);
375     PetscInt Nminusk = Nk - Nminuskminus;
376 
377     if (j < Nminuskminus) {
378       perm[l++] = i;
379       Nk = Nminuskminus;
380     } else {
381       subcomp[m++] = i;
382       j -= Nminuskminus;
383       odd ^= ((k - l) & 1);
384       Nk = Nminusk;
385     }
386   }
387   for (; i < n; i++) {
388     subcomp[m++] = i;
389   }
390   if (isOdd) *isOdd = odd ? PETSC_TRUE : PETSC_FALSE;
391   PetscFunctionReturn(0);
392 }
393 
394 #endif
395