File: tensor_product_polynomials.cc

package info (click to toggle)
deal.ii 9.7.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,024 kB
  • sloc: cpp: 440,899; ansic: 77,337; python: 3,307; perl: 1,041; sh: 1,022; xml: 252; makefile: 97; javascript: 14
file content (1007 lines) | stat: -rw-r--r-- 31,461 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
// ------------------------------------------------------------------------
//
// SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2000 - 2025 by the deal.II authors
//
// This file is part of the deal.II library.
//
// Part of the source code is dual licensed under Apache-2.0 WITH
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
// governing the source code and code contributions can be found in
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
//
// ------------------------------------------------------------------------

#include <deal.II/base/exceptions.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/polynomials_piecewise.h>
#include <deal.II/base/table.h>
#include <deal.II/base/tensor_product_polynomials.h>

#include <boost/container/small_vector.hpp>

#include <array>
#include <memory>

DEAL_II_NAMESPACE_OPEN



/* ------------------- TensorProductPolynomials -------------- */


namespace internal
{
  namespace
  {
    template <std::size_t dim>
    inline void
    compute_tensor_index(const unsigned int,
                         const unsigned int,
                         const unsigned int,
                         std::array<unsigned int, dim> &)
    {
      DEAL_II_NOT_IMPLEMENTED();
    }

    inline void
    compute_tensor_index(const unsigned int n,
                         const unsigned int,
                         const unsigned int,
                         std::array<unsigned int, 1> &indices)
    {
      indices[0] = n;
    }

    inline void
    compute_tensor_index(const unsigned int n,
                         const unsigned int n_pols_0,
                         const unsigned int,
                         std::array<unsigned int, 2> &indices)
    {
      indices[0] = n % n_pols_0;
      indices[1] = n / n_pols_0;
    }

    inline void
    compute_tensor_index(const unsigned int           n,
                         const unsigned int           n_pols_0,
                         const unsigned int           n_pols_1,
                         std::array<unsigned int, 3> &indices)
    {
      indices[0] = n % n_pols_0;
      indices[1] = (n / n_pols_0) % n_pols_1;
      indices[2] = n / (n_pols_0 * n_pols_1);
    }
  } // namespace
} // namespace internal



template <int dim, typename PolynomialType>
inline void
TensorProductPolynomials<dim, PolynomialType>::compute_index(
  const unsigned int             i,
  std::array<unsigned int, dim> &indices) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)indices;
      DEAL_II_NOT_IMPLEMENTED();
    }
  else
    {
      Assert(i < Utilities::fixed_power<dim>(polynomials.size()),
             ExcInternalError());
      internal::compute_tensor_index(index_map[i],
                                     polynomials.size(),
                                     polynomials.size(),
                                     indices);
    }
}



template <int dim, typename PolynomialType>
void
TensorProductPolynomials<dim, PolynomialType>::output_indices(
  std::ostream &out) const
{
  if constexpr (dim == 0)
    {
      (void)out;
      DEAL_II_NOT_IMPLEMENTED();
    }
  else
    {
      std::array<unsigned int, dim> ix;
      for (unsigned int i = 0; i < this->n(); ++i)
        {
          compute_index(i, ix);
          out << i << "\t";
          for (unsigned int d = 0; d < dim; ++d)
            out << ix[d] << " ";
          out << std::endl;
        }
    }
}



template <int dim>
inline const std::vector<unsigned int> &
AnisotropicPolynomials<dim>::get_numbering() const
{
  return index_map;
}



template <int dim>
inline const std::vector<unsigned int> &
AnisotropicPolynomials<dim>::get_numbering_inverse() const
{
  return index_map_inverse;
}



template <int dim>
void
AnisotropicPolynomials<dim>::set_numbering(
  const std::vector<unsigned int> &renumber)
{
  Assert(renumber.size() == index_map.size(),
         ExcDimensionMismatch(renumber.size(), index_map.size()));

  index_map = renumber;
  for (unsigned int i = 0; i < index_map.size(); ++i)
    index_map_inverse[index_map[i]] = i;
}



template <>
void
AnisotropicPolynomials<0>::set_numbering(const std::vector<unsigned int> &)
{
  AssertThrow(false, ExcNotImplemented("This function does not work in 0-d!"));
}



