libMesh
elem_refinement.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 // C++ includes
21 
22 // Local includes
23 #include "libmesh/elem.h"
24 #include "libmesh/mesh_refinement.h"
25 #include "libmesh/remote_elem.h"
26 
27 namespace libMesh
28 {
29 
30 
31 //--------------------------------------------------------------------
32 // Elem methods
33 
39 #ifdef LIBMESH_ENABLE_AMR
40 
41 void Elem::set_p_level(unsigned int p)
42 {
43  // Maintain the parent's p level as the minimum of it's children
44  if (this->parent() != nullptr)
45  {
46  unsigned int parent_p_level = this->parent()->p_level();
47 
48  // If our new p level is less than our parents, our parents drops
49  if (parent_p_level > p)
50  {
51  this->parent()->set_p_level(p);
52 
53  // And we should keep track of the drop, in case we need to
54  // do a projection later.
56  }
57  // If we are the lowest p level and it increases, so might
58  // our parent's, but we have to check every other child to see
59  else if (parent_p_level == _p_level && _p_level < p)
60  {
61  _p_level = cast_int<unsigned char>(p);
62  parent_p_level = cast_int<unsigned char>(p);
63  for (auto & c : this->parent()->child_ref_range())
64  parent_p_level = std::min(parent_p_level,
65  c.p_level());
66 
67  // When its children all have a higher p level, the parent's
68  // should rise
69  if (parent_p_level > this->parent()->p_level())
70  {
71  this->parent()->set_p_level(parent_p_level);
72 
73  // And we should keep track of the rise, in case we need to
74  // do a projection later.
76  }
77 
78  return;
79  }
80  }
81 
82  this->hack_p_level(p);
83 }
84 
85 
86 
87 void Elem::refine (MeshRefinement & mesh_refinement)
88 {
89  libmesh_assert_equal_to (this->refinement_flag(), Elem::REFINE);
90  libmesh_assert (this->active());
91 
92  const unsigned int nc = this->n_children();
93 
94  // Create my children if necessary
95  if (!_children)
96  {
97  _children = std::make_unique<Elem *[]>(nc);
98 
99  unsigned int parent_p_level = this->p_level();
100  const unsigned int nei = this->n_extra_integers();
101  for (unsigned int c = 0; c != nc; c++)
102  {
103  auto current_child = Elem::build(this->type(), this);
104  _children[c] = current_child.get();
105 
106  current_child->set_refinement_flag(Elem::JUST_REFINED);
107  current_child->set_p_level(parent_p_level);
108  current_child->set_p_refinement_flag(this->p_refinement_flag());
109 
110  for (auto cnode : current_child->node_index_range())
111  {
112  Node * node =
113  mesh_refinement.add_node(*this, c, cnode,
114  current_child->processor_id());
115  node->set_n_systems (this->n_systems());
116  current_child->set_node(cnode, node);
117  }
118 
119  Elem * added_child = mesh_refinement.add_elem (std::move(current_child));
120  added_child->set_n_systems(this->n_systems());
121  libmesh_assert_equal_to (added_child->n_extra_integers(),
122  this->n_extra_integers());
123  for (unsigned int i=0; i != nei; ++i)
124  added_child->set_extra_integer(i, this->get_extra_integer(i));
125  }
126  }
127  else
128  {
129  unsigned int parent_p_level = this->p_level();
130  for (unsigned int c = 0; c != nc; c++)
131  {
132  Elem * current_child = this->child_ptr(c);
133  if (current_child != remote_elem)
134  {
135  libmesh_assert(current_child->subactive());
136  current_child->set_refinement_flag(Elem::JUST_REFINED);
137  current_child->set_p_level(parent_p_level);
138  current_child->set_p_refinement_flag(this->p_refinement_flag());
139  }
140  }
141  }
142 
143  // Un-set my refinement flag now
145 
146  // Leave the p refinement flag set - we will need that later to get
147  // projection operations correct
148  // this->set_p_refinement_flag(Elem::INACTIVE);
149 
150 #ifndef NDEBUG
151  for (unsigned int c = 0; c != nc; c++)
152  if (this->child_ptr(c) != remote_elem)
153  {
154  libmesh_assert_equal_to (this->child_ptr(c)->parent(), this);
155  libmesh_assert(this->child_ptr(c)->active());
156  }
157 #endif
158  libmesh_assert (this->ancestor());
159 }
160 
161 
162 
164 {
165  libmesh_assert_equal_to (this->refinement_flag(), Elem::COARSEN_INACTIVE);
166  libmesh_assert (!this->active());
167 
168  // We no longer delete children until MeshRefinement::contract()
169 
170  unsigned int parent_p_level = 0;
171 
172  const unsigned int n_n = this->n_nodes();
173 
174  // re-compute hanging node nodal locations
175  for (unsigned int c = 0, nc = this->n_children(); c != nc; ++c)
176  {
177  Elem * mychild = this->child_ptr(c);
178  if (mychild == remote_elem)
179  continue;
180  for (auto cnode : mychild->node_index_range())
181  {
182  Point new_pos;
183  bool calculated_new_pos = false;
184 
185  for (unsigned int n=0; n<n_n; n++)
186  {
187  // The value from the embedding matrix
188  const Real em_val = this->embedding_matrix(c,cnode,n);
189 
190  // The node location is somewhere between existing vertices
191  if ((em_val != 0.) && (em_val != 1.))
192  {
193  new_pos.add_scaled (this->point(n), em_val);
194  calculated_new_pos = true;
195  }
196  }
197 
198  if (calculated_new_pos)
199  {
200  //Move the existing node back into it's original location
201  for (unsigned int i=0; i<LIBMESH_DIM; i++)
202  {
203  Point & child_node = mychild->point(cnode);
204  child_node(i)=new_pos(i);
205  }
206  }
207  }
208  }
209 
210  for (auto & mychild : this->child_ref_range())
211  {
212  if (&mychild == remote_elem)
213  continue;
214  libmesh_assert_equal_to (mychild.refinement_flag(), Elem::COARSEN);
215  mychild.set_refinement_flag(Elem::INACTIVE);
216  if (mychild.p_level() > parent_p_level)
217  parent_p_level = mychild.p_level();
218  }
219 
221  this->set_p_level(parent_p_level);
222 
223  libmesh_assert (this->active());
224 }
225 
226 
227 
229 {
230  // Subactive elements get deleted entirely, not contracted
231  libmesh_assert (this->active());
232 
233  // Active contracted elements no longer can have children
234  _children.reset(nullptr);
235 
236  if (this->refinement_flag() == Elem::JUST_COARSENED)
238 }
239 
240 #endif // #ifdef LIBMESH_ENABLE_AMR
241 
242 
243 } // namespace libMesh
void set_p_level(const unsigned int p)
Sets the value of the p-refinement level for the element.
RefinementState refinement_flag() const
Definition: elem.h:3210
const Elem * parent() const
Definition: elem.h:3030
void add_scaled(const TypeVector< T2 > &, const T &)
Add a scaled value to this vector without creating a temporary.
Definition: type_vector.h:629
A Node is like a Point, but with more information.
Definition: node.h:52
unsigned char _p_level
p refinement level - the difference between the polynomial degree on this element and the minimum pol...
Definition: elem.h:2290
void contract()
Contract an active element, i.e.
RefinementState p_refinement_flag() const
Definition: elem.h:3226
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
void set_refinement_flag(const RefinementState rflag)
Sets the value of the refinement flag for the element.
Definition: elem.h:3218
virtual unsigned int n_children() const =0
unsigned int p_level() const
Definition: elem.h:3108
The libMesh namespace provides an interface to certain functionality in the library.
SimpleRange< ChildRefIter > child_ref_range()
Returns a range with all children of a parent element, usable in range-based for loops.
Definition: elem.h:2346
bool ancestor() const
Definition: elem.C:2010
virtual Real embedding_matrix(const unsigned int child_num, const unsigned int child_node_num, const unsigned int parent_node_num) const =0
virtual void refine(MeshRefinement &mesh_refinement)
Refine the element.
Implements (adaptive) mesh refinement algorithms for a MeshBase.
virtual unsigned int n_nodes() const =0
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:444
unsigned int n_systems() const
Definition: dof_object.h:937
libmesh_assert(ctx)
void set_n_systems(const unsigned int s)
Sets the number of systems for this DofObject.
Definition: dof_object.C:150
Node * add_node(Elem &parent, unsigned int child, unsigned int node, processor_id_type proc_id)
Add a node to the mesh.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Elem * add_elem(Elem *elem)
Adds the element elem to the mesh.
void coarsen()
Coarsen the element.
bool subactive() const
Definition: elem.h:2959
IntRange< unsigned short > node_index_range() const
Definition: elem.h:2683
unsigned int n_extra_integers() const
Returns how many extra integers are associated to the DofObject.
Definition: dof_object.h:1170
void set_p_refinement_flag(const RefinementState pflag)
Sets the value of the p-refinement flag for the element.
Definition: elem.h:3234
bool active() const
Definition: elem.h:2941
void hack_p_level(const unsigned int p)
Sets the value of the p-refinement level for the element without altering the p-level of its ancestor...
Definition: elem.h:3264
virtual ElemType type() const =0
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
const Point & point(const unsigned int i) const
Definition: elem.h:2453
std::unique_ptr< Elem *[]> _children
unique_ptr to array of this element&#39;s children.
Definition: elem.h:2261
void set_extra_integer(const unsigned int index, const dof_id_type value)
Sets the value on this object of the extra integer associated with index, which should have been obta...
Definition: dof_object.h:1086
const Elem * child_ptr(unsigned int i) const
Definition: elem.h:3163
const RemoteElem * remote_elem
Definition: remote_elem.C:57