libMesh
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
libMesh::VariationalSmootherConstraint Class Reference

Constraint class for the VariationalMeshSmoother. More...

#include <variational_smoother_constraint.h>

Inheritance diagram for libMesh::VariationalSmootherConstraint:
[legend]

Public Member Functions

 VariationalSmootherConstraint (System &sys, const bool &preserve_subdomain_boundaries, const unsigned int verbosity)
 Constructor.
 
virtual ~VariationalSmootherConstraint () override
 
virtual void constrain () override
 Constraint function.
 

Private Member Functions

void fix_node (const Node &node)
 Constrain (i.e., fix) a node to not move during mesh smoothing.
 
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.
 
void constrain_node_to_line (const Node &node, const Point &line_vec)
 Constrain a node to remain on the given line 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).
 

Static Private Member Functions

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.
 
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.
 
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.
 
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 node based on its neighbors.
 

Private Attributes

const unsigned int _verbosity
 Verbosity setting.
 
System & _sys
 
const bool _preserve_subdomain_boundaries
 Whether nodes on subdomain boundaries are subject to change via smoothing.
 

Detailed Description

Constraint class for the VariationalMeshSmoother.

Currently, all mesh boundary nodes are constrained to not move during smoothing. If requested (preserve_subdomain_boundaries = true), nodes on subdomain boundaries are also constrained to not move.

Definition at line 389 of file variational_smoother_constraint.h.

Constructor & Destructor Documentation

◆ VariationalSmootherConstraint()

libMesh::VariationalSmootherConstraint::VariationalSmootherConstraint ( System &  sys,
const bool &  preserve_subdomain_boundaries,
const unsigned int  verbosity 
)

Constructor.

Parameters
sysSystem to constrain.
preserve_subdomain_boundariesWhether to constrain nodes on subdomain boundaries to not move.

Definition at line 341 of file variational_smoother_constraint.C.

343 : Constraint(), _verbosity(verbosity), _sys(sys), _preserve_subdomain_boundaries(preserve_subdomain_boundaries) {
344}
const unsigned int _verbosity
Verbosity setting.
const bool _preserve_subdomain_boundaries
Whether nodes on subdomain boundaries are subject to change via smoothing.

◆ ~VariationalSmootherConstraint()

libMesh::VariationalSmootherConstraint::~VariationalSmootherConstraint ( )
overridevirtualdefault

Member Function Documentation

◆ constrain()

void libMesh::VariationalSmootherConstraint::constrain ( )
overridevirtual

Constraint function.

This function will be called to constrain the system prior to a solve and must be provided by the user in a derived class.

Implements libMesh::System::Constraint.

Definition at line 348 of file variational_smoother_constraint.C.