template <int dim, typename PolynomialType>
void
TensorProductPolynomials<dim, PolynomialType>::set_numbering(
  const std::vector<unsigned int> &renumber)
{
  Assert(renumber.size() == index_map.size(),
         ExcDimensionMismatch(renumber.size(), index_map.size()));

  index_map = renumber;
  for (unsigned int i = 0; i < index_map.size(); ++i)
    index_map_inverse[index_map[i]] = i;
}



template <>
void
TensorProductPolynomials<0, Polynomials::Polynomial<double>>::set_numbering(
  const std::vector<unsigned int> &)
{
  AssertThrow(false, ExcNotImplemented("This function does not work in 0-d!"));
}



template <int dim, typename PolynomialType>
double
TensorProductPolynomials<dim, PolynomialType>::compute_value(
  const unsigned int i,
  const Point<dim>  &p) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)p;
      DEAL_II_NOT_IMPLEMENTED();
      return 0;
    }
  else
    {
      std::array<unsigned int, dim> indices;
      compute_index(i, indices);

      double value = 1.;
      for (unsigned int d = 0; d < dim; ++d)
        value *= polynomials[indices[d]].value(p[d]);

      return value;
    }
}



template <int dim, typename PolynomialType>
Tensor<1, dim>
TensorProductPolynomials<dim, PolynomialType>::compute_grad(
  const unsigned int i,
  const Point<dim>  &p) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)p;
      DEAL_II_NOT_IMPLEMENTED();
      return {};
    }
  else
    {
      std::array<unsigned int, dim> indices;
      compute_index(i, indices);

      // compute values and
      // uni-directional derivatives at
      // the given point in each
      // coordinate direction
      ndarray<double, dim, 2> v;
      {
        std::vector<double> tmp(2);
        for (unsigned int d = 0; d < dim; ++d)
          {
            polynomials[indices[d]].value(p[d], tmp);
            v[d][0] = tmp[0];
            v[d][1] = tmp[1];
          }
      }

      Tensor<1, dim> grad;
      for (unsigned int d = 0; d < dim; ++d)
        {
          grad[d] = 1.;
          for (unsigned int x = 0; x < dim; ++x)
            grad[d] *= v[x][d == x];
        }

      return grad;
    }
}



template <int dim, typename PolynomialType>
Tensor<2, dim>
TensorProductPolynomials<dim, PolynomialType>::compute_grad_grad(
  const unsigned int i,
  const Point<dim>  &p) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)p;
      DEAL_II_NOT_IMPLEMENTED();
      return {};
    }
  else
    {
      std::array<unsigned int, dim> indices;
      compute_index(i, indices);

      ndarray<double, dim, 3> v;
      {
        std::vector<double> tmp(3);
        for (unsigned int d = 0; d < dim; ++d)
          {
            polynomials[indices[d]].value(p[d], tmp);
            v[d][0] = tmp[0];
            v[d][1] = tmp[1];
            v[d][2] = tmp[2];
          }
      }

      Tensor<2, dim> grad_grad;
      for (unsigned int d1 = 0; d1 < dim; ++d1)
        for (unsigned int d2 = 0; d2 < dim; ++d2)
          {
            grad_grad[d1][d2] = 1.;
            for (unsigned int x = 0; x < dim; ++x)
              {
                unsigned int derivative = 0;
                if (d1 == x || d2 == x)
                  {
                    if (d1 == d2)
                      derivative = 2;
                    else
                      derivative = 1;
                  }
                grad_grad[d1][d2] *= v[x][derivative];
              }
          }

      return grad_grad;
    }
}



