Chaste  Release::3.4
Node.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 
36 #include <cassert>
37 
38 #include "Node.hpp"
39 #include "Exception.hpp"
40 
42 // Constructors
44 
45 template<unsigned SPACE_DIM>
46 void Node<SPACE_DIM>::CommonConstructor(unsigned index, bool isBoundaryNode)
47 {
48  mIndex = index;
49  mIsBoundaryNode = isBoundaryNode;
50  mIsInternal = false;
51  mIsDeleted = false;
52  mpNodeAttributes = NULL;
53 }
54 
55 template<unsigned SPACE_DIM>
56 Node<SPACE_DIM>::Node(unsigned index, ChastePoint<SPACE_DIM> point, bool isBoundaryNode)
57 {
58  mLocation = point.rGetLocation();
59  CommonConstructor(index, isBoundaryNode);
60 }
61 
62 template<unsigned SPACE_DIM>
63 Node<SPACE_DIM>::Node(unsigned index, std::vector<double> coords, bool isBoundaryNode)
64 {
65  for (unsigned i=0; i<SPACE_DIM; i++)
66  {
67  mLocation(i) = coords.at(i);
68  }
69  CommonConstructor(index, isBoundaryNode);
70 }
71 
72 template<unsigned SPACE_DIM>
73 Node<SPACE_DIM>::Node(unsigned index, c_vector<double, SPACE_DIM> location, bool isBoundaryNode)
74 {
75  mLocation = location;
76  CommonConstructor(index, isBoundaryNode);
77 }
78 
79 template<unsigned SPACE_DIM>
80 Node<SPACE_DIM>::Node(unsigned index, bool isBoundaryNode, double v1, double v2, double v3)
81 {
82  mLocation[0] = v1;
83  if (SPACE_DIM > 1)
84  {
85  mLocation[1] = v2;
86  if (SPACE_DIM > 2)
87  {
88  mLocation[2] = v3;
89  }
90  }
91  CommonConstructor(index, isBoundaryNode);
92 }
93 
94 template<unsigned SPACE_DIM>
95 Node<SPACE_DIM>::Node(unsigned index, double *location, bool isBoundaryNode)
96 {
97  for (unsigned i=0; i<SPACE_DIM; i++)
98  {
99  mLocation(i) = location[i];
100  }
101  CommonConstructor(index, isBoundaryNode);
102 }
103 
104 template<unsigned SPACE_DIM>
106 {
107  delete mpNodeAttributes;
108 }
109 
111 // Methods dealing with node location
113 
114 template<unsigned SPACE_DIM>
116 {
117  mLocation = point.rGetLocation();
118 }
119 
120 template<unsigned SPACE_DIM>
121 void Node<SPACE_DIM>::SetIndex(unsigned index)
122 {
123  mIndex = index;
124 }
125 
126 template<unsigned SPACE_DIM>
128 {
129  mIsBoundaryNode = value;
130 }
131 
132 
133 template<unsigned SPACE_DIM>
135 {
136  return ChastePoint<SPACE_DIM>(mLocation);
137 }
138 
139 template<unsigned SPACE_DIM>
140 const c_vector<double, SPACE_DIM>& Node<SPACE_DIM>::rGetLocation() const
141 {
142  // This assert statement is a useful warning: when new nodes are created we overwrite previously deleted nodes if there are any.
143  // This means that we can not use this method to interrogate deleted nodes about their position before deletion because we can't
144  // guarantee that the node has not been overwritten already. Hence, when implementing new functionality we need to make sure
145  // that this functionality does not rely on being able to interrogate deleted nodes for their location.
146  // \todo #2401: make this an exception.
147  assert(!mIsDeleted);
148  return mLocation;
149 }
150 
151 template<unsigned SPACE_DIM>
152 c_vector<double, SPACE_DIM>& Node<SPACE_DIM>::rGetModifiableLocation()
153 {
154  assert(!mIsDeleted);
155  return mLocation;
156 }
157 
158 template<unsigned SPACE_DIM>
160 {
161  return mIndex;
162 }
163 
164 template<unsigned SPACE_DIM>
166 {
167  return mIsBoundaryNode;
168 }
169 
170 template<unsigned SPACE_DIM>
171 void Node<SPACE_DIM>::AddNodeAttribute(double attribute)
172 {
173  ConstructNodeAttributes();
174 
175  mpNodeAttributes->AddAttribute(attribute);
176 }
177 
178 template<unsigned SPACE_DIM>
180 {
181  CheckForNodeAttributes();
182 
183  return mpNodeAttributes->rGetAttributes();
184 }
185 
186 template<unsigned SPACE_DIM>
188 {
189  unsigned num_attributes;
190  if (!mpNodeAttributes)
191  {
192  num_attributes = 0u;
193  }
194  else
195  {
196  num_attributes = mpNodeAttributes->rGetAttributes().size();
197  }
198 
199  return num_attributes;
200 }
201 
202 template<unsigned SPACE_DIM>
204 {
205  return (mpNodeAttributes != NULL);
206 }
207 
208 template<unsigned SPACE_DIM>
209 c_vector<double, SPACE_DIM>& Node<SPACE_DIM>::rGetAppliedForce()
210 {
211  CheckForNodeAttributes();
212 
213  return mpNodeAttributes->rGetAppliedForce();
214 }
215 
216 template<unsigned SPACE_DIM>
218 {
219  ConstructNodeAttributes();
220 
221  mpNodeAttributes->ClearAppliedForce();
222 }
223 
224 template<unsigned SPACE_DIM>
225 void Node<SPACE_DIM>::AddAppliedForceContribution(c_vector<double, SPACE_DIM>& forceContribution)
226 {
227  ConstructNodeAttributes();
228 
229  mpNodeAttributes->AddAppliedForceContribution(forceContribution);
230 }
231 
232 template<unsigned SPACE_DIM>
234 {
235  CheckForNodeAttributes();
236 
237  return mpNodeAttributes->IsParticle();
238 }
239 
240 template<unsigned SPACE_DIM>
241 void Node<SPACE_DIM>::SetIsParticle(bool isParticle)
242 {
243  ConstructNodeAttributes();
244 
245  mpNodeAttributes->SetIsParticle(isParticle);
246 }
247 
248 template<unsigned SPACE_DIM>
250 {
251  CheckForNodeAttributes();
252 
253  return mpNodeAttributes->GetRadius();
254 }
255 
256 template<unsigned SPACE_DIM>
257 void Node<SPACE_DIM>::SetRadius(double radius)
258 {
259  ConstructNodeAttributes();
260 
261  mpNodeAttributes->SetRadius(radius);
262 }
263 
265 // Tracking (boundary) elements which contain this node as a vertex
267 
268 template<unsigned SPACE_DIM>
269 void Node<SPACE_DIM>::AddElement(unsigned index)
270 {
271  mElementIndices.insert(index);
272 }
273 
274 template<unsigned SPACE_DIM>
275 void Node<SPACE_DIM>::RemoveElement(unsigned index)
276 {
277  unsigned count = mElementIndices.erase(index);
278  if (count == 0)
279  {
280  EXCEPTION("Tried to remove an index which was not in the set");
281  }
282 }
283 
284 template<unsigned SPACE_DIM>
286 {
287  unsigned count = mBoundaryElementIndices.erase(index);
288  if (count == 0)
289  {
290  EXCEPTION("Tried to remove an index which was not in the set");
291  }
292 }
293 
294 template<unsigned SPACE_DIM>
296 {
297  mBoundaryElementIndices.insert(index);
298 }
299 
300 template<unsigned SPACE_DIM>
302 {
303  return mElementIndices;
304 }
305 
306 template<unsigned SPACE_DIM>
308 {
309  return mBoundaryElementIndices;
310 }
311 
312 template<unsigned SPACE_DIM>
314 {
315  return mElementIndices.size();
316 }
317 
318 template<unsigned SPACE_DIM>
320 {
321  return mBoundaryElementIndices.size();
322 }
323 
325 // Methods dealing with some node flags (deleted, region)
327 
328 template<unsigned SPACE_DIM>
330 {
331  if (mpNodeAttributes == NULL)
332  {
333  EXCEPTION("Node has no attributes associated with it. Construct attributes first");
334  }
335 }
336 
337 template<unsigned SPACE_DIM>
339 {
340  if (mpNodeAttributes == NULL)
341  {
342  mpNodeAttributes = new NodeAttributes<SPACE_DIM>();
343  }
344 }
345 
346 template<unsigned SPACE_DIM>
348 {
349  mIsDeleted = true;
350 }
351 
352 template<unsigned SPACE_DIM>
354 {
355  return mIsDeleted;
356 }
357 
358 template<unsigned SPACE_DIM>
360 {
361  mIsInternal = true;
362 }
363 
364 template<unsigned SPACE_DIM>
366 {
367  return mIsInternal;
368 }
369 
370 template<unsigned SPACE_DIM>
371 void Node<SPACE_DIM>::SetRegion(unsigned region)
372 {
373  ConstructNodeAttributes();
374  mpNodeAttributes->SetRegion(region);
375 }
376 
377 template<unsigned SPACE_DIM>
379 {
380  unsigned region = 0;
381 
382  if (mpNodeAttributes)
383  {
384  region = mpNodeAttributes->GetRegion();
385  }
386 
387  return region;
388 }
389 
390 
392 // Explicit instantiation
394 
395 template class Node<1>;
396 template class Node<2>;
397 template class Node<3>;
void CheckForNodeAttributes() const
Definition: Node.cpp:329
bool IsInternal() const
Definition: Node.cpp:365
void SetPoint(ChastePoint< SPACE_DIM > point)
Definition: Node.cpp:115
Node(unsigned index, ChastePoint< SPACE_DIM > point, bool isBoundaryNode=false)
Definition: Node.cpp:56
void ConstructNodeAttributes()
Definition: Node.cpp:338
Definition: Node.hpp:58
c_vector< double, DIM > & rGetLocation()
Definition: ChastePoint.cpp:76
void SetAsBoundaryNode(bool value=true)
Definition: Node.cpp:127
void MarkAsInternal()
Definition: Node.cpp:359
void RemoveElement(unsigned index)
Definition: Node.cpp:275
void CommonConstructor(unsigned index, bool isBoundaryNode)
Definition: Node.cpp:46
void AddAppliedForceContribution(c_vector< double, SPACE_DIM > &forceContribution)
Definition: Node.cpp:225
void AddNodeAttribute(double attribute)
Definition: Node.cpp:171
void SetRadius(double radius)
Definition: Node.cpp:257
bool IsBoundaryNode() const
Definition: Node.cpp:165
#define EXCEPTION(message)
Definition: Exception.hpp:143
unsigned GetNumBoundaryElements() const
Definition: Node.cpp:319
void RemoveBoundaryElement(unsigned index)
Definition: Node.cpp:285
std::set< unsigned > & rGetContainingElementIndices()
Definition: Node.cpp:301
void SetIndex(unsigned index)
Definition: Node.cpp:121
bool IsParticle()
Definition: Node.cpp:233
std::vector< double > & rGetNodeAttributes()
Definition: Node.cpp:179
void ClearAppliedForce()
Definition: Node.cpp:217
unsigned GetNumNodeAttributes()
Definition: Node.cpp:187
unsigned GetRegion() const
Definition: Node.cpp:378
bool HasNodeAttributes()
Definition: Node.cpp:203
void SetIsParticle(bool isParticle)
Definition: Node.cpp:241
std::set< unsigned > & rGetContainingBoundaryElementIndices()
Definition: Node.cpp:307
void SetRegion(unsigned region)
Definition: Node.cpp:371
const c_vector< double, SPACE_DIM > & rGetLocation() const
Definition: Node.cpp:140
void AddElement(unsigned index)
Definition: Node.cpp:269
ChastePoint< SPACE_DIM > GetPoint() const
Definition: Node.cpp:134
void AddBoundaryElement(unsigned index)
Definition: Node.cpp:295
c_vector< double, SPACE_DIM > & rGetAppliedForce()
Definition: Node.cpp:209
unsigned GetNumContainingElements() const
Definition: Node.cpp:313
unsigned GetIndex() const
Definition: Node.cpp:159
c_vector< double, SPACE_DIM > & rGetModifiableLocation()
Definition: Node.cpp:152
void MarkAsDeleted()
Definition: Node.cpp:347
bool IsDeleted() const
Definition: Node.cpp:353
~Node()
Definition: Node.cpp:105
double GetRadius()
Definition: Node.cpp:249