349{
350 const auto & mesh = _sys.get_mesh();
351 const auto dim = mesh.mesh_dimension();
352 const auto & proc_id = mesh.processor_id();
353
354 // Only compute the node to elem map once
355 std::unordered_map<dof_id_type, std::vector<const Elem *>> nodes_to_elem_map;
356 MeshTools::build_nodes_to_elem_map(mesh, nodes_to_elem_map);
357
358 const auto & boundary_info = mesh.get_boundary_info();
359
360 const auto boundary_node_ids = MeshTools::find_boundary_nodes(mesh);
361
362 // Identify/constrain subdomain boundary nodes, if requested
363 std::unordered_map<dof_id_type, ConstraintVariant> subdomain_boundary_map;
365 {
366 for (const auto * elem : mesh.active_element_ptr_range())
367 {
368 const auto & sub_id1 = elem->subdomain_id();
369 for (const auto side : elem->side_index_range())
370 {
371 const auto * neighbor = elem->neighbor_ptr(side);
372 if (neighbor == nullptr)
373 continue;
374
375 const auto & sub_id2 = neighbor->subdomain_id();
376 if (sub_id1 == sub_id2)
377 continue;
378
379 // elem and neighbor are in different subdomains, and share nodes
380 // that need to be constrained
381 for (const auto local_node_id : elem->nodes_on_side(side))
382 {
383 const auto & node = mesh.node_ref(elem->node_id(local_node_id));
384 // Make sure we haven't already processed this node.
385 // We leave the responsibility of computing the constraint to
386 // the proc that owns the node.
387 if (subdomain_boundary_map.count(node.id()) ||
388 node.processor_id() != proc_id)
389 continue;
390
391 // Get the relevant nodal neighbors for the subdomain constraint
392 const auto side_grouped_boundary_neighbors =
394 mesh, node, sub_id1, nodes_to_elem_map);
395
396 // Determine which constraint should be imposed
397 const auto subdomain_constraint =
398 determine_constraint(node, dim, side_grouped_boundary_neighbors);
399
400 // This subdomain boundary node does not lie on an external boundary,
401 // go ahead and impose constraint
402 if (boundary_node_ids.find(node.id()) == boundary_node_ids.end())
403 {
404 if (_verbosity > 20)
405 libMesh::out << "Imposing subdomain constraint on "
406 << std::endl << " " << node << std::endl
407 << " " << subdomain_constraint << std::endl;
408
409 this->impose_constraint(node, subdomain_constraint);
410 }
411
412 // This subdomain boundary node could lie on an external boundary, save it
413 // for later to combine with the external boundary constraint.
414 // We also save constraints for non-boundary nodes so we don't try to
415 // re-constrain the node when accessed from the neighboring elem.
416 // See subdomain_boundary_map.count call above.
417 subdomain_boundary_map[node.id()] = subdomain_constraint;
418
419 } // for local_node_id
420
421 } // for side
422 } // for elem
423 }
424
425 // Loop through boundary nodes and impose constraints
426 for (const auto & bid : boundary_node_ids)
427 {
428 const auto & node = mesh.node_ref(bid);
429 // We leave the responsibility of computing the constraint to the proc
430 // that owns the node.
431 if (node.processor_id() != proc_id)
432 continue;
433
434 // Get the relevant nodal neighbors for the boundary constraint
435 const auto side_grouped_boundary_neighbors = get_neighbors_for_boundary_constraint(
436 mesh, node, boundary_node_ids, boundary_info, nodes_to_elem_map);
437
438 // Determine which constraint should be imposed
439 const auto boundary_constraint =
440 determine_constraint(node, dim, side_grouped_boundary_neighbors);
441
442 // Check for the case where this boundary node is also part of a subdomain id boundary
443 if (const auto it = subdomain_boundary_map.find(bid); it != subdomain_boundary_map.end())
444 {
445 const auto & subdomain_constraint = it->second;
446 // Combine current boundary constraint with previously determined
447 // subdomain_constraint
448 auto combined_constraint = intersect_constraints(subdomain_constraint, boundary_constraint);
449
450 // This will catch cases where constraints have no intersection
451 // Fall back to fixed node constraint
452 if (std::holds_alternative<InvalidConstraint>(combined_constraint))
453 combined_constraint = PointConstraint(node);
454
455 if (_verbosity > 20)
456 libMesh::out << "Imposing boundary/subdomain constraint on "
457 << std::endl << " " << node << std::endl
458 << " " << combined_constraint << std::endl;
459
460 this->impose_constraint(node, combined_constraint);
461 }
462
463 else
464 {
465 if (_verbosity > 20)
466 libMesh::out << "Imposing boundary constraint on "
467 << std::endl << node << std::endl
468 << " " << boundary_constraint << std::endl;
469
470 this->impose_constraint(node, boundary_constraint);
471 }
472
473 } // end bid
474}
unsigned int dim
unsigned int mesh_dimension() const
Definition mesh_base.C:515
const MeshBase & get_mesh() const
Definition system.h:2432
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.
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).
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...
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.
MeshBase & mesh
void build_nodes_to_elem_map(const MeshBase &mesh, std::vector< std::vector< dof_id_type > > &nodes_to_elem_map)
After calling this function the input vector nodes_to_elem_map will contain the node to element conne...
Definition mesh_tools.C:520
std::unordered_set< dof_id_type > find_boundary_nodes(const MeshBase &mesh)
Returns a std::set containing Node IDs for all of the boundary nodes.
Definition mesh_tools.C:588
OStreamProxy out
ConstraintVariant intersect_constraints(const ConstraintVariant &a, const ConstraintVariant &b)
Dispatch intersection between two constraint variants.

References _preserve_subdomain_boundaries, _sys, _verbosity, libMesh::MeshTools::build_nodes_to_elem_map(), determine_constraint(), dim, libMesh::MeshTools::find_boundary_nodes(), libMesh::System::get_mesh(), get_neighbors_for_boundary_constraint(), get_neighbors_for_subdomain_constraint(), impose_constraint(), libMesh::intersect_constraints(), mesh, libMesh::MeshBase::mesh_dimension(), and libMesh::out.

