libMesh
mesh_refinement.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 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 
20 #ifndef LIBMESH_MESH_REFINEMENT_H
21 #define LIBMESH_MESH_REFINEMENT_H
22 
23 
24 
25 #include "libmesh/libmesh_config.h"
26 
27 #ifdef LIBMESH_ENABLE_AMR
28 
29 // Local Includes
30 #include "libmesh/libmesh_common.h"
31 #include "libmesh/libmesh.h" // libMesh::invalid_uint
32 #include "libmesh/topology_map.h"
33 #include "libmesh/parallel_object.h"
34 
35 // C++ Includes
36 #include <vector>
37 
38 namespace libMesh
39 {
40 
41 // Forward Declarations
42 class MeshBase;
43 class Point;
44 class Node;
45 class ErrorVector;
46 class PeriodicBoundaries;
47 class Elem;
48 class PointLocatorBase;
49 
62 {
63 public:
64 
68  explicit
70 
71 private:
72  // Both the copy ctor and the assignment operator are
73  // declared private but not implemented. This is the
74  // standard practice to prevent them from being used.
77 
78 public:
79 
89  {
90  public:
94  virtual ~ElementFlagging () = default;
95 
99  virtual void flag_elements () = 0;
100  };
101 
106 
110  ~MeshRefinement ();
111 
115  void clear ();
116 
128  void flag_elements_by_error_fraction (const ErrorVector & error_per_cell,
129  const Real refine_fraction = 0.3,
130  const Real coarsen_fraction = 0.0,
131  const unsigned int max_level = libMesh::invalid_uint);
132 
147  void flag_elements_by_error_tolerance (const ErrorVector & error_per_cell);
148 
163  bool flag_elements_by_nelem_target (const ErrorVector & error_per_cell);
164 
178  void flag_elements_by_elem_fraction (const ErrorVector & error_per_cell,
179  const Real refine_fraction = 0.3,
180  const Real coarsen_fraction = 0.0,
181  const unsigned int max_level = libMesh::invalid_uint);
182 
196  void flag_elements_by_mean_stddev (const ErrorVector & error_per_cell,
197  const Real refine_fraction = 1.0,
198  const Real coarsen_fraction = 0.0,
199  const unsigned int max_level = libMesh::invalid_uint);
200 
205  void flag_elements_by (ElementFlagging & element_flagging);
206 
212 
217  void add_p_to_h_refinement();
218 
232 
249  bool coarsen_elements ();
250 
262  bool refine_elements ();
263 
267  void uniformly_refine (unsigned int n=1);
268 
272  void uniformly_coarsen (unsigned int n=1);
273 
277  void uniformly_p_refine (unsigned int n=1);
278 
282  void uniformly_p_coarsen (unsigned int n=1);
283 
288  void clean_refinement_flags ();
289 
297  bool test_level_one (bool libmesh_assert_yes = false) const;
298 
306  bool test_unflagged (bool libmesh_assert_yes = false) const;
307 
317  Node * add_node (Elem & parent,
318  unsigned int child,
319  unsigned int node,
320  processor_id_type proc_id);
321 
325  Elem * add_elem (Elem * elem);
326 
334  Elem * add_elem (std::unique_ptr<Elem> elem);
335 
340  const MeshBase & get_mesh () const { return _mesh; }
341 
346  MeshBase & get_mesh () { return _mesh; }
347 
355  bool & coarsen_by_parents();
356 
364  Real & refine_fraction();
365 
374 
381  unsigned int & max_h_level();
382 
392 
401 
412 
423  unsigned char & face_level_mismatch_limit();
424 
434  unsigned char & edge_level_mismatch_limit();
435 
445  unsigned char & node_level_mismatch_limit();
446 
464  signed char & overrefined_boundary_limit();
465 
481  signed char & underrefined_boundary_limit();
482 
492  bool & allow_unrefined_patches();
493 
501 
508 #ifdef LIBMESH_ENABLE_DEPRECATED
510 #endif
511 
518 #ifdef LIBMESH_ENABLE_DEPRECATED
520 #endif
521 
527 
528 private:
529 
543  bool _coarsen_elements ();
544 
553  bool _refine_elements ();
554 
563  void _smooth_flags (bool refining, bool coarsening);
564 
565  //------------------------------------------------------
566  // "Smoothing" algorithms for refined meshes
567 
631  bool limit_level_mismatch_at_node (const unsigned int max_mismatch);
632 
633  /*
634  * This algorithm restricts the maximum level mismatch
635  * at any edge in the mesh. See the ASCII art in the comment of
636  * limit_level_mismatch_at_node, and pretend they're hexes.
637  */
638  bool limit_level_mismatch_at_edge (const unsigned int max_mismatch);
639 
640  /*
641  * This algorithm flags interior elements for refinement as needed
642  * to prevent corresponding boundary element refinement mismatch
643  * from exceeding the given limit.
644  */
645  bool limit_overrefined_boundary (const signed char max_mismatch);
646 
647  /*
648  * This algorithm flags boundary elements for refinement as needed
649  * to prevent corresponding interior element refinement mismatch
650  * from exceeding the given limit.
651  */
652  bool limit_underrefined_boundary (const signed char max_mismatch);
653 
708 
709 
710  //---------------------------------------------
711  // Utility algorithms
712 
718  void create_parent_error_vector (const ErrorVector & error_per_cell,
719  ErrorVector & error_per_parent,
720  Real & parent_error_min,
721  Real & parent_error_max);
722 
726  void update_nodes_map ();
727 
736 
745 
750  Elem * topological_neighbor (Elem * elem,
751  const PointLocatorBase * point_locator,
752  const unsigned int side) const;
753 
758  bool has_topological_neighbor (const Elem * elem,
759  const PointLocatorBase * point_locator,
760  const Elem * neighbor) const;
761 
766 
771 
778 
784 
786 
788 
789  unsigned int _max_h_level;
790 
792 
794 
796 
800 
803 
805 
875 
887  NeighborType nt,
888  unsigned max_mismatch);
889 
890 #ifdef LIBMESH_ENABLE_PERIODIC
892 #endif
893 };
894 
895 
896 
897 // ------------------------------------------------------------
898 // MeshRefinement class inline members
899 
901 {
902  _use_member_parameters = true;
903  return _coarsen_by_parents;
904 }
905 
907 {
908  _use_member_parameters = true;
909  return _refine_fraction;
910 }
911 
913 {
914  _use_member_parameters = true;
915  return _coarsen_fraction;
916 }
917 
918 inline unsigned int & MeshRefinement::max_h_level()
919 {
920  _use_member_parameters = true;
921  return _max_h_level;
922 }
923 
925 {
926  _use_member_parameters = true;
927  return _coarsen_threshold;
928 }
929 
931 {
932  _use_member_parameters = true;
933  return _nelem_target;
934 }
935 
937 {
938  _use_member_parameters = true;
940 }
941 
943 {
945 }
946 
948 {
950 }
951 
953 {
955 }
956 
958 {
960 }
961 
963 {
965 }
966 
968 {
970 }
971 
972 #ifdef LIBMESH_ENABLE_DEPRECATED
974 {
975  libmesh_deprecated();
977 }
978 
980 {
981  libmesh_deprecated();
983 }
984 #endif
985 
987 {
989 }
990 
991 
992 
993 } // namespace libMesh
994 
995 #endif // end #ifdef LIBMESH_ENABLE_AMR
996 #endif // LIBMESH_MESH_REFINEMENT_H
bool limit_level_mismatch_at_edge(const unsigned int max_mismatch)
bool & enforce_mismatch_limit_prior_to_refinement()
Get/set the _enforce_mismatch_limit_prior_to_refinement flag.
~MeshRefinement()
Destructor.
void uniformly_p_refine(unsigned int n=1)
Uniformly p refines the mesh n times.
Data structures that enable topology-based lookups of nodes created by mesh refinement.
Definition: topology_map.h:57
bool _refine_elements()
Refines user-requested elements.
A Node is like a Point, but with more information.
Definition: node.h:52
NeighborType
This helper function enforces the desired mismatch limits prior to refinement.
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition: libmesh.h:286
bool test_level_one(bool libmesh_assert_yes=false) const
bool limit_level_mismatch_at_node(const unsigned int max_mismatch)
This algorithm restricts the maximum level mismatch at any node in the mesh.
void flag_elements_by_error_tolerance(const ErrorVector &error_per_cell)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell...
void update_nodes_map()
Updates the _new_nodes_map.
TopologyMap _new_nodes_map
Data structure that holds the new nodes information.
bool limit_underrefined_boundary(const signed char max_mismatch)
bool has_topological_neighbor(const Elem *elem, const PointLocatorBase *point_locator, const Elem *neighbor) const
Local dispatch function for checking the correct has_neighbor function from the Elem class...
MeshBase & _mesh
Reference to the mesh.
Real & absolute_global_tolerance()
If absolute_global_tolerance is set to a nonzero value, methods like flag_elements_by_global_toleranc...
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
Definition: error_vector.h:50
We&#39;re using a class instead of a typedef to allow forward declarations and future flexibility...
bool refine_elements()
Only refines the user-requested elements.
bool flag_elements_by_nelem_target(const ErrorVector &error_per_cell)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell...
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
Real & coarsen_fraction()
The coarsen_fraction sets either a desired target or a desired maximum number of elements to flag for...
MeshBase & mesh
void uniformly_p_coarsen(unsigned int n=1)
Attempts to uniformly p coarsen the mesh n times.
unsigned char & edge_level_mismatch_limit()
If edge_level_mismatch_limit is set to a nonzero value, then refinement and coarsening will produce m...
bool limit_overrefined_boundary(const signed char max_mismatch)
Real & refine_fraction()
The refine_fraction sets either a desired target or a desired maximum number of elements to flag for ...
MeshRefinement & operator=(const MeshRefinement &)
void clean_refinement_flags()
Sets the refinement flag to Elem::DO_NOTHING for each element in the mesh.
void create_parent_error_vector(const ErrorVector &error_per_cell, ErrorVector &error_per_parent, Real &parent_error_min, Real &parent_error_max)
Calculates the error on all coarsenable parents.
The libMesh namespace provides an interface to certain functionality in the library.
PeriodicBoundaries * _periodic_boundaries
unsigned int & max_h_level()
The max_h_level is the greatest refinement level an element should reach.
bool & coarsen_by_parents()
If coarsen_by_parents is true, complete groups of sibling elements (elements with the same parent) wi...
unsigned int max_level(const MeshBase &mesh)
Find the maximum h-refinement level in a mesh.
signed char _underrefined_boundary_limit
uint8_t processor_id_type
Definition: id_types.h:104
This is the MeshBase class.
Definition: mesh_base.h:74
bool make_refinement_compatible()
Take user-specified refinement flags and augment them so that level-one dependency is satisfied...
dof_id_type & nelem_target()
If nelem_target is set to a nonzero value, methods like flag_elements_by_nelem_target() will attempt ...
bool coarsen_elements()
Only coarsens the user-requested elements.
signed char & underrefined_boundary_limit()
If underrefined_boundary_limit is set to a nonnegative value, then refinement and coarsening will pro...
void switch_h_to_p_refinement()
Takes a mesh whose elements are flagged for h refinement and coarsening, and switches those flags to ...
void flag_elements_by_mean_stddev(const ErrorVector &error_per_cell, const Real refine_fraction=1.0, const Real coarsen_fraction=0.0, const unsigned int max_level=libMesh::invalid_uint)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell...
unsigned char & node_level_mismatch_limit()
If node_level_mismatch_limit is set to a nonzero value, then refinement and coarsening will produce m...
Implements (adaptive) mesh refinement algorithms for a MeshBase.
virtual ~ElementFlagging()=default
Destructor.
unsigned char _face_level_mismatch_limit
Abstract base class to be used for user-specified element flagging.
bool & allow_unrefined_patches()
This flag defaults to false in order to maintain the original behavior of the code, which was to always eliminate unrefined element patches.
bool _use_member_parameters
For backwards compatibility, we initialize this as false and then set it to true if the user uses any...
void uniformly_coarsen(unsigned int n=1)
Attempts to uniformly coarsen the mesh n times.
Real & coarsen_threshold()
The coarsen_threshold provides hysteresis in AMR/C strategies.
bool test_unflagged(bool libmesh_assert_yes=false) const
void set_enforce_mismatch_limit_prior_to_refinement(bool enforce)
Set _enforce_mismatch_limit_prior_to_refinement option.
bool make_flags_parallel_consistent()
Copy refinement flags on ghost elements from their local processors.
This is the base class for point locators.
signed char _overrefined_boundary_limit
bool make_coarsening_compatible()
Take user-specified coarsening flags and augment them so that level-one dependency is satisfied...
void flag_elements_by(ElementFlagging &element_flagging)
Flag elements based on a function object.
bool _enforce_mismatch_limit_prior_to_refinement
This option enforces the mismatch level prior to refinement by checking if refining any element mar...
unsigned char & face_level_mismatch_limit()
If face_level_mismatch_limit is set to a nonzero value, then refinement and coarsening will produce m...
An object whose state is distributed along a set of processors.
const MeshBase & get_mesh() const
void flag_elements_by_elem_fraction(const ErrorVector &error_per_cell, const Real refine_fraction=0.3, const Real coarsen_fraction=0.0, const unsigned int max_level=libMesh::invalid_uint)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell...
MeshRefinement(MeshBase &mesh)
Constructor.
Node * add_node(Elem &parent, unsigned int child, unsigned int node, processor_id_type proc_id)
Add a node to the mesh.
unsigned char _node_level_mismatch_limit
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Elem * add_elem(Elem *elem)
Adds the element elem to the mesh.
signed char & overrefined_boundary_limit()
If overrefined_boundary_limit is set to a nonnegative value, then refinement and coarsening will prod...
bool refine_and_coarsen_elements()
Refines and coarsens user-requested elements.
void clear()
Deletes all the data that are currently stored.
Elem * topological_neighbor(Elem *elem, const PointLocatorBase *point_locator, const unsigned int side) const
Local dispatch function for getting the correct topological neighbor from the Elem class...
void flag_elements_by_error_fraction(const ErrorVector &error_per_cell, const Real refine_fraction=0.3, const Real coarsen_fraction=0.0, const unsigned int max_level=libMesh::invalid_uint)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell...
void _smooth_flags(bool refining, bool coarsening)
Smooths refinement flags according to current settings.
void add_p_to_h_refinement()
Takes a mesh whose elements are flagged for h refinement and coarsening, and adds flags to request p ...
bool get_enforce_mismatch_limit_prior_to_refinement()
unsigned char _edge_level_mismatch_limit
bool _coarsen_elements()
Coarsens user-requested elements.
bool eliminate_unrefined_patches()
This algorithm selects an element for refinement if all of its neighbors are (or will be) refined...
void set_periodic_boundaries_ptr(PeriodicBoundaries *pb_ptr)
Sets the PeriodicBoundaries pointer.
bool _coarsen_by_parents
Refinement parameter values.
uint8_t dof_id_type
Definition: id_types.h:67
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.
virtual void flag_elements()=0
Callback function to be used for marking elements for refinement.