namespace internal
{
  namespace TensorProductPolynomials
  {
    // This function computes the tensor product of some tabulated
    // one-dimensional polynomials (also the anisotropic case is supported)
    // with tensor product indices of all dimensions except the first one
    // tabulated in the 'indices' array; the first dimension is manually
    // iterated through because these are possibly performance-critical loops,
    // so we want to avoid indirect addressing.
    template <int dim, std::size_t dim1>
    void
    evaluate_tensor_product(
      const unsigned int n_derivatives,
      const boost::container::small_vector<dealii::ndarray<double, 5, dim>, 10>
                        &values_1d,
      const unsigned int size_x,
      const boost::container::small_vector<std::array<unsigned int, dim1>, 64>
                                      &indices,
      const std::vector<unsigned int> &index_map,
      std::vector<double>             &values,
      std::vector<Tensor<1, dim>>     &grads,
      std::vector<Tensor<2, dim>>     &grad_grads,
      std::vector<Tensor<3, dim>>     &third_derivatives,
      std::vector<Tensor<4, dim>>     &fourth_derivatives)
    {
      const bool update_values = (values.size() == indices.size() * size_x);
      const bool update_grads  = (grads.size() == indices.size() * size_x);
      const bool update_grad_grads =
        (grad_grads.size() == indices.size() * size_x);
      const bool update_3rd_derivatives =
        (third_derivatives.size() == indices.size() * size_x);
      const bool update_4th_derivatives =
        (fourth_derivatives.size() == indices.size() * size_x);

      // For values, 1st and 2nd derivatives use a more lengthy code that
      // minimizes the number of arithmetic operations and memory accesses
      if (n_derivatives == 0)
        for (unsigned int i = 0, i1 = 0; i1 < indices.size(); ++i1)
          {
            double value_outer = 1.;
            if constexpr (dim > 1)
              for (unsigned int d = 1; d < dim; ++d)
                value_outer *= values_1d[indices[i1][d - 1]][0][d];
            if (index_map.empty())
              for (unsigned int ix = 0; ix < size_x; ++ix, ++i)
                values[i] = value_outer * values_1d[ix][0][0];
            else
              for (unsigned int ix = 0; ix < size_x; ++ix, ++i)
                values[index_map[i]] = value_outer * values_1d[ix][0][0];
          }
      else
        for (unsigned int iy = 0, i1 = 0; i1 < indices.size(); ++i1)
          {
            // prepare parts of products in y (and z) directions
            std::array<double, dim + (dim * (dim - 1)) / 2> value_outer;
            value_outer[0] = 1.;
            if constexpr (dim > 1)
              {
                for (unsigned int x = 1; x < dim; ++x)
                  value_outer[0] *= values_1d[indices[i1][x - 1]][0][x];
                for (unsigned int d = 1; d < dim; ++d)
                  {
                    value_outer[d] = values_1d[indices[i1][d - 1]][1][d];
                    for (unsigned int x = 1; x < dim; ++x)
                      if (x != d)
                        value_outer[d] *= values_1d[indices[i1][x - 1]][0][x];
                  }
                for (unsigned int d1 = 1, count = dim; d1 < dim; ++d1)
                  for (unsigned int d2 = d1; d2 < dim; ++d2, ++count)
                    {
                      value_outer[count] = 1.;
                      for (unsigned int x = 1; x < dim; ++x)
                        {
                          unsigned int derivative = 0;
                          if (d1 == x)
                            ++derivative;
                          if (d2 == x)
                            ++derivative;

                          value_outer[count] *=
                            values_1d[indices[i1][x - 1]][derivative][x];
                        }
                    }
              }

            // now run the loop over x and multiply by the values/derivatives
            // in x direction
            for (unsigned int ix = 0, i = iy; ix < size_x; ++ix, ++i)
              {
                std::array<double, 3> val_x{{values_1d[ix][0][0],
                                             values_1d[ix][1][0],
                                             values_1d[ix][2][0]}};
                const unsigned int    index =
                  (index_map.empty() ? i : index_map[i]);

                if (update_values)
                  values[index] = value_outer[0] * val_x[0];

                if (update_grads)
                  {
                    grads[index][0] = value_outer[0] * val_x[1];
                    if constexpr (dim > 1)
                      for (unsigned int d = 1; d < dim; ++d)
                        grads[index][d] = value_outer[d] * val_x[0];
                  }

                if (update_grad_grads)
                  {
                    grad_grads[index][0][0] = value_outer[0] * val_x[2];
                    if constexpr (dim > 1)
                      {
                        for (unsigned int d = 1; d < dim; ++d)
                          grad_grads[index][0][d] = grad_grads[index][d][0] =
                            value_outer[d] * val_x[1];
                        for (unsigned int d1 = 1, count = dim; d1 < dim; ++d1)
                          for (unsigned int d2 = d1; d2 < dim; ++d2, ++count)
                            grad_grads[index][d1][d2] =
                              grad_grads[index][d2][d1] =
                                value_outer[count] * val_x[0];
                      }
                  }
              }

            // Use slower code for 3rd and 4th derivatives
            if (update_3rd_derivatives)
              for (unsigned int ix = 0, i = iy; ix < size_x; ++ix, ++i)
                {
                  const unsigned int index =
                    (index_map.empty() ? i : index_map[i]);
                  std::array<unsigned int, dim> my_indices;
                  my_indices[0] = ix;
                  if constexpr (dim > 1)
                    for (unsigned int d = 1; d < dim; ++d)
                      my_indices[d] = indices[i1][d - 1];
                  for (unsigned int d1 = 0; d1 < dim; ++d1)
                    for (unsigned int d2 = 0; d2 < dim; ++d2)
                      for (unsigned int d3 = 0; d3 < dim; ++d3)
                        {
                          double der3 = 1.;
                          for (unsigned int x = 0; x < dim; ++x)
                            {
                              unsigned int derivative = 0;
                              if (d1 == x)
                                ++derivative;
                              if (d2 == x)
                                ++derivative;
                              if (d3 == x)
                                ++derivative;

                              der3 *= values_1d[my_indices[x]][derivative][x];
                            }
                          third_derivatives[index][d1][d2][d3] = der3;
                        }
                }

            if (update_4th_derivatives)
              for (unsigned int ix = 0, i = iy; ix < size_x; ++ix, ++i)
                {
                  const unsigned int index =
                    (index_map.empty() ? i : index_map[i]);
                  std::array<unsigned int, dim> my_indices;
                  my_indices[0] = ix;
                  if constexpr (dim > 1)
                    for (unsigned int d = 1; d < dim; ++d)
                      my_indices[d] = indices[i1][d - 1];
                  for (unsigned int d1 = 0; d1 < dim; ++d1)
                    for (unsigned int d2 = 0; d2 < dim; ++d2)
                      for (unsigned int d3 = 0; d3 < dim; ++d3)
                        for (unsigned int d4 = 0; d4 < dim; ++d4)
                          {
                            double der4 = 1.;
                            for (unsigned int x = 0; x < dim; ++x)
                              {
                                unsigned int derivative = 0;
                                if (d1 == x)
                                  ++derivative;
                                if (d2 == x)
                                  ++derivative;
                                if (d3 == x)
                                  ++derivative;
                                if (d4 == x)
                                  ++derivative;

                                der4 *= values_1d[my_indices[x]][derivative][x];
                              }
                            fourth_derivatives[index][d1][d2][d3][d4] = der4;
                          }
                }

            iy += size_x;
          }
    }
  } // namespace TensorProductPolynomials
} // namespace internal



