Debian Codegraph @

include/deal.II/base/tensor_product_polynomials_bubbles.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2013 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_tensor_product_polynomials_bubbles_h
16 #define dealii_tensor_product_polynomials_bubbles_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/point.h>
23 #include <deal.II/base/polynomial.h>
24 #include <deal.II/base/tensor.h>
25 #include <deal.II/base/tensor_product_polynomials.h>
26 #include <deal.II/base/utilities.h>
27
28 #include <vector>
29
30 DEAL_II_NAMESPACE_OPEN
31
32 /**
33 * @addtogroup Polynomials
34 * @{
35 */
36
37 /**
38 * A class that represents a space of tensor product polynomials, augmented
39 * by $dim$ (non-normalized) bubble functions of form
40 * $\varphi_j(\mathbf x)
41 * = 2^{\text{degree}-1}\left(x_j-frac 12\right)^{\text{degree}-1}
42 * \left[\prod_{i=0}^{dim-1}(x_i(1-x_i))\right]$
43 * for $j=0,\ldots,dim-1$. If `degree` is one, then the first factor
44 * disappears and one receives the usual bubble function centered
45 * at the mid-point of the cell.
46 *
47 * This class inherits
48 * most of its functionality from TensorProductPolynomials. The bubble
49 * enrichments are added for the last index.
50 */
51 template <int dim>
52 class TensorProductPolynomialsBubbles : public ScalarPolynomialsBase<dim>
53 {
54 public:
55 /**
56 * Access to the dimension of this object, for checking and automatic
57 * setting of dimension in other classes.
58 */
59 static constexpr unsigned int dimension = dim;
60
61 /**
62 * Constructor. <tt>pols</tt> is a vector of objects that should be derived
63 * or otherwise convertible to one-dimensional polynomial objects. It will
64 * be copied element by element into a private variable.
65 */
66 template <class Pol>
67 TensorProductPolynomialsBubbles(const std::vector<Pol> &pols);
68
69 /**
70 * Print the list of <tt>tensor_polys</tt> indices to <tt>out</tt>.
71 */
72 void
73 output_indices(std::ostream &out) const;
74
75 /**
76 * Set the ordering of the polynomials. Requires
77 * <tt>renumber.size()==tensor_polys.n()</tt>. Stores a copy of
78 * <tt>renumber</tt>.
79 */
80 void
81 set_numbering(const std::vector<unsigned int> &renumber);
82
83 /**
84 * Give read access to the renumber vector.
85 */
86 const std::vector<unsigned int> &
87 get_numbering() const;
88
89 /**
90 * Give read access to the inverse renumber vector.
91 */
92 const std::vector<unsigned int> &
93 get_numbering_inverse() const;
94
95 /**
96 * Compute the value and the first and second derivatives of each tensor
97 * product polynomial at <tt>unit_point</tt>.
98 *
99 * The size of the vectors must either be equal 0 or equal n(). In the first
100 * case, the function will not compute these values.
101 *
102 * If you need values or derivatives of all tensor product polynomials then
103 * use this function, rather than using any of the compute_value(),
104 * compute_grad() or compute_grad_grad() functions, see below, in a loop
105 * over all tensor product polynomials.
106 */
107 void
108 evaluate(const Point<dim> &unit_point,
109 std::vector<double> &values,
110 std::vector<Tensor<1, dim>> &grads,
111 std::vector<Tensor<2, dim>> &grad_grads,
112 std::vector<Tensor<3, dim>> &third_derivatives,
113 std::vector<Tensor<4, dim>> &fourth_derivatives) const override;
114
115 /**
116 * Compute the value of the <tt>i</tt>th tensor product polynomial at
117 * <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor product
118 * numbering.
119 *
120 * Note, that using this function within a loop over all tensor product
121 * polynomials is not efficient, because then each point value of the
122 * underlying (one-dimensional) polynomials is (unnecessarily) computed
123 * several times. Instead use the evaluate() function with
124 * <tt>values.size()==</tt>n() to get the point values of all tensor
125 * polynomials all at once and in a much more efficient way.
126 */
127 double
128 compute_value(const unsigned int i, const Point<dim> &p) const override;
129
130 /**
131 * Compute the order @p order derivative of the <tt>i</tt>th tensor product
132 * polynomial at <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor
133 * product numbering.
134 *
135 * Note, that using this function within a loop over all tensor product
136 * polynomials is not efficient, because then each derivative value of the
137 * underlying (one-dimensional) polynomials is (unnecessarily) computed
138 * several times. Instead use the evaluate() function, see above, with the
139 * size of the appropriate parameter set to n() to get the point value of
140 * all tensor polynomials all at once and in a much more efficient way.
141 */
142 template <int order>
143 Tensor<order, dim>
144 compute_derivative(const unsigned int i, const Point<dim> &p) const;
145
146 /**
147 * @copydoc ScalarPolynomialsBase::compute_1st_derivative()
148 */
149 virtual Tensor<1, dim>
150 compute_1st_derivative(const unsigned int i,
151 const Point<dim> &p) const override;
152
153 /**
154 * @copydoc ScalarPolynomialsBase::compute_2nd_derivative()
155 */
156 virtual Tensor<2, dim>
157 compute_2nd_derivative(const unsigned int i,
158 const Point<dim> &p) const override;
159
160 /**
161 * @copydoc ScalarPolynomialsBase::compute_3rd_derivative()
162 */
163 virtual Tensor<3, dim>
164 compute_3rd_derivative(const unsigned int i,
165 const Point<dim> &p) const override;
166
167 /**
168 * @copydoc ScalarPolynomialsBase::compute_4th_derivative()
169 */
170 virtual Tensor<4, dim>
171 compute_4th_derivative(const unsigned int i,
172 const Point<dim> &p) const override;
173
174 /**
175 * Compute the grad of the <tt>i</tt>th tensor product polynomial at
176 * <tt>unit_point</tt>. Here <tt>i</tt> is given in tensor product
177 * numbering.
178 *
179 * Note, that using this function within a loop over all tensor product
180 * polynomials is not efficient, because then each derivative value of the
181 * underlying (one-dimensional) polynomials is (unnecessarily) computed
182 * several times. Instead use the evaluate() function, see above, with
183 * <tt>grads.size()==</tt>n() to get the point value of all tensor
184 * polynomials all at once and in a much more efficient way.
185 */
186 Tensor<1, dim>
187 compute_grad(const unsigned int i, const Point<dim> &p) const override;
188
189 /**
190 * Compute the second derivative (grad_grad) of the <tt>i</tt>th tensor
191 * product polynomial at <tt>unit_point</tt>. Here <tt>i</tt> is given in
192 * tensor product numbering.
193 *
194 * Note, that using this function within a loop over all tensor product
195 * polynomials is not efficient, because then each derivative value of the
196 * underlying (one-dimensional) polynomials is (unnecessarily) computed
197 * several times. Instead use the evaluate() function, see above, with
198 * <tt>grad_grads.size()==</tt>n() to get the point value of all tensor
199 * polynomials all at once and in a much more efficient way.
200 */
201 Tensor<2, dim>
202 compute_grad_grad(const unsigned int i, const Point<dim> &p) const override;
203
204 /**
205 * Return the number of tensor product polynomials plus the bubble
206 * enrichments. For <i>n</i> 1d polynomials this is <i>n<sup>dim</sup>+1</i>
207 * if the maximum degree of the polynomials is one and
208 * <i>n<sup>dim</sup>+dim</i> otherwise.
209 */
210 unsigned int
211 n() const;
212
213 /**
214 * Return the name of the space, which is
215 * <tt>TensorProductPolynomialsBubbles</tt>.
216 */
217 std::string
218 name() const override;
219
220 /**
221 * @copydoc ScalarPolynomialsBase::clone()
222 */
223 virtual std::unique_ptr<ScalarPolynomialsBase<dim>>
224 clone() const override;
225
226 private:
227 /**
228 * The TensorProductPolynomials object
229 */
230 TensorProductPolynomials<dim> tensor_polys;
231
232 /**
233 * Index map for reordering the polynomials.
234 */
235 std::vector<unsigned int> index_map;
236
237 /**
238 * Index map for reordering the polynomials.
239 */
240 std::vector<unsigned int> index_map_inverse;
241 };
242
243 /** @} */
244
245
246 /* ---------------- template and inline functions ---------- */
247
248 #ifndef DOXYGEN
249
250 template <int dim>
251 template <class Pol>
252 inline TensorProductPolynomialsBubbles<dim>::TensorProductPolynomialsBubbles(
253 const std::vector<Pol> &pols)
254 : ScalarPolynomialsBase<dim>(1,
255 Utilities::fixed_power<dim>(pols.size()) + dim)
256 , tensor_polys(pols)
257 , index_map(tensor_polys.n() +
258 ((tensor_polys.polynomials.size() <= 2) ? 1 : dim))
259 , index_map_inverse(tensor_polys.n() +
260 ((tensor_polys.polynomials.size() <= 2) ? 1 : dim))
261 {
262 const unsigned int q_degree = tensor_polys.polynomials.size() - 1;
263 const unsigned int n_bubbles = ((q_degree <= 1) ? 1 : dim);
264 // append index for renumbering
265 for (unsigned int i = 0; i < tensor_polys.n() + n_bubbles; ++i)
266 {
267 index_map[i] = i;
268 index_map_inverse[i] = i;
269 }
270 }
271
272
273 template <int dim>
274 inline unsigned int
275 TensorProductPolynomialsBubbles<dim>::n() const
276 {
277 return tensor_polys.n() + dim;
278 }
279
280
281 template <>
282 inline unsigned int
283 TensorProductPolynomialsBubbles<0>::n() const
284 {
285 return numbers::invalid_unsigned_int;
286 }
287
288
289 template <int dim>
290 inline const std::vector<unsigned int> &
291 TensorProductPolynomialsBubbles<dim>::get_numbering() const
292 {
293 return index_map;
294 }
295
296
297 template <int dim>
298 inline const std::vector<unsigned int> &
299 TensorProductPolynomialsBubbles<dim>::get_numbering_inverse() const
300 {
301 return index_map_inverse;
302 }
303
304
305 template <int dim>
306 inline std::string
307 TensorProductPolynomialsBubbles<dim>::name() const
308 {
309 return "TensorProductPolynomialsBubbles";
310 }
311
312
313 template <int dim>
314 template <int order>
315 Tensor<order, dim>
316 TensorProductPolynomialsBubbles<dim>::compute_derivative(
317 const unsigned int i,
318 const Point<dim> &p) const
319 {
320 const unsigned int q_degree = tensor_polys.polynomials.size() - 1;
321 const unsigned int max_q_indices = tensor_polys.n();
322 Assert(i < max_q_indices + /* n_bubbles= */ ((q_degree <= 1) ? 1 : dim),
323 ExcInternalError());
324
325 // treat the regular basis functions
326 if (i < max_q_indices)
327 return tensor_polys.template compute_derivative<order>(i, p);
328
329 [[maybe_unused]] const unsigned int comp = i - tensor_polys.n();
330
331 if constexpr (order == 1)
332 {
333 Tensor<1, dim> derivative;
334 for (unsigned int d = 0; d < dim; ++d)
335 {
336 derivative[d] = 1.;
337 // compute grad(4*\prod_{i=1}^d (x_i(1-x_i)))(p)
338 for (unsigned j = 0; j < dim; ++j)
339 derivative[d] *=
340 (d == j ? 4 * (1 - 2 * p[j]) : 4 * p[j] * (1 - p[j]));
341 // and multiply with (2*x_i-1)^{r-1}
342 for (unsigned int i = 0; i < q_degree - 1; ++i)
343 derivative[d] *= 2 * p[comp] - 1;
344 }
345
346 if (q_degree >= 2)
347 {
348 // add \prod_{i=1}^d 4*(x_i(1-x_i))(p)
349 double value = 1.;
350 for (unsigned int j = 0; j < dim; ++j)
351 value *= 4 * p[j] * (1 - p[j]);
352 // and multiply with grad(2*x_i-1)^{r-1}
353 double tmp = value * 2 * (q_degree - 1);
354 for (unsigned int i = 0; i < q_degree - 2; ++i)
355 tmp *= 2 * p[comp] - 1;
356 derivative[comp] += tmp;
357 }
358
359 return derivative;
360 }
361 else if constexpr (order == 2)
362 {
363 Tensor<2, dim> derivative;
364
365 double v[dim + 1][3];
366 {
367 for (unsigned int c = 0; c < dim; ++c)
368 {
369 v[c][0] = 4 * p[c] * (1 - p[c]);
370 v[c][1] = 4 * (1 - 2 * p[c]);
371 v[c][2] = -8;
372 }
373
374 double tmp = 1.;
375 for (unsigned int i = 0; i < q_degree - 1; ++i)
376 tmp *= 2 * p[comp] - 1;
377 v[dim][0] = tmp;
378
379 if (q_degree >= 2)
380 {
381 double tmp = 2 * (q_degree - 1);
382 for (unsigned int i = 0; i < q_degree - 2; ++i)
383 tmp *= 2 * p[comp] - 1;
384 v[dim][1] = tmp;
385 }
386 else
387 v[dim][1] = 0.;
388
389 if (q_degree >= 3)
390 {
391 double tmp = 4 * (q_degree - 2) * (q_degree - 1);
392 for (unsigned int i = 0; i < q_degree - 3; ++i)
393 tmp *= 2 * p[comp] - 1;
394 v[dim][2] = tmp;
395 }
396 else
397 v[dim][2] = 0.;
398 }
399
400 // calculate (\partial_j \partial_k \psi) * monomial
401 Tensor<2, dim> grad_grad_1;
402 for (unsigned int d1 = 0; d1 < dim; ++d1)
403 for (unsigned int d2 = 0; d2 < dim; ++d2)
404 {
405 grad_grad_1[d1][d2] = v[dim][0];
406 for (unsigned int x = 0; x < dim; ++x)
407 {
408 unsigned int derivative = 0;
409 if (d1 == x || d2 == x)
410 {
411 if (d1 == d2)
412 derivative = 2;
413 else
414 derivative = 1;
415 }
416 grad_grad_1[d1][d2] *= v[x][derivative];
417 }
418 }
419
420 // calculate (\partial_j \psi) *(\partial_k monomial)
421 // and (\partial_k \psi) *(\partial_j monomial)
422 Tensor<2, dim> grad_grad_2;
423 Tensor<2, dim> grad_grad_3;
424 for (unsigned int d = 0; d < dim; ++d)
425 {
426 grad_grad_2[d][comp] = v[dim][1];
427 grad_grad_3[comp][d] = v[dim][1];
428 for (unsigned int x = 0; x < dim; ++x)
429 {
430 grad_grad_2[d][comp] *= v[x][d == x];
431 grad_grad_3[comp][d] *= v[x][d == x];
432 }
433 }
434
435 // calculate \psi *(\partial j \partial_k monomial) and sum
436 double psi_value = 1.;
437 for (unsigned int x = 0; x < dim; ++x)
438 psi_value *= v[x][0];
439
440 for (unsigned int d1 = 0; d1 < dim; ++d1)
441 for (unsigned int d2 = 0; d2 < dim; ++d2)
442 derivative[d1][d2] =
443 grad_grad_1[d1][d2] + grad_grad_2[d1][d2] + grad_grad_3[d1][d2];
444 derivative[comp][comp] += psi_value * v[dim][2];
445
446 return derivative;
447 }
448 else
449 {
450 DEAL_II_NOT_IMPLEMENTED();
451 return {};
452 }
453 }
454
455
456
457 template <int dim>
458 inline Tensor<1, dim>
459 TensorProductPolynomialsBubbles<dim>::compute_1st_derivative(
460 const unsigned int i,
461 const Point<dim> &p) const
462 {
463 return compute_derivative<1>(i, p);
464 }
465
466
467
468 template <int dim>
469 inline Tensor<2, dim>
470 TensorProductPolynomialsBubbles<dim>::compute_2nd_derivative(
471 const unsigned int i,
472 const Point<dim> &p) const
473 {
474 return compute_derivative<2>(i, p);
475 }
476
477
478
479 template <int dim>
480 inline Tensor<3, dim>
481 TensorProductPolynomialsBubbles<dim>::compute_3rd_derivative(
482 const unsigned int i,
483 const Point<dim> &p) const
484 {
485 return compute_derivative<3>(i, p);
486 }
487
488
489
490 template <int dim>
491 inline Tensor<4, dim>
492 TensorProductPolynomialsBubbles<dim>::compute_4th_derivative(
493 const unsigned int i,
494 const Point<dim> &p) const
495 {
496 return compute_derivative<4>(i, p);
497 }
498
499 #endif // DOXYGEN
500 DEAL_II_NAMESPACE_CLOSE
501
502 #endif