Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
ExclusionCaBasedDivisionRule.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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
39template<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
65template<unsigned SPACE_DIM>
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
144
145// Serialization for Boost >= 1.36
#define EXCEPTION(message)
const unsigned UNSIGNED_UNSET
Definition Exception.hpp:53
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
unsigned GetLocationIndexUsingCell(CellPtr pCell)
virtual double EvaluateDivisionPropensity(unsigned currentNodeIndex, unsigned targetNodeIndex, CellPtr pCell)
PottsMesh< DIM > & rGetMesh()
virtual bool IsSiteAvailable(unsigned index, CellPtr pCell)
virtual bool IsRoomToDivide(CellPtr pParentCell, CaBasedCellPopulation< SPACE_DIM > &rCellPopulation)
virtual unsigned CalculateDaughterNodeIndex(CellPtr pNewCell, CellPtr pParentCell, CaBasedCellPopulation< SPACE_DIM > &rCellPopulation)
std::set< unsigned > GetMooreNeighbouringNodeIndices(unsigned nodeIndex)
static RandomNumberGenerator * Instance()