template <int dim, typename PolynomialType>
void
TensorProductPolynomials<dim, PolynomialType>::evaluate(
  const Point<dim>            &p,
  std::vector<double>         &values,
  std::vector<Tensor<1, dim>> &grads,
  std::vector<Tensor<2, dim>> &grad_grads,
  std::vector<Tensor<3, dim>> &third_derivatives,
  std::vector<Tensor<4, dim>> &fourth_derivatives) const
{
  if constexpr (dim == 0)
    {
      (void)p;
      (void)values;
      (void)grads;
      (void)grad_grads;
      (void)third_derivatives;
      (void)fourth_derivatives;
      DEAL_II_NOT_IMPLEMENTED();
    }
  else
    {
      Assert(dim <= 3, ExcNotImplemented());
      Assert(values.size() == this->n() || values.empty(),
             ExcDimensionMismatch2(values.size(), this->n(), 0));
      Assert(grads.size() == this->n() || grads.empty(),
             ExcDimensionMismatch2(grads.size(), this->n(), 0));
      Assert(grad_grads.size() == this->n() || grad_grads.empty(),
             ExcDimensionMismatch2(grad_grads.size(), this->n(), 0));
      Assert(third_derivatives.size() == this->n() || third_derivatives.empty(),
             ExcDimensionMismatch2(third_derivatives.size(), this->n(), 0));
      Assert(fourth_derivatives.size() == this->n() ||
               fourth_derivatives.empty(),
             ExcDimensionMismatch2(fourth_derivatives.size(), this->n(), 0));

      // check how many values/derivatives we have to compute
      unsigned int n_derivatives = 0;
      if (values.size() == this->n())
        n_derivatives = 0;
      if (grads.size() == this->n())
        n_derivatives = 1;
      if (grad_grads.size() == this->n())
        n_derivatives = 2;
      if (third_derivatives.size() == this->n())
        n_derivatives = 3;
      if (fourth_derivatives.size() == this->n())
        n_derivatives = 4;

      // Compute the values (and derivatives, if necessary) of all 1d
      // polynomials at this evaluation point. We can use the more optimized
      // values_of_array function to compute 'dim' polynomials at once
      const unsigned int n_polynomials = polynomials.size();
      boost::container::small_vector<ndarray<double, 5, dim>, 10> values_1d(
        n_polynomials);
      if constexpr (std::is_same_v<PolynomialType,
                                   dealii::Polynomials::Polynomial<double>>)
        {
          std::array<double, dim> point_array;
          for (unsigned int d = 0; d < dim; ++d)
            point_array[d] = p[d];
          for (unsigned int i = 0; i < n_polynomials; ++i)
            polynomials[i].values_of_array(point_array,
                                           n_derivatives,
                                           values_1d[i].data());
        }
      else
        for (unsigned int i = 0; i < n_polynomials; ++i)
          for (unsigned int d = 0; d < dim; ++d)
            {
              std::array<double, 5> derivatives;
              polynomials[i].value(p[d], n_derivatives, derivatives.data());
              for (unsigned int j = 0; j <= n_derivatives; ++j)
                values_1d[i][j][d] = derivatives[j];
            }

      // Unroll the tensor product indices of all but the first dimension in
      // arbitrary dimension
      constexpr unsigned int dim1 = dim > 1 ? dim - 1 : 1;
      boost::container::small_vector<std::array<unsigned int, dim1>, 64>
        indices(1);
      if constexpr (dim > 1)
        for (unsigned int d = 1; d < dim; ++d)
          {
            const unsigned int size = indices.size();
            for (unsigned int i = 1; i < n_polynomials; ++i)
              for (unsigned int j = 0; j < size; ++j)
                {
                  std::array<unsigned int, dim1> next_index = indices[j];
                  next_index[d - 1]                         = i;
                  indices.push_back(next_index);
                }
          }
      AssertDimension(indices.size(), Utilities::pow(n_polynomials, dim - 1));

      internal::TensorProductPolynomials::evaluate_tensor_product<dim>(
        n_derivatives,
        values_1d,
        n_polynomials,
        indices,
        index_map_inverse,
        values,
        grads,
        grad_grads,
        third_derivatives,
        fourth_derivatives);
    }
}