◆ constrain_node_to_line()

void libMesh::VariationalSmootherConstraint::constrain_node_to_line ( const Node &  node,
const Point &  line_vec 
)
private

Constrain a node to remain on the given line during mesh smoothing.

Parameters
nodeNode to constrain
line_vecvector parallel to the constraining line. This, along with the coordinates of node, are used to define the constraining line.

Definition at line 540 of file variational_smoother_constraint.C.

542{
543 const auto dim = _sys.get_mesh().mesh_dimension();
544
545 // We will free the dimension most parallel to line_vec to keep the
546 // constraint coefficients small
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);
550 });
551 const unsigned int free_dim = std::distance(line_vec_coefs.begin(), it);
552 const auto free_dof_index = node.dof_number(_sys.number(), free_dim, 0);
553
554 // A line is parameterized as r(t) = node + t * line_vec, so
555 // x(t) = node(x) + t * line_vec(x)
556 // y(t) = node(y) + t * line_vec(y)
557 // z(t) = node(z) + t * line_vec(z)
558 // Let's say we leave x free. Then t = (x(t) - node(x)) / line_vec(x)
559 // Then y and z can be constrained as
560 // y = node(y) + line_vec_y * (x(t) - node(x)) / line_vec(x)
561 // = x(t) * line_vec(y) / line_vec(x) + (node(y) - node(x) * line_vec(y) / line_vec(x))
562 // z = x(t) * line_vec(z) / line_vec(x) + (node(z) - node(x) * line_vec(z) / line_vec(x))
563
564 libmesh_assert(!relative_fuzzy_equals(line_vec(free_dim), 0.));
565 for (const auto constrained_dim : make_range(dim))
566 {
567 if (constrained_dim == free_dim)
568 continue;
569
570 DofConstraintRow constraint_row;
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);
574 const auto constrained_dof_index = node.dof_number(_sys.number(), constrained_dim, 0);
576 constrained_dof_index, constraint_row, inhomogeneous_part, true);
577 }
578}
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 ...
const DofMap & get_dof_map() const
Definition system.h:2448
unsigned int number() const
Definition system.h:2424
static const Real b
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.
libmesh_assert(ctx)
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.
Definition dof_map.h:100
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...
Definition int_range.h:176

References _sys, libMesh::DofMap::add_constraint_row(), b, dim, libMesh::DofObject::dof_number(), libMesh::System::get_dof_map(), libMesh::System::get_mesh(), libMesh::libmesh_assert(), libMesh::make_range(), libMesh::MeshBase::mesh_dimension(), libMesh::System::number(), and libMesh::relative_fuzzy_equals().

Referenced by impose_constraint().

◆ constrain_node_to_plane()

void libMesh::VariationalSmootherConstraint::constrain_node_to_plane ( const Node &  node,
const Point &  ref_normal_vec 
)
private

Constrain a node to remain in the given plane during mesh smoothing.

Parameters
nodeNode to constrain
ref_normal_vecReference normal vector to the constraining plane. This, along with the coordinates of node, are used to define the constraining plane.

Definition at line 490 of file variational_smoother_constraint.C.

