CellwiseData.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
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 
00029 #include "CellwiseData.hpp"
00030 #include "MeshBasedCellPopulationWithGhostNodes.hpp"
00031 
00032 template<unsigned DIM>
00033 CellwiseData<DIM>* CellwiseData<DIM>::mpInstance = NULL;
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 template<unsigned DIM>
00046 CellwiseData<DIM>::CellwiseData()
00047     : mpCellPopulation(NULL),
00048       mAllocatedMemory(false),
00049       mNumberOfVariables(UNSIGNED_UNSET),
00050       mUseConstantDataForTesting(false)
00051 {
00052     // Make sure there's only one instance - enforces correct serialization
00053     assert(mpInstance == NULL);
00054 }
00055 
00056 template<unsigned DIM>
00057 CellwiseData<DIM>::~CellwiseData()
00058 {
00059 }
00060 
00061 template<unsigned DIM>
00062 void CellwiseData<DIM>::Destroy()
00063 {
00064     if (mpInstance)
00065     {
00066         delete mpInstance;
00067         mpInstance = NULL;
00068     }
00069 }
00070 
00071 template<unsigned DIM>
00072 double CellwiseData<DIM>::GetValue(CellPtr pCell, unsigned variableNumber)
00073 {
00074     if (variableNumber >= mNumberOfVariables)
00075     {
00076         EXCEPTION("Request for variable above mNumberOfVariables. Call SetNumCellsAndVars() to increase it.");
00077     }
00078 
00079     // To test a cell and cell-cycle models without a cell population
00080     if (mUseConstantDataForTesting)
00081     {
00082         return mConstantDataForTesting[variableNumber];
00083     }
00084 
00085     assert(IsSetUp());
00086     assert(mpCellPopulation != NULL);
00087     assert(mAllocatedMemory);
00088 
00089     unsigned location_index = mpCellPopulation->GetLocationIndexUsingCell(pCell);
00090     unsigned vector_index = location_index*mNumberOfVariables + variableNumber;
00091     return mData[vector_index];
00092 }
00093 
00094 template<unsigned DIM>
00095 void CellwiseData<DIM>::SetValue(double value, unsigned locationIndex, unsigned variableNumber)
00096 {
00097     assert(IsSetUp());
00098     if (variableNumber >= mNumberOfVariables)
00099     {
00100         EXCEPTION("Request for variable above mNumberOfVariables. Call SetNumCellsAndVars() to increase it.");
00101     }
00102 
00103     unsigned vector_index = locationIndex*mNumberOfVariables + variableNumber;
00104     assert(vector_index < mData.size());
00105     mData[vector_index] = value;
00106 }
00107 
00108 template<unsigned DIM>
00109 void CellwiseData<DIM>::SetCellPopulation(AbstractCellPopulation<DIM>* pCellPopulation)
00110 {
00111     if (dynamic_cast<MeshBasedCellPopulationWithGhostNodes<DIM>*>(pCellPopulation))
00112     {
00113         EXCEPTION("CellwiseData does not work with ghost nodes.");
00114     }
00115 
00116     if (mAllocatedMemory == false)
00117     {
00118         EXCEPTION("SetCellPopulation must be called after SetNumCellsAndVars()");
00119     }
00120 
00121     mpCellPopulation = pCellPopulation;
00122 }
00123 
00124 template<unsigned DIM>
00125 AbstractCellPopulation<DIM>& CellwiseData<DIM>::rGetCellPopulation()
00126 {
00127     return *mpCellPopulation;
00128 }
00129 
00130 template<unsigned DIM>
00131 void CellwiseData<DIM>::SetNumCellsAndVars(unsigned numCells, unsigned numberOfVariables)
00132 {
00133     if (mpCellPopulation!=NULL)
00134     {
00135         EXCEPTION("SetNumCellsAndVars() must be called before setting the CellPopulation (and after a Destroy)");
00136     }
00137 
00138     assert(numberOfVariables > 0);
00139     assert(mAllocatedMemory == false);
00140 
00141     mNumberOfVariables = numberOfVariables;
00142     mData.clear();
00143     mData.resize(numCells * mNumberOfVariables, 0.0);
00144 
00145     mAllocatedMemory = true;
00146 }
00147 
00148 template<unsigned DIM>
00149 bool CellwiseData<DIM>::IsSetUp()
00150 {
00151     return ((mAllocatedMemory) && (mpInstance!=NULL) && (mpCellPopulation!=NULL));
00152 }
00153 
00154 template<unsigned DIM>
00155 void CellwiseData<DIM>::ReallocateMemory()
00156 {
00157     assert(mAllocatedMemory==true);
00158     assert(mpCellPopulation!=NULL);
00159 
00160     unsigned num_cells = mpCellPopulation->GetNumRealCells();
00161 
00162     if (mData.size() != num_cells*mNumberOfVariables)
00163     {
00164         mData.clear();
00165         mData.resize(num_cells*mNumberOfVariables, 0.0);
00166     }
00167 }
00168 
00169 template<unsigned DIM>
00170 void CellwiseData<DIM>::SetConstantDataForTesting(std::vector<double>& rValues)
00171 {
00172     mConstantDataForTesting = rValues;
00173     mUseConstantDataForTesting = true;
00174     mNumberOfVariables = 1;
00175 }
00176 
00177 template<unsigned DIM>
00178 unsigned CellwiseData<DIM>::GetNumVariables()
00179 {
00180     return mNumberOfVariables;
00181 }
00182 
00184 // Explicit instantiation
00186 
00187 template class CellwiseData<1>;
00188 template class CellwiseData<2>;
00189 template class CellwiseData<3>;
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3