template <int dim, typename PolynomialType>
std::unique_ptr<ScalarPolynomialsBase<dim>>
TensorProductPolynomials<dim, PolynomialType>::clone() const
{
  return std::make_unique<TensorProductPolynomials<dim, PolynomialType>>(*this);
}



template <int dim, typename PolynomialType>
std::size_t
TensorProductPolynomials<dim, PolynomialType>::memory_consumption() const
{
  return (MemoryConsumption::memory_consumption(polynomials) +
          MemoryConsumption::memory_consumption(index_map) +
          MemoryConsumption::memory_consumption(index_map_inverse));
}



template <int dim, typename PolynomialType>
std::vector<PolynomialType>
TensorProductPolynomials<dim, PolynomialType>::get_underlying_polynomials()
  const
{
  return polynomials;
}



/* ------------------- AnisotropicPolynomials -------------- */


template <int dim>
AnisotropicPolynomials<dim>::AnisotropicPolynomials(
  const std::vector<std::vector<Polynomials::Polynomial<double>>> &pols)
  : ScalarPolynomialsBase<dim>(1, get_n_tensor_pols(pols))
  , polynomials(pols)
  , index_map(this->n())
  , index_map_inverse(this->n())
{
  Assert(pols.size() == dim, ExcDimensionMismatch(pols.size(), dim));
  for (const auto &pols_d : pols)
    {
      (void)pols_d;
      Assert(pols_d.size() > 0,
             ExcMessage("The number of polynomials must be larger than zero "
                        "for all coordinate directions."));
    }

  // per default set this index map to identity. This map can be changed by
  // the user through the set_numbering() function
  for (unsigned int i = 0; i < this->n(); ++i)
    {
      index_map[i]         = i;
      index_map_inverse[i] = i;
    }
}



