19#include "libmesh/libmesh_config.h"
23#include "libmesh/enforce_ieee754.h"
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"
41#include "libmesh/meshfree_interpolation.h"
56 const unsigned int num_nearest_pts,
57 const unsigned int power,
58 const Real background_value,
59 const Real background_eff_dist):
61 _num_nearest_pts(num_nearest_pts),
63 _background_value(background_value),
64 _background_eff_dist(background_eff_dist),
75 const std::vector<Real> & input_vals)
77 std::vector<std::string> field_vars{
"f"};
80#ifdef LIBMESH_USE_COMPLEX_NUMBERS
81 std::vector<Number> input_complex_vals;
82 for (
const auto & input_val : input_vals)
83 input_complex_vals.push_back(
Complex (input_val, 0.0));
97 std::vector<Point> target_pts;
98 std::vector<Number> target_vals;
100 target_pts.push_back(p);
101 target_vals.resize(1);
120 _minimum_angle(20.0),
121 _triangulation_type(GENERATE_CONVEX_HULL),
122 _insert_extra_points(false),
123 _smooth_after_generating(true),
125 _fixup_tri7_center_nodes(false),
126 _auto_area_function(nullptr)
170 std::map<Point, dof_id_type> point_id_map;
172 for (
Node * node :
_mesh.node_ptr_range())
176 (point_id_map.count(*node),
177 "TriangulatorInterface does not support overlapping nodes found at "
178 <<
static_cast<Point&
>(*node));
180 point_id_map.emplace(*node, node->id());
187 for (
Elem * elem :
_mesh.element_ptr_range())
188 for (
auto n :
make_range(elem->n_vertices(), elem->n_nodes()))
189 point_id_map.erase(elem->point(n));
204 std::unordered_set<Node *> nodes_to_delete;
206 for (
Elem * elem :
_mesh.element_ptr_range())
207 for (
auto n :
make_range(elem->n_vertices(), elem->n_nodes()))
208 nodes_to_delete.insert(elem->node_ptr(n));
212 for (
auto & node :
_mesh.node_ptr_range())
213 if (!mh.contains(*node))
214 nodes_to_delete.insert(node);
223 const std::size_t np = mh.n_points();
226 const Point pt = mh.point(i);
227 const dof_id_type id0 = libmesh_map_find(point_id_map, pt);
229 const Point next_pt = mh.point((np+i+1)%np);
230 const dof_id_type id1 = libmesh_map_find(point_id_map, next_pt);
231 this->
segments.emplace_back(id0, id1);
239 for (
Node * node : nodes_to_delete)
256 for (
auto node_it =
_mesh.nodes_begin(),
257 node_end =
_mesh.nodes_end();
258 node_it != node_end;)
260 Node * node = *node_it;
264 if (node->
id() >= max_node_id)
269 Node * next_node = (node_it == node_end) ?
270 *
_mesh.nodes_begin() : *node_it;
272 this->
segments.emplace_back(node->
id(), next_node->
id());
277 std::vector<Point> outer_pts;
279 outer_pts.push_back(
_mesh.
point(segment.first));
305 std::vector<std::pair<unsigned int, unsigned int>> old_segments =
318 for (
auto old_segment : old_segments)
326 const Point new_point =
327 ((n_interpolated-i) * *(
Point *)(begin_node) +
328 (i+1) * *(
Point *)(end_node)) /
329 (n_interpolated + 1);
331 this->
segments.emplace_back(current_id,
333 current_id = next_node->
id();
335 this->
segments.emplace_back(current_id,
356 libmesh_not_implemented();
363 std::map<std::pair<Point, unsigned int>,
Point> all_midpoints;
364 unsigned int n_midpoints =
366 libmesh_assert_equal_to(this->
segments.size() * n_midpoints,
367 this->segment_midpoints.size());
372 all_midpoints[{p,m}] =
379 if (!hole->n_midpoints())
382 n_midpoints = hole->n_midpoints();
383 else if (hole->n_midpoints() != n_midpoints)
384 libmesh_not_implemented_msg
385 (
"Differing boundary midpoint counts " <<
386 hole->n_midpoints() <<
" and " << n_midpoints);
393 const auto n_hole_points = hole->n_points();
399 const Point & p = hole->point(i+1);
400 all_midpoints[{p,m}] = hole->midpoint(n_midpoints-m-1, i);
402 const Point & p = hole->point(0);
403 all_midpoints[{p,m}] = hole->midpoint(n_midpoints-m-1, n_hole_points-1);
410 libmesh_not_implemented_msg
411 (
"Cannot construct triangles with more than 1 midpoint per edge");
418 [&all_midpoints](
Elem * elem)
422 libmesh_assert_equal_to(elem->n_vertices(), 3);
423 libmesh_assert_not_equal_to(elem->default_order(), FIRST);
425 for (auto n : make_range(3))
429 if (elem->neighbor_ptr(n))
432 const Point & p = elem->point(n);
434 if (const auto it = all_midpoints.find({p,0});
435 it != all_midpoints.end())
436 elem->point(n+3) = it->second;
443 if (_elem_type ==
TRI7 && _fixup_tri7_center_nodes)
444 this->fixup_tri7_center_nodes();
446 this->verify_quadratic_elements();
450void TriangulatorInterface::fixup_tri7_center_nodes()
452 libmesh_assert_equal_to(_elem_type,
TRI7);
462 for (
Elem * elem : _mesh.element_ptr_range())
464 libmesh_assert_equal_to(elem->n_vertices(), 3);
465 libmesh_assert_equal_to(elem->n_nodes(), 7u);
467 elem->point(6) = wv * (elem->point(0) +
470 wm * (elem->point(3) +
477void TriangulatorInterface::verify_quadratic_elements()
479 if (_elem_type !=
TRI6 && _elem_type !=
TRI7)
491 for (
Elem * elem : _mesh.element_ptr_range())
493 libmesh_assert_equal_to(elem->n_vertices(), 3);
494 libmesh_assert_greater_equal(elem->n_nodes(), 6u);
496 const Point & x0 = elem->point(0);
497 const Point & x1 = elem->point(1);
498 const Point & x2 = elem->point(2);
499 const Point & x3 = elem->point(3);
500 const Point & x4 = elem->point(4);
501 const Point & x5 = elem->point(5);
505 const Point a1 = 4*x0 + 4*x1 - 8*x3;
506 const Point b1 = 4*x0 - 4*x3 + 4*x4 - 4*x5;
507 const Point c1 = -3*x0 - 1*x1 + 4*x3;
508 const Point b2 = 4*x0 + 4*x2 - 8*x5;
509 const Point c2 = -3*x0 - 1*x2 + 4*x5;
514 const Real jac_tol = TOLERANCE * ref_area;
516 Real min_jac = std::numeric_limits<Real>::max();
517 unsigned int worst_sample = 0;
518 for (
unsigned int s = 0; s != 7; ++s)
520 const Real xi = xi_samples[s];
521 const Real eta = eta_samples[s];
522 const Point dxi = xi*a1 + eta*b1 + c1;
523 const Point deta = xi*b1 + eta*b2 + c2;
525 const Real jac = dxi(0)*deta(1) - dxi(1)*deta(0);
533 if (min_jac > jac_tol)
539 std::ostringstream sides;
540 for (
unsigned int n = 0; n != 3; ++n)
541 if (!elem->neighbor_ptr(n))
543 const Point straight =
544 0.5 * (elem->point(n) + elem->point((n+1) % 3));
545 sides <<
" (boundary side " << n
546 <<
": straight midpoint " << straight
547 <<
", snapped midpoint " << elem->point(n+3) <<
")";
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, "
564void TriangulatorInterface::verify_holes(
const Hole & outer_bdy)
566 for (
const Hole * hole : *_holes)
568 for (
const Hole * hole2 : *_holes)
574 if (hole->contains(hole2->point(i)))
576 (
"Found point " << hole2->point(i) <<
577 " on one hole boundary and another's interior");
581 if (!outer_bdy.
contains(hole->point(i)))
583 (
"Found point " << hole->point(i) <<
584 " on hole boundary but outside outer boundary");
589unsigned int TriangulatorInterface::total_hole_points()
596 unsigned int n_hole_points = 0;
599 for (
const auto & hole : *_holes)
601 n_hole_points += hole->n_points();
606 libmesh_assert_greater(hole->segment_indices().size(), 1);
607 libmesh_assert_equal_to(hole->segment_indices().back(), hole->n_points());
610 return n_hole_points;
614 const unsigned int num_nearest_pts,
615 const unsigned int power,
616 const Real background_value,
617 const Real background_eff_dist)
619 _auto_area_function = std::make_unique<AutoAreaFunction>(comm, num_nearest_pts, power, background_value, background_eff_dist);
624 if (!_auto_area_function->initialized())
627 std::vector<Point> function_points;
628 std::vector<Real> function_sizes;
629 calculate_auto_desired_area_samples(function_points, function_sizes);
630 _auto_area_function->init_mfi(function_points, function_sizes);
632 return _auto_area_function.get();
635void TriangulatorInterface::calculate_auto_desired_area_samples(std::vector<Point> & function_points,
636 std::vector<Real> & function_sizes,
637 const Real & area_factor)
644 for (
unsigned int i = 0; i < bdry_mh.n_points(); i++)
646 function_points.push_back((bdry_mh.point(i) + bdry_mh.point((i + 1) % bdry_mh.n_points())) /
648 function_sizes.push_back(
649 (bdry_mh.point(i) - bdry_mh.point((i + 1) % bdry_mh.n_points())).norm());
653 for (
const Hole * hole : *_holes)
655 for (
unsigned int i = 0; i < hole->n_points(); i++)
657 function_points.push_back(
658 (hole->point(i) + hole->point((i + 1) % hole->n_points())) /
Real(2.0));
659 function_sizes.push_back(
660 (hole->point(i) - hole->point((i + 1) % hole->n_points())).norm());
665 function_sizes.begin(), function_sizes.end(), [&area_factor](
Real & a) { a = a * a * area_factor * std::sqrt(3.0) / 4.0; });
673#include "libmesh/restore_ieee754.h"
AutoAreaFunction(const Parallel::Communicator &comm, const unsigned int num_nearest_pts, const unsigned int power, const Real background_value, const Real background_eff_dist)
void init_mfi(const std::vector< Point > &input_pts, const std::vector< Real > &input_vals)
std::unique_ptr< InverseDistanceInterpolation< 3 > > _auto_area_mfi
virtual Real operator()(const Point &p, const Real) override
virtual ~AutoAreaFunction()
This is the base class from which all geometric element types are derived.
Base class for functors that can be evaluated at a point and (optionally) time.
bool _initialized
When init() was called so that everything is ready for calls to operator() (...), then this bool is t...
bool _is_time_dependent
Cache whether or not this function is actually time-dependent.
Inverse distance interpolation.
virtual const Point & point(const dof_id_type i) const =0
virtual const Node * node_ptr(const dof_id_type i) const =0
virtual dof_id_type n_elem() const =0
void all_second_order(const bool full_ordered=true)
Calls the range-based version of this function with a range consisting of all elements in the mesh.
virtual void delete_node(Node *n)=0
Removes the Node n from the mesh.
const ElemRange & element_stored_range()
virtual dof_id_type max_node_id() const =0
virtual Node * add_point(const Point &p, const dof_id_type id=DofObject::invalid_id, const processor_id_type proc_id=DofObject::invalid_processor_id)=0
Add a new Node at Point p to the end of the vertex array, with processor_id procid.
virtual void clear_elems()=0
Deletes all the element data that is currently stored.
virtual void all_complete_order()
Calls the range-based version of this function with a range consisting of all elements in the mesh.
A Node is like a Point, but with more information.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Another concrete instantiation of the hole, this one should be sufficiently general for most non-poly...
An abstract class for defining a 2-dimensional hole.
bool contains(Point p) const
Return true iff p lies inside the hole.
Another concrete instantiation of the hole, as general as ArbitraryHole, but based on an existing 1D ...
void increase_triangle_order()
Helper function to upconvert Tri3 to any higher order triangle type if requested via _elem_type.
bool _verify_hole_boundaries
Flag which tells if we want to check hole geometry.
ElemType _elem_type
The type of elements to generate.
void nodes_to_segments(dof_id_type max_node_id)
Helper function to create PSLG segments from our node ordering, up to the maximum node id,...
std::set< std::size_t > _bdy_ids
A set of ids to allow on the outer boundary loop.
void elems_to_segments()
Helper function to create PSLG segments from our other boundary-defining options (1D mesh edges,...
int _interpolate_boundary_points
Flag which tells how many additional nodes should be inserted between each pair of original mesh poin...
void insert_any_extra_boundary_points()
Helper function to add extra points (midpoints of initial segments) to a PSLG triangulation.
@ PSLG
Triangulate the interior of a Planar Straight Line Graph, which is defined implicitly by the order of...
const std::vector< Hole * > * _holes
A pointer to a vector of Hole*s.
TriangulatorInterface(UnstructuredMesh &mesh)
The constructor.
std::vector< std::pair< unsigned int, unsigned int > > segments
When constructing a PSLG, if the node numbers do not define the desired boundary segments implicitly ...
UnstructuredMesh & _mesh
Reference to the mesh which is to be created by triangle.
int get_interpolate_boundary_points() const
Complicated getter, for compatibility with insert_extra_points()
void verify_holes(const Hole &outer_bdy)
Helper function to check holes for intersections if requested.
std::vector< Point > segment_midpoints
When constructing a second-order triangulation from a second-order boundary, we may do the triangulat...
TriangulationType _triangulation_type
The type of triangulation to perform: choices are: convex hull PSLG.
std::vector< Point > segment_midpoints_keys
When saving the midpoint location data, we need to save the corresponding segment information too.
bool _insert_extra_points
Flag which tells whether or not to insert additional nodes before triangulation.
void set_interpolate_boundary_points(int n_points)
Complicated setter, for compatibility with insert_extra_points()
The UnstructuredMesh class is derived from the MeshBase class.
void parallel_for_each(Range &&range, Body &&body, unsigned int n_threads=libMesh::n_threads())
A nicer syntax for parallel_for calls in cases where no setup or teardown is needed outside the subra...
The libMesh namespace provides an interface to certain functionality in the library.
LIBMESH_DEVICE_INLINE T cross_norm(const TypeVector< T > &b, const TypeVector< T > &c)
Calls cross_norm_sq() and takes the square root of the result.
std::complex< Real > Complex
LIBMESH_DEVICE_INLINE T libmesh_real(T a)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...