Chaste  Release::3.4
AbstractElement.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 "AbstractElement.hpp"
37 
38 #include "MathsCustomFunctions.hpp"
39 #include "Exception.hpp"
40 
41 #include <cassert>
42 
44 // Implementation
46 
47 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
49  : mNodes(rNodes),
50  mIndex(index),
51  mIsDeleted(false),
52  mOwnership(true),
53  mpElementAttributes(NULL)
54 {
55  // Sanity checking
56  assert(ELEMENT_DIM <= SPACE_DIM);
57 }
58 
59 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
61  : mIndex(index),
62  mIsDeleted(false),
63  mOwnership(true),
64  mpElementAttributes(NULL)
65 {}
66 
67 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
69 {
70  delete mpElementAttributes;
71 }
72 
73 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
75 {
76  assert(pOldNode != pNewNode);
77  for (unsigned i=0; i<this->mNodes.size(); i++)
78  {
79  if (this->mNodes[i] == pOldNode)
80  {
81  UpdateNode(i, pNewNode);
82  return;
83  }
84  }
85  EXCEPTION("You didn't have that node to start with.");
86 }
87 
88 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
89 double AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeLocation(unsigned localIndex, unsigned dimension) const
90 {
91  assert(dimension < SPACE_DIM);
92  assert((unsigned)localIndex < mNodes.size());
93  return mNodes[localIndex]->rGetLocation()[dimension];
94 }
95 
96 /*
97  * Note for future reference: this used to return a reference to a c_vector, in which case a
98  * weird error arose where it compiled, ran and passed on some machines but failed the tests
99  * (bad_size errors) on another machine. So be careful if you think about changing it!
100  */
101 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
102 c_vector<double, SPACE_DIM> AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeLocation(unsigned localIndex) const
103 {
104  assert((unsigned)localIndex < mNodes.size());
105  return mNodes[localIndex]->rGetLocation();
106 }
107 
108 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
110 {
111  assert((unsigned)localIndex < mNodes.size());
112  return mNodes[localIndex]->GetIndex();
113 }
114 
115 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
117 {
118  assert((unsigned)localIndex < mNodes.size());
119  return mNodes[localIndex];
120 }
121 
122 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
124 {
125  return mNodes.size();
126 }
127 
128 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
130 {
131  mNodes.push_back(pNode);
132 }
133 
134 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
136 {
137  return mIsDeleted;
138 }
139 
140 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
142 {
143  return mIndex;
144 }
145 
146 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
148 {
149  mIndex = index;
150 }
151 
152 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
154 {
155  return mOwnership;
156 }
157 
158 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
160 {
161  mOwnership = ownership;
162 }
163 
164 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
166 {
167  ConstructElementAttributes();
168 
169  mpElementAttributes->SetFirstAttribute(attribute);
170 }
171 
172 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
174 {
175  ConstructElementAttributes();
176 
177  return mpElementAttributes->GetFirstAttribute();
178 }
179 
180 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
182 {
183  double double_attr = GetAttribute();
184  unsigned unsigned_attr = (unsigned) (double_attr + 0.5);
185 
186  if( CompareDoubles::WithinAnyTolerance(double_attr, unsigned_attr) == false )
187  {
188  EXCEPTION("Element attribute '"<< double_attr <<"' cannot be converted to an unsigned.");
189  }
190  return unsigned_attr;
191 }
192 
193 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
195 {
196  if (mpElementAttributes == NULL)
197  {
198  mpElementAttributes = new ElementAttributes<ELEMENT_DIM, SPACE_DIM>();
199  }
200 }
201 
202 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
204 {
205  ConstructElementAttributes();
206 
207  mpElementAttributes->AddAttribute(attribute);
208 }
209 
210 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
212 {
213  if (mpElementAttributes == NULL)
214  {
215  EXCEPTION("Element has no attributes associated with it. Construct attributes first");
216  }
217 
218  return mpElementAttributes->rGetAttributes();
219 }
220 
221 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
223 {
224  return mpElementAttributes == NULL ? 0 : mpElementAttributes->rGetAttributes().size();
225 }
226 
227 
229 // Explicit instantiation
231 
232 template class AbstractElement<0,1>;
233 template class AbstractElement<1,1>;
234 template class AbstractElement<0,2>;
235 template class AbstractElement<1,2>;
236 template class AbstractElement<2,2>;
237 template class AbstractElement<0,3>;
238 template class AbstractElement<1,3>;
239 template class AbstractElement<2,3>;
240 template class AbstractElement<3,3>;
void SetAttribute(double attribute)
void AddElementAttribute(double attribute)
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
Definition: Node.hpp:58
unsigned GetNodeGlobalIndex(unsigned localIndex) const
void SetIndex(unsigned index)
#define EXCEPTION(message)
Definition: Exception.hpp:143
void ReplaceNode(Node< SPACE_DIM > *pOldNode, Node< SPACE_DIM > *pNewNode)
void SetOwnership(bool ownership)
unsigned GetNumElementAttributes()
bool IsDeleted() const
Node< SPACE_DIM > * GetNode(unsigned localIndex) const
AbstractElement(unsigned index, const std::vector< Node< SPACE_DIM > * > &rNodes)
bool GetOwnership() const
void ConstructElementAttributes()
std::vector< double > & rGetElementAttributes()
unsigned GetNumNodes() const
virtual ~AbstractElement()
unsigned GetUnsignedAttribute()
unsigned GetIndex() const
void AddNode(Node< SPACE_DIM > *pNode)
double GetNodeLocation(unsigned localIndex, unsigned dimension) const