template <int dim>
void
AnisotropicPolynomials<dim>::compute_index(
  const unsigned int             i,
  std::array<unsigned int, dim> &indices) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)indices;
      DEAL_II_NOT_IMPLEMENTED();
    }
  else
    {
      if constexpr (running_in_debug_mode())
        {
          unsigned int n_poly = 1;
          for (unsigned int d = 0; d < dim; ++d)
            n_poly *= polynomials[d].size();
          Assert(i < n_poly, ExcInternalError());
        }

      if (dim == 0)
        {
        }
      else if (dim == 1)
        internal::compute_tensor_index(index_map[i],
                                       polynomials[0].size(),
                                       0 /*not used*/,
                                       indices);
      else
        internal::compute_tensor_index(index_map[i],
                                       polynomials[0].size(),
                                       polynomials[1].size(),
                                       indices);
    }
}



template <int dim>
double
AnisotropicPolynomials<dim>::compute_value(const unsigned int i,
                                           const Point<dim>  &p) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)p;
      DEAL_II_NOT_IMPLEMENTED();
      return {};
    }
  else
    {
      std::array<unsigned int, dim> indices;
      compute_index(i, indices);

      double value = 1.;
      for (unsigned int d = 0; d < dim; ++d)
        value *= polynomials[d][indices[d]].value(p[d]);

      return value;
    }
}



template <int dim>
Tensor<1, dim>
AnisotropicPolynomials<dim>::compute_grad(const unsigned int i,
                                          const Point<dim>  &p) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)p;
      DEAL_II_NOT_IMPLEMENTED();
      return {};
    }
  else
    {
      std::array<unsigned int, dim> indices;
      compute_index(i, indices);

      // compute values and
      // uni-directional derivatives at
      // the given point in each
      // coordinate direction
      ndarray<double, dim, 2> v;
      for (unsigned int d = 0; d < dim; ++d)
        polynomials[d][indices[d]].value(p[d], 1, v[d].data());

      Tensor<1, dim> grad;
      for (unsigned int d = 0; d < dim; ++d)
        {
          grad[d] = 1.;
          for (unsigned int x = 0; x < dim; ++x)
            grad[d] *= v[x][d == x];
        }

      return grad;
    }
}



template <int dim>
Tensor<2, dim>
AnisotropicPolynomials<dim>::compute_grad_grad(const unsigned int i,
                                               const Point<dim>  &p) const
{
  if constexpr (dim == 0)
    {
      (void)i;
      (void)p;
      DEAL_II_NOT_IMPLEMENTED();
      return {};
    }
  else
    {
      std::array<unsigned int, dim> indices;
      compute_index(i, indices);

      ndarray<double, dim, 3> v;
      for (unsigned int d = 0; d < dim; ++d)
        polynomials[d][indices[d]].value(p[d], 2, v[d].data());

      Tensor<2, dim> grad_grad;
      for (unsigned int d1 = 0; d1 < dim; ++d1)
        for (unsigned int d2 = 0; d2 < dim; ++d2)
          {
            grad_grad[d1][d2] = 1.;
            for (unsigned int x = 0; x < dim; ++x)
              {
                unsigned int derivative = 0;
                if (d1 == x || d2 == x)
                  {
                    if (d1 == d2)
                      derivative = 2;
                    else
                      derivative = 1;
                  }
                grad_grad[d1][d2] *= v[x][derivative];
              }
          }

      return grad_grad;
    }
}



