Chaste  Build::
LatticeBasedMigrationRule.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 "GeometryTools.hpp"
37 #include "LatticeBasedMigrationRule.hpp"
38 #include "RandomNumberGenerator.hpp"
39 
40 template<unsigned DIM>
42  : AbstractMigrationRule<DIM>(),
43  mMovementProbability(0.01)
44 {
45 
46 }
47 
48 template <unsigned DIM>
49 boost::shared_ptr<LatticeBasedMigrationRule<DIM> > LatticeBasedMigrationRule<DIM>::Create()
50 {
51  MAKE_PTR(LatticeBasedMigrationRule<DIM>, pSelf);
52  return pSelf;
53 }
54 
55 template<unsigned DIM>
57 {
58 
59 }
60 
61 template<unsigned DIM>
63  std::vector<unsigned> neighbourIndices, unsigned gridIndex)
64 {
65  std::vector<double> probability_of_moving(neighbourIndices.size(), 0.0);
66  for(unsigned idx=0; idx<neighbourIndices.size(); idx++)
67  {
68  // Make sure that tip cell does not try to move into a location already occupied by the vessel that it comes from
69  DimensionalChastePoint<DIM> neighbour_location = this->mpGrid->GetLocationOf1dIndex(neighbourIndices[idx]);
70 
71  bool already_attached = false;
72  for (unsigned seg_index = 0; seg_index < pNode->GetNumberOfSegments(); seg_index++)
73  {
74  if(pNode->GetSegment(seg_index)->GetOppositeNode(pNode)->IsCoincident(neighbour_location))
75  {
76  already_attached = true;
77  break;
78  }
79  }
80 
81  if(already_attached)
82  {
83  continue;
84  }
85 
86  // Simple rule, equal probability for all directions
87  probability_of_moving[idx] = mMovementProbability * SimulationTime::Instance()->GetTimeStep();
88  }
89  return probability_of_moving;
90 }
91 
92 template<unsigned DIM>
93 int LatticeBasedMigrationRule<DIM>::GetNeighbourMovementIndex(std::vector<double> movementProbabilities,
94  std::vector<unsigned> neighbourIndices)
95 {
96  int location_index = -1;
97 
98  // Check that the cumulative movement probability is less than one, otherwise our time-step is too large
99  std::vector<double> cumulativeProbabilityVector(movementProbabilities.size());
100  std::partial_sum(movementProbabilities.begin(), movementProbabilities.end(), cumulativeProbabilityVector.begin());
101 
102  if (cumulativeProbabilityVector.back() > 1.0)
103  {
104  EXCEPTION("Cumulative probability of tip cell moving is greater than one");
105  }
106 
107  // Use roulette-wheel style selection to select which location the tip will move into
108  double cumulativeProbability = cumulativeProbabilityVector.back();
109  double random_number = RandomNumberGenerator::Instance()->ranf();
110 
111  // If we move, choose a node to go to
112  if(random_number < cumulativeProbability)
113  {
114  for (unsigned ind = 0; ind < cumulativeProbabilityVector.size(); ind++)
115  {
116  if (random_number <= cumulativeProbabilityVector[ind])
117  {
118  location_index = neighbourIndices[ind];
119  break;
120  }
121  }
122  }
123  return location_index;
124 }
125 
126 template<unsigned DIM>
128 {
129  mMovementProbability = movementProbability;
130 }
131 
132 template<unsigned DIM>
133 std::vector<int> LatticeBasedMigrationRule<DIM>::GetIndices(const std::vector<boost::shared_ptr<VesselNode<DIM> > >& rNodes)
134 {
135  if(!this->mpGrid)
136  {
137  EXCEPTION("A regular grid is required for this type of migration rule.");
138  }
139 
140  if(!this->mpVesselNetwork)
141  {
142  EXCEPTION("A vessel network is required for this type of migration rule.");
143  }
144 
145  // Set up the output indices vector
146  std::vector<int> indices(rNodes.size(), -1);
147 
148  // Get the point-node map from the regular grid
149  std::vector<std::vector<boost::shared_ptr<VesselNode<DIM> > > > point_node_map = this->mpGrid->GetPointNodeMap();
150 
151  // Get the neighbour data from the regular grid
152  std::vector<std::vector<unsigned> > neighbour_indices = this->mpGrid->GetNeighbourData();
153 
154  // Loop over all nodes, if they can move set the index
155  for(unsigned idx = 0; idx < rNodes.size(); idx++)
156  {
157  // Get the grid index of the node
158  unsigned grid_index = this->mpGrid->GetNearestGridIndex(rNodes[idx]->rGetLocation());
159 
160  // Get the probability of moving into each of the neighbour sites
161  std::vector<double> probability_of_moving = GetNeighbourMovementProbabilities(rNodes[idx], neighbour_indices[grid_index], grid_index);
162 
163  // Get the index of the neighbour to move into
164  double sum = std::fabs(std::accumulate(probability_of_moving.begin(), probability_of_moving.end(), 0.0));
165  if(sum > 0.0)
166  {
167  indices[idx] = GetNeighbourMovementIndex(probability_of_moving, neighbour_indices[grid_index]);
168  }
169  }
170  return indices;
171 }
172 
173 // Explicit instantiation
174 template class LatticeBasedMigrationRule<2> ;
175 template class LatticeBasedMigrationRule<3> ;
virtual std::vector< int > GetIndices(const std::vector< boost::shared_ptr< VesselNode< DIM > > > &rNodes)
virtual int GetNeighbourMovementIndex(std::vector< double > movementProbabilities, std::vector< unsigned > neighbourIndices)
boost::shared_ptr< RegularGrid< DIM > > mpGrid
static boost::shared_ptr< LatticeBasedMigrationRule< DIM > > Create()
virtual std::vector< double > GetNeighbourMovementProbabilities(boost::shared_ptr< VesselNode< DIM > > pNode, std::vector< unsigned > neighbourIndices, unsigned gridIndex)
void SetMovementProbability(double movementProbability)
boost::shared_ptr< VesselNetwork< DIM > > mpVesselNetwork