Chaste Release::3.1
CryptSimulationBoundaryCondition.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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 "CryptSimulationBoundaryCondition.hpp"
00037 #include "WntConcentration.hpp"
00038 #include "AbstractCentreBasedCellPopulation.hpp"
00039 #include "RandomNumberGenerator.hpp"
00040 
00041 template<unsigned DIM>
00042 CryptSimulationBoundaryCondition<DIM>::CryptSimulationBoundaryCondition(AbstractCellPopulation<DIM>* pCellPopulation)
00043     : AbstractCellPopulationBoundaryCondition<DIM>(pCellPopulation),
00044       mUseJiggledBottomCells(false)
00045 {
00046 }
00047 
00048 template<unsigned DIM>
00049 void CryptSimulationBoundaryCondition<DIM>::ImposeBoundaryCondition(const std::vector< c_vector<double, DIM> >& rOldLocations)
00050 {
00051     // We only allow jiggling of bottom cells in 2D
00052     if (DIM == 1)
00053     {
00054         mUseJiggledBottomCells = false;
00055     }
00056 
00057     // Check whether a WntConcentration singleton has been set up
00058     bool is_wnt_included = WntConcentration<DIM>::Instance()->IsWntSetUp();
00059     if (!is_wnt_included)
00060     {
00061         WntConcentration<DIM>::Destroy();
00062     }
00063 
00064     // We iterate differently depending on whether we are using a centre- or vertex-based model
00065     if (dynamic_cast<AbstractCentreBasedCellPopulation<DIM>*>(this->mpCellPopulation))
00066     {
00067         // Iterate over all nodes associated with real cells to update their positions
00068         for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
00069              cell_iter != this->mpCellPopulation->End();
00070              ++cell_iter)
00071         {
00072             // Get index of node associated with cell
00073             unsigned node_index = this->mpCellPopulation->GetLocationIndexUsingCell(*cell_iter);
00074 
00075             // Get pointer to this node
00076             Node<DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
00077 
00078             if (!is_wnt_included)
00079             {
00080                 /*
00081                  * If WntConcentration is not set up then stem cells must be pinned,
00082                  * so we reset the location of each stem cell.
00083                  */
00084                 if (cell_iter->GetCellProliferativeType() == STEM)
00085                 {
00086                     // Get old node location
00087                     c_vector<double, DIM> old_node_location = rOldLocations[node_index];
00088 
00089                     // Return node to old location
00090                     p_node->rGetModifiableLocation() = old_node_location;
00091                 }
00092             }
00093 
00094             // Any cell that has moved below the bottom of the crypt must be moved back up
00095             if (p_node->rGetLocation()[DIM-1] < 0.0)
00096             {
00097                 p_node->rGetModifiableLocation()[DIM-1] = 0.0;
00098 
00099                 if (mUseJiggledBottomCells)
00100                 {
00101                    /*
00102                     * Here we give the cell a push upwards so that it doesn't
00103                     * get stuck on the bottom of the crypt (as per #422).
00104                     *
00105                     * Note that all stem cells may get moved to the same height, so
00106                     * we use a random perturbation to help ensure we are not simply
00107                     * faced with the same problem at a different height!
00108                     */
00109                     p_node->rGetModifiableLocation()[DIM-1] = 0.05*RandomNumberGenerator::Instance()->ranf();
00110                 }
00111             }
00112             assert(p_node->rGetLocation()[DIM-1] >= 0.0);
00113         }
00114     }
00115     else
00116     {
00117         // Iterate over all nodes to update their positions
00118         for (unsigned node_index=0; node_index<this->mpCellPopulation->GetNumNodes(); node_index++)
00119         {
00120             // Get pointer to this node
00121             Node<DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
00122 
00123             if (!is_wnt_included)
00124             {
00125                 /*
00126                  * If WntConcentration is not set up then stem cells must be pinned,
00127                  * so we reset the location of each node whose height was close to zero.
00128                  */
00129                 double node_height = rOldLocations[node_index][DIM-1];
00130                 if (node_height < DBL_EPSILON)
00131                 {
00132                     // Return node to its old height, but allow it to slide left or right
00133                     p_node->rGetModifiableLocation()[DIM-1] = node_height;
00134                 }
00135             }
00136 
00137             // Any node that has moved below the bottom of the crypt must be moved back up
00138             if (p_node->rGetLocation()[DIM-1] < 0.0)
00139             {
00140                 p_node->rGetModifiableLocation()[DIM-1] = 0.0;
00141 
00142                 if (mUseJiggledBottomCells)
00143                 {
00144                    /*
00145                     * Here we give the node a push upwards so that it doesn't
00146                     * get stuck on the bottom of the crypt.
00147                     */
00148                     p_node->rGetModifiableLocation()[DIM-1] = 0.05*RandomNumberGenerator::Instance()->ranf();
00149                 }
00150             }
00151             assert(p_node->rGetLocation()[DIM-1] >= 0.0);
00152         }
00153     }
00154 }
00155 
00156 template<unsigned DIM>
00157 bool CryptSimulationBoundaryCondition<DIM>::VerifyBoundaryCondition()
00158 {
00159     bool boundary_condition_satisfied = true;
00160 
00161     /*
00162      * Here we verify that the boundary condition is still satisfied by simply
00163      * checking that no cells lies below the y=0 boundary.
00164      */
00165     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
00166          cell_iter != this->mpCellPopulation->End();
00167          ++cell_iter)
00168     {
00169         // Get index of node associated with cell
00170         unsigned node_index = this->mpCellPopulation->GetLocationIndexUsingCell(*cell_iter);
00171 
00172         // Get pointer to this node
00173         Node<DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
00174 
00175         // If this node lies below the y=0 boundary, break and return false
00176         if (p_node->rGetLocation()[DIM-1] < 0.0)
00177         {
00178             boundary_condition_satisfied = false;
00179             break;
00180         }
00181     }
00182 
00183     return boundary_condition_satisfied;
00184 }
00185 
00186 template<unsigned DIM>
00187 void CryptSimulationBoundaryCondition<DIM>::SetUseJiggledBottomCells(bool useJiggledBottomCells)
00188 {
00189     mUseJiggledBottomCells = useJiggledBottomCells;
00190 }
00191 
00192 template<unsigned DIM>
00193 bool CryptSimulationBoundaryCondition<DIM>::GetUseJiggledBottomCells()
00194 {
00195     return mUseJiggledBottomCells;
00196 }
00197 
00198 template<unsigned DIM>
00199 void CryptSimulationBoundaryCondition<DIM>::OutputCellPopulationBoundaryConditionParameters(out_stream& rParamsFile)
00200 {
00201     *rParamsFile << "\t\t<UseJiggledBottomCells>" << mUseJiggledBottomCells << "</UseJiggledBottomCells>\n";
00203     // Call method on direct parent class
00204     AbstractCellPopulationBoundaryCondition<DIM>::OutputCellPopulationBoundaryConditionParameters(rParamsFile);
00205 }
00206 
00208 // Explicit instantiation
00210 
00211 template class CryptSimulationBoundaryCondition<1>;
00212 template class CryptSimulationBoundaryCondition<2>;
00213 template class CryptSimulationBoundaryCondition<3>;
00214 
00215 // Serialization for Boost >= 1.36
00216 #include "SerializationExportWrapperForCpp.hpp"
00217 EXPORT_TEMPLATE_CLASS_SAME_DIMS(CryptSimulationBoundaryCondition)