elem_refinement.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 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"
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::refine (MeshRefinement & mesh_refinement)
42 {
43  libmesh_assert_equal_to (this->refinement_flag(), Elem::REFINE);
44  libmesh_assert (this->active());
45 
46  const unsigned int nc = this->n_children();
47 
48  // Create my children if necessary
49  if (!_children)
50  {
51  _children = new Elem *[nc];
52 
53  unsigned int parent_p_level = this->p_level();
54  for (unsigned int c = 0; c != nc; c++)
55  {
56  _children[c] = Elem::build(this->type(), this).release();
57  Elem * current_child = this->child_ptr(c);
58 
60  current_child->set_p_level(parent_p_level);
61  current_child->set_p_refinement_flag(this->p_refinement_flag());
62 
63  for (unsigned int cnode=0; cnode != current_child->n_nodes(); ++cnode)
64  {
65  Node * node =
66  mesh_refinement.add_node(*this, c, cnode,
67  current_child->processor_id());
68  node->set_n_systems (this->n_systems());
69  current_child->set_node(cnode) = node;
70  }
71 
72  mesh_refinement.add_elem (current_child);
73  current_child->set_n_systems(this->n_systems());
74  }
75  }
76  else
77  {
78  unsigned int parent_p_level = this->p_level();
79  for (unsigned int c = 0; c != nc; c++)
80  {
81  Elem * current_child = this->child_ptr(c);
82  libmesh_assert(current_child->subactive());
84  current_child->set_p_level(parent_p_level);
85  current_child->set_p_refinement_flag(this->p_refinement_flag());
86  }
87  }
88 
89  // Un-set my refinement flag now
91 
92  // Leave the p refinement flag set - we will need that later to get
93  // projection operations correct
94  // this->set_p_refinement_flag(Elem::INACTIVE);
95 
96  for (unsigned int c = 0; c != nc; c++)
97  {
98  libmesh_assert_equal_to (this->child_ptr(c)->parent(), this);
99  libmesh_assert(this->child_ptr(c)->active());
100  }
101  libmesh_assert (this->ancestor());
102 }
103 
104 
105 
107 {
108  libmesh_assert_equal_to (this->refinement_flag(), Elem::COARSEN_INACTIVE);
109  libmesh_assert (!this->active());
110 
111  // We no longer delete children until MeshRefinement::contract()
112  // delete [] _children;
113  // _children = nullptr;
114 
115  unsigned int parent_p_level = 0;
116 
117  // re-compute hanging node nodal locations
118  for (unsigned int c = 0, nc = this->n_children(); c != nc; ++c)
119  {
120  Elem * mychild = this->child_ptr(c);
121  if (mychild == remote_elem)
122  continue;
123  for (unsigned int cnode=0; cnode != mychild->n_nodes(); ++cnode)
124  {
125  Point new_pos;
126  bool calculated_new_pos = false;
127 
128  for (unsigned int n=0; n<this->n_nodes(); n++)
129  {
130  // The value from the embedding matrix
131  const float em_val = this->embedding_matrix(c,cnode,n);
132 
133  // The node location is somewhere between existing vertices
134  if ((em_val != 0.) && (em_val != 1.))
135  {
136  new_pos.add_scaled (this->point(n), em_val);
137  calculated_new_pos = true;
138  }
139  }
140 
141  if (calculated_new_pos)
142  {
143  //Move the existing node back into it's original location
144  for (unsigned int i=0; i<LIBMESH_DIM; i++)
145  {
146  Point & child_node = mychild->point(cnode);
147  child_node(i)=new_pos(i);
148  }
149  }
150  }
151  }
152 
153  for (auto & mychild : this->child_ref_range())
154  {
155  if (&mychild == remote_elem)
156  continue;
157  libmesh_assert_equal_to (mychild.refinement_flag(), Elem::COARSEN);
158  mychild.set_refinement_flag(Elem::INACTIVE);
159  if (mychild.p_level() > parent_p_level)
160  parent_p_level = mychild.p_level();
161  }
162 
164  this->set_p_level(parent_p_level);
165 
166  libmesh_assert (this->active());
167 }
168 
169 
170 
172 {
173  // Subactive elements get deleted entirely, not contracted
174  libmesh_assert (this->active());
175 
176  // Active contracted elements no longer can have children
177  delete [] _children;
178  _children = nullptr;
179 
180  if (this->refinement_flag() == Elem::JUST_COARSENED)
182 }
183 
184 #endif // #ifdef LIBMESH_ENABLE_AMR
185 
186 
187 } // namespace libMesh
void set_p_level(const unsigned int p)
Definition: elem.h:2692
RefinementState refinement_flag() const
Definition: elem.h:2638
const Elem * parent() const
Definition: elem.h:2479
virtual Node *& set_node(const unsigned int i)
Definition: elem.h:2024
A geometric point in (x,y,z) space associated with a DOF.
Definition: node.h:52
void add_scaled(const TypeVector< T2 > &, const T)
Definition: type_vector.h:627
RefinementState p_refinement_flag() const
Definition: elem.h:2654
The base class for all geometric element types.
Definition: elem.h:100
void set_refinement_flag(const RefinementState rflag)
Definition: elem.h:2646
virtual float embedding_matrix(const unsigned int child_num, const unsigned int child_node_num, const unsigned int parent_node_num) const =0
virtual unsigned int n_children() const =0
unsigned int p_level() const
Definition: elem.h:2555
dof_id_type node(const unsigned int i) const
Definition: elem.h:1927
SimpleRange< ChildRefIter > child_ref_range()
Definition: elem.h:1779
bool ancestor() const
Definition: elem.C:1427
virtual void refine(MeshRefinement &mesh_refinement)
Responsible for mesh refinement algorithms and data.
virtual unsigned int n_nodes() const =0
static std::unique_ptr< Elem > build(const ElemType type, Elem *p=nullptr)
Definition: elem.C:245
unsigned int n_systems() const
Definition: dof_object.h:749
Elem ** _children
Definition: elem.h:1707
void set_n_systems(const unsigned int s)
Definition: dof_object.C:165
Node * add_node(Elem &parent, unsigned int child, unsigned int node, processor_id_type proc_id)
Elem * add_elem(Elem *elem)
bool subactive() const
Definition: elem.h:2408
void set_p_refinement_flag(const RefinementState pflag)
Definition: elem.h:2662
bool active() const
Definition: elem.h:2390
processor_id_type processor_id() const
Definition: dof_object.h:717
virtual ElemType type() const =0
A geometric point in (x,y,z) space.
Definition: point.h:38
const Point & point(const unsigned int i) const
Definition: elem.h:1892
const Elem * child_ptr(unsigned int i) const
Definition: elem.h:2578
const RemoteElem * remote_elem
Definition: remote_elem.C:57