Chaste  Release::2017.1
ExclusionCaBasedDivisionRule.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 {
42  bool is_room = false;
43 
44  // Get node index corresponding to this cell
45  unsigned node_index = rCellPopulation.GetLocationIndexUsingCell(pParentCell);
46 
47  // Get the set of neighbouring node indices
48  std::set<unsigned> neighbouring_node_indices = static_cast<PottsMesh<SPACE_DIM>*>(&(rCellPopulation.rGetMesh()))->GetMooreNeighbouringNodeIndices(node_index);
49 
50  // Iterate through the neighbours to see if there are any available sites
51  for (std::set<unsigned>::iterator neighbour_iter = neighbouring_node_indices.begin();
52  neighbour_iter != neighbouring_node_indices.end();
53  ++neighbour_iter)
54  {
55  if (rCellPopulation.IsSiteAvailable(*neighbour_iter, pParentCell))
56  {
57  is_room = true;
58  break;
59  }
60  }
61 
62  return is_room;
63 }
64 
65 template<unsigned SPACE_DIM>
66 unsigned ExclusionCaBasedDivisionRule<SPACE_DIM>::CalculateDaughterNodeIndex(CellPtr pNewCell, CellPtr pParentCell, CaBasedCellPopulation<SPACE_DIM>& rCellPopulation)
67 {
68  if (!IsRoomToDivide(pParentCell,rCellPopulation))
69  {
70  EXCEPTION("Trying to divide when there is no room to divide, check your division rule");
71  }
72 
73  // Get node index corresponding to the parent cell
74  unsigned parent_node_index = rCellPopulation.GetLocationIndexUsingCell(pParentCell);
75 
76  PottsMesh<SPACE_DIM>* static_cast_mesh = static_cast<PottsMesh<SPACE_DIM>*>(&(rCellPopulation.rGetMesh()));
77 
78  // Get the set of neighbouring node indices
79  std::set<unsigned> neighbouring_node_indices = static_cast_mesh->GetMooreNeighbouringNodeIndices(parent_node_index);
80  unsigned num_neighbours = neighbouring_node_indices.size();
81 
82  // Each node must have at least one neighbour
83  assert(!neighbouring_node_indices.empty());
84 
85  std::vector<double> neighbouring_node_propensities;
86  std::vector<unsigned> neighbouring_node_indices_vector;
87 
88  double total_propensity = 0.0;
89 
90  // Select neighbour at random
91  for (std::set<unsigned>::iterator neighbour_iter = neighbouring_node_indices.begin();
92  neighbour_iter != neighbouring_node_indices.end();
93  ++neighbour_iter)
94  {
95  neighbouring_node_indices_vector.push_back(*neighbour_iter);
96 
97  double propensity_dividing_into_neighbour = rCellPopulation.EvaluateDivisionPropensity(parent_node_index,*neighbour_iter,pParentCell);
98 
99  if (!(rCellPopulation.IsSiteAvailable(*neighbour_iter, pParentCell)))
100  {
101  propensity_dividing_into_neighbour = 0.0;
102  }
103 
104  neighbouring_node_propensities.push_back(propensity_dividing_into_neighbour);
105  total_propensity += propensity_dividing_into_neighbour;
106  }
107  assert(total_propensity > 0); // If this trips the cell can't divide, so we need to include this in the IsSiteAvailable() method
108 
109  for (unsigned i=0; i<num_neighbours; i++)
110  {
111  neighbouring_node_propensities[i] /= total_propensity;
112  }
113 
114  // Sample random number to specify which move to make
116  double random_number = p_gen->ranf();
117 
118  double total_probability = 0.0;
119  unsigned daughter_node_index = UNSIGNED_UNSET;
120 
121  unsigned counter;
122  for (counter=0; counter < num_neighbours; counter++)
123  {
124  total_probability += neighbouring_node_propensities[counter];
125  if (total_probability >= random_number)
126  {
127  // Divide the parent cell to this neighbour location
128  daughter_node_index = neighbouring_node_indices_vector[counter];
129  break;
130  }
131  }
132  // This loop should always break as sum(neighbouring_node_propensities) = 1
133 
134  assert(daughter_node_index != UNSIGNED_UNSET);
135  assert(daughter_node_index < static_cast_mesh->GetNumNodes());
136 
137  return daughter_node_index;
138 }
139 
140 // Explicit instantiation
141 template class ExclusionCaBasedDivisionRule<1>;
142 template class ExclusionCaBasedDivisionRule<2>;
143 template class ExclusionCaBasedDivisionRule<3>;
144 
145 // 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:233
#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)