492{
493 const auto dim = _sys.get_mesh().mesh_dimension();
494 // determine equation of plane: c_x * x + c_y * y + c_z * z + c = 0
495 std::vector<Real> xyz_coefs; // vector to hold c_x, c_y, c_z
496 Real c = 0.;
497
498 // We choose to constrain the dimension with the largest magnitude coefficient
499 // This approach ensures the coefficients added to the constraint_row
500 // (i.e., -c_xyz / c_max) have as small magnitude as possible
501
502 // We initialize this to avoid maybe-uninitialized compiler error
503 unsigned int constrained_dim = 0;
504
505 // Let's assert that we have a nonzero normal to ensure that constrained_dim
506 // is always set
507 libmesh_assert(ref_normal_vec.norm() > TOLERANCE);
508
509 Real max_abs_coef = 0.;
510 for (const auto d : make_range(dim))
511 {
512 const auto coef = ref_normal_vec(d);
513 xyz_coefs.push_back(coef);
514 c -= coef * node(d);
515
516 const auto coef_abs = std::abs(coef);
517 if (coef_abs > max_abs_coef)
518 {
519 max_abs_coef = coef_abs;
520 constrained_dim = d;
521 }
522 }
523
524 DofConstraintRow constraint_row;
525 for (const auto free_dim : make_range(dim))
526 {
527 if (free_dim == constrained_dim)
528 continue;
529
530 const auto free_dof_index = node.dof_number(_sys.number(), free_dim, 0);
531 constraint_row[free_dof_index] = -xyz_coefs[free_dim] / xyz_coefs[constrained_dim];
532 }
533
534 const auto inhomogeneous_part = -c / xyz_coefs[constrained_dim];
535 const auto constrained_dof_index = node.dof_number(_sys.number(), constrained_dim, 0);
537 constrained_dof_index, constraint_row, inhomogeneous_part, true);
538}
static constexpr Real TOLERANCE
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References _sys, libMesh::DofMap::add_constraint_row(), dim, libMesh::DofObject::dof_number(), libMesh::System::get_dof_map(), libMesh::System::get_mesh(), libMesh::libmesh_assert(), libMesh::make_range(), libMesh::MeshBase::mesh_dimension(), libMesh::TypeVector< T >::norm(), libMesh::System::number(), libMesh::Real, and libMesh::TOLERANCE.

Referenced by impose_constraint().

◆ determine_constraint()

ConstraintVariant libMesh::VariationalSmootherConstraint::determine_constraint ( const Node &  node,
const unsigned int  dim,
const std::set< std::set< const Node * > > &  side_grouped_boundary_neighbors 
)
staticprivate

Determines the appropriate constraint (PointConstraint, LineConstraint, or PlaneConstraint) for a node based on its neighbors.

Parameters
nodeThe node to constrain.
dimThe mesh dimension.
Returns
The best-fit constraint for the given geometry.

Definition at line 805 of file variational_smoother_constraint.C.

809{
810 // Determines the appropriate geometric constraint for a node based on its
811 // neighbors.
812
813 // Extract neighbors in flat vector
814 std::vector<const Node *> neighbors;
815 for (const auto & side : side_grouped_boundary_neighbors)
816 neighbors.insert(neighbors.end(), side.begin(), side.end());
817
818 // Constrain the node to it's current location
819 // Note that neighbors.size() <= 1 is used here instead of == 1 so we don't
820 // crash when trying to access *neighbors[0] a few lines below in the
821 // dim == 2 case
822 if (dim == 1 || neighbors.size() <= 1)
823 return PointConstraint{node};
824
825 if (dim == 2 || neighbors.size() == 2)
826 {
827 // Determine whether node + all neighbors are colinear
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()))
831 {
832 const Point delta = *(neighbors[i]) - node;
833 libmesh_assert(delta.norm() >= TOLERANCE);
834 const Point dir = delta.unit();
835 if (!dir.relative_fuzzy_equals(ref_dir) && !dir.relative_fuzzy_equals(-ref_dir))
836 {
837 all_colinear = false;
838 break;
839 }
840 }
841
842 if (all_colinear)
843 return LineConstraint{node, ref_dir};
844
845 return PointConstraint{node};
846 }
847
848 // dim == 3, neighbors.size() >= 3
849 std::set<PlaneConstraint> valid_planes;
850 for (const auto & side_nodes : side_grouped_boundary_neighbors)
851 {
852 std::vector<const Node *> side_nodes_vec(side_nodes.begin(), side_nodes.end());
853 for (const auto i : index_range(side_nodes_vec))
854 {
855 const Point vec_i = (*side_nodes_vec[i] - node);
856 for (const auto j : make_range(i))
857 {
858 const Point vec_j = (*side_nodes_vec[j] - node);
859 Point candidate_normal = vec_i.cross(vec_j);
860 if (candidate_normal.norm() <= TOLERANCE)
861 continue;
862
863 const PlaneConstraint candidate_plane{node, candidate_normal.unit()};
864 valid_planes.emplace(candidate_plane);
865 }
866 }
867 }
868
869 // Fall back to point constraint
870 if (valid_planes.empty())
871 return PointConstraint(node);
872
873 // Combine all the planes together to get a common constraint
874 auto it = valid_planes.begin();
875 ConstraintVariant current = *it++;
876 for (; it != valid_planes.end(); ++it)
877 {
878 current = intersect_constraints(current, *it);
879
880 // This will catch cases where constraints have no intersection
881 // (i.e., the element surface is non-planar)
882 // Fall back to fixed node constraint
883 if (std::holds_alternative<InvalidConstraint>(current))
884 {
885 current = PointConstraint(node);
886 break;
887 }
888 }
889
890 return current;
891}
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
std::variant< PointConstraint, LineConstraint, PlaneConstraint, InvalidConstraint > ConstraintVariant
Type used to store a constraint that may be a PlaneConstraint, LineConstraint, or PointConstraint.

