Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
AbstractElement.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "AbstractElement.hpp"
37
39#include "Exception.hpp"
40
41#include <cassert>
42
44// Implementation
46
47template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
49 : mNodes(rNodes),
50 mIndex(index),
51 mIsDeleted(false),
52 mOwnership(true),
53 mpElementAttributes(nullptr)
54{
55 // Sanity checking
56 assert(ELEMENT_DIM <= SPACE_DIM);
57}
58
59template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
61 : mIndex(index),
62 mIsDeleted(false),
63 mOwnership(true),
64 mpElementAttributes(nullptr)
65{}
66
67template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
69{
70 delete mpElementAttributes;
71}
72
73template <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
88template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
89double AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeLocation(unsigned localIndex, unsigned dimension) const
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 */
101template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
102c_vector<double, SPACE_DIM> AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeLocation(unsigned localIndex) const
104 assert((unsigned)localIndex < mNodes.size());
105 return mNodes[localIndex]->rGetLocation();
106}
107
108template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
110{
111 assert((unsigned)localIndex < mNodes.size());
112 return mNodes[localIndex]->GetIndex();
113}
114
115template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
117{
118 assert((unsigned)localIndex < mNodes.size());
119 return mNodes[localIndex];
120}
121
122template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
124{
125 return mNodes.size();
126}
127
128template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
130{
131 mNodes.push_back(pNode);
132}
133
134template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
136{
137 return mIsDeleted;
138}
139
140template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
142{
143 return mIndex;
144}
145
146template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
149 mIndex = index;
150}
151
152template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
154{
155 return mOwnership;
156}
158template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
160{
161 mOwnership = ownership;
162}
163
164template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
166{
167 ConstructElementAttributes();
168
169 mpElementAttributes->SetFirstAttribute(attribute);
171
172template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
174{
175 ConstructElementAttributes();
176
177 return mpElementAttributes->GetFirstAttribute();
178}
179
180template <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.");
190 return unsigned_attr;
191}
192
193template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
195{
196 if (mpElementAttributes == nullptr)
197 {
198 mpElementAttributes = new ElementAttributes<ELEMENT_DIM, SPACE_DIM>();
199 }
200}
202template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
204{
205 ConstructElementAttributes();
206
207 mpElementAttributes->AddAttribute(attribute);
209
210template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
212{
213 if (mpElementAttributes == nullptr)
214 {
215 EXCEPTION("Element has no attributes associated with it. Construct attributes first");
216 }
217
218 return mpElementAttributes->rGetAttributes();
219}
221template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
223{
224 return mpElementAttributes == nullptr ? 0 : mpElementAttributes->rGetAttributes().size();
225}
226
227// Explicit instantiation
228template class AbstractElement<0,1>;
229template class AbstractElement<1,1>;
230template class AbstractElement<0,2>;
231template class AbstractElement<1,2>;
232template class AbstractElement<2,2>;
233template class AbstractElement<0,3>;
234template class AbstractElement<1,3>;
235template class AbstractElement<2,3>;
236template class AbstractElement<3,3>;
#define EXCEPTION(message)
void SetAttribute(double attribute)
void ReplaceNode(Node< SPACE_DIM > *pOldNode, Node< SPACE_DIM > *pNewNode)
Node< SPACE_DIM > * GetNode(unsigned localIndex) const
unsigned GetUnsignedAttribute()
virtual ~AbstractElement()
void ConstructElementAttributes()
std::vector< double > & rGetElementAttributes()
double GetNodeLocation(unsigned localIndex, unsigned dimension) const
unsigned GetNumElementAttributes()
void AddElementAttribute(double attribute)
bool IsDeleted() const
unsigned GetNumNodes() const
AbstractElement(unsigned index, const std::vector< Node< SPACE_DIM > * > &rNodes)
unsigned GetNodeGlobalIndex(unsigned localIndex) const
bool GetOwnership() const
void AddNode(Node< SPACE_DIM > *pNode)
unsigned GetIndex() const
void SetOwnership(bool ownership)
void SetIndex(unsigned index)
static bool WithinAnyTolerance(double number1, double number2, double relTol=DBL_EPSILON, double absTol=DBL_EPSILON, bool printError=false)
Definition Node.hpp:59