Line data Source code
1 : // The libMesh Finite Element Library.
2 : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 :
4 : // This library is free software; you can redistribute it and/or
5 : // modify it under the terms of the GNU Lesser General Public
6 : // License as published by the Free Software Foundation; either
7 : // version 2.1 of the License, or (at your option) any later version.
8 :
9 : // This library is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : // Lesser General Public License for more details.
13 :
14 : // You should have received a copy of the GNU Lesser General Public
15 : // License along with this library; if not, write to the Free Software
16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 :
18 :
19 : #include "libmesh/libmesh_config.h"
20 :
21 : // We'd like reproduceability here even when different FP rounding can
22 : // lead to different triangle splitting decisions
23 : #include "libmesh/enforce_ieee754.h"
24 :
25 : // libmesh includes
26 : #include "libmesh/mesh_triangle_interface.h"
27 : #include "libmesh/unstructured_mesh.h"
28 : #include "libmesh/face_tri3.h"
29 : #include "libmesh/face_tri6.h"
30 : #include "libmesh/mesh_generation.h"
31 : #include "libmesh/mesh_smoother_laplace.h"
32 : #include "libmesh/boundary_info.h"
33 : #include "libmesh/mesh_triangle_holes.h"
34 : #include "libmesh/mesh_triangle_wrapper.h"
35 : #include "libmesh/enum_elem_type.h"
36 : #include "libmesh/enum_order.h"
37 : #include "libmesh/enum_to_string.h"
38 : #include "libmesh/utility.h"
39 : #include "libmesh/elem_range.h"
40 :
41 : #include "libmesh/meshfree_interpolation.h"
42 :
43 : // C/C++ includes
44 : #include <limits>
45 : #include <sstream>
46 :
47 :
48 : namespace libMesh
49 : {
50 : //
51 : // Function definitions for the AutoAreaFunction class
52 : //
53 :
54 : // Constructor
55 0 : AutoAreaFunction::AutoAreaFunction (const Parallel::Communicator &comm,
56 : const unsigned int num_nearest_pts,
57 : const unsigned int power,
58 : const Real background_value,
59 0 : const Real background_eff_dist):
60 0 : _comm(comm),
61 0 : _num_nearest_pts(num_nearest_pts),
62 0 : _power(power),
63 0 : _background_value(background_value),
64 0 : _background_eff_dist(background_eff_dist),
65 0 : _auto_area_mfi(std::make_unique<InverseDistanceInterpolation<3>>(_comm, _num_nearest_pts, _power, _background_value, _background_eff_dist))
66 : {
67 0 : this->_initialized = false;
68 0 : this->_is_time_dependent = false;
69 0 : }
70 :
71 : // Destructor
72 0 : AutoAreaFunction::~AutoAreaFunction () = default;
73 :
74 0 : void AutoAreaFunction::init_mfi (const std::vector<Point> & input_pts,
75 : const std::vector<Real> & input_vals)
76 : {
77 0 : std::vector<std::string> field_vars{"f"};
78 0 : _auto_area_mfi->set_field_variables(field_vars);
79 0 : _auto_area_mfi->get_source_points() = input_pts;
80 : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
81 : std::vector<Number> input_complex_vals;
82 0 : for (const auto & input_val : input_vals)
83 0 : input_complex_vals.push_back(Complex (input_val, 0.0));
84 0 : _auto_area_mfi->get_source_vals() = input_complex_vals;
85 : #else
86 0 : _auto_area_mfi->get_source_vals() = input_vals;
87 : #endif
88 0 : _auto_area_mfi->prepare_for_use();
89 0 : this->_initialized = true;
90 0 : }
91 :
92 0 : Real AutoAreaFunction::operator() (const Point & p,
93 : const Real /*time*/)
94 : {
95 0 : libmesh_assert(this->_initialized);
96 :
97 0 : std::vector<Point> target_pts;
98 0 : std::vector<Number> target_vals;
99 :
100 0 : target_pts.push_back(p);
101 0 : target_vals.resize(1);
102 :
103 0 : _auto_area_mfi->interpolate_field_data(_auto_area_mfi->field_variables(), target_pts, target_vals);
104 :
105 0 : return libmesh_real(target_vals.front());
106 : }
107 :
108 : //
109 : // Function definitions for the TriangulatorInterface class
110 : //
111 :
112 : // Constructor
113 284 : TriangulatorInterface::TriangulatorInterface(UnstructuredMesh & mesh)
114 96 : : _mesh(mesh),
115 96 : _holes(nullptr),
116 96 : _markers(nullptr),
117 96 : _regions(nullptr),
118 96 : _elem_type(TRI3),
119 96 : _desired_area(0.1),
120 96 : _minimum_angle(20.0),
121 96 : _triangulation_type(GENERATE_CONVEX_HULL),
122 96 : _insert_extra_points(false),
123 96 : _smooth_after_generating(true),
124 96 : _quiet(true),
125 96 : _fixup_tri7_center_nodes(false),
126 472 : _auto_area_function(nullptr)
127 284 : {}
128 :
129 :
130 29 : void TriangulatorInterface::set_interpolate_boundary_points (int n_points)
131 : {
132 : // Maybe we'll reserve a meaning for negatives later?
133 10 : libmesh_assert(n_points >= 0);
134 :
135 29 : _interpolate_boundary_points = n_points;
136 :
137 : // backwards compatibility - someone (including us) might want to
138 : // query this via the old API.
139 29 : _insert_extra_points = n_points;
140 29 : }
141 :
142 :
143 :
144 700 : int TriangulatorInterface::get_interpolate_boundary_points () const
145 : {
146 : // backwards compatibility - someone might have turned this off via
147 : // the old API
148 700 : if (!_insert_extra_points)
149 298 : return 0;
150 :
151 29 : return _interpolate_boundary_points;
152 : }
153 :
154 :
155 :
156 721 : void TriangulatorInterface::elems_to_segments()
157 : {
158 : // Don't try to override manually specified segments
159 721 : if (!this->segments.empty())
160 4 : return;
161 :
162 : // If we have edges, they should form the polyline with the ordering
163 : // we want. Let's turn them into segments for later use, because
164 : // we're going to delete the original elements to replace with our
165 : // triangulation.
166 710 : if (_mesh.n_elem())
167 : {
168 : // Mapping from points to node ids, to back those out from
169 : // MeshedHole results later
170 56 : std::map<Point, dof_id_type> point_id_map;
171 :
172 801 : for (Node * node : _mesh.node_ptr_range())
173 : {
174 : // We're not going to support overlapping nodes on the boundary
175 651 : libmesh_error_msg_if
176 : (point_id_map.count(*node),
177 : "TriangulatorInterface does not support overlapping nodes found at "
178 : << static_cast<Point&>(*node));
179 :
180 651 : point_id_map.emplace(*node, node->id());
181 33 : }
182 :
183 : // We don't support directly generating Tri6, so for
184 : // compatibility with future stitching we need to be working
185 : // with first-order elements. Let's get rid of any non-vertex
186 : // nodes we just added.
187 738 : for (Elem * elem : _mesh.element_ptr_range())
188 616 : for (auto n : make_range(elem->n_vertices(), elem->n_nodes()))
189 145 : point_id_map.erase(elem->point(n));
190 :
191 : // We'll steal the ordering calculation from
192 : // the MeshedHole code
193 200 : const TriangulatorInterface::MeshedHole mh { _mesh, this->_bdy_ids };
194 :
195 : // If we've specified only a subset of the mesh as our outer
196 : // boundary, then we may have nodes that don't actually fall
197 : // inside that boundary. Triangulator code doesn't like Steiner
198 : // points that aren't inside the triangulation domain, so we
199 : // need to get rid of them.
200 : //
201 : // Also, if we're using Edge3 elements to define our outer
202 : // boundary, we're only dealing with their 2 end nodes and we'll
203 : // need to get rid of their central nodes.
204 44 : std::unordered_set<Node *> nodes_to_delete;
205 :
206 592 : for (Elem * elem : _mesh.element_ptr_range())
207 539 : for (auto n : make_range(elem->n_vertices(), elem->n_nodes()))
208 264 : nodes_to_delete.insert(elem->node_ptr(n));
209 :
210 68 : if (!this->_bdy_ids.empty())
211 : {
212 336 : for (auto & node : _mesh.node_ptr_range())
213 225 : if (!mh.contains(*node))
214 76 : nodes_to_delete.insert(node);
215 : }
216 :
217 : // And now we're done with elements. Delete them lest they have
218 : // dangling pointers to nodes we'll be deleting.
219 68 : _mesh.clear_elems();
220 :
221 : // Make segments from boundary nodes; also make sure we don't
222 : // delete them.
223 68 : const std::size_t np = mh.n_points();
224 333 : for (auto i : make_range(np))
225 : {
226 265 : const Point pt = mh.point(i);
227 265 : const dof_id_type id0 = libmesh_map_find(point_id_map, pt);
228 265 : nodes_to_delete.erase(_mesh.node_ptr(id0));
229 265 : const Point next_pt = mh.point((np+i+1)%np);
230 265 : const dof_id_type id1 = libmesh_map_find(point_id_map, next_pt);
231 265 : this->segments.emplace_back(id0, id1);
232 414 : for (auto m : make_range(mh.n_midpoints()))
233 : {
234 149 : this->segment_midpoints.emplace_back(mh.midpoint(m, i));
235 149 : this->segment_midpoints_keys.emplace_back(pt);
236 : }
237 : }
238 :
239 342 : for (Node * node : nodes_to_delete)
240 274 : _mesh.delete_node(node);
241 :
242 68 : if (this->_verify_hole_boundaries && _holes)
243 0 : this->verify_holes(mh);
244 24 : }
245 : }
246 :
247 :
248 :
249 700 : void TriangulatorInterface::nodes_to_segments(dof_id_type max_node_id)
250 : {
251 : // Don't try to override manually specified segments, or try to add
252 : // segments if we're doing a convex hull
253 700 : if (!this->segments.empty() || _triangulation_type != PSLG)
254 256 : return;
255 :
256 213 : for (auto node_it = _mesh.nodes_begin(),
257 213 : node_end = _mesh.nodes_end();
258 897 : node_it != node_end;)
259 : {
260 736 : Node * node = *node_it;
261 :
262 : // If we're out of boundary nodes, the rest are going to be
263 : // Steiner points or hole points
264 736 : if (node->id() >= max_node_id)
265 0 : break;
266 :
267 500 : ++node_it;
268 :
269 1236 : Node * next_node = (node_it == node_end) ?
270 345 : *_mesh.nodes_begin() : *node_it;
271 :
272 736 : this->segments.emplace_back(node->id(), next_node->id());
273 : }
274 :
275 161 : if (this->_verify_hole_boundaries && _holes)
276 : {
277 60 : std::vector<Point> outer_pts;
278 543 : for (auto segment : this->segments)
279 452 : outer_pts.push_back(_mesh.point(segment.first));
280 :
281 149 : ArbitraryHole ah(outer_pts);
282 91 : this->verify_holes(ah);
283 33 : }
284 : }
285 :
286 :
287 :
288 700 : void TriangulatorInterface::insert_any_extra_boundary_points()
289 : {
290 : // If the initial PSLG is really simple, e.g. an L-shaped domain or
291 : // a square/rectangle, the resulting triangulation may be very
292 : // "structured" looking. Sometimes this is a problem if your
293 : // intention is to work with an "unstructured" looking grid. We can
294 : // attempt to work around this limitation by inserting midpoints
295 : // into the original PSLG. Inserting additional points into a
296 : // set of points meant to be a convex hull usually makes less sense.
297 :
298 700 : const int n_interpolated = this->get_interpolate_boundary_points();
299 700 : if ((_triangulation_type==PSLG) && n_interpolated)
300 : {
301 : // If we were lucky enough to start with contiguous node ids,
302 : // let's keep them that way.
303 29 : dof_id_type nn = _mesh.max_node_id();
304 :
305 : std::vector<std::pair<unsigned int, unsigned int>> old_segments =
306 39 : std::move(this->segments);
307 :
308 : // We expect to have converted any elems and/or nodes into
309 : // segments by now.
310 10 : libmesh_assert(!old_segments.empty());
311 :
312 10 : this->segments.clear();
313 :
314 : // Insert a new point on each segment at evenly spaced locations
315 : // between existing boundary points.
316 : // np=index into new points vector
317 : // n =index into original points vector
318 145 : for (auto old_segment : old_segments)
319 : {
320 116 : Node * begin_node = _mesh.node_ptr(old_segment.first);
321 116 : Node * end_node = _mesh.node_ptr(old_segment.second);
322 116 : dof_id_type current_id = begin_node->id();
323 360 : for (auto i : make_range(n_interpolated))
324 : {
325 : // new points are equispaced along the original segments
326 : const Point new_point =
327 244 : ((n_interpolated-i) * *(Point *)(begin_node) +
328 244 : (i+1) * *(Point *)(end_node)) /
329 324 : (n_interpolated + 1);
330 244 : Node * next_node = _mesh.add_point(new_point, nn++);
331 84 : this->segments.emplace_back(current_id,
332 244 : next_node->id());
333 244 : current_id = next_node->id();
334 : }
335 36 : this->segments.emplace_back(current_id,
336 116 : end_node->id());
337 : }
338 : }
339 700 : }
340 :
341 :
342 204 : void TriangulatorInterface::increase_triangle_order()
343 : {
344 204 : switch (_elem_type)
345 : {
346 48 : case TRI3:
347 : // Nothing to do if we're not requested to increase order
348 168 : return;
349 29 : case TRI6:
350 29 : _mesh.all_second_order();
351 10 : break;
352 7 : case TRI7:
353 7 : _mesh.all_complete_order();
354 2 : break;
355 0 : default:
356 0 : libmesh_not_implemented();
357 : }
358 :
359 : // If we have any midpoint location data, we'll want to look it up
360 : // by point. all_midpoints[{p, m}] will be the mth midpoint
361 : // location following after point p (when traversing a triangle
362 : // counter-clockwise)
363 14 : std::map<std::pair<Point, unsigned int>, Point> all_midpoints;
364 : unsigned int n_midpoints =
365 60 : this->segment_midpoints.size() / this->segments.size();
366 12 : libmesh_assert_equal_to(this->segments.size() * n_midpoints,
367 : this->segment_midpoints.size());
368 61 : for (auto m : make_range(n_midpoints))
369 118 : for (auto i : make_range(this->segments.size()))
370 : {
371 93 : const Point & p = segment_midpoints_keys[i*n_midpoints+m];
372 96 : all_midpoints[{p,m}] =
373 60 : this->segment_midpoints[i*n_midpoints+m];
374 : }
375 :
376 36 : if (_holes)
377 22 : for (const Hole * hole : *_holes)
378 : {
379 11 : if (!hole->n_midpoints())
380 0 : continue;
381 11 : if (!n_midpoints)
382 11 : n_midpoints = hole->n_midpoints();
383 0 : else if (hole->n_midpoints() != n_midpoints)
384 0 : libmesh_not_implemented_msg
385 : ("Differing boundary midpoint counts " <<
386 : hole->n_midpoints() << " and " << n_midpoints);
387 :
388 : // Our inner holes are expected to have points in
389 : // counter-clockwise order, which is backwards from how we
390 : // want to traverse them when iterating in counter-clockwise
391 : // order over a triangle, so we'll need to reverse our maps
392 : // carefully here.
393 11 : const auto n_hole_points = hole->n_points();
394 4 : libmesh_assert(n_hole_points);
395 22 : for (auto m : make_range(n_midpoints))
396 : {
397 88 : for (auto i : make_range(n_hole_points-1))
398 : {
399 77 : const Point & p = hole->point(i+1);
400 77 : all_midpoints[{p,m}] = hole->midpoint(n_midpoints-m-1, i);
401 : }
402 11 : const Point & p = hole->point(0);
403 13 : all_midpoints[{p,m}] = hole->midpoint(n_midpoints-m-1, n_hole_points-1);
404 : }
405 : }
406 :
407 : // The n_midpoints > 1 case is for future proofing, but in the
408 : // present we have EDGE4 and no TRI10 yet.
409 36 : if (n_midpoints > 1)
410 0 : libmesh_not_implemented_msg
411 : ("Cannot construct triangles with more than 1 midpoint per edge");
412 :
413 36 : if (!n_midpoints)
414 0 : return;
415 :
416 : Threads::parallel_for_each
417 36 : (_mesh.element_stored_range(),
418 427 : [&all_midpoints](Elem * elem)
419 : {
420 : // This should only be called right after we've finished
421 : // converting a triangulation to higher order
422 62 : libmesh_assert_equal_to(elem->n_vertices(), 3);
423 62 : libmesh_assert_not_equal_to(elem->default_order(), FIRST);
424 :
425 700 : for (auto n : make_range(3))
426 : {
427 : // Only hole/outer boundary segments need adjusted
428 : // midpoints
429 711 : if (elem->neighbor_ptr(n))
430 192 : continue;
431 :
432 156 : const Point & p = elem->point(n);
433 :
434 303 : if (const auto it = all_midpoints.find({p,0});
435 78 : it != all_midpoints.end())
436 243 : elem->point(n+3) = it->second;
437 : }
438 175 : });
439 :
440 : // Moving boundary mid-edge nodes can displace the TRI7 interior node
441 : // and tangle the element map. Repositioning the interior node is
442 : // opt-in (off by default); the validity check always runs.
443 36 : if (_elem_type == TRI7 && _fixup_tri7_center_nodes)
444 7 : this->fixup_tri7_center_nodes();
445 :
446 36 : this->verify_quadratic_elements();
447 : }
448 :
449 :
450 7 : void TriangulatorInterface::fixup_tri7_center_nodes()
451 : {
452 2 : libmesh_assert_equal_to(_elem_type, TRI7);
453 :
454 : // Place the interior node at the image of the reference centroid
455 : // (xi, eta) = (1/3, 1/3) under the curved Tri6 map, using the Tri6
456 : // shape function values there as weights: -1/9 on the vertices and
457 : // 4/9 on the mid-edges. This reduces to the straight-edge centroid
458 : // when no boundary midpoint has moved.
459 : static const Real wv = -Real(1)/9;
460 : static const Real wm = Real(4)/9;
461 :
462 32 : for (Elem * elem : _mesh.element_ptr_range())
463 : {
464 4 : libmesh_assert_equal_to(elem->n_vertices(), 3);
465 4 : libmesh_assert_equal_to(elem->n_nodes(), 7u);
466 :
467 14 : elem->point(6) = wv * (elem->point(0) +
468 4 : elem->point(1) +
469 8 : elem->point(2)) +
470 0 : wm * (elem->point(3) +
471 8 : elem->point(4) +
472 16 : elem->point(5));
473 3 : }
474 7 : }
475 :
476 :
477 36 : void TriangulatorInterface::verify_quadratic_elements()
478 : {
479 36 : if (_elem_type != TRI6 && _elem_type != TRI7)
480 0 : return;
481 :
482 : // Once fixup_tri7_center_nodes() has placed node 6, the TRI6 and TRI7
483 : // mappings coincide and this Tri6 formula serves both.
484 : static const Real xi_samples[7] = {Real(0), Real(1), Real(0),
485 : Real(1)/2, Real(1)/2, Real(0),
486 : Real(1)/3};
487 : static const Real eta_samples[7] = {Real(0), Real(0), Real(1),
488 : Real(0), Real(1)/2, Real(1)/2,
489 : Real(1)/3};
490 :
491 285 : for (Elem * elem : _mesh.element_ptr_range())
492 : {
493 62 : libmesh_assert_equal_to(elem->n_vertices(), 3);
494 62 : libmesh_assert_greater_equal(elem->n_nodes(), 6u);
495 :
496 124 : const Point & x0 = elem->point(0);
497 62 : const Point & x1 = elem->point(1);
498 62 : const Point & x2 = elem->point(2);
499 62 : const Point & x3 = elem->point(3);
500 62 : const Point & x4 = elem->point(4);
501 62 : const Point & x5 = elem->point(5);
502 :
503 : // Tri6 mapping derivative coefficients (see Tri6::volume()):
504 : // dx/dxi = xi*a1 + eta*b1 + c1, dx/deta = xi*b1 + eta*b2 + c2.
505 62 : const Point a1 = 4*x0 + 4*x1 - 8*x3;
506 62 : const Point b1 = 4*x0 - 4*x3 + 4*x4 - 4*x5;
507 62 : const Point c1 = -3*x0 - 1*x1 + 4*x3;
508 62 : const Point b2 = 4*x0 + 4*x2 - 8*x5;
509 62 : const Point c2 = -3*x0 - 1*x2 + 4*x5;
510 :
511 : // Scale the tolerance by the straight-edge triangle area, which
512 : // is strictly positive for the valid TRI3 poly2tri input.
513 175 : const Real ref_area = 0.5 * cross_norm(x1 - x0, x2 - x0);
514 175 : const Real jac_tol = TOLERANCE * ref_area;
515 :
516 62 : Real min_jac = std::numeric_limits<Real>::max();
517 62 : unsigned int worst_sample = 0;
518 1400 : for (unsigned int s = 0; s != 7; ++s)
519 : {
520 1225 : const Real xi = xi_samples[s];
521 1225 : const Real eta = eta_samples[s];
522 434 : const Point dxi = xi*a1 + eta*b1 + c1;
523 434 : const Point deta = xi*b1 + eta*b2 + c2;
524 : // z-component of the cross product; the elements are planar.
525 1225 : const Real jac = dxi(0)*deta(1) - dxi(1)*deta(0);
526 1225 : if (jac < min_jac)
527 : {
528 100 : min_jac = jac;
529 100 : worst_sample = s;
530 : }
531 : }
532 :
533 175 : if (min_jac > jac_tol)
534 60 : continue;
535 :
536 : // Build a diagnostic naming every snapped boundary side on this
537 : // element so the user can immediately see which curved-boundary
538 : // input caused the tangle.
539 11 : std::ostringstream sides;
540 28 : for (unsigned int n = 0; n != 3; ++n)
541 27 : if (!elem->neighbor_ptr(n))
542 : {
543 : const Point straight =
544 21 : 0.5 * (elem->point(n) + elem->point((n+1) % 3));
545 15 : sides << " (boundary side " << n
546 15 : << ": straight midpoint " << straight
547 27 : << ", snapped midpoint " << elem->point(n+3) << ")";
548 : }
549 :
550 37 : libmesh_error_msg(
551 : "TriangulatorInterface: snapping a boundary midpoint produced a "
552 : "tangled quadratic triangle (element " << elem->id()
553 : << ", non-positive Jacobian " << min_jac
554 : << " at reference sample (" << xi_samples[worst_sample] << ", "
555 : << eta_samples[worst_sample] << "); reference triangle area "
556 : << ref_area << ")." << sides.str()
557 : << " Refine the boundary discretization so that recorded "
558 : "midpoints lie closer to their straight-line midpoints, "
559 : "then retry.");
560 15 : }
561 : }
562 :
563 :
564 91 : void TriangulatorInterface::verify_holes(const Hole & outer_bdy)
565 : {
566 249 : for (const Hole * hole : *_holes)
567 : {
568 846 : for (const Hole * hole2 : *_holes)
569 : {
570 688 : if (hole == hole2)
571 112 : continue;
572 :
573 3214 : for (auto i : make_range(hole2->n_points()))
574 2684 : if (hole->contains(hole2->point(i)))
575 0 : libmesh_error_msg
576 : ("Found point " << hole2->point(i) <<
577 : " on one hole boundary and another's interior");
578 : }
579 :
580 1160 : for (auto i : make_range(hole->n_points()))
581 1002 : if (!outer_bdy.contains(hole->point(i)))
582 0 : libmesh_error_msg
583 : ("Found point " << hole->point(i) <<
584 : " on hole boundary but outside outer boundary");
585 : }
586 91 : }
587 :
588 :
589 504 : unsigned int TriangulatorInterface::total_hole_points()
590 : {
591 : // If the holes vector is non-nullptr (and non-empty) we need to determine
592 : // the number of additional points which the holes will add to the
593 : // triangulation.
594 : // Note that the number of points is always equal to the number of segments
595 : // that form the holes.
596 252 : unsigned int n_hole_points = 0;
597 :
598 504 : if (_holes)
599 40 : for (const auto & hole : *_holes)
600 : {
601 24 : n_hole_points += hole->n_points();
602 : // A hole at least has one enclosure.
603 : // Points on enclosures are ordered so that we can add segments implicitly.
604 : // Elements in segment_indices() indicates the starting points of all enclosures.
605 : // The last element in segment_indices() is the number of total points.
606 12 : libmesh_assert_greater(hole->segment_indices().size(), 1);
607 12 : libmesh_assert_equal_to(hole->segment_indices().back(), hole->n_points());
608 : }
609 :
610 504 : return n_hole_points;
611 : }
612 :
613 0 : void TriangulatorInterface::set_auto_area_function(const Parallel::Communicator &comm,
614 : const unsigned int num_nearest_pts,
615 : const unsigned int power,
616 : const Real background_value,
617 : const Real background_eff_dist)
618 : {
619 0 : _auto_area_function = std::make_unique<AutoAreaFunction>(comm, num_nearest_pts, power, background_value, background_eff_dist);
620 0 : }
621 :
622 0 : FunctionBase<Real> * TriangulatorInterface::get_auto_area_function()
623 : {
624 0 : if (!_auto_area_function->initialized())
625 : {
626 : // Points and target element sizes for the interpolation
627 0 : std::vector<Point> function_points;
628 0 : std::vector<Real> function_sizes;
629 0 : calculate_auto_desired_area_samples(function_points, function_sizes);
630 0 : _auto_area_function->init_mfi(function_points, function_sizes);
631 : }
632 0 : return _auto_area_function.get();
633 : }
634 :
635 0 : void TriangulatorInterface::calculate_auto_desired_area_samples(std::vector<Point> & function_points,
636 : std::vector<Real> & function_sizes,
637 : const Real & area_factor)
638 : {
639 : // Get the hole mesh of the outer boundary
640 : // Holes should already be attached if applicable when this function is called
641 0 : const TriangulatorInterface::MeshedHole bdry_mh { _mesh, this->_bdy_ids };
642 : // Collect all the centroid points of the outer boundary segments
643 : // and the corresponding element sizes
644 0 : for (unsigned int i = 0; i < bdry_mh.n_points(); i++)
645 : {
646 0 : function_points.push_back((bdry_mh.point(i) + bdry_mh.point((i + 1) % bdry_mh.n_points())) /
647 0 : Real(2.0));
648 0 : function_sizes.push_back(
649 0 : (bdry_mh.point(i) - bdry_mh.point((i + 1) % bdry_mh.n_points())).norm());
650 : }
651 : // If holes are present, do the same for the hole boundaries
652 0 : if(_holes)
653 0 : for (const Hole * hole : *_holes)
654 : {
655 0 : for (unsigned int i = 0; i < hole->n_points(); i++)
656 : {
657 0 : function_points.push_back(
658 0 : (hole->point(i) + hole->point((i + 1) % hole->n_points())) / Real(2.0));
659 0 : function_sizes.push_back(
660 0 : (hole->point(i) - hole->point((i + 1) % hole->n_points())).norm());
661 : }
662 : }
663 :
664 0 : std::for_each(
665 0 : function_sizes.begin(), function_sizes.end(), [&area_factor](Real & a) { a = a * a * area_factor * std::sqrt(3.0) / 4.0; });
666 :
667 0 : }
668 : } // namespace libMesh
669 :
670 :
671 : // Unnecessary at end of file, *except* maybe we'll do a unity build
672 : // someday, and in the meantime test coverage is nice.
673 : #include "libmesh/restore_ieee754.h"
|