References libMesh::TypeVector< T >::cross(), dim, libMesh::index_range(), libMesh::intersect_constraints(), libMesh::libmesh_assert(), libMesh::make_range(), libMesh::TypeVector< T >::norm(), libMesh::TypeVector< T >::relative_fuzzy_equals(), libMesh::TOLERANCE, and libMesh::TypeVector< T >::unit().

Referenced by constrain().

◆ fix_node()

void libMesh::VariationalSmootherConstraint::fix_node ( const Node &  node)
private

Constrain (i.e., fix) a node to not move during mesh smoothing.

Parameters
nodeNode to fix.

Definition at line 476 of file variational_smoother_constraint.C.

477{
478 for (const auto d : make_range(_sys.get_mesh().mesh_dimension()))
479 {
480 const auto constrained_dof_index = node.dof_number(_sys.number(), d, 0);
481 DofConstraintRow constraint_row;
482 // Leave the constraint row as all zeros so this dof is independent from other dofs
483 const auto constrained_value = node(d);
484 // Simply constrain this dof to retain it's current value
486 constrained_dof_index, constraint_row, constrained_value, true);
487 }
488}

References _sys, libMesh::DofMap::add_constraint_row(), libMesh::DofObject::dof_number(), libMesh::System::get_dof_map(), libMesh::System::get_mesh(), libMesh::make_range(), libMesh::MeshBase::mesh_dimension(), and libMesh::System::number().

Referenced by impose_constraint().

◆ get_neighbors_for_boundary_constraint()

std::set< std::set< const Node * > > libMesh::VariationalSmootherConstraint::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 
)
staticprivate

Get the relevant nodal neighbors for an external boundary constraint.

Parameters
meshThe mesh being smoothed.
nodeThe node (on the external boundary) being constrained.
boundary_node_idsThe set of mesh's external boundary node ids.
boundary_infoThe mesh's BoundaryInfo.
nodes_to_elem_mapA mapping from node id to containing element ids.
Returns
A set of node pointer sets containing nodal neighbors to 'node' on the external boundary. The subsets are grouped by element faces that form the external boundary. Note that 'node' itself does not appear in this set.

Definition at line 724 of file variational_smoother_constraint.C.

730{
731
732 // Find all the nodal neighbors... that is the nodes directly connected
733 // to this node through one edge, or if none exists, use other nodes on the
734 // containing face
735 std::vector<const Node *> neighbors;
737 mesh, node, nodes_to_elem_map, neighbors);
738
739 // Each constituent set corresponds to neighbors sharing a face on the
740 // boundary
741 std::set<std::set<const Node *>> side_grouped_boundary_neighbors;
742
743 for (const auto * neigh : neighbors)
744 {
745 const bool is_neighbor_boundary_node =
746 boundary_node_ids.find(neigh->id()) != boundary_node_ids.end();
747 if (!is_neighbor_boundary_node)
748 continue;
749
750 // Determine whether nodes share a common boundary id
751 // First, find the common element that both node and neigh belong to
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)
756 {
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)
761 continue;
762 common_elem = neigh_elem;
763 // Keep this in the neigh_elem loop because there can be multiple common
764 // elements Now, determine whether node and neigh share a common boundary
765 // id
766 const bool nodes_have_common_bid = VariationalSmootherConstraint::nodes_share_boundary_id(
767 node, *neigh, *common_elem, boundary_info);
768 if (nodes_have_common_bid)
769 {
770 // Find the side coinciding with the shared boundary
771 for (const auto side : common_elem->side_index_range())
772 {
773 // We only care about external boundaries here, make sure side doesn't
774 // have a neighbor
775 if (common_elem->neighbor_ptr(side))
776 continue;
777
778 bool node_found_on_side = false;
779 bool neigh_found_on_side = false;
780 const auto nodes_on_side = common_elem->nodes_on_side(side);
781 for (const auto local_node_id : nodes_on_side)
782 {
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;
787 }
788 if (!(node_found_on_side && neigh_found_on_side))
789 continue;
790
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);
796 }
797 continue;
798 }
799 }
800 }
801
802 return side_grouped_boundary_neighbors;
803}
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.
void find_nodal_or_face_neighbors(const MeshBase &mesh, const Node &node, const std::unordered_map< dof_id_type, std::vector< const Elem * > > &nodes_to_elem_map, std::vector< const Node * > &neighbors)
Given a mesh and a node in the mesh, the vector will be filled with every node directly attached to t...

