-
Notifications
You must be signed in to change notification settings - Fork 708
/
mapping_q.cc
1941 lines (1648 loc) · 71.1 KB
/
mapping_q.cc
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
// ---------------------------------------------------------------------
//
// Copyright (C) 2000 - 2021 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#include <deal.II/base/array_view.h>
#include <deal.II/base/derivative_form.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/qprojector.h>
#include <deal.II/base/quadrature.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/table.h>
#include <deal.II/base/tensor_product_polynomials.h>
#include <deal.II/fe/fe_dgq.h>
#include <deal.II/fe/fe_tools.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/mapping_q.h>
#include <deal.II/fe/mapping_q1.h>
#include <deal.II/fe/mapping_q_internal.h>
#include <deal.II/grid/manifold_lib.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/tria_iterator.h>
DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
#include <boost/container/small_vector.hpp>
DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
#include <algorithm>
#include <array>
#include <cmath>
#include <memory>
#include <numeric>
DEAL_II_NAMESPACE_OPEN
template <int dim, int spacedim>
MappingQ<dim, spacedim>::InternalData::InternalData(
const unsigned int polynomial_degree)
: polynomial_degree(polynomial_degree)
, n_shape_functions(Utilities::fixed_power<dim>(polynomial_degree + 1))
, line_support_points(QGaussLobatto<1>(polynomial_degree + 1))
, tensor_product_quadrature(false)
{}
template <int dim, int spacedim>
std::size_t
MappingQ<dim, spacedim>::InternalData::memory_consumption() const
{
return (
Mapping<dim, spacedim>::InternalDataBase::memory_consumption() +
MemoryConsumption::memory_consumption(shape_values) +
MemoryConsumption::memory_consumption(shape_derivatives) +
MemoryConsumption::memory_consumption(covariant) +
MemoryConsumption::memory_consumption(contravariant) +
MemoryConsumption::memory_consumption(unit_tangentials) +
MemoryConsumption::memory_consumption(aux) +
MemoryConsumption::memory_consumption(mapping_support_points) +
MemoryConsumption::memory_consumption(cell_of_current_support_points) +
MemoryConsumption::memory_consumption(volume_elements) +
MemoryConsumption::memory_consumption(polynomial_degree) +
MemoryConsumption::memory_consumption(n_shape_functions));
}
template <int dim, int spacedim>
void
MappingQ<dim, spacedim>::InternalData::initialize(
const UpdateFlags update_flags,
const Quadrature<dim> &q,
const unsigned int n_original_q_points)
{
// store the flags in the internal data object so we can access them
// in fill_fe_*_values()
this->update_each = update_flags;
const unsigned int n_q_points = q.size();
const bool needs_higher_order_terms =
this->update_each &
(update_jacobian_pushed_forward_grads | update_jacobian_2nd_derivatives |
update_jacobian_pushed_forward_2nd_derivatives |
update_jacobian_3rd_derivatives |
update_jacobian_pushed_forward_3rd_derivatives);
if (this->update_each & update_covariant_transformation)
covariant.resize(n_original_q_points);
if (this->update_each & update_contravariant_transformation)
contravariant.resize(n_original_q_points);
if (this->update_each & update_volume_elements)
volume_elements.resize(n_original_q_points);
tensor_product_quadrature = q.is_tensor_product();
// use of MatrixFree only for higher order elements and with more than one
// point where tensor products do not make sense
if (polynomial_degree < 2 || n_q_points == 1)
tensor_product_quadrature = false;
if (dim > 1)
{
// find out if the one-dimensional formula is the same
// in all directions
if (tensor_product_quadrature)
{
const std::array<Quadrature<1>, dim> quad_array =
q.get_tensor_basis();
for (unsigned int i = 1; i < dim && tensor_product_quadrature; ++i)
{
if (quad_array[i - 1].size() != quad_array[i].size())
{
tensor_product_quadrature = false;
break;
}
else
{
const std::vector<Point<1>> &points_1 =
quad_array[i - 1].get_points();
const std::vector<Point<1>> &points_2 =
quad_array[i].get_points();
const std::vector<double> &weights_1 =
quad_array[i - 1].get_weights();
const std::vector<double> &weights_2 =
quad_array[i].get_weights();
for (unsigned int j = 0; j < quad_array[i].size(); ++j)
{
if (std::abs(points_1[j][0] - points_2[j][0]) > 1.e-10 ||
std::abs(weights_1[j] - weights_2[j]) > 1.e-10)
{
tensor_product_quadrature = false;
break;
}
}
}
}
if (tensor_product_quadrature)
{
// use a 1D FE_DGQ and adjust the hierarchic -> lexicographic
// numbering manually (building an FE_Q<dim> is relatively
// expensive due to constraints)
const FE_DGQ<1> fe(polynomial_degree);
shape_info.reinit(q.get_tensor_basis()[0], fe);
shape_info.lexicographic_numbering =
FETools::lexicographic_to_hierarchic_numbering<dim>(
polynomial_degree);
shape_info.n_q_points = q.size();
shape_info.dofs_per_component_on_cell =
Utilities::pow(polynomial_degree + 1, dim);
}
}
}
// Only fill the big arrays on demand in case we cannot use the tensor
// product quadrature code path
if (dim == 1 || !tensor_product_quadrature || needs_higher_order_terms)
{
// see if we need the (transformation) shape function values
// and/or gradients and resize the necessary arrays
if (this->update_each & update_quadrature_points)
shape_values.resize(n_shape_functions * n_q_points);
if (this->update_each &
(update_covariant_transformation |
update_contravariant_transformation | update_JxW_values |
update_boundary_forms | update_normal_vectors | update_jacobians |
update_jacobian_grads | update_inverse_jacobians |
update_jacobian_pushed_forward_grads |
update_jacobian_2nd_derivatives |
update_jacobian_pushed_forward_2nd_derivatives |
update_jacobian_3rd_derivatives |
update_jacobian_pushed_forward_3rd_derivatives))
shape_derivatives.resize(n_shape_functions * n_q_points);
if (this->update_each &
(update_jacobian_grads | update_jacobian_pushed_forward_grads))
shape_second_derivatives.resize(n_shape_functions * n_q_points);
if (this->update_each & (update_jacobian_2nd_derivatives |
update_jacobian_pushed_forward_2nd_derivatives))
shape_third_derivatives.resize(n_shape_functions * n_q_points);
if (this->update_each & (update_jacobian_3rd_derivatives |
update_jacobian_pushed_forward_3rd_derivatives))
shape_fourth_derivatives.resize(n_shape_functions * n_q_points);
// now also fill the various fields with their correct values
compute_shape_function_values(q.get_points());
}
}
template <int dim, int spacedim>
void
MappingQ<dim, spacedim>::InternalData::initialize_face(
const UpdateFlags update_flags,
const Quadrature<dim> &q,
const unsigned int n_original_q_points)
{
initialize(update_flags, q, n_original_q_points);
if (dim > 1 && tensor_product_quadrature)
{
constexpr unsigned int facedim = dim - 1;
const FE_DGQ<1> fe(polynomial_degree);
shape_info.reinit(q.get_tensor_basis()[0], fe);
shape_info.lexicographic_numbering =
FETools::lexicographic_to_hierarchic_numbering<facedim>(
polynomial_degree);
shape_info.n_q_points = n_original_q_points;
shape_info.dofs_per_component_on_cell =
Utilities::pow(polynomial_degree + 1, dim);
}
if (dim > 1)
{
if (this->update_each &
(update_boundary_forms | update_normal_vectors | update_jacobians |
update_JxW_values | update_inverse_jacobians))
{
aux.resize(dim - 1,
AlignedVector<Tensor<1, spacedim>>(n_original_q_points));
// Compute tangentials to the unit cell.
for (const unsigned int i : GeometryInfo<dim>::face_indices())
{
unit_tangentials[i].resize(n_original_q_points);
std::fill(unit_tangentials[i].begin(),
unit_tangentials[i].end(),
GeometryInfo<dim>::unit_tangential_vectors[i][0]);
if (dim > 2)
{
unit_tangentials[GeometryInfo<dim>::faces_per_cell + i]
.resize(n_original_q_points);
std::fill(
unit_tangentials[GeometryInfo<dim>::faces_per_cell + i]
.begin(),
unit_tangentials[GeometryInfo<dim>::faces_per_cell + i]
.end(),
GeometryInfo<dim>::unit_tangential_vectors[i][1]);
}
}
}
}
}
template <int dim, int spacedim>
void
MappingQ<dim, spacedim>::InternalData::compute_shape_function_values(
const std::vector<Point<dim>> &unit_points)
{
const unsigned int n_points = unit_points.size();
// Construct the tensor product polynomials used as shape functions for
// the Qp mapping of cells at the boundary.
const TensorProductPolynomials<dim> tensor_pols(
Polynomials::generate_complete_Lagrange_basis(
line_support_points.get_points()));
Assert(n_shape_functions == tensor_pols.n(), ExcInternalError());
// then also construct the mapping from lexicographic to the Qp shape
// function numbering
const std::vector<unsigned int> renumber =
FETools::hierarchic_to_lexicographic_numbering<dim>(polynomial_degree);
std::vector<double> values;
std::vector<Tensor<1, dim>> grads;
if (shape_values.size() != 0)
{
Assert(shape_values.size() == n_shape_functions * n_points,
ExcInternalError());
values.resize(n_shape_functions);
}
if (shape_derivatives.size() != 0)
{
Assert(shape_derivatives.size() == n_shape_functions * n_points,
ExcInternalError());
grads.resize(n_shape_functions);
}
std::vector<Tensor<2, dim>> grad2;
if (shape_second_derivatives.size() != 0)
{
Assert(shape_second_derivatives.size() == n_shape_functions * n_points,
ExcInternalError());
grad2.resize(n_shape_functions);
}
std::vector<Tensor<3, dim>> grad3;
if (shape_third_derivatives.size() != 0)
{
Assert(shape_third_derivatives.size() == n_shape_functions * n_points,
ExcInternalError());
grad3.resize(n_shape_functions);
}
std::vector<Tensor<4, dim>> grad4;
if (shape_fourth_derivatives.size() != 0)
{
Assert(shape_fourth_derivatives.size() == n_shape_functions * n_points,
ExcInternalError());
grad4.resize(n_shape_functions);
}
if (shape_values.size() != 0 || shape_derivatives.size() != 0 ||
shape_second_derivatives.size() != 0 ||
shape_third_derivatives.size() != 0 ||
shape_fourth_derivatives.size() != 0)
for (unsigned int point = 0; point < n_points; ++point)
{
tensor_pols.evaluate(
unit_points[point], values, grads, grad2, grad3, grad4);
if (shape_values.size() != 0)
for (unsigned int i = 0; i < n_shape_functions; ++i)
shape(point, i) = values[renumber[i]];
if (shape_derivatives.size() != 0)
for (unsigned int i = 0; i < n_shape_functions; ++i)
derivative(point, i) = grads[renumber[i]];
if (shape_second_derivatives.size() != 0)
for (unsigned int i = 0; i < n_shape_functions; ++i)
second_derivative(point, i) = grad2[renumber[i]];
if (shape_third_derivatives.size() != 0)
for (unsigned int i = 0; i < n_shape_functions; ++i)
third_derivative(point, i) = grad3[renumber[i]];
if (shape_fourth_derivatives.size() != 0)
for (unsigned int i = 0; i < n_shape_functions; ++i)
fourth_derivative(point, i) = grad4[renumber[i]];
}
}
template <int dim, int spacedim>
MappingQ<dim, spacedim>::MappingQ(const unsigned int p)
: polynomial_degree(p)
, line_support_points(
QGaussLobatto<1>(this->polynomial_degree + 1).get_points())
, polynomials_1d(
Polynomials::generate_complete_Lagrange_basis(line_support_points))
, renumber_lexicographic_to_hierarchic(
FETools::lexicographic_to_hierarchic_numbering<dim>(p))
, unit_cell_support_points(
internal::MappingQImplementation::unit_support_points<dim>(
line_support_points,
renumber_lexicographic_to_hierarchic))
, support_point_weights_perimeter_to_interior(
internal::MappingQImplementation::
compute_support_point_weights_perimeter_to_interior(
this->polynomial_degree,
dim))
, support_point_weights_cell(
internal::MappingQImplementation::compute_support_point_weights_cell<dim>(
this->polynomial_degree))
{
Assert(p >= 1,
ExcMessage("It only makes sense to create polynomial mappings "
"with a polynomial degree greater or equal to one."));
}
template <int dim, int spacedim>
MappingQ<dim, spacedim>::MappingQ(const unsigned int p, const bool)
: polynomial_degree(p)
, line_support_points(
QGaussLobatto<1>(this->polynomial_degree + 1).get_points())
, polynomials_1d(
Polynomials::generate_complete_Lagrange_basis(line_support_points))
, renumber_lexicographic_to_hierarchic(
FETools::lexicographic_to_hierarchic_numbering<dim>(p))
, unit_cell_support_points(
internal::MappingQImplementation::unit_support_points<dim>(
line_support_points,
renumber_lexicographic_to_hierarchic))
, support_point_weights_perimeter_to_interior(
internal::MappingQImplementation::
compute_support_point_weights_perimeter_to_interior(
this->polynomial_degree,
dim))
, support_point_weights_cell(
internal::MappingQImplementation::compute_support_point_weights_cell<dim>(
this->polynomial_degree))
{
Assert(p >= 1,
ExcMessage("It only makes sense to create polynomial mappings "
"with a polynomial degree greater or equal to one."));
}
template <int dim, int spacedim>
MappingQ<dim, spacedim>::MappingQ(const MappingQ<dim, spacedim> &mapping)
: polynomial_degree(mapping.polynomial_degree)
, line_support_points(mapping.line_support_points)
, polynomials_1d(mapping.polynomials_1d)
, renumber_lexicographic_to_hierarchic(
mapping.renumber_lexicographic_to_hierarchic)
, support_point_weights_perimeter_to_interior(
mapping.support_point_weights_perimeter_to_interior)
, support_point_weights_cell(mapping.support_point_weights_cell)
{}
template <int dim, int spacedim>
std::unique_ptr<Mapping<dim, spacedim>>
MappingQ<dim, spacedim>::clone() const
{
return std::make_unique<MappingQ<dim, spacedim>>(*this);
}
template <int dim, int spacedim>
unsigned int
MappingQ<dim, spacedim>::get_degree() const
{
return polynomial_degree;
}
template <int dim, int spacedim>
Point<spacedim>
MappingQ<dim, spacedim>::transform_unit_to_real_cell(
const typename Triangulation<dim, spacedim>::cell_iterator &cell,
const Point<dim> & p) const
{
return Point<spacedim>(internal::evaluate_tensor_product_value_and_gradient(
polynomials_1d,
this->compute_mapping_support_points(cell),
p,
polynomials_1d.size() == 2,
renumber_lexicographic_to_hierarchic)
.first);
}
// In the code below, GCC tries to instantiate MappingQ<3,4> when
// seeing which of the overloaded versions of
// do_transform_real_to_unit_cell_internal() to call. This leads to bad
// error messages and, generally, nothing very good. Avoid this by ensuring
// that this class exists, but does not have an inner InternalData
// type, thereby ruling out the codim-1 version of the function
// below when doing overload resolution.
template <>
class MappingQ<3, 4>
{};
// visual studio freaks out when trying to determine if
// do_transform_real_to_unit_cell_internal with dim=3 and spacedim=4 is a good
// candidate. So instead of letting the compiler pick the correct overload, we
// use template specialization to make sure we pick up the right function to
// call:
template <int dim, int spacedim>
Point<dim>
MappingQ<dim, spacedim>::transform_real_to_unit_cell_internal(
const typename Triangulation<dim, spacedim>::cell_iterator &,
const Point<spacedim> &,
const Point<dim> &) const
{
// default implementation (should never be called)
Assert(false, ExcInternalError());
return {};
}
template <>
Point<1>
MappingQ<1, 1>::transform_real_to_unit_cell_internal(
const Triangulation<1, 1>::cell_iterator &cell,
const Point<1> & p,
const Point<1> & initial_p_unit) const
{
// dispatch to the various specializations for spacedim=dim,
// spacedim=dim+1, etc
return internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal<1>(
p,
initial_p_unit,
this->compute_mapping_support_points(cell),
polynomials_1d,
renumber_lexicographic_to_hierarchic);
}
template <>
Point<2>
MappingQ<2, 2>::transform_real_to_unit_cell_internal(
const Triangulation<2, 2>::cell_iterator &cell,
const Point<2> & p,
const Point<2> & initial_p_unit) const
{
return internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal<2>(
p,
initial_p_unit,
this->compute_mapping_support_points(cell),
polynomials_1d,
renumber_lexicographic_to_hierarchic);
}
template <>
Point<3>
MappingQ<3, 3>::transform_real_to_unit_cell_internal(
const Triangulation<3, 3>::cell_iterator &cell,
const Point<3> & p,
const Point<3> & initial_p_unit) const
{
return internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal<3>(
p,
initial_p_unit,
this->compute_mapping_support_points(cell),
polynomials_1d,
renumber_lexicographic_to_hierarchic);
}
template <>
Point<1>
MappingQ<1, 2>::transform_real_to_unit_cell_internal(
const Triangulation<1, 2>::cell_iterator &cell,
const Point<2> & p,
const Point<1> & initial_p_unit) const
{
const int dim = 1;
const int spacedim = 2;
const Quadrature<dim> point_quadrature(initial_p_unit);
UpdateFlags update_flags = update_quadrature_points | update_jacobians;
if (spacedim > dim)
update_flags |= update_jacobian_grads;
auto mdata = Utilities::dynamic_unique_cast<InternalData>(
get_data(update_flags, point_quadrature));
mdata->mapping_support_points = this->compute_mapping_support_points(cell);
// dispatch to the various specializations for spacedim=dim,
// spacedim=dim+1, etc
return internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal_codim1<1>(cell,
p,
initial_p_unit,
*mdata);
}
template <>
Point<2>
MappingQ<2, 3>::transform_real_to_unit_cell_internal(
const Triangulation<2, 3>::cell_iterator &cell,
const Point<3> & p,
const Point<2> & initial_p_unit) const
{
const int dim = 2;
const int spacedim = 3;
const Quadrature<dim> point_quadrature(initial_p_unit);
UpdateFlags update_flags = update_quadrature_points | update_jacobians;
if (spacedim > dim)
update_flags |= update_jacobian_grads;
auto mdata = Utilities::dynamic_unique_cast<InternalData>(
get_data(update_flags, point_quadrature));
mdata->mapping_support_points = this->compute_mapping_support_points(cell);
// dispatch to the various specializations for spacedim=dim,
// spacedim=dim+1, etc
return internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal_codim1<2>(cell,
p,
initial_p_unit,
*mdata);
}
template <>
Point<1>
MappingQ<1, 3>::transform_real_to_unit_cell_internal(
const Triangulation<1, 3>::cell_iterator &,
const Point<3> &,
const Point<1> &) const
{
Assert(false, ExcNotImplemented());
return {};
}
template <int dim, int spacedim>
Point<dim>
MappingQ<dim, spacedim>::transform_real_to_unit_cell(
const typename Triangulation<dim, spacedim>::cell_iterator &cell,
const Point<spacedim> & p) const
{
// Use an exact formula if one is available. this is only the case
// for Q1 mappings in 1d, and in 2d if dim==spacedim
if (this->preserves_vertex_locations() && (polynomial_degree == 1) &&
((dim == 1) || ((dim == 2) && (dim == spacedim))))
{
// The dimension-dependent algorithms are much faster (about 25-45x in
// 2D) but fail most of the time when the given point (p) is not in the
// cell. The dimension-independent Newton algorithm given below is
// slower, but more robust (though it still sometimes fails). Therefore
// this function implements the following strategy based on the
// p's dimension:
//
// * In 1D this mapping is linear, so the mapping is always invertible
// (and the exact formula is known) as long as the cell has non-zero
// length.
// * In 2D the exact (quadratic) formula is called first. If either the
// exact formula does not succeed (negative discriminant in the
// quadratic formula) or succeeds but finds a solution outside of the
// unit cell, then the Newton solver is called. The rationale for the
// second choice is that the exact formula may provide two different
// answers when mapping a point outside of the real cell, but the
// Newton solver (if it converges) will only return one answer.
// Otherwise the exact formula successfully found a point in the unit
// cell and that value is returned.
// * In 3D there is no (known to the authors) exact formula, so the Newton
// algorithm is used.
const auto vertices_ = this->get_vertices(cell);
std::array<Point<spacedim>, GeometryInfo<dim>::vertices_per_cell>
vertices;
for (unsigned int i = 0; i < vertices.size(); ++i)
vertices[i] = vertices_[i];
try
{
switch (dim)
{
case 1:
{
// formula not subject to any issues in 1d
if (spacedim == 1)
return internal::MappingQ1::transform_real_to_unit_cell(
vertices, p);
else
break;
}
case 2:
{
const Point<dim> point =
internal::MappingQ1::transform_real_to_unit_cell(vertices,
p);
// formula not guaranteed to work for points outside of
// the cell. only take the computed point if it lies
// inside the reference cell
const double eps = 1e-15;
if (-eps <= point(1) && point(1) <= 1 + eps &&
-eps <= point(0) && point(0) <= 1 + eps)
{
return point;
}
else
break;
}
default:
{
// we should get here, based on the if-condition at the top
Assert(false, ExcInternalError());
}
}
}
catch (
const typename Mapping<spacedim, spacedim>::ExcTransformationFailed &)
{
// simply fall through and continue on to the standard Newton code
}
}
else
{
// we can't use an explicit formula,
}
// Find the initial value for the Newton iteration by a normal
// projection to the least square plane determined by the vertices
// of the cell
Point<dim> initial_p_unit;
if (this->preserves_vertex_locations())
{
initial_p_unit = cell->real_to_unit_cell_affine_approximation(p);
// in 1d with spacedim > 1 the affine approximation is exact
if (dim == 1 && polynomial_degree == 1)
return initial_p_unit;
}
else
{
// else, we simply use the mid point
for (unsigned int d = 0; d < dim; ++d)
initial_p_unit[d] = 0.5;
}
// perform the Newton iteration and return the result. note that this
// statement may throw an exception, which we simply pass up to the caller
const Point<dim> p_unit =
this->transform_real_to_unit_cell_internal(cell, p, initial_p_unit);
if (p_unit[0] == std::numeric_limits<double>::infinity())
AssertThrow(false,
(typename Mapping<dim, spacedim>::ExcTransformationFailed()));
return p_unit;
}
template <int dim, int spacedim>
void
MappingQ<dim, spacedim>::transform_points_real_to_unit_cell(
const typename Triangulation<dim, spacedim>::cell_iterator &cell,
const ArrayView<const Point<spacedim>> & real_points,
const ArrayView<Point<dim>> & unit_points) const
{
// Go to base class functions for dim < spacedim because it is not yet
// implemented with optimized code.
if (dim < spacedim)
{
Mapping<dim, spacedim>::transform_points_real_to_unit_cell(cell,
real_points,
unit_points);
return;
}
AssertDimension(real_points.size(), unit_points.size());
const std::vector<Point<spacedim>> support_points =
this->compute_mapping_support_points(cell);
// From the given (high-order) support points, now only pick the first
// 2^dim points and construct an affine approximation from those.
internal::MappingQImplementation::InverseQuadraticApproximation<dim, spacedim>
inverse_approximation(support_points, unit_cell_support_points);
const unsigned int n_points = real_points.size();
const unsigned int n_lanes = VectorizedArray<double>::size();
// Use the more heavy VectorizedArray code path if there is more than
// one point left to compute
for (unsigned int i = 0; i < n_points; i += n_lanes)
if (n_points - i > 1)
{
Point<spacedim, VectorizedArray<double>> p_vec;
for (unsigned int j = 0; j < n_lanes; ++j)
if (i + j < n_points)
for (unsigned int d = 0; d < spacedim; ++d)
p_vec[d][j] = real_points[i + j][d];
else
for (unsigned int d = 0; d < spacedim; ++d)
p_vec[d][j] = real_points[i][d];
Point<dim, VectorizedArray<double>> unit_point =
internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal<dim, spacedim>(
p_vec,
inverse_approximation.compute(p_vec),
support_points,
polynomials_1d,
renumber_lexicographic_to_hierarchic);
// If the vectorized computation failed, it could be that only some of
// the lanes failed but others would have succeeded if we had let them
// compute alone without interference (like negative Jacobian
// determinants) from other SIMD lanes. Repeat the computation in this
// unlikely case with scalar arguments.
for (unsigned int j = 0; j < n_lanes && i + j < n_points; ++j)
if (unit_point[0][j] == std::numeric_limits<double>::infinity())
unit_points[i + j] = internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal<dim, spacedim>(
real_points[i + j],
inverse_approximation.compute(real_points[i + j]),
support_points,
polynomials_1d,
renumber_lexicographic_to_hierarchic);
else
for (unsigned int d = 0; d < dim; ++d)
unit_points[i + j][d] = unit_point[d][j];
}
else
unit_points[i] = internal::MappingQImplementation::
do_transform_real_to_unit_cell_internal<dim, spacedim>(
real_points[i],
inverse_approximation.compute(real_points[i]),
support_points,
polynomials_1d,
renumber_lexicographic_to_hierarchic);
}
template <int dim, int spacedim>
UpdateFlags
MappingQ<dim, spacedim>::requires_update_flags(const UpdateFlags in) const
{
// add flags if the respective quantities are necessary to compute
// what we need. note that some flags appear in both the conditions
// and in subsequent set operations. this leads to some circular
// logic. the only way to treat this is to iterate. since there are
// 5 if-clauses in the loop, it will take at most 5 iterations to
// converge. do them:
UpdateFlags out = in;
for (unsigned int i = 0; i < 5; ++i)
{
// The following is a little incorrect:
// If not applied on a face,
// update_boundary_forms does not
// make sense. On the other hand,
// it is necessary on a
// face. Currently,
// update_boundary_forms is simply
// ignored for the interior of a
// cell.
if ((out & (update_JxW_values | update_normal_vectors)) != 0u)
out |= update_boundary_forms;
if ((out & (update_covariant_transformation | update_JxW_values |
update_jacobians | update_jacobian_grads |
update_boundary_forms | update_normal_vectors)) != 0u)
out |= update_contravariant_transformation;
if ((out &
(update_inverse_jacobians | update_jacobian_pushed_forward_grads |
update_jacobian_pushed_forward_2nd_derivatives |
update_jacobian_pushed_forward_3rd_derivatives)) != 0u)
out |= update_covariant_transformation;
// The contravariant transformation is used in the Piola
// transformation, which requires the determinant of the Jacobi
// matrix of the transformation. Because we have no way of
// knowing here whether the finite element wants to use the
// contravariant or the Piola transforms, we add the JxW values
// to the list of flags to be updated for each cell.
if ((out & update_contravariant_transformation) != 0u)
out |= update_volume_elements;
// the same is true when computing normal vectors: they require
// the determinant of the Jacobian
if ((out & update_normal_vectors) != 0u)
out |= update_volume_elements;
}
return out;
}
template <int dim, int spacedim>
std::unique_ptr<typename Mapping<dim, spacedim>::InternalDataBase>
MappingQ<dim, spacedim>::get_data(const UpdateFlags update_flags,
const Quadrature<dim> &q) const
{
std::unique_ptr<typename Mapping<dim, spacedim>::InternalDataBase> data_ptr =
std::make_unique<InternalData>(polynomial_degree);
auto &data = dynamic_cast<InternalData &>(*data_ptr);
data.initialize(this->requires_update_flags(update_flags), q, q.size());
return data_ptr;
}
template <int dim, int spacedim>
std::unique_ptr<typename Mapping<dim, spacedim>::InternalDataBase>
MappingQ<dim, spacedim>::get_face_data(
const UpdateFlags update_flags,
const hp::QCollection<dim - 1> &quadrature) const
{
AssertDimension(quadrature.size(), 1);
std::unique_ptr<typename Mapping<dim, spacedim>::InternalDataBase> data_ptr =
std::make_unique<InternalData>(polynomial_degree);
auto &data = dynamic_cast<InternalData &>(*data_ptr);
data.initialize_face(this->requires_update_flags(update_flags),
QProjector<dim>::project_to_all_faces(
ReferenceCells::get_hypercube<dim>(), quadrature[0]),
quadrature[0].size());
return data_ptr;
}
template <int dim, int spacedim>
std::unique_ptr<typename Mapping<dim, spacedim>::InternalDataBase>
MappingQ<dim, spacedim>::get_subface_data(
const UpdateFlags update_flags,
const Quadrature<dim - 1> &quadrature) const
{
std::unique_ptr<typename Mapping<dim, spacedim>::InternalDataBase> data_ptr =
std::make_unique<InternalData>(polynomial_degree);
auto &data = dynamic_cast<InternalData &>(*data_ptr);
data.initialize_face(this->requires_update_flags(update_flags),
QProjector<dim>::project_to_all_subfaces(
ReferenceCells::get_hypercube<dim>(), quadrature),
quadrature.size());
return data_ptr;
}
template <int dim, int spacedim>
CellSimilarity::Similarity
MappingQ<dim, spacedim>::fill_fe_values(
const typename Triangulation<dim, spacedim>::cell_iterator &cell,
const CellSimilarity::Similarity cell_similarity,
const Quadrature<dim> & quadrature,
const typename Mapping<dim, spacedim>::InternalDataBase & internal_data,
internal::FEValuesImplementation::MappingRelatedData<dim, spacedim>
&output_data) const
{
// ensure that the following static_cast is really correct:
Assert(dynamic_cast<const InternalData *>(&internal_data) != nullptr,
ExcInternalError());
const InternalData &data = static_cast<const InternalData &>(internal_data);
const unsigned int n_q_points = quadrature.size();
// recompute the support points of the transformation of this
// cell. we tried to be clever here in an earlier version of the
// library by checking whether the cell is the same as the one we
// had visited last, but it turns out to be difficult to determine
// that because a cell for the purposes of a mapping is
// characterized not just by its (triangulation, level, index)
// triple, but also by the locations of its vertices, the manifold
// object attached to the cell and all of its bounding faces/edges,
// etc. to reliably test that the "cell" we are on is, therefore,
// not easily done
data.mapping_support_points = this->compute_mapping_support_points(cell);
data.cell_of_current_support_points = cell;
// if the order of the mapping is greater than 1, then do not reuse any cell
// similarity information. This is necessary because the cell similarity
// value is computed with just cell vertices and does not take into account
// cell curvature.
const CellSimilarity::Similarity computed_cell_similarity =
(polynomial_degree == 1 ? cell_similarity : CellSimilarity::none);
if (dim > 1 && data.tensor_product_quadrature)
{
internal::MappingQImplementation::
maybe_update_q_points_Jacobians_and_grads_tensor<dim, spacedim>(
computed_cell_similarity,
data,
output_data.quadrature_points,
output_data.jacobian_grads);
}
else
{
internal::MappingQImplementation::maybe_compute_q_points<dim, spacedim>(
QProjector<dim>::DataSetDescriptor::cell(),
data,
output_data.quadrature_points);
internal::MappingQImplementation::maybe_update_Jacobians<dim, spacedim>(
computed_cell_similarity,
QProjector<dim>::DataSetDescriptor::cell(),
data);