/
quadrature_generator.cc
1358 lines (1114 loc) · 48.1 KB
/
quadrature_generator.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) 2021 - 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/function_tools.h>
#include <deal.II/grid/reference_cell.h>
#include <deal.II/non_matching/quadrature_generator.h>
#include <boost/math/special_functions/relative_difference.hpp>
#include <boost/math/special_functions/sign.hpp>
#include <boost/math/tools/roots.hpp>
#include <algorithm>
#include <vector>
DEAL_II_NAMESPACE_OPEN
namespace NonMatching
{
namespace internal
{
namespace QuadratureGeneratorImplementation
{
template <int dim>
void
tensor_point_with_1D_quadrature(const Point<dim - 1> &point,
const double weight,
const Quadrature<1> & quadrature1D,
const double start,
const double end,
const unsigned int component_in_dim,
ExtendableQuadrature<dim> &quadrature)
{
Assert(start < end,
ExcMessage("Interval start must be less than interval end."));
const double length = end - start;
for (unsigned int j = 0; j < quadrature1D.size(); ++j)
{
const double x = start + length * quadrature1D.point(j)[0];
quadrature.push_back(dealii::internal::create_higher_dim_point(
point, component_in_dim, x),
length * weight * quadrature1D.weight(j));
}
}
/**
* For each (point, weight) in lower create a dim-dimensional quadrature
* using tensor_point_with_1D_quadrature and add the results to @p quadrature.
*/
template <int dim>
void
add_tensor_product(const Quadrature<dim - 1> &lower,
const Quadrature<1> & quadrature1D,
const double start,
const double end,
const unsigned int component_in_dim,
ExtendableQuadrature<dim> &quadrature)
{
for (unsigned int j = 0; j < lower.size(); ++j)
{
tensor_point_with_1D_quadrature(lower.point(j),
lower.weight(j),
quadrature1D,
start,
end,
component_in_dim,
quadrature);
}
}
template <int dim>
Definiteness
pointwise_definiteness(
const std::vector<std::reference_wrapper<const Function<dim>>>
& functions,
const Point<dim> &point)
{
Assert(functions.size() > 0,
ExcMessage(
"The incoming vector must contain at least one function."));
const int sign_of_first =
boost::math::sign(functions[0].get().value(point));
if (sign_of_first == 0)
return Definiteness::indefinite;
for (unsigned int j = 1; j < functions.size(); ++j)
{
const int sign = boost::math::sign(functions[j].get().value(point));
if (sign != sign_of_first)
return Definiteness::indefinite;
}
// If we got here all functions have the same sign.
if (sign_of_first < 0)
return Definiteness::negative;
else
return Definiteness::positive;
}
/**
* Given the incoming lower and upper bounds on the value of a function
* $[L, U]$, return the minimum/maximum of $[L, U]$ and the function
* values at the vertices. That is, this function returns
*
* $[\min(L, L_f), \max(U, U_f)]$,
*
* where $L_f = \min_{v} f(x_v)$, $U_f = \max_{v} f(x_v)|$,
* and $x_v$ is a vertex.
*
* It is assumed that the incoming function is scalar valued.
*/
template <int dim>
void
take_min_max_at_vertices(const Function<dim> & function,
const BoundingBox<dim> & box,
std::pair<double, double> &value_bounds)
{
const ReferenceCell &cube = ReferenceCells::get_hypercube<dim>();
for (unsigned int i = 0; i < cube.n_vertices(); ++i)
{
const double vertex_value = function.value(box.vertex(i));
value_bounds.first = std::min(value_bounds.first, vertex_value);
value_bounds.second = std::max(value_bounds.second, vertex_value);
}
}
/**
* Estimate bounds on each of the functions in the incoming vector over
* the incoming box.
*
* Bounds on the functions value and the gradient components are first
* computed using FunctionTools::taylor_estimate_function_bounds.
* In addition, the function value is checked for min/max at the at
* the vertices of the box. The gradient is not checked at the box
* vertices.
*/
template <int dim>
void
estimate_function_bounds(
const std::vector<std::reference_wrapper<const Function<dim>>>
& functions,
const BoundingBox<dim> & box,
std::vector<FunctionBounds<dim>> &all_function_bounds)
{
all_function_bounds.clear();
all_function_bounds.reserve(functions.size());
for (const Function<dim> &function : functions)
{
FunctionBounds<dim> bounds;
FunctionTools::taylor_estimate_function_bounds<dim>(
function, box, bounds.value, bounds.gradient);
take_min_max_at_vertices(function, box, bounds.value);
all_function_bounds.push_back(bounds);
}
}
template <int dim>
std::pair<double, double>
find_extreme_values(const std::vector<FunctionBounds<dim>> &bounds)
{
Assert(bounds.size() > 0, ExcMessage("The incoming vector is empty."));
std::pair<double, double> extremes = bounds[0].value;
for (unsigned int i = 1; i < bounds.size(); ++i)
{
extremes.first = std::min(extremes.first, bounds[i].value.first);
extremes.second = std::max(extremes.second, bounds[i].value.second);
}
return extremes;
}
/**
* Return true if the incoming function bounds correspond to a function
* which is indefinite, i.e., that is not negative or positive definite.
*/
inline bool
is_indefinite(const std::pair<double, double> &function_bounds)
{
if (function_bounds.first > 0)
return false;
if (function_bounds.second < 0)
return false;
return true;
}
/**
* Return a lower bound, $L_a$, on the absolute value of a function,
* $f(x)$:
*
* $L_a \leq |f(x)|$,
*
* by estimating it from the incoming lower and upper bounds:
* $L \leq f(x) \leq U$.
*
* By rewriting the lower and upper bounds as
* $F - C \leq f(x) \leq F + C$,
* where $L = F - C$, $U = F + C$ (or $F = (U + L)/2$, $C = (U - L)/2$),
* we get $|f(x) - F| \leq C$.
* Using the inverse triangle inequality gives
* $|F| - |f(x)| \leq |f(x) - F| \leq C$.
* Thus, $L_a = |F| - C$.
*
* Note that the returned value can be negative. This is used to indicate
* "how far away" a function is from being definite.
*/
inline double
lower_bound_on_abs(const std::pair<double, double> &function_bounds)
{
Assert(function_bounds.first <= function_bounds.second,
ExcMessage("Function bounds reversed, max < min."));
return 0.5 * (std::abs(function_bounds.second + function_bounds.first) -
(function_bounds.second - function_bounds.first));
}
HeightDirectionData::HeightDirectionData()
{
direction = numbers::invalid_unsigned_int;
min_abs_dfdx = 0;
}
template <int dim>
std_cxx17::optional<HeightDirectionData>
find_best_height_direction(
const std::vector<FunctionBounds<dim>> &all_function_bounds)
{
// Minimum (taken over the indefinite functions) on the lower bound on
// each component of the gradient.
std_cxx17::optional<std::array<double, dim>> min_lower_abs_grad;
for (const FunctionBounds<dim> &bounds : all_function_bounds)
{
if (is_indefinite(bounds.value))
{
// For the first indefinite function we find, we write the lower
// bounds on each gradient component to min_lower_abs_grad.
if (!min_lower_abs_grad)
{
min_lower_abs_grad.emplace();
for (int d = 0; d < dim; ++d)
{
(*min_lower_abs_grad)[d] =
lower_bound_on_abs(bounds.gradient[d]);
}
}
else
{
for (int d = 0; d < dim; ++d)
{
(*min_lower_abs_grad)[d] =
std::min((*min_lower_abs_grad)[d],
lower_bound_on_abs(bounds.gradient[d]));
}
}
}
}
if (min_lower_abs_grad)
{
const auto max_element =
std::max_element(min_lower_abs_grad->begin(),
min_lower_abs_grad->end());
HeightDirectionData data;
data.direction =
std::distance(min_lower_abs_grad->begin(), max_element);
data.min_abs_dfdx = *max_element;
return data;
}
return std_cxx17::optional<HeightDirectionData>();
}
/**
* Return true if there are exactly two incoming FunctionBounds and
* they corresponds to one function being positive definite and
* one being negative definite. Return false otherwise.
*/
template <int dim>
inline bool
one_positive_one_negative_definite(
const std::vector<FunctionBounds<dim>> &all_function_bounds)
{
if (all_function_bounds.size() != 2)
return false;
else
{
const FunctionBounds<dim> &bounds0 = all_function_bounds.at(0);
const FunctionBounds<dim> &bounds1 = all_function_bounds.at(1);
if (bounds0.value.first > 0 && bounds1.value.second < 0)
return true;
if (bounds1.value.first > 0 && bounds0.value.second < 0)
return true;
return false;
}
}
/**
* Transform the points and weights of the incoming quadrature,
* unit_quadrature, from unit space to the incoming box and add these to
* quadrature.
*
* Note that unit_quadrature should be a quadrature over [0,1]^dim.
*/
template <int dim>
void
map_quadrature_to_box(const Quadrature<dim> & unit_quadrature,
const BoundingBox<dim> & box,
ExtendableQuadrature<dim> &quadrature)
{
for (unsigned int i = 0; i < unit_quadrature.size(); i++)
{
const Point<dim> point = box.unit_to_real(unit_quadrature.point(i));
const double weight = unit_quadrature.weight(i) * box.volume();
quadrature.push_back(point, weight);
}
}
/**
* For each of the incoming dim-dimensional functions, create the
* restriction to the top and bottom of the incoming BoundingBox and add
* these two (dim-1)-dimensional functions to @p restrictions. Here, top and bottom is
* meant with respect to the incoming @p direction. For each function, the
* "bottom-restriction" will be added before the "top-restriction"
*
* @note @p restrictions will be cleared, so after this function
* restrictions.size() == 2 * functions.size().
*/
template <int dim>
void
restrict_to_top_and_bottom(
const std::vector<std::reference_wrapper<const Function<dim>>>
& functions,
const BoundingBox<dim> & box,
const unsigned int direction,
std::vector<Functions::CoordinateRestriction<dim - 1>> &restrictions)
{
AssertIndexRange(direction, dim);
restrictions.clear();
restrictions.reserve(2 * functions.size());
const double bottom = box.lower_bound(direction);
const double top = box.upper_bound(direction);
for (const auto &function : functions)
{
restrictions.push_back(Functions::CoordinateRestriction<dim - 1>(
function, direction, bottom));
restrictions.push_back(Functions::CoordinateRestriction<dim - 1>(
function, direction, top));
}
}
/**
* Restrict each of the incoming @p functions to @p point,
* while keeping the coordinate direction @p open_direction open,
* and add the restriction to @p restrictions.
*
* @note @p restrictions will be cleared, so after this function
* restrictions.size() == functions.size().
*/
template <int dim>
void
restrict_to_point(
const std::vector<std::reference_wrapper<const Function<dim>>>
& functions,
const Point<dim - 1> & point,
const unsigned int open_direction,
std::vector<Functions::PointRestriction<dim - 1>> &restrictions)
{
AssertIndexRange(open_direction, dim);
restrictions.clear();
restrictions.reserve(functions.size());
for (const auto &function : functions)
{
restrictions.push_back(Functions::PointRestriction<dim - 1>(
function, open_direction, point));
}
}
/**
* Let $\{ y_0, ..., y_{n+1} \}$ be such that $[y_0, y_{n+1}]$ is the
* @p interval and $\{ y_1, ..., y_n \}$ are the @p roots. In each
* subinterval, $[y_i, y_{i+1}]$, distribute point according to the
* 1D-quadrature rule $\{(x_q, w_q)\}_q$ (@p quadrature1D).
* Take the tensor product with the quadrature point $(x, w)$
* (@p point, @p weight) to create dim-dimensional quadrature points
* @f[
* X_q = x_I \times (y_i + (y_{i+1} - y_i) x_q),
* W_q = w_I (y_{i+1} - y_i) w_q,
* @f]
* and add these points to @p q_partitioning.
*/
template <int dim>
void
distribute_points_between_roots(
const Quadrature<1> & quadrature1D,
const BoundingBox<1> & interval,
const std::vector<double> &roots,
const Point<dim - 1> & point,
const double weight,
const unsigned int height_function_direction,
const std::vector<std::reference_wrapper<const Function<1>>>
& level_sets,
const AdditionalQGeneratorData &additional_data,
QPartitioning<dim> & q_partitioning)
{
// Make this int to avoid a warning signed/unsigned comparision.
const int n_roots = roots.size();
// The number of intervals are roots.size() + 1
for (int i = -1; i < n_roots; ++i)
{
// Start and end point of the subinterval.
const double start = i < 0 ? interval.lower_bound(0) : roots[i];
const double end =
i + 1 < n_roots ? roots[i + 1] : interval.upper_bound(0);
const double length = end - start;
// It might be that the end points of the subinterval are roots.
// If this is the case then the subinterval has length zero.
// Don't distribute points on the subinterval if it is shorter than
// some tolerance.
if (length > additional_data.min_distance_between_roots)
{
// All points on the interval belong to the same region in
// the QPartitioning. Determine the quadrature we should add
// the points to.
const Point<1> center(start + 0.5 * length);
const Definiteness definiteness =
pointwise_definiteness(level_sets, center);
ExtendableQuadrature<dim> &target_quadrature =
q_partitioning.quadrature_by_definiteness(definiteness);
tensor_point_with_1D_quadrature(point,
weight,
quadrature1D,
start,
end,
height_function_direction,
target_quadrature);
}
}
}
RootFinder::AdditionalData::AdditionalData(
const double tolerance,
const unsigned int max_recursion_depth,
const unsigned int max_iterations)
: tolerance(tolerance)
, max_recursion_depth(max_recursion_depth)
, max_iterations(max_iterations)
{}
RootFinder::RootFinder(const AdditionalData &data)
: additional_data(data)
{}
void
RootFinder::find_roots(
const std::vector<std::reference_wrapper<const Function<1>>> &functions,
const BoundingBox<1> & interval,
std::vector<double> & roots)
{
for (const Function<1> &function : functions)
{
const unsigned int recursion_depth = 0;
find_roots(function, interval, recursion_depth, roots);
}
// Sort and make sure no roots are duplicated
std::sort(roots.begin(), roots.end());
const auto roots_are_equal = [this](const double &a, const double &b) {
return std::abs(a - b) < additional_data.tolerance;
};
roots.erase(unique(roots.begin(), roots.end(), roots_are_equal),
roots.end());
}
void
RootFinder::find_roots(const Function<1> & function,
const BoundingBox<1> &interval,
const unsigned int recursion_depth,
std::vector<double> & roots)
{
// Compute function values at end points.
const double left_value = function.value(interval.vertex(0));
const double right_value = function.value(interval.vertex(1));
// If we have a sign change we solve for the root.
if (boost::math::sign(left_value) != boost::math::sign(right_value))
{
const auto lambda = [&function](const double x) {
return function.value(Point<1>(x));
};
const auto stopping_criteria = [this](const double &a,
const double &b) {
return std::abs(a - b) < additional_data.tolerance;
};
boost::uintmax_t iterations = additional_data.max_iterations;
const std::pair<double, double> root_bracket =
boost::math::tools::toms748_solve(lambda,
interval.lower_bound(0),
interval.upper_bound(0),
left_value,
right_value,
stopping_criteria,
iterations);
const double root = .5 * (root_bracket.first + root_bracket.second);
roots.push_back(root);
}
else
{
// Compute bounds on the incoming function to check if there are
// roots. If the function is positive or negative on the whole
// interval we do nothing.
std::pair<double, double> value_bounds;
std::array<std::pair<double, double>, 1> gradient_bounds;
FunctionTools::taylor_estimate_function_bounds<1>(function,
interval,
value_bounds,
gradient_bounds);
// Since we already know the function values at the interval ends we
// might as well check these for min/max too.
const double function_min =
std::min(std::min(left_value, right_value), value_bounds.first);
// If the functions is positive there are no roots.
if (function_min > 0)
return;
const double function_max =
std::max(std::max(left_value, right_value), value_bounds.second);
// If the functions is negative there are no roots.
if (function_max < 0)
return;
// If we can't say that the function is strictly positive/negative
// we split the interval in half. We can't split forever, so if we
// have reached the max recursion, we stop looking for roots.
if (recursion_depth < additional_data.max_recursion_depth)
{
find_roots(function,
interval.child(0),
recursion_depth + 1,
roots);
find_roots(function,
interval.child(1),
recursion_depth + 1,
roots);
}
}
}
template <int dim>
ExtendableQuadrature<dim>::ExtendableQuadrature(
const Quadrature<dim> &quadrature)
: Quadrature<dim>(quadrature)
{}
template <int dim>
void
ExtendableQuadrature<dim>::push_back(const Point<dim> &point,
const double weight)
{
this->quadrature_points.push_back(point);
this->weights.push_back(weight);
}
template <int dim>
ExtendableQuadrature<dim> &
QPartitioning<dim>::quadrature_by_definiteness(
const Definiteness definiteness)
{
switch (definiteness)
{
case Definiteness::negative:
return negative;
case Definiteness::positive:
return positive;
default:
return indefinite;
}
}
/**
* Takes a (dim-1)-dimensional point from the cross-section (orthogonal
* to direction) of the box. Creates the two dim-dimensional points, which
* are the projections from the cross section to the faces of the box and
* returns the point closest to the zero-contour of the incoming level set
* function.
*/
template <int dim>
Point<dim>
face_projection_closest_zero_contour(const Point<dim - 1> & point,
const unsigned int direction,
const BoundingBox<dim> &box,
const Function<dim> & level_set)
{
const Point<dim> bottom_point =
dealii::internal::create_higher_dim_point(point,
direction,
box.lower_bound(direction));
const double bottom_value = level_set.value(bottom_point);
const Point<dim> top_point =
dealii::internal::create_higher_dim_point(point,
direction,
box.upper_bound(direction));
const double top_value = level_set.value(top_point);
// The end point closest to the zero-contour is the one with smallest
// absolute value.
return std::abs(bottom_value) < std::abs(top_value) ? bottom_point :
top_point;
}
template <int dim, int spacedim>
UpThroughDimensionCreator<dim, spacedim>::UpThroughDimensionCreator(
const hp::QCollection<1> & q_collection1D,
const AdditionalQGeneratorData &additional_data)
: q_collection1D(&q_collection1D)
, additional_data(additional_data)
, root_finder(
RootFinder::AdditionalData(additional_data.root_finder_tolerance,
additional_data.max_root_finder_splits))
{
q_index = 0;
}
template <int dim, int spacedim>
void
UpThroughDimensionCreator<dim, spacedim>::generate(
const std::vector<std::reference_wrapper<const Function<dim>>>
& level_sets,
const BoundingBox<dim> & box,
const Quadrature<dim - 1> &low_dim_quadrature,
const unsigned int height_function_direction,
QPartitioning<dim> & q_partitioning)
{
const Quadrature<1> &quadrature1D = (*q_collection1D)[q_index];
for (unsigned int q = 0; q < low_dim_quadrature.size(); ++q)
{
const Point<dim - 1> &point = low_dim_quadrature.point(q);
const double weight = low_dim_quadrature.weight(q);
restrict_to_point(level_sets,
point,
height_function_direction,
point_restrictions);
// We need a vector of references to do the recursive call.
const std::vector<std::reference_wrapper<const Function<1>>>
restrictions(point_restrictions.begin(),
point_restrictions.end());
const BoundingBox<1> bounds_in_direction =
box.bounds(height_function_direction);
roots.clear();
root_finder.find_roots(restrictions, bounds_in_direction, roots);
distribute_points_between_roots(quadrature1D,
bounds_in_direction,
roots,
point,
weight,
height_function_direction,
restrictions,
additional_data,
q_partitioning);
if (dim == spacedim)
create_surface_point(point,
weight,
level_sets,
box,
height_function_direction,
q_partitioning.surface);
}
point_restrictions.clear();
}
template <int dim, int spacedim>
void
UpThroughDimensionCreator<dim, spacedim>::create_surface_point(
const Point<dim - 1> &point,
const double weight,
const std::vector<std::reference_wrapper<const Function<dim>>>
& level_sets,
const BoundingBox<dim> & box,
const unsigned int height_function_direction,
ImmersedSurfaceQuadrature<dim> &surface_quadrature)
{
AssertIndexRange(roots.size(), 2);
Assert(level_sets.size() == 1, ExcInternalError());
const Function<dim> &level_set = level_sets.at(0);
Point<dim> surface_point;
if (roots.size() == 1)
{
surface_point = dealii::internal::create_higher_dim_point(
point, height_function_direction, roots[0]);
}
else
{
// If we got here, we have missed roots in the lower dimensional
// algorithm. This is a rare event but can happen if the
// zero-contour has a high curvature. The problem is that the
// incoming point has been incorrectly added to the indefinite
// quadrature in QPartitioning<dim-1>. Since we missed a root on
// this box, we will likely miss it on the neighboring box too. If
// this happens, the point will NOT be in the indefinite quadrature
// on the neighbor. The best thing we can do is to compute the
// surface point by projecting the lower dimensional point to the
// face closest to the zero-contour. We should add a surface point
// because the neighbor will not.
surface_point = face_projection_closest_zero_contour(
point, height_function_direction, box, level_set);
}
const Tensor<1, dim> gradient = level_set.gradient(surface_point);
Tensor<1, dim> normal = gradient;
normal *= 1. / normal.norm();
// Note that gradient[height_function_direction] is non-zero
// because of the implicit function theorem.
const double surface_weight =
weight * gradient.norm() /
std::abs(gradient[height_function_direction]);
surface_quadrature.push_back(surface_point, surface_weight, normal);
}
template <int dim, int spacedim>
void
UpThroughDimensionCreator<dim, spacedim>::set_1D_quadrature(
unsigned int q_index)
{
AssertIndexRange(q_index, q_collection1D->size());
this->q_index = q_index;
}
template <int dim, int spacedim>
QGeneratorBase<dim, spacedim>::QGeneratorBase(
const hp::QCollection<1> & q_collection1D,
const AdditionalQGeneratorData &additional_data)
: additional_data(additional_data)
, q_collection1D(&q_collection1D)
{
q_index = 0;
}
template <int dim, int spacedim>
QGenerator<dim, spacedim>::QGenerator(
const hp::QCollection<1> & q_collection1D,
const AdditionalQGeneratorData &additional_data)
: QGeneratorBase<dim, spacedim>(q_collection1D, additional_data)
, low_dim_algorithm(q_collection1D, additional_data)
, up_through_dimension_creator(q_collection1D, additional_data)
{
for (unsigned int i = 0; i < q_collection1D.size(); i++)
tensor_products.push_back(Quadrature<dim>(q_collection1D[i]));
}
template <int dim, int spacedim>
void
QGeneratorBase<dim, spacedim>::clear_quadratures()
{
q_partitioning = QPartitioning<dim>();
}
template <int dim, int spacedim>
const QPartitioning<dim> &
QGeneratorBase<dim, spacedim>::get_quadratures() const
{
return q_partitioning;
}
template <int dim, int spacedim>
void
QGenerator<dim, spacedim>::generate(
const std::vector<std::reference_wrapper<const Function<dim>>>
& level_sets,
const BoundingBox<dim> &box,
const unsigned int n_box_splits)
{
std::vector<FunctionBounds<dim>> all_function_bounds;
estimate_function_bounds(level_sets, box, all_function_bounds);
const std::pair<double, double> extreme_values =
find_extreme_values(all_function_bounds);
if (extreme_values.first > this->additional_data.limit_to_be_definite)
{
map_quadrature_to_box(tensor_products[this->q_index],
box,
this->q_partitioning.positive);
}
else if (extreme_values.second <
-(this->additional_data.limit_to_be_definite))
{
map_quadrature_to_box(tensor_products[this->q_index],
box,
this->q_partitioning.negative);
}
else if (one_positive_one_negative_definite(all_function_bounds))
{
map_quadrature_to_box(tensor_products[this->q_index],
box,
this->q_partitioning.indefinite);
}
else
{
const std_cxx17::optional<HeightDirectionData> data =
find_best_height_direction(all_function_bounds);
// Check larger than a constant to avoid that min_abs_dfdx is only
// larger by 0 by floating point precision.
if (data && data->min_abs_dfdx >
this->additional_data.lower_bound_implicit_function)
{
create_low_dim_quadratures(data->direction,
level_sets,
box,
n_box_splits);
create_high_dim_quadratures(data->direction, level_sets, box);
}
else if (n_box_splits < this->additional_data.max_box_splits)
{
split_box_and_recurse(level_sets, box, data, n_box_splits);
}
else
{
// We can't split the box recursively forever. Use the midpoint
// method as a last resort.
use_midpoint_method(level_sets, box);
}
}
}
/**
* Return the coordinate direction of the largest side of the box.
* If two or more sides have the same length the returned std::optional
* will be non-set.
*/
template <int dim>
std_cxx17::optional<unsigned int>
direction_of_largest_extent(const BoundingBox<dim> &box)
{
// Get the side lengths for each direction and sort them.
std::array<std::pair<double, unsigned int>, dim> side_lengths;
for (int i = 0; i < dim; i++)
{
side_lengths[i].first = box.side_length(i);
side_lengths[i].second = i;
}
// Sort is lexicographic, so this sorts based on side length first.
std::sort(side_lengths.begin(), side_lengths.end());
// Check if the two largest side lengths have the same length. This
// function isn't called in 1D, so the (dim - 2)-element exists.
if (boost::math::epsilon_difference(side_lengths[dim - 1].first,
side_lengths[dim - 2].first) < 100)
return std_cxx17::optional<unsigned int>();
return side_lengths.back().second;
}
/**
* Return the coordinate direction that the box should be split in,
* assuming that the box should be split it half.
*
* If the box is larger in one coordante direction, this direction is
* returned. If the box have the same extent in all directions, we choose
* the coordinate direction which is closest to being a height-function
* direction. That is, the direction $i$ that has a least negative
* estimate of $|\partial_i \psi_j|$. As a last resort, we choose the
* direction 0, if @p height_direction_data non-set.
*/
template <int dim>
unsigned int
compute_split_direction(
const BoundingBox<dim> & box,
const std_cxx17::optional<HeightDirectionData> &height_direction_data)
{
const std_cxx17::optional<unsigned int> direction =
direction_of_largest_extent(box);
if (direction)
return *direction;
// This direction is closest to being a height direction, so
// we split in this direction.
if (height_direction_data)
return height_direction_data->direction;
// We have to choose some direction, we might aswell take 0.
return 0;
}