References libMesh::MeshTools::find_nodal_or_face_neighbors(), libMesh::DofObject::id(), mesh, libMesh::Elem::neighbor_ptr(), libMesh::Elem::node_id(), libMesh::Elem::node_ptr(), libMesh::Elem::nodes_on_side(), nodes_share_boundary_id(), and libMesh::Elem::side_index_range().

Referenced by constrain().

◆ get_neighbors_for_subdomain_constraint()

std::set< std::set< const Node * > > libMesh::VariationalSmootherConstraint::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 
)
staticprivate

Get the relevant nodal neighbors for a subdomain constraint.

Parameters
meshThe mesh being smoothed.
nodeThe node (on the subdomain boundary) being constrained.
sub_idThe subdomain id of the block on one side of the subdomain boundary.
nodes_to_elem_mapA mapping from node id to containing element ids.
Returns
A set of node pointer sets containing nodal neighbors to 'node' on the sub_id1-sub_id2 boundary. The subsets are grouped by element faces that form the subdomain boundary. Note that 'node' itself does not appear in this set.

Definition at line 639 of file variational_smoother_constraint.C.

644{
645
646 // Find all the nodal neighbors... that is the nodes directly connected
647 // to this node through one edge, or if none exists, use other nodes on the
648 // containing face
649 std::vector<const Node *> neighbors;
651 mesh, node, nodes_to_elem_map, neighbors);
652
653 // Each constituent set corresponds to neighbors sharing a face on the
654 // subdomain boundary
655 std::set<std::set<const Node *>> side_grouped_boundary_neighbors;
656
657 for (const auto * neigh : neighbors)
658 {
659 // Determine whether the neighbor is on the subdomain boundary
660 // First, find the common elements that both node and neigh belong to
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)
665 {
666 if ((std::find(elems_containing_node.begin(), elems_containing_node.end(), neigh_elem) !=
667 elems_containing_node.end())
668 // We should be able to find a common element on the sub_id boundary
669 && (neigh_elem->subdomain_id() == sub_id))
670 common_elem = neigh_elem;
671 else
672 continue;
673
674 // Now, determine whether node and neigh are on a side coincident
675 // with the subdomain boundary
676 for (const auto common_side : common_elem->side_index_range())
677 {
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))
681 {
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;
686 }
687
688 if (!(node_found_on_side && neigh_found_on_side &&
689 common_elem->neighbor_ptr(common_side)))
690 continue;
691
692 const auto matched_side = common_side;
693 // There could be multiple matched sides, so keep this next part
694 // inside the common_side loop
695 //
696 // Does matched_side, containing both node and neigh, lie on the
697 // sub_id subdomain boundary?
698 const auto matched_neighbor_sub_id =
699 common_elem->neighbor_ptr(matched_side)->subdomain_id();
700 const bool is_matched_side_on_subdomain_boundary = matched_neighbor_sub_id != sub_id;
701
702 if (is_matched_side_on_subdomain_boundary)
703 {
704 // Store all nodes that live on this side
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);
711
712 continue;
713 }
714
715 } // for common_side
716
717 } // for neigh_elem
718 }
719
720 return side_grouped_boundary_neighbors;
721}

References libMesh::MeshTools::find_nodal_or_face_neighbors(), libMesh::DofObject::id(), mesh, libMesh::Elem::neighbor_ptr(), libMesh::Elem::node_id(), libMesh::Elem::node_ptr(), libMesh::Elem::nodes_on_side(), libMesh::Elem::side_index_range(), and libMesh::Elem::subdomain_id().

Referenced by constrain().

