CellwiseData.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 
00031 template<unsigned DIM>
00032 CellwiseData<DIM>* CellwiseData<DIM>::mpInstance = NULL;
00033 
00034 
00035 /*
00036  * Return a pointer to the CellwiseData object.
00037  * The first time this is called, the object is created.
00038  */
00039 template<unsigned DIM>
00040 CellwiseData<DIM>* CellwiseData<DIM>::Instance()
00041 {
00042     if (mpInstance == NULL)
00043     {
00044         mpInstance = new CellwiseData<DIM>;
00045     }
00046     return mpInstance;
00047 }
00048 
00049 
00050 template<unsigned DIM>
00051 CellwiseData<DIM>::CellwiseData()
00052  :  mpTissue(NULL),
00053     mAllocatedMemory(false),
00054     mConstantDataForTesting(0),
00055     mUseConstantDataForTesting(false)
00056 {
00057     // Make sure there's only one instance - enforces correct serialization
00058     assert(mpInstance == NULL);
00059 }
00060 
00061 
00062 template<unsigned DIM>
00063 CellwiseData<DIM>::~CellwiseData()
00064 {
00065 }
00066 
00067 
00068 template<unsigned DIM>
00069 void CellwiseData<DIM>::Destroy()
00070 {
00071     if (mpInstance)
00072     {
00073         delete mpInstance;
00074         mpInstance = NULL;
00075     }
00076 }
00077 
00078 template<unsigned DIM>
00079 double CellwiseData<DIM>::GetValue(TissueCell* pCell, unsigned variableNumber)
00080 {
00081     // To test a cell and cell cycle models without a tissue
00082     if (mUseConstantDataForTesting)
00083     {
00084         return mConstantDataForTesting[variableNumber];
00085     }
00086 
00087     assert(IsSetUp());
00088     assert(mpTissue!=NULL);
00089     assert(mAllocatedMemory);
00090     assert(variableNumber < mNumberOfVariables);
00091     assert(pCell!=NULL);
00092 
00093     unsigned node_index = mpTissue->GetLocationIndexUsingCell(pCell);
00094     unsigned vector_index = node_index*mNumberOfVariables + variableNumber;
00095     return mData[vector_index];
00096 }
00097 
00098 template<unsigned DIM>
00099 void CellwiseData<DIM>::SetValue(double value, Node<DIM>* pNode, unsigned variableNumber)
00100 {
00101     assert(IsSetUp());
00102     assert(variableNumber < mNumberOfVariables);
00103     unsigned vector_index = pNode->GetIndex()*mNumberOfVariables + variableNumber;
00104     mData[vector_index] = value;
00105 }
00106 
00107 template<unsigned DIM>
00108 void CellwiseData<DIM>::SetTissue(MeshBasedTissue<DIM>& rTissue)
00109 {
00110     if (mAllocatedMemory == false)
00111     {
00112         EXCEPTION("SetTissue must be called after SetNumNodesAndVars()");
00113     }
00114 
00115     mpTissue=&rTissue;
00116 }
00117 
00118 template<unsigned DIM>
00119 MeshBasedTissue<DIM>& CellwiseData<DIM>::rGetTissue()
00120 {
00121     return *mpTissue;
00122 }
00123 
00124 
00125 template<unsigned DIM>
00126 void CellwiseData<DIM>::SetNumNodesAndVars(unsigned numNodes, unsigned numberOfVariables)
00127 {
00128     if (mpTissue!=NULL)
00129     {
00130         EXCEPTION("SetNumNodesAndVars() must be called before setting the Tissue (and after a Destroy)");
00131     }
00132 
00133     assert(numberOfVariables>0);
00134     assert(mAllocatedMemory==false);
00135 
00136     mNumberOfVariables = numberOfVariables;
00137     mData.clear();
00138     mData.resize(numNodes * mNumberOfVariables, 0.0);
00139 
00140     mAllocatedMemory = true;
00141 }
00142 
00143 template<unsigned DIM>
00144 bool CellwiseData<DIM>::IsSetUp()
00145 {
00146     return ((mAllocatedMemory) && (mpInstance!=NULL) && (mpTissue!=NULL));
00147 }
00148 
00149 template<unsigned DIM>
00150 void CellwiseData<DIM>::ReallocateMemory()
00151 {
00152     assert(mAllocatedMemory==true);
00153     assert(mpTissue!=NULL);
00154 
00155     unsigned num_nodes = mpTissue->rGetMesh().GetNumNodes();
00156     if (mData.size() != num_nodes*mNumberOfVariables)
00157     {
00158         mData.clear();
00159         mData.resize(num_nodes * mNumberOfVariables, 0.0);
00160     }
00161 }
00162 
00163 template<unsigned DIM>
00164 void CellwiseData<DIM>::SetConstantDataForTesting(std::vector<double> values)
00165 {
00166     mConstantDataForTesting = values;
00167     mUseConstantDataForTesting = true;
00168 }
00169 
00170 
00172 // Explicit instantiation
00174 
00175 template class CellwiseData<1>;
00176 template class CellwiseData<2>;
00177 template class CellwiseData<3>;

Generated on Tue Aug 4 16:10:21 2009 for Chaste by  doxygen 1.5.5