AbstractElement.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "AbstractElement.hpp"
00037 
00038 #include "MathsCustomFunctions.hpp"
00039 #include "Exception.hpp"
00040 
00041 #include <cassert>
00042 
00044 // Implementation
00046 
00047 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00048 AbstractElement<ELEMENT_DIM, SPACE_DIM>::AbstractElement(unsigned index, const std::vector<Node<SPACE_DIM>*>& rNodes)
00049     : mNodes(rNodes),
00050       mIndex(index),
00051       mAttribute(0.0),
00052       mIsDeleted(false),
00053       mOwnership(true),
00054       mFlag(false)
00055 {
00056     // Sanity checking
00057     assert(ELEMENT_DIM <= SPACE_DIM);
00058 
00059     // Flags must be initialised before the Jacobian calculations, or assertions trip
00060 }
00061 
00062 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00063 AbstractElement<ELEMENT_DIM, SPACE_DIM>::AbstractElement(unsigned index)
00064     : mIndex(index),
00065       mAttribute(0.0),
00066       mIsDeleted(false),
00067       mOwnership(true),
00068       mFlag(false)
00069 {}
00070 
00071 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00072 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::ReplaceNode(Node<SPACE_DIM>* pOldNode, Node<SPACE_DIM>* pNewNode)
00073 {
00074     assert(pOldNode != pNewNode);
00075     for (unsigned i=0; i<this->mNodes.size(); i++)
00076     {
00077         if (this->mNodes[i] == pOldNode)
00078         {
00079             UpdateNode(i, pNewNode);
00080             return;
00081         }
00082     }
00083     EXCEPTION("You didn't have that node to start with.");
00084 }
00085 
00086 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00087 double AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeLocation(unsigned localIndex, unsigned dimension) const
00088 {
00089     assert(dimension < SPACE_DIM);
00090     assert((unsigned)localIndex < mNodes.size());
00091     return mNodes[localIndex]->rGetLocation()[dimension];
00092 }
00093 
00094 /*
00095  * Note for future reference: this used to return a reference to a c_vector, in which case a
00096  * weird error arose where it compiled, ran and passed on some machines but failed the tests
00097  * (bad_size errors) on another machine.  So be careful if you think about changing it!
00098  */
00099 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00100 c_vector<double, SPACE_DIM> AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeLocation(unsigned localIndex) const
00101 {
00102     assert((unsigned)localIndex < mNodes.size());
00103     return mNodes[localIndex]->rGetLocation();
00104 }
00105 
00106 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00107 unsigned AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNodeGlobalIndex(unsigned localIndex) const
00108 {
00109     assert((unsigned)localIndex < mNodes.size());
00110     return mNodes[localIndex]->GetIndex();
00111 }
00112 
00113 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00114 Node<SPACE_DIM>* AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNode(unsigned localIndex) const
00115 {
00116     assert((unsigned)localIndex < mNodes.size());
00117     return mNodes[localIndex];
00118 }
00119 
00120 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00121 unsigned AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetNumNodes() const
00122 {
00123     return mNodes.size();
00124 }
00125 
00126 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00127 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::AddNode(Node<SPACE_DIM>* pNode)
00128 {
00129     mNodes.push_back(pNode);
00130 }
00131 
00132 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00133 bool AbstractElement<ELEMENT_DIM, SPACE_DIM>::IsDeleted() const
00134 {
00135     return mIsDeleted;
00136 }
00137 
00138 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00139 unsigned AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetIndex() const
00140 {
00141     return mIndex;
00142 }
00143 
00144 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00145 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::SetIndex(unsigned index)
00146 {
00147     mIndex = index;
00148 }
00149 
00150 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00151 bool AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetOwnership() const
00152 {
00153     return mOwnership;
00154 }
00155 
00156 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00157 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::SetOwnership(bool ownership)
00158 {
00159     mOwnership = ownership;
00160 }
00161 
00162 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00163 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::Flag()
00164 {
00165     mFlag = true;
00166 }
00167 
00168 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00169 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::Unflag()
00170 {
00171     mFlag = false;
00172 }
00173 
00174 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00175 bool AbstractElement<ELEMENT_DIM, SPACE_DIM>::IsFlagged() const
00176 {
00177     return mFlag;
00178 }
00179 
00180 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00181 void AbstractElement<ELEMENT_DIM, SPACE_DIM>::SetAttribute(double attribute)
00182 {
00183     mAttribute = attribute;
00184 }
00185 
00186 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00187 double AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetAttribute()
00188 {
00189     return mAttribute;
00190 }
00191 
00192 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00193 unsigned AbstractElement<ELEMENT_DIM, SPACE_DIM>::GetUnsignedAttribute()
00194 {
00195     double double_attr = GetAttribute();
00196     unsigned unsigned_attr = (unsigned) (double_attr + 0.5);
00197 
00198     if( CompareDoubles::WithinAnyTolerance(double_attr, unsigned_attr) == false )
00199     {
00200         EXCEPTION("Element attribute '"<< double_attr <<"' cannot be converted to an unsigned.");
00201     }
00202     return unsigned_attr;
00203 }
00204 
00205 
00207 // Explicit instantiation
00209 
00210 template class AbstractElement<0,1>;
00211 template class AbstractElement<1,1>;
00212 template class AbstractElement<0,2>;
00213 template class AbstractElement<1,2>;
00214 template class AbstractElement<2,2>;
00215 template class AbstractElement<0,3>;
00216 template class AbstractElement<1,3>;
00217 template class AbstractElement<2,3>;
00218 template class AbstractElement<3,3>;

Generated by  doxygen 1.6.2