template <int dim>
void
AnisotropicPolynomials<dim>::evaluate(
  const Point<dim>            &p,
  std::vector<double>         &values,
  std::vector<Tensor<1, dim>> &grads,
  std::vector<Tensor<2, dim>> &grad_grads,
  std::vector<Tensor<3, dim>> &third_derivatives,
  std::vector<Tensor<4, dim>> &fourth_derivatives) const
{
  if constexpr (dim == 0)
    {
      (void)p;
      (void)values;
      (void)grads;
      (void)grad_grads;
      (void)third_derivatives;
      (void)fourth_derivatives;
      DEAL_II_NOT_IMPLEMENTED();
    }
  else
    {
      Assert(values.size() == this->n() || values.empty(),
             ExcDimensionMismatch2(values.size(), this->n(), 0));
      Assert(grads.size() == this->n() || grads.empty(),
             ExcDimensionMismatch2(grads.size(), this->n(), 0));
      Assert(grad_grads.size() == this->n() || grad_grads.empty(),
             ExcDimensionMismatch2(grad_grads.size(), this->n(), 0));
      Assert(third_derivatives.size() == this->n() || third_derivatives.empty(),
             ExcDimensionMismatch2(third_derivatives.size(), this->n(), 0));
      Assert(fourth_derivatives.size() == this->n() ||
               fourth_derivatives.empty(),
             ExcDimensionMismatch2(fourth_derivatives.size(), this->n(), 0));

      // check how many values/derivatives we have to compute
      unsigned int n_derivatives = 0;
      if (values.size() == this->n())
        n_derivatives = 0;
      if (grads.size() == this->n())
        n_derivatives = 1;
      if (grad_grads.size() == this->n())
        n_derivatives = 2;
      if (third_derivatives.size() == this->n())
        n_derivatives = 3;
      if (fourth_derivatives.size() == this->n())
        n_derivatives = 4;

      // compute the values (and derivatives, if necessary) of all polynomials
      // at this evaluation point
      std::size_t max_n_polynomials = 0;
      for (unsigned int d = 0; d < dim; ++d)
        max_n_polynomials = std::max(max_n_polynomials, polynomials[d].size());

      // 5 is enough to store values and derivatives in all supported cases
      boost::container::small_vector<ndarray<double, 5, dim>, 10> values_1d(
        max_n_polynomials);
      if (n_derivatives == 0)
        for (unsigned int d = 0; d < dim; ++d)
          for (unsigned int i = 0; i < polynomials[d].size(); ++i)
            values_1d[i][0][d] = polynomials[d][i].value(p[d]);
      else
        for (unsigned int d = 0; d < dim; ++d)
          for (unsigned int i = 0; i < polynomials[d].size(); ++i)
            {
              // The isotropic tensor product function wants us to use a
              // different innermost index, so we cannot pass the values_1d
              // array into the function directly
              std::array<double, 5> derivatives;
              polynomials[d][i].value(p[d], n_derivatives, derivatives.data());
              for (unsigned int j = 0; j <= n_derivatives; ++j)
                values_1d[i][j][d] = derivatives[j];
            }

      // Unroll the tensor product indices in arbitrary dimension
      constexpr unsigned int dim1 = dim > 1 ? dim - 1 : 1;
      boost::container::small_vector<std::array<unsigned int, dim1>, 64>
        indices(1);
      for (unsigned int d = 1; d < dim; ++d)
        {
          const unsigned int size = indices.size();
          for (unsigned int i = 1; i < polynomials[d].size(); ++i)
            for (unsigned int j = 0; j < size; ++j)
              {
                std::array<unsigned int, dim1> next_index = indices[j];
                next_index[d - 1]                         = i;
                indices.push_back(next_index);
              }
        }

      internal::TensorProductPolynomials::evaluate_tensor_product<dim>(
        n_derivatives,
        values_1d,
        polynomials[0].size(),
        indices,
        index_map_inverse,
        values,
        grads,
        grad_grads,
        third_derivatives,
        fourth_derivatives);
    }
}



template <int dim>
unsigned int
AnisotropicPolynomials<dim>::get_n_tensor_pols(
  const std::vector<std::vector<Polynomials::Polynomial<double>>> &pols)
{
  if constexpr (dim == 0)
    {
      (void)pols;
      DEAL_II_NOT_IMPLEMENTED();
      return {};
    }
  else
    {
      unsigned int y = 1;
      for (unsigned int d = 0; d < dim; ++d)
        y *= pols[d].size();
      return y;
    }
}



template <int dim>
std::unique_ptr<ScalarPolynomialsBase<dim>>
AnisotropicPolynomials<dim>::clone() const
{
  return std::make_unique<AnisotropicPolynomials<dim>>(*this);
}



/* ------------------- explicit instantiations -------------- */
template class TensorProductPolynomials<0, Polynomials::Polynomial<double>>;
template class TensorProductPolynomials<1, Polynomials::Polynomial<double>>;
template class TensorProductPolynomials<2, Polynomials::Polynomial<double>>;
template class TensorProductPolynomials<3, Polynomials::Polynomial<double>>;

template class TensorProductPolynomials<
  1,
  Polynomials::PiecewisePolynomial<double>>;
template class TensorProductPolynomials<
  2,
  Polynomials::PiecewisePolynomial<double>>;
template class TensorProductPolynomials<
  3,
  Polynomials::PiecewisePolynomial<double>>;

template class AnisotropicPolynomials<0>;
template class AnisotropicPolynomials<1>;
template class AnisotropicPolynomials<2>;
template class AnisotropicPolynomials<3>;

DEAL_II_NAMESPACE_CLOSE