Chaste  Release::3.4
MutableElement.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 #include "MutableElement.hpp"
36 #include "RandomNumberGenerator.hpp"
37 #include <cassert>
38 
39 
40 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
42  : AbstractElement<ELEMENT_DIM, SPACE_DIM>(index)
43 {
44 }
45 
46 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
48  const std::vector<Node<SPACE_DIM>*>& rNodes)
49  : AbstractElement<ELEMENT_DIM, SPACE_DIM>(index, rNodes)
50 {
51  if (SPACE_DIM == ELEMENT_DIM)
52  {
54  }
55 }
56 
57 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
59 {
60 }
61 
62 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
64 {
65  for (unsigned i=0; i<this->mNodes.size(); i++)
66  {
67  this->mNodes[i]->AddElement(this->mIndex);
68  }
69 }
70 
71 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
73 {
74  // Mark element as deleted
75  this->mIsDeleted = true;
76 
77  // Update nodes in the element so they know they are not contained by it
78  for (unsigned i=0; i<this->GetNumNodes(); i++)
79  {
80  this->mNodes[i]->RemoveElement(this->mIndex);
81  }
82 }
83 
84 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
86 {
87  for (unsigned i=0; i<this->GetNumNodes(); i++)
88  {
89  this->mNodes[i]->RemoveElement(this->mIndex);
90  }
91  this->mIndex = index;
92  RegisterWithNodes();
93 }
94 
95 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
97 {
98  assert(rIndex < this->mNodes.size());
99 
100  // Remove it from the node at this location
101  this->mNodes[rIndex]->RemoveElement(this->mIndex);
102 
103  // Update the node at this location
104  this->mNodes[rIndex] = pNode;
105 
106  // Add element to this node
107  this->mNodes[rIndex]->AddElement(this->mIndex);
108 }
109 
110 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
112 {
113  assert(rIndex < this->mNodes.size());
114 
115  // Remove element from the node at this location
116  this->mNodes[rIndex]->RemoveElement(this->mIndex);
117 
118  // Remove the node at rIndex (removes node from element)
119  this->mNodes.erase(this->mNodes.begin() + rIndex);
120 }
121 
122 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
124 {
130  if (this->mNodes.empty())
131  {
132  // Populate mNodes with pNode
133  this->mNodes.push_back(pNode);
134 
135  // Add element to this node
136  this->mNodes[0]->AddElement(this->mIndex);
137  }
138  else
139  {
140  assert(rIndex < this->mNodes.size());
141 
142  // Add pNode to rIndex+1 element of mNodes pushing the others up
143  this->mNodes.insert(this->mNodes.begin() + rIndex+1, pNode);
144 
145  // Add element to this node
146  this->mNodes[rIndex+1]->AddElement(this->mIndex);
147  }
148 }
149 
150 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
152 {
153  unsigned local_index = UINT_MAX;
154  for (unsigned i=0; i<this->mNodes.size(); i++)
155  {
156  if (this->GetNodeGlobalIndex(i) == globalIndex)
157  {
158  local_index = i;
159  }
160  }
161  return local_index;
162 }
163 
164 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
166 {
167  bool is_element_on_boundary = false;
168  for (unsigned i=0; i<this->mNodes.size(); i++)
169  {
170  if (this->GetNode(i)->IsBoundaryNode())
171  {
172  is_element_on_boundary = true;
173  break;
174  }
175  }
176  return is_element_on_boundary;
177 }
178 
180 // Specialization for 1d elements //
181 // //
182 // 1d elements are just edges (lines) //
184 
189 template<unsigned SPACE_DIM>
190 MutableElement<1, SPACE_DIM>::MutableElement(unsigned index, const std::vector<Node<SPACE_DIM>*>& rNodes)
191  : AbstractElement<1, SPACE_DIM>(index, rNodes)
192 {
193  // Sanity checking
194  assert(this->mNodes.size() == 2);
195  assert(SPACE_DIM > 0);
196 }
197 
198 template<unsigned SPACE_DIM>
200 {
201 }
202 
203 template<unsigned SPACE_DIM>
205 {
206  for (unsigned i=0; i<this->mNodes.size(); i++)
207  {
208  this->mNodes[i]->AddElement(this->mIndex);
209  }
210 }
211 
212 template<unsigned SPACE_DIM>
214 {
215  // Mark element as deleted
216  this->mIsDeleted = true;
217 
218  // Update nodes in the element so they know they are not contained by it
219  for (unsigned i=0; i<this->GetNumNodes(); i++)
220  {
221  this->mNodes[i]->RemoveElement(this->mIndex);
222  }
223 }
224 
225 template <unsigned SPACE_DIM>
227 {
228  for (unsigned i=0; i<this->GetNumNodes(); i++)
229  {
230  this->mNodes[i]->RemoveElement(this->mIndex);
231  }
232  this->mIndex = index;
234 }
235 
236 template<unsigned SPACE_DIM>
237 void MutableElement<1, SPACE_DIM>::UpdateNode(const unsigned& rIndex, Node<SPACE_DIM>* pNode)
238 {
239  assert(rIndex < this->mNodes.size());
240 
241  // Remove it from the node at this location
242  this->mNodes[rIndex]->RemoveElement(this->mIndex);
243 
244  // Update the node at this location
245  this->mNodes[rIndex] = pNode;
246 
247  // Add element to this node
248  this->mNodes[rIndex]->AddElement(this->mIndex);
249 }
250 
251 template<unsigned SPACE_DIM>
252 void MutableElement<1, SPACE_DIM>::DeleteNode(const unsigned& rIndex)
253 {
254  assert(rIndex < this->mNodes.size());
255 
256  // Remove element from the node at this location
257  this->mNodes[rIndex]->RemoveElement(this->mIndex);
258 
259  // Remove the node at rIndex (removes node from element)
260  this->mNodes.erase(this->mNodes.begin() + rIndex);
261 }
262 
263 template<unsigned SPACE_DIM>
264 void MutableElement<1, SPACE_DIM>::AddNode(Node<SPACE_DIM>* pNode, const unsigned& rIndex)
265 {
266  assert(rIndex < this->mNodes.size());
267 
268  // Add pNode to rIndex+1 element of mNodes pushing the others up
269  this->mNodes.insert(this->mNodes.begin() + rIndex+1, pNode);
270 
271  // Add element to this node
272  this->mNodes[rIndex+1]->AddElement(this->mIndex);
273 }
274 
275 template<unsigned SPACE_DIM>
276 unsigned MutableElement<1, SPACE_DIM>::GetNodeLocalIndex(unsigned globalIndex) const
277 {
278  unsigned local_index = UINT_MAX;
279  for (unsigned i=0; i<this->mNodes.size(); i++)
280  {
281  if (this->GetNodeGlobalIndex(i) == globalIndex)
282  {
283  local_index = i;
284  }
285  }
286  return local_index;
287 }
288 
289 template<unsigned SPACE_DIM>
291 {
292  bool is_element_on_boundary = false;
293  for (unsigned i=0; i<this->mNodes.size(); i++)
294  {
295  if (this->GetNode(i)->IsBoundaryNode())
296  {
297  is_element_on_boundary = true;
298  break;
299  }
300  }
301  return is_element_on_boundary;
302 }
303 
305 // Explicit instantiation
307 
308 template class MutableElement<1,1>;
309 template class MutableElement<1,2>;
310 template class MutableElement<1,3>;
311 template class MutableElement<2,2>;
312 template class MutableElement<2,3>;
313 template class MutableElement<3,3>;
void UpdateNode(const unsigned &rIndex, Node< SPACE_DIM > *pNode)
Definition: Node.hpp:58
unsigned GetNodeGlobalIndex(unsigned localIndex) const
Node< SPACE_DIM > * GetNode(unsigned localIndex) const
void ResetIndex(unsigned index)
std::vector< Node< SPACE_DIM > * > mNodes
void AddNode(Node< SPACE_DIM > *pNode, const unsigned &rIndex)
unsigned GetNumNodes() const
void RegisterWithNodes()
MutableElement(unsigned index)
void AddElement(unsigned index)
Definition: Node.cpp:269
bool IsElementOnBoundary() const
void DeleteNode(const unsigned &rIndex)
unsigned GetNodeLocalIndex(unsigned globalIndex) const