◆ impose_constraint()

void libMesh::VariationalSmootherConstraint::impose_constraint ( const Node &  node,
const ConstraintVariant &  constraint 
)
private

Applies a given constraint to a node (e.g., fixing it, restricting it to a line or plane).

Parameters
nodeThe node to constrain.
constraintThe geometric constraint variant to apply.
Exceptions
libMesh::logicErrorIf the constraint cannot be imposed.

Definition at line 895 of file variational_smoother_constraint.C.

897{
898
899 libmesh_assert_msg(!std::holds_alternative<InvalidConstraint>(constraint),
900 "Cannot impose constraint using InvalidConstraint.");
901
902 if (std::holds_alternative<PointConstraint>(constraint))
903 fix_node(node);
904 else if (std::holds_alternative<LineConstraint>(constraint))
905 constrain_node_to_line(node, std::get<LineConstraint>(constraint).direction());
906 else if (std::holds_alternative<PlaneConstraint>(constraint))
907 constrain_node_to_plane(node, std::get<PlaneConstraint>(constraint).normal());
908
909 else
910 libmesh_assert_msg(false, "Unknown constraint type.");
911}
void fix_node(const Node &node)
Constrain (i.e., fix) a node to not move during mesh smoothing.
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.
void constrain_node_to_line(const Node &node, const Point &line_vec)
Constrain a node to remain on the given line during mesh smoothing.

References constrain_node_to_line(), constrain_node_to_plane(), and fix_node().

Referenced by constrain().

◆ nodes_share_boundary_id()

bool libMesh::VariationalSmootherConstraint::nodes_share_boundary_id ( const Node &  boundary_node,
const Node &  neighbor_node,
const Elem &  containing_elem,
const BoundaryInfo &  boundary_info 
)
staticprivate

Determines whether two neighboring nodes share a common boundary id.

Parameters
boundary_nodeThe first of the two prospective nodes.
neighbor_nodeThe second of the two prospective nodes.
containing_elemThe element containing node1 and node2.
boundary_infoThe mesh's BoundaryInfo.
Returns
nodes_share_bid Whether node1 and node2 share a common boundary id.

Definition at line 607 of file variational_smoother_constraint.C.

611{
612 bool nodes_share_bid = false;
613
614 // Node ids local to containing_elem
615 const auto node_id = containing_elem.get_node_index(&boundary_node);
616 const auto neighbor_id = containing_elem.get_node_index(&neighbor_node);
617
618 for (const auto side_id : containing_elem.side_index_range())
619 {
620 // We don't care about this side if it doesn't contain our boundary and neighbor nodes
621 if (!(containing_elem.is_node_on_side(node_id, side_id) &&
622 containing_elem.is_node_on_side(neighbor_id, side_id)))
623 continue;
624
625 // If the current side, containing boundary_node and neighbor_node, lies on a boundary,
626 // we can say that boundary_node and neighbor_node have a common boundary id.
627 std::vector<boundary_id_type> boundary_ids;
628 boundary_info.boundary_ids(&containing_elem, side_id, boundary_ids);
629 if (boundary_ids.size())
630 {
631 nodes_share_bid = true;
632 break;
633 }
634 }
635 return nodes_share_bid;
636}
const boundary_id_type side_id

References libMesh::BoundaryInfo::boundary_ids(), libMesh::Elem::get_node_index(), libMesh::Elem::is_node_on_side(), side_id, and libMesh::Elem::side_index_range().

Referenced by get_neighbors_for_boundary_constraint().

Member Data Documentation

◆ _preserve_subdomain_boundaries

const bool libMesh::VariationalSmootherConstraint::_preserve_subdomain_boundaries
private

Whether nodes on subdomain boundaries are subject to change via smoothing.

Definition at line 410 of file variational_smoother_constraint.h.

Referenced by constrain().

◆ _sys

System& libMesh::VariationalSmootherConstraint::_sys
private

◆ _verbosity

const unsigned int libMesh::VariationalSmootherConstraint::_verbosity
private

Verbosity setting.

The verbosity levels and the corresponding information output are as follows:

verbosity = 0: No information.

20 < verbosity: Prints:

  • Constraint information on all boundary and subdomain nodes

Definition at line 403 of file variational_smoother_constraint.h.

Referenced by constrain().


The documentation for this class was generated from the following files: