Chaste  Release::3.4
ExclusionCaBasedDivisionRule.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "ExclusionCaBasedDivisionRule.hpp"
37 #include "RandomNumberGenerator.hpp"
38 
39 template<unsigned SPACE_DIM>
41  CellPtr pParentCell,
42  CaBasedCellPopulation<SPACE_DIM>& rCellPopulation)
43  {
44  bool is_room = false;
45 
46  //Get node index corresponding to this cell
47  unsigned node_index = rCellPopulation.GetLocationIndexUsingCell(pParentCell);
48 
49  // Get the set of neighbouring node indices
50  std::set<unsigned> neighbouring_node_indices = static_cast<PottsMesh<SPACE_DIM> *>(&(rCellPopulation.rGetMesh()))->GetMooreNeighbouringNodeIndices(node_index);
51 
52  // Iterate through the neighbours to see if there are any available sites
53  for (std::set<unsigned>::iterator neighbour_iter = neighbouring_node_indices.begin();
54  neighbour_iter != neighbouring_node_indices.end();
55  ++neighbour_iter)
56  {
57  if (rCellPopulation.IsSiteAvailable(*neighbour_iter, pParentCell))
58  {
59  is_room = true;
60  break;
61  }
62  }
63 
64  return is_room;
65  }
66 
67 template<unsigned SPACE_DIM>
69  CellPtr pNewCell,
70  CellPtr pParentCell,
71  CaBasedCellPopulation<SPACE_DIM>& rCellPopulation)
72  {
73  // Check there is room to divide This will always pass as
74  // IsRoomToDivide is called before CalculateDaughterNodeIndex in the simulator.solve() method
75  if (!IsRoomToDivide(pParentCell,rCellPopulation))
76  {
77  EXCEPTION("Trying to divide when there is no room to divide, check your division rule");
78  }
79 
80  // Get node index corresponding to the parent cell
81  unsigned parent_node_index = rCellPopulation.GetLocationIndexUsingCell(pParentCell);
82 
83  PottsMesh<SPACE_DIM>* static_cast_mesh = static_cast<PottsMesh<SPACE_DIM>*>(&(rCellPopulation.rGetMesh()));
84 
85  // Get the set of neighbouring node indices
86  std::set<unsigned> neighbouring_node_indices = static_cast_mesh->GetMooreNeighbouringNodeIndices(parent_node_index);
87  unsigned num_neighbours = neighbouring_node_indices.size();
88 
89  // Each node must have at least one neighbour
90  assert(!neighbouring_node_indices.empty());
91 
92  std::vector<double> neighbouring_node_propensities;
93  std::vector<unsigned> neighbouring_node_indices_vector;
94 
95  double total_propensity = 0.0;
96 
97  // Select Neighbour at random
98  for (std::set<unsigned>::iterator neighbour_iter = neighbouring_node_indices.begin();
99  neighbour_iter != neighbouring_node_indices.end();
100  ++neighbour_iter)
101  {
102  neighbouring_node_indices_vector.push_back(*neighbour_iter);
103 
104  double propensity_dividing_into_neighbour = rCellPopulation.EvaluateDivisionPropensity(parent_node_index,*neighbour_iter,pParentCell);
105 
106  if (!(rCellPopulation.IsSiteAvailable(*neighbour_iter, pParentCell)))
107  {
108  propensity_dividing_into_neighbour = 0.0;
109  }
110 
111  neighbouring_node_propensities.push_back(propensity_dividing_into_neighbour);
112  total_propensity += propensity_dividing_into_neighbour;
113  }
114  assert(total_propensity>0); // if this trips the cell can't divided so need to include this in the IsSiteAvailable method
115 
116  for (unsigned i=0; i<num_neighbours; i++)
117  {
118  neighbouring_node_propensities[i] /= total_propensity;
119  }
120 
121  // Sample random number to specify which move to make
123  double random_number = p_gen->ranf();
124 
125  double total_probability = 0.0;
126  unsigned daughter_node_index = UNSIGNED_UNSET;
127 
128  unsigned counter;
129  for (counter=0; counter < num_neighbours; counter++)
130  {
131  total_probability += neighbouring_node_propensities[counter];
132  if (total_probability >= random_number)
133  {
134  // Divide the parent cell to this neighbour location
135  daughter_node_index = neighbouring_node_indices_vector[counter];
136  break;
137  }
138  }
139  // This loop should always break as sum(neighbouring_node_propensities) = 1
140 
141  assert(daughter_node_index != UNSIGNED_UNSET);
142  assert(daughter_node_index < static_cast_mesh->GetNumNodes());
143 
144  return daughter_node_index;
145 }
146 
147 // Explicit instantiation
148 template class ExclusionCaBasedDivisionRule<1>;
149 template class ExclusionCaBasedDivisionRule<2>;
150 template class ExclusionCaBasedDivisionRule<3>;
151 
152 // Serialization for Boost >= 1.36
virtual double EvaluateDivisionPropensity(unsigned currentNodeIndex, unsigned targetNodeIndex, CellPtr pCell)
PottsMesh< DIM > & rGetMesh()
unsigned GetLocationIndexUsingCell(CellPtr pCell)
#define EXCEPTION(message)
Definition: Exception.hpp:143
virtual bool IsSiteAvailable(unsigned index, CellPtr pCell)
std::set< unsigned > GetMooreNeighbouringNodeIndices(unsigned nodeIndex)
Definition: PottsMesh.cpp:234
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
const unsigned UNSIGNED_UNSET
Definition: Exception.hpp:52
static RandomNumberGenerator * Instance()
virtual unsigned CalculateDaughterNodeIndex(CellPtr pNewCell, CellPtr pParentCell, CaBasedCellPopulation< SPACE_DIM > &rCellPopulation)
virtual bool IsRoomToDivide(CellPtr pParentCell, CaBasedCellPopulation< SPACE_DIM > &rCellPopulation)