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

Generated on Mon Apr 18 11:35:27 2011 for Chaste by  doxygen 1.5.5