19#include "libmesh/variational_smoother_constraint.h"
20#include "libmesh/mesh_tools.h"
21#include "libmesh/boundary_info.h"
34 Point canonical{0, 0, 0};
39 canonical(dim_id) = 1.;
43 const auto dot_prod = vec * canonical;
46 return (dot_prod > 0) ? vec :
Point(-vec);
71 if (!o.contains_point(*
this))
84 "Can't define a line with zero magnitude direction vector.");
129 using T = std::decay_t<
decltype(o)>;
130 if constexpr (std::is_same_v<T, LineConstraint>)
148 const Real cross_dot = (delta.
cross(o.direction())) * cross_d1_d2;
151 const Real t = cross_dot / denom;
155 if (o.direction().cross(intersection - o.point()).norm() >
_tol)
162 else if constexpr (std::is_same_v<T, PlaneConstraint>)
163 return o.intersect(*
this);
165 else if constexpr (std::is_same_v<T, PointConstraint>)
175 libmesh_error_msg(
"Unsupported constraint type in Line::intersect.");
184 "Can't define a plane with zero magnitude direction vector.");
218 return std::abs(dist) <
_tol;
225 return base_on_plane && dir_orthogonal;
235 using T = std::decay_t<
decltype(o)>;
236 if constexpr (std::is_same_v<T, PlaneConstraint>)
264 const Real n2_dot_n2 = o.normal() * o.normal();
266 const Real n2_dot_w = o.normal() * w;
275 const Real s = -(n1_dot_n2 * n2_dot_w - n2_dot_n2 * n1_dot_w) / denom;
281 else if constexpr (std::is_same_v<T, LineConstraint>)
303 else if constexpr (std::is_same_v<T, PointConstraint>)
313 libmesh_error_msg(
"Unsupported constraint type in Plane::intersect.");
320 if (std::holds_alternative<PointConstraint>(c))
321 os <<
"point constraint: point=" << std::get<PointConstraint>(c).point();
323 else if (std::holds_alternative<LineConstraint>(c))
325 const auto & line = std::get<LineConstraint>(c);
326 os <<
"line constraint: point=" << line.point() <<
", direction=" << line.direction();
329 else if (std::holds_alternative<PlaneConstraint>(c))
331 const auto & plane = std::get<PlaneConstraint>(c);
332 os <<
"plane constraint: point=" << plane.point() <<
", normal=" << plane.normal();
335 else if (std::holds_alternative<InvalidConstraint>(c))
336 os <<
"invalid constraint";
342 System & sys,
const bool & preserve_subdomain_boundaries,
const unsigned int verbosity)
343 : Constraint(), _verbosity(verbosity), _sys(sys), _preserve_subdomain_boundaries(preserve_subdomain_boundaries) {
352 const auto & proc_id =
mesh.processor_id();
355 std::unordered_map<dof_id_type, std::vector<const Elem *>> nodes_to_elem_map;
358 const auto & boundary_info =
mesh.get_boundary_info();
363 std::unordered_map<dof_id_type, ConstraintVariant> subdomain_boundary_map;
366 for (
const auto * elem :
mesh.active_element_ptr_range())
368 const auto & sub_id1 = elem->subdomain_id();
369 for (
const auto side : elem->side_index_range())
371 const auto * neighbor = elem->neighbor_ptr(side);
372 if (neighbor ==
nullptr)
375 const auto & sub_id2 = neighbor->subdomain_id();
376 if (sub_id1 == sub_id2)
381 for (
const auto local_node_id : elem->nodes_on_side(side))
383 const auto & node =
mesh.node_ref(elem->node_id(local_node_id));
387 if (subdomain_boundary_map.count(node.id()) ||
388 node.processor_id() != proc_id)
392 const auto side_grouped_boundary_neighbors =
394 mesh, node, sub_id1, nodes_to_elem_map);
397 const auto subdomain_constraint =
402 if (boundary_node_ids.find(node.id()) == boundary_node_ids.end())
406 << std::endl <<
" " << node << std::endl
407 <<
" " << subdomain_constraint << std::endl;
417 subdomain_boundary_map[node.id()] = subdomain_constraint;
426 for (
const auto & bid : boundary_node_ids)
428 const auto & node =
mesh.node_ref(bid);
431 if (node.processor_id() != proc_id)
436 mesh, node, boundary_node_ids, boundary_info, nodes_to_elem_map);
439 const auto boundary_constraint =
443 if (
const auto it = subdomain_boundary_map.find(bid); it != subdomain_boundary_map.end())
445 const auto & subdomain_constraint = it->second;
452 if (std::holds_alternative<InvalidConstraint>(combined_constraint))
456 libMesh::out <<
"Imposing boundary/subdomain constraint on "
457 << std::endl <<
" " << node << std::endl
458 <<
" " << combined_constraint << std::endl;
467 << std::endl << node << std::endl
468 <<
" " << boundary_constraint << std::endl;
483 const auto constrained_value = node(d);
486 constrained_dof_index, constraint_row, constrained_value,
true);
491 const Point & ref_normal_vec)
495 std::vector<Real> xyz_coefs;
503 unsigned int constrained_dim = 0;
509 Real max_abs_coef = 0.;
512 const auto coef = ref_normal_vec(d);
513 xyz_coefs.push_back(coef);
516 const auto coef_abs = std::abs(coef);
517 if (coef_abs > max_abs_coef)
519 max_abs_coef = coef_abs;
527 if (free_dim == constrained_dim)
531 constraint_row[free_dof_index] = -xyz_coefs[free_dim] / xyz_coefs[constrained_dim];
534 const auto inhomogeneous_part = -c / xyz_coefs[constrained_dim];
537 constrained_dof_index, constraint_row, inhomogeneous_part,
true);
541 const Point & line_vec)
547 const std::vector<Real> line_vec_coefs{line_vec(0), line_vec(1), line_vec(2)};
548 auto it = std::max_element(line_vec_coefs.begin(), line_vec_coefs.end(), [](
double a,
double b) {
549 return std::abs(a) < std::abs(b);
551 const unsigned int free_dim = std::distance(line_vec_coefs.begin(), it);
567 if (constrained_dim == free_dim)
571 constraint_row[free_dof_index] = line_vec(constrained_dim) / line_vec(free_dim);
572 const auto inhomogeneous_part =
573 node(constrained_dim) - node(free_dim) * line_vec(constrained_dim) / line_vec(free_dim);
576 constrained_dof_index, constraint_row, inhomogeneous_part,
true);
608 const Node & neighbor_node,
609 const Elem & containing_elem,
612 bool nodes_share_bid =
false;
615 const auto node_id = containing_elem.
get_node_index(&boundary_node);
616 const auto neighbor_id = containing_elem.
get_node_index(&neighbor_node);
627 std::vector<boundary_id_type> boundary_ids;
629 if (boundary_ids.size())
631 nodes_share_bid =
true;
635 return nodes_share_bid;
638std::set<std::set<const Node *>>
643 const std::unordered_map<
dof_id_type, std::vector<const Elem *>> & nodes_to_elem_map)
649 std::vector<const Node *> neighbors;
651 mesh, node, nodes_to_elem_map, neighbors);
655 std::set<std::set<const Node *>> side_grouped_boundary_neighbors;
657 for (
const auto * neigh : neighbors)
661 const auto & elems_containing_node = libmesh_map_find(nodes_to_elem_map, node.
id());
662 const auto & elems_containing_neigh = libmesh_map_find(nodes_to_elem_map, neigh->id());
663 const Elem * common_elem =
nullptr;
664 for (
const auto * neigh_elem : elems_containing_neigh)
666 if ((std::find(elems_containing_node.begin(), elems_containing_node.end(), neigh_elem) !=
667 elems_containing_node.end())
669 && (neigh_elem->subdomain_id() == sub_id))
670 common_elem = neigh_elem;
678 bool node_found_on_side =
false;
679 bool neigh_found_on_side =
false;
680 for (
const auto local_node_id : common_elem->
nodes_on_side(common_side))
682 if (common_elem->
node_id(local_node_id) == node.
id())
683 node_found_on_side =
true;
684 else if (common_elem->
node_id(local_node_id) == neigh->id())
685 neigh_found_on_side =
true;
688 if (!(node_found_on_side && neigh_found_on_side &&
692 const auto matched_side = common_side;
698 const auto matched_neighbor_sub_id =
700 const bool is_matched_side_on_subdomain_boundary = matched_neighbor_sub_id != sub_id;
702 if (is_matched_side_on_subdomain_boundary)
705 const auto nodes_on_side = common_elem->
nodes_on_side(common_side);
706 std::set<const Node *> node_ptrs_on_side;
707 for (
const auto local_node_id : nodes_on_side)
708 node_ptrs_on_side.insert(common_elem->
node_ptr(local_node_id));
709 node_ptrs_on_side.erase(node_ptrs_on_side.find(&node));
710 side_grouped_boundary_neighbors.insert(node_ptrs_on_side);
720 return side_grouped_boundary_neighbors;
723std::set<std::set<const Node *>>
727 const std::unordered_set<dof_id_type> & boundary_node_ids,
729 const std::unordered_map<
dof_id_type, std::vector<const Elem *>> & nodes_to_elem_map)
735 std::vector<const Node *> neighbors;
737 mesh, node, nodes_to_elem_map, neighbors);
741 std::set<std::set<const Node *>> side_grouped_boundary_neighbors;
743 for (
const auto * neigh : neighbors)
745 const bool is_neighbor_boundary_node =
746 boundary_node_ids.find(neigh->id()) != boundary_node_ids.end();
747 if (!is_neighbor_boundary_node)
752 const auto & elems_containing_node = libmesh_map_find(nodes_to_elem_map, node.
id());
753 const auto & elems_containing_neigh = libmesh_map_find(nodes_to_elem_map, neigh->id());
754 const Elem * common_elem =
nullptr;
755 for (
const auto * neigh_elem : elems_containing_neigh)
757 const bool is_neigh_common =
758 std::find(elems_containing_node.begin(), elems_containing_node.end(), neigh_elem) !=
759 elems_containing_node.end();
760 if (!is_neigh_common)
762 common_elem = neigh_elem;
767 node, *neigh, *common_elem, boundary_info);
768 if (nodes_have_common_bid)
778 bool node_found_on_side =
false;
779 bool neigh_found_on_side =
false;
781 for (
const auto local_node_id : nodes_on_side)
783 if (common_elem->
node_id(local_node_id) == node.
id())
784 node_found_on_side =
true;
785 else if (common_elem->
node_id(local_node_id) == neigh->id())
786 neigh_found_on_side =
true;
788 if (!(node_found_on_side && neigh_found_on_side))
791 std::set<const Node *> node_ptrs_on_side;
792 for (
const auto local_node_id : nodes_on_side)
793 node_ptrs_on_side.insert(common_elem->
node_ptr(local_node_id));
794 node_ptrs_on_side.erase(node_ptrs_on_side.find(&node));
795 side_grouped_boundary_neighbors.insert(node_ptrs_on_side);
802 return side_grouped_boundary_neighbors;
807 const unsigned int dim,
808 const std::set<std::set<const Node *>> & side_grouped_boundary_neighbors)
814 std::vector<const Node *> neighbors;
815 for (
const auto & side : side_grouped_boundary_neighbors)
816 neighbors.insert(neighbors.end(), side.begin(), side.end());
822 if (
dim == 1 || neighbors.size() <= 1)
825 if (
dim == 2 || neighbors.size() == 2)
828 bool all_colinear =
true;
829 const Point ref_dir = (*neighbors[0] - node).unit();
830 for (
const auto i :
make_range(std::size_t(1), neighbors.size()))
832 const Point delta = *(neighbors[i]) - node;
837 all_colinear =
false;
849 std::set<PlaneConstraint> valid_planes;
850 for (
const auto & side_nodes : side_grouped_boundary_neighbors)
852 std::vector<const Node *> side_nodes_vec(side_nodes.begin(), side_nodes.end());
855 const Point vec_i = (*side_nodes_vec[i] - node);
858 const Point vec_j = (*side_nodes_vec[j] - node);
864 valid_planes.emplace(candidate_plane);
870 if (valid_planes.empty())
874 auto it = valid_planes.begin();
876 for (; it != valid_planes.end(); ++it)
883 if (std::holds_alternative<InvalidConstraint>(current))
899 libmesh_assert_msg(!std::holds_alternative<InvalidConstraint>(constraint),
900 "Cannot impose constraint using InvalidConstraint.");
902 if (std::holds_alternative<PointConstraint>(constraint))
904 else if (std::holds_alternative<LineConstraint>(constraint))
906 else if (std::holds_alternative<PlaneConstraint>(constraint))
910 libmesh_assert_msg(
false,
"Unknown constraint type.");
The BoundaryInfo class contains information relevant to boundary conditions including storing faces,...
void boundary_ids(const Node *node, std::vector< boundary_id_type > &vec_to_fill) const
Fills a user-provided std::vector with the boundary ids associated with Node node.
void add_constraint_row(const dof_id_type dof_number, const DofConstraintRow &constraint_row, const Number constraint_rhs, const bool forbid_constraint_overwrite)
Adds a copy of the user-defined row to the constraint matrix, using an inhomogeneous right-hand-side ...
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
This is the base class from which all geometric element types are derived.
virtual bool is_node_on_side(const unsigned int n, const unsigned int s) const =0
virtual std::vector< unsigned int > nodes_on_side(const unsigned int) const =0
subdomain_id_type subdomain_id() const
const Node * node_ptr(const unsigned int i) const
unsigned int get_node_index(const Node *node_ptr) const
dof_id_type node_id(const unsigned int i) const
const Elem * neighbor_ptr(unsigned int i) const
IntRange< unsigned short > side_index_range() const
Represents an invalid constraint (i.e., when the two constraints don't intersect)
Represents a line constraint defined by a base point and direction vector.
bool contains_point(const PointConstraint &p) const
Query whether a point lies on the line.
const Point & direction() const
Const getter for the _direction attribute.
bool operator<(const LineConstraint &other) const
Comparison operator for ordering LineConstraint objects.
bool operator==(const LineConstraint &other) const
Equality operator.
bool is_parallel(const LineConstraint &l) const
Query whether a line is parallel to this line.
const Point & point() const
Const getter for the _point attribute.
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.
ConstraintVariant intersect(const ConstraintVariant &other) const
Computes the intersection of this line with another constraint.
Point _point
A point on the constraining line.
This is the MeshBase class.
unsigned int mesh_dimension() const
A Node is like a Point, but with more information.
Represents a plane constraint defined by a point and normal vector.
PlaneConstraint()=default
const Point & point() const
Const getter for the _point attribute.
const Point & normal() const
Const getter for the _normal attribute.
bool operator==(const PlaneConstraint &other) const
Equality operator.
Point _point
A point on the constraining plane.
Point _normal
The direction normal to the constraining plane.
ConstraintVariant intersect(const ConstraintVariant &other) const
Computes the intersection of this plane with another constraint.
bool is_parallel(const PlaneConstraint &p) const
Query whether a plane is parallel to this plane.
bool contains_line(const LineConstraint &l) const
Query whether a line lies on the plane.
bool contains_point(const PointConstraint &p) const
Query whether a point lies on the plane.
Real _tol
Tolerance to use for numerical comparisons.
bool operator<(const PlaneConstraint &other) const
Comparison operator for ordering PlaneConstraint objects.
Represents a fixed point constraint.
bool operator==(const PointConstraint &other) const
Equality operator.
const Point & point() const
Const getter for the _point attribute.
bool operator<(const PointConstraint &other) const
Comparison operator for ordering PointConstraint objects.
Point _point
Location of constraint.
PointConstraint()=default
ConstraintVariant intersect(const ConstraintVariant &other) const
Computes the intersection of this point with another constraint.
Real _tol
Tolerance to use for numerical comparisons.
A Point defines a location in LIBMESH_DIM dimensional Real space.
Manages consistently variables, degrees of freedom, and coefficient vectors.
const DofMap & get_dof_map() const
unsigned int number() const
const MeshBase & get_mesh() const
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
TypeVector< typename CompareTypes< T, T2 >::supertype > cross(const TypeVector< T2 > &v) const
LIBMESH_DEVICE_INLINE TypeVector< T > unit() const
bool relative_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
static std::set< std::set< const Node * > > get_neighbors_for_subdomain_constraint(const MeshBase &mesh, const Node &node, const subdomain_id_type sub_id, const std::unordered_map< dof_id_type, std::vector< const Elem * > > &nodes_to_elem_map)
Get the relevant nodal neighbors for a subdomain constraint.
const unsigned int _verbosity
Verbosity setting.
void fix_node(const Node &node)
Constrain (i.e., fix) a node to not move during mesh smoothing.
void impose_constraint(const Node &node, const ConstraintVariant &constraint)
Applies a given constraint to a node (e.g., fixing it, restricting it to a line or plane).
virtual void constrain() override
Constraint function.
void constrain_node_to_plane(const Node &node, const Point &ref_normal_vec)
Constrain a node to remain in the given plane during mesh smoothing.
VariationalSmootherConstraint(System &sys, const bool &preserve_subdomain_boundaries, const unsigned int verbosity)
Constructor.
static bool nodes_share_boundary_id(const Node &boundary_node, const Node &neighbor_node, const Elem &containing_elem, const BoundaryInfo &boundary_info)
Determines whether two neighboring nodes share a common boundary id.
virtual ~VariationalSmootherConstraint() override
const bool _preserve_subdomain_boundaries
Whether nodes on subdomain boundaries are subject to change via smoothing.
static ConstraintVariant determine_constraint(const Node &node, const unsigned int dim, const std::set< std::set< const Node * > > &side_grouped_boundary_neighbors)
Determines the appropriate constraint (PointConstraint, LineConstraint, or PlaneConstraint) for a nod...
void constrain_node_to_line(const Node &node, const Point &line_vec)
Constrain a node to remain on the given line during mesh smoothing.
static std::set< std::set< const Node * > > get_neighbors_for_boundary_constraint(const MeshBase &mesh, const Node &node, const std::unordered_set< dof_id_type > &boundary_node_ids, const BoundaryInfo &boundary_info, const std::unordered_map< dof_id_type, std::vector< const Elem * > > &nodes_to_elem_map)
Get the relevant nodal neighbors for an external boundary constraint.
The libMesh namespace provides an interface to certain functionality in the library.
bool relative_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within a relative tolerance.
bool absolute_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
Function to check whether two variables are equal within an absolute tolerance.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
std::variant< PointConstraint, LineConstraint, PlaneConstraint, InvalidConstraint > ConstraintVariant
Type used to store a constraint that may be a PlaneConstraint, LineConstraint, or PointConstraint.
std::map< dof_id_type, Real, std::less< dof_id_type >, Threads::scalable_allocator< std::pair< const dof_id_type, Real > > > DofConstraintRow
A row of the Dof constraint matrix.
static constexpr Real TOLERANCE
ConstraintVariant intersect_constraints(const ConstraintVariant &a, const ConstraintVariant &b)
Dispatch intersection between two constraint variants.
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...
const boundary_id_type side_id