www.mooseframework.org
EFAElement.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
3 /* */
4 /* All contents are licensed under LGPL V2.1 */
5 /* See LICENSE for full restrictions */
6 /****************************************************************/
7 
8 #include "EFAElement.h"
9 
10 #include "EFANode.h"
11 #include "EFAError.h"
12 #include "EFAFuncs.h"
13 
14 EFAElement::EFAElement(unsigned int eid, unsigned int n_nodes)
15  : _id(eid),
16  _num_nodes(n_nodes),
17  _nodes(_num_nodes, NULL),
18  _parent(NULL),
19  _crack_tip_split_element(false)
20 {
21 }
22 
24 
25 unsigned int
27 {
28  return _id;
29 }
30 
31 unsigned int
33 {
34  return _num_nodes;
35 }
36 
37 void
38 EFAElement::setNode(unsigned int node_id, EFANode * node)
39 {
40  _nodes[node_id] = node;
41 }
42 
43 EFANode *
44 EFAElement::getNode(unsigned int node_id) const
45 {
46  return _nodes[node_id];
47 }
48 
49 bool
51 {
52  for (unsigned int i = 0; i < _nodes.size(); ++i)
53  if (_nodes[i] == node)
54  return true;
55  return false;
56 }
57 
58 void
59 EFAElement::printNodes(std::ostream & ostream) const
60 {
61  ostream << "***** nodes for element " << _id << " *****" << std::endl;
62  for (unsigned int i = 0; i < _num_nodes; ++i)
63  ostream << "addr " << _nodes[i] << ", ID " << _nodes[i]->idCatString() << ", category "
64  << _nodes[i]->category() << std::endl;
65 }
66 
67 EFANode *
69 {
70  // Given a global node, create a new local node
71  if (global_node->category() != EFANode::N_CATEGORY_PERMANENT &&
72  global_node->category() != EFANode::N_CATEGORY_TEMP &&
74  EFAError("In createLocalNodeFromGlobalNode node is not global");
75 
76  EFANode * new_local_node = NULL;
77  unsigned int inode = 0;
78  for (; inode < _nodes.size(); ++inode)
79  {
80  if (_nodes[inode] == global_node)
81  {
82  new_local_node = new EFANode(inode, EFANode::N_CATEGORY_LOCAL_INDEX);
83  break;
84  }
85  }
86  if (!new_local_node)
87  EFAError("In createLocalNodeFromGlobalNode could not find global node");
88 
89  return new_local_node;
90 }
91 
92 EFANode *
94 {
95  // Given a local node, find the global node corresponding to that node
96  if (local_node->category() != EFANode::N_CATEGORY_LOCAL_INDEX)
97  EFAError("In getGlobalNodeFromLocalNode node passed in is not local");
98 
99  EFANode * global_node = _nodes[local_node->id()];
100 
101  if (global_node->category() != EFANode::N_CATEGORY_PERMANENT &&
102  global_node->category() != EFANode::N_CATEGORY_TEMP)
103  EFAError("In getGlobalNodeFromLocalNode, the node stored by the element is not global");
104 
105  return global_node;
106 }
107 
108 unsigned int
110 {
111  unsigned int local_node_id = 99999;
112  bool found_local_node = false;
113  for (unsigned int i = 0; i < _num_nodes; ++i)
114  {
115  if (_nodes[i] == node)
116  {
117  found_local_node = true;
118  local_node_id = i;
119  break;
120  }
121  }
122  if (!found_local_node)
123  EFAError("In EFAelement::getLocalNodeIndex, cannot find the given node");
124  return local_node_id;
125 }
126 
127 void
129 {
131 }
132 
133 bool
135 {
137 }
138 
139 unsigned int
141 {
142  return _crack_tip_neighbors.size();
143 }
144 
145 unsigned int
146 EFAElement::getCrackTipNeighbor(unsigned int index) const
147 {
148  if (index < _crack_tip_neighbors.size())
149  return _crack_tip_neighbors[index];
150  else
151  EFAError("in getCrackTipNeighbor index out of bounds");
152 }
153 
154 void
156 {
157  // Find out what side the specified element is on, and add it as a crack tip neighbor
158  // element for that side.
159  unsigned int neighbor_index = getNeighborIndex(neighbor_elem);
160  bool crack_tip_neighbor_exist = false;
161  for (unsigned int i = 0; i < _crack_tip_neighbors.size(); ++i)
162  {
163  if (_crack_tip_neighbors[i] == neighbor_index)
164  {
165  crack_tip_neighbor_exist = true;
166  break;
167  }
168  }
169  if (!crack_tip_neighbor_exist)
170  _crack_tip_neighbors.push_back(neighbor_index);
171 }
172 
173 EFAElement *
175 {
176  return _parent;
177 }
178 
179 EFAElement *
180 EFAElement::getChild(unsigned int child_id) const
181 {
182  if (child_id < _children.size())
183  return _children[child_id];
184  else
185  EFAError("child_id out of bounds");
186 }
187 
188 void
190 {
191  _parent = parent;
192 }
193 
194 unsigned int
196 {
197  return _children.size();
198 }
199 
200 void
202 {
203  _children.push_back(child);
204 }
205 
206 void
208 {
209  _parent = NULL;
210  _children.clear();
211 }
212 
213 void
214 EFAElement::findGeneralNeighbors(std::map<EFANode *, std::set<EFAElement *>> & InverseConnectivity)
215 {
216  _general_neighbors.clear();
217  std::set<EFAElement *> patch_elements;
218  for (unsigned int inode = 0; inode < _num_nodes; ++inode)
219  {
220  std::set<EFAElement *> this_node_connected_elems = InverseConnectivity[_nodes[inode]];
221  patch_elements.insert(this_node_connected_elems.begin(), this_node_connected_elems.end());
222  }
223 
224  std::set<EFAElement *>::iterator eit2;
225  for (eit2 = patch_elements.begin(); eit2 != patch_elements.end(); ++eit2)
226  {
227  EFAElement * neigh_elem = *eit2;
228  if (neigh_elem != this)
229  _general_neighbors.push_back(neigh_elem);
230  }
231 }
232 
233 EFAElement *
234 EFAElement::getGeneralNeighbor(unsigned int index) const
235 {
236  return _general_neighbors[index];
237 }
238 
239 unsigned int
241 {
242  return _general_neighbors.size();
243 }
244 
245 void
247  EFANode *& childOfNeighborNode,
248  EFAElement * childOfNeighborElem,
249  std::map<unsigned int, EFANode *> & PermanentNodes,
250  std::map<unsigned int, EFANode *> & TempNodes)
251 {
252  // Important: this must be run only on child elements that were just created
253  if (!_parent)
254  EFAError("no getParent element for child element ", _id, " in mergeNodes");
255 
256  EFAElement * childElem = this;
257  if (childNode != childOfNeighborNode)
258  {
259  if (childNode->category() == EFANode::N_CATEGORY_PERMANENT)
260  {
261  if (childOfNeighborNode->category() == EFANode::N_CATEGORY_PERMANENT)
262  {
263  if (childOfNeighborNode->parent() == childNode) // merge into childNode
264  {
265  childOfNeighborElem->switchNode(childNode, childOfNeighborNode, true);
266  if (!Efa::deleteFromMap(PermanentNodes, childOfNeighborNode))
267  {
268  EFAError("Attempted to delete node: ",
269  childOfNeighborNode->id(),
270  " from PermanentNodes, but couldn't find it");
271  }
272  childOfNeighborNode = childNode;
273  }
274  else if (childNode->parent() == childOfNeighborNode) // merge into childOfNeighborNode
275  {
276  childElem->switchNode(childOfNeighborNode, childNode, true);
277  if (!Efa::deleteFromMap(PermanentNodes, childNode))
278  {
279  EFAError("Attempted to delete node: ",
280  childNode->id(),
281  " from PermanentNodes, but couldn't find it");
282  }
283  childNode = childOfNeighborNode;
284  }
285  else if (childNode->parent() != NULL &&
286  childNode->parent() == childOfNeighborNode->parent())
287  {
288  // merge into childNode if both nodes are child permanent
289  childOfNeighborElem->switchNode(childNode, childOfNeighborNode, true);
290  if (!Efa::deleteFromMap(PermanentNodes,
291  childOfNeighborNode)) // delete childOfNeighborNode
292  {
293  EFAError("Attempted to delete node: ",
294  childOfNeighborNode->id(),
295  " from PermanentNodes, but couldn't find it");
296  }
297  childOfNeighborNode = childNode;
298  }
299  else
300  {
301  EFAError("Attempting to merge nodes: ",
302  childNode->id(),
303  " and ",
304  childOfNeighborNode->id(),
305  " but both are permanent themselves");
306  }
307  }
308  else
309  {
310  if (childOfNeighborNode->parent() != childNode &&
311  childOfNeighborNode->parent() != childNode->parent())
312  {
313  EFAError("Attempting to merge nodes ",
314  childOfNeighborNode->idCatString(),
315  " and ",
316  childNode->idCatString(),
317  " but neither the 2nd node nor its parent is parent of the 1st");
318  }
319  childOfNeighborElem->switchNode(childNode, childOfNeighborNode, true);
320  if (!Efa::deleteFromMap(TempNodes, childOfNeighborNode))
321  EFAError("Attempted to delete node: ",
322  childOfNeighborNode->id(),
323  " from TempNodes, but couldn't find it");
324  childOfNeighborNode = childNode;
325  }
326  }
327  else if (childOfNeighborNode->category() == EFANode::N_CATEGORY_PERMANENT)
328  {
329  if (childNode->parent() != childOfNeighborNode &&
330  childNode->parent() != childOfNeighborNode->parent())
331  {
332  EFAError("Attempting to merge nodes ",
333  childNode->id(),
334  " and ",
335  childOfNeighborNode->id(),
336  " but neither the 2nd node nor its parent is parent of the 1st");
337  }
338  childElem->switchNode(childOfNeighborNode, childNode, true);
339  if (!Efa::deleteFromMap(TempNodes, childNode))
340  EFAError(
341  "Attempted to delete node: ", childNode->id(), " from TempNodes, but couldn't find it");
342  childNode = childOfNeighborNode;
343  }
344  else // both nodes are temporary -- create new permanent node and delete temporary nodes
345  {
346  unsigned int new_node_id = Efa::getNewID(PermanentNodes);
347  EFANode * newNode =
348  new EFANode(new_node_id, EFANode::N_CATEGORY_PERMANENT, childNode->parent());
349  PermanentNodes.insert(std::make_pair(new_node_id, newNode));
350 
351  childOfNeighborElem->switchNode(newNode, childOfNeighborNode, true);
352  childElem->switchNode(newNode, childNode, true);
353 
354  if (childNode->parent() != childOfNeighborNode->parent())
355  {
356  EFAError("Attempting to merge nodes ",
357  childNode->id(),
358  " and ",
359  childOfNeighborNode->id(),
360  " but they don't share a common parent");
361  }
362 
363  if (!Efa::deleteFromMap(TempNodes, childOfNeighborNode))
364  EFAError("Attempted to delete node: ",
365  childOfNeighborNode->id(),
366  " from TempNodes, but couldn't find it");
367  if (!Efa::deleteFromMap(TempNodes, childNode))
368  EFAError(
369  "Attempted to delete node: ", childNode->id(), " from TempNodes, but couldn't find it");
370  childOfNeighborNode = newNode;
371  childNode = newNode;
372  }
373  }
374 }
void clearParentAndChildren()
Definition: EFAElement.C:207
unsigned int getLocalNodeIndex(EFANode *node) const
Definition: EFAElement.C:109
virtual unsigned int getNeighborIndex(const EFAElement *neighbor_elem) const =0
EFANode * parent() const
Definition: EFANode.C:46
EFANode * getNode(unsigned int node_id) const
Definition: EFAElement.C:44
EFANode * getGlobalNodeFromLocalNode(const EFANode *local_node) const
Definition: EFAElement.C:93
unsigned int _id
Definition: EFAElement.h:26
std::string idCatString()
Definition: EFANode.C:18
unsigned int numChildren() const
Definition: EFAElement.C:195
bool deleteFromMap(std::map< unsigned int, T * > &theMap, T *elemToDelete, bool delete_elem=true)
Definition: EFAFuncs.h:21
bool isCrackTipSplit() const
Definition: EFAElement.C:134
unsigned int id() const
Definition: EFAElement.C:26
EFAElement(unsigned int eid, unsigned int n_nodes)
Definition: EFAElement.C:14
void addChild(EFAElement *child)
Definition: EFAElement.C:201
unsigned int numNodes() const
Definition: EFAElement.C:32
N_CATEGORY category() const
Definition: EFANode.C:40
std::vector< EFANode * > _nodes
Definition: EFAElement.h:28
void setParent(EFAElement *parent)
Definition: EFAElement.C:189
unsigned int _num_nodes
Definition: EFAElement.h:27
virtual void switchNode(EFANode *new_node, EFANode *old_node, bool descend_to_parent)=0
virtual ~EFAElement()
Definition: EFAElement.C:23
EFAElement * _parent
Definition: EFAElement.h:30
EFAElement * getGeneralNeighbor(unsigned int index) const
Definition: EFAElement.C:234
unsigned int numGeneralNeighbors() const
Definition: EFAElement.C:240
unsigned int numCrackTipNeighbors() const
Definition: EFAElement.C:140
unsigned int getCrackTipNeighbor(unsigned int index) const
Definition: EFAElement.C:146
EFAElement * getParent() const
Definition: EFAElement.C:174
unsigned int id() const
Definition: EFANode.C:34
void findGeneralNeighbors(std::map< EFANode *, std::set< EFAElement * >> &InverseConnectivity)
Definition: EFAElement.C:214
bool containsNode(EFANode *node) const
Definition: EFAElement.C:50
unsigned int getNewID(std::map< unsigned int, T * > &theMap)
Definition: EFAFuncs.h:37
void mergeNodes(EFANode *&childNode, EFANode *&childOfNeighborNode, EFAElement *childOfNeighborElem, std::map< unsigned int, EFANode * > &PermanentNodes, std::map< unsigned int, EFANode * > &TempNodes)
Definition: EFAElement.C:246
void setCrackTipSplit()
Definition: EFAElement.C:128
void addCrackTipNeighbor(EFAElement *neighbor_elem)
Definition: EFAElement.C:155
std::vector< EFAElement * > _general_neighbors
Definition: EFAElement.h:35
std::vector< EFAElement * > _children
Definition: EFAElement.h:31
EFAElement * getChild(unsigned int child_id) const
Definition: EFAElement.C:180
bool _crack_tip_split_element
Definition: EFAElement.h:32
void printNodes(std::ostream &ostream) const
Definition: EFAElement.C:59
std::vector< unsigned int > _crack_tip_neighbors
Definition: EFAElement.h:33
EFANode * createLocalNodeFromGlobalNode(const EFANode *global_node) const
Definition: EFAElement.C:68
void setNode(unsigned int node_id, EFANode *node)
Definition: EFAElement.C:38