CellwiseData.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 #include "CellwiseData.hpp"
00029 
00030 
00031 template<unsigned DIM>
00032 CellwiseData<DIM>* CellwiseData<DIM>::mpInstance = NULL;
00033 
00034 
00035 template<unsigned DIM>
00036 CellwiseData<DIM>* CellwiseData<DIM>::Instance()
00037 {
00038     if (mpInstance == NULL)
00039     {
00040         mpInstance = new CellwiseData<DIM>;
00041     }
00042     return mpInstance;
00043 }
00044 
00045 
00046 template<unsigned DIM>
00047 CellwiseData<DIM>::CellwiseData()
00048  :  mpTissue(NULL),
00049     mAllocatedMemory(false),
00050     mConstantDataForTesting(0),
00051     mUseConstantDataForTesting(false)
00052 {
00053     // Make sure there's only one instance - enforces correct serialization
00054     assert(mpInstance == NULL);
00055 }
00056 
00057 
00058 template<unsigned DIM>
00059 CellwiseData<DIM>::~CellwiseData()
00060 {
00061 }
00062 
00063 
00064 template<unsigned DIM>
00065 void CellwiseData<DIM>::Destroy()
00066 {
00067     if (mpInstance)
00068     {
00069         delete mpInstance;
00070         mpInstance = NULL;
00071     }
00072 }
00073 
00074 
00075 template<unsigned DIM>
00076 double CellwiseData<DIM>::GetValue(TissueCell& rCell, unsigned variableNumber)
00077 {
00078     // To test a cell and cell cycle models without a tissue
00079     if (mUseConstantDataForTesting)
00080     {
00081         return mConstantDataForTesting[variableNumber];
00082     }
00083 
00084     assert(IsSetUp());
00085     assert(mpTissue!=NULL);
00086     assert(mAllocatedMemory);
00087     assert(variableNumber < mNumberOfVariables);
00088 
00089     unsigned node_index = mpTissue->GetLocationIndexUsingCell(rCell);
00090     unsigned vector_index = node_index*mNumberOfVariables + variableNumber;
00091     return mData[vector_index];
00092 }
00093 
00094 
00095 template<unsigned DIM>
00096 void CellwiseData<DIM>::SetValue(double value, Node<DIM>* pNode, unsigned variableNumber)
00097 {
00098     assert(IsSetUp());
00099     assert(variableNumber < mNumberOfVariables);
00100     unsigned vector_index = pNode->GetIndex()*mNumberOfVariables + variableNumber;
00101     mData[vector_index] = value;
00102 }
00103 
00104 
00105 template<unsigned DIM>
00106 void CellwiseData<DIM>::SetTissue(MeshBasedTissue<DIM>& rTissue)
00107 {
00108     if (mAllocatedMemory == false)
00109     {
00110         EXCEPTION("SetTissue must be called after SetNumNodesAndVars()");
00111     }
00112 
00113     mpTissue=&rTissue;
00114 }
00115 
00116 
00117 template<unsigned DIM>
00118 MeshBasedTissue<DIM>& CellwiseData<DIM>::rGetTissue()
00119 {
00120     return *mpTissue;
00121 }
00122 
00123 
00124 template<unsigned DIM>
00125 void CellwiseData<DIM>::SetNumNodesAndVars(unsigned numNodes, unsigned numberOfVariables)
00126 {
00127     if (mpTissue!=NULL)
00128     {
00129         EXCEPTION("SetNumNodesAndVars() must be called before setting the Tissue (and after a Destroy)");
00130     }
00131 
00132     assert(numberOfVariables > 0);
00133     assert(mAllocatedMemory==false);
00134 
00135     mNumberOfVariables = numberOfVariables;
00136     mData.clear();
00137     mData.resize(numNodes * mNumberOfVariables, 0.0);
00138 
00139     mAllocatedMemory = true;
00140 }
00141 
00142 
00143 template<unsigned DIM>
00144 bool CellwiseData<DIM>::IsSetUp()
00145 {
00146     return ((mAllocatedMemory) && (mpInstance!=NULL) && (mpTissue!=NULL));
00147 }
00148 
00149 
00150 template<unsigned DIM>
00151 void CellwiseData<DIM>::ReallocateMemory()
00152 {
00153     assert(mAllocatedMemory==true);
00154     assert(mpTissue!=NULL);
00155 
00156     unsigned num_nodes = mpTissue->rGetMesh().GetNumNodes();
00157     if (mData.size() != num_nodes*mNumberOfVariables)
00158     {
00159         mData.clear();
00160         mData.resize(num_nodes * mNumberOfVariables, 0.0);
00161     }
00162 }
00163 
00164 
00165 template<unsigned DIM>
00166 void CellwiseData<DIM>::SetConstantDataForTesting(std::vector<double>& rValues)
00167 {
00168     mConstantDataForTesting = rValues;
00169     mUseConstantDataForTesting = true;
00170 }
00171 
00172 
00174 // Explicit instantiation
00176 
00177 template class CellwiseData<1>;
00178 template class CellwiseData<2>;
00179 template class CellwiseData<3>;

Generated by  doxygen 1.6.2