Chaste  Build::
CellPopulationMigrationRule.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 "CellPopulationMigrationRule.hpp"
38 #include "RandomNumberGenerator.hpp"
39 
40 template<unsigned DIM>
43  mVolumeFractionMap(),
44  mPointCellMap()
45 {
46 
47 }
48 
49 template <unsigned DIM>
50 boost::shared_ptr<CellPopulationMigrationRule<DIM> > CellPopulationMigrationRule<DIM>::Create()
51 {
52  MAKE_PTR(CellPopulationMigrationRule<DIM>, pSelf);
53  return pSelf;
54 }
55 
56 template<unsigned DIM>
58 {
59 
60 }
61 
62 template<unsigned DIM>
63 void CellPopulationMigrationRule<DIM>::SetVolumeFraction(boost::shared_ptr<AbstractCellMutationState> mutation_state, double volume_fraction)
64 {
65  if(volume_fraction >1.0)
66  {
67  EXCEPTION("Specified volume fractions should not be greater than 1.");
68  }
69 
70  typedef std::map<boost::shared_ptr<AbstractCellMutationState> , double>::iterator it_type; it_type iterator;
71  for(iterator = mVolumeFractionMap.begin(); iterator != mVolumeFractionMap.end(); iterator++)
72  {
73  if (iterator->first->IsSame(mutation_state))
74  {
75  iterator->second = volume_fraction;
76  break;
77  }
78  }
79 
80  // if mutation state does not exist in map yet then add it to the map
81  if (iterator == mVolumeFractionMap.end())
82  {
83  mVolumeFractionMap[mutation_state] = volume_fraction;
84  }
85 }
86 
87 template<unsigned DIM>
88 double CellPopulationMigrationRule<DIM>::GetOccupyingVolumeFraction(boost::shared_ptr<AbstractCellMutationState> mutation_state)
89 {
90  typedef std::map<boost::shared_ptr<AbstractCellMutationState> , double>::iterator it_type; it_type iterator;
91  for(iterator = mVolumeFractionMap.begin(); iterator != mVolumeFractionMap.end(); iterator++)
92  {
93  if (iterator->first->IsSame(mutation_state))
94  {
95  return iterator->second;
96  }
97  }
98 
99  // if a map is not provided or if the prescribed mutation state is not in the map then the
100  // occupying volume fraction is 1.
101  return 1;
102 }
103 
104 template<unsigned DIM>
106  std::vector<unsigned> neighbourIndices, unsigned gridIndex)
107 {
108  std::vector<double> probability_of_moving(neighbourIndices.size(), 0.0);
109 
110  for(unsigned idx=0; idx<neighbourIndices.size(); idx++)
111  {
112  // Check for cell occupancy, if it is occupied don;t go anywhere
113  double total_occupancy = 0.0;
114  for(unsigned jdx=0; jdx<mPointCellMap[idx].size(); jdx++)
115  {
116  total_occupancy += GetOccupyingVolumeFraction(mPointCellMap[idx][jdx]->GetMutationState());
117  }
118 
119  if(total_occupancy>=1.0)
120  {
121  continue;
122  }
123 
124  // Make sure that tip cell does not try to move into a location already occupied by the vessel that it comes from
125  DimensionalChastePoint<DIM> neighbour_location = this->mpGrid->GetLocationOf1dIndex(neighbourIndices[idx]);
126 
127  bool already_attached = false;
128  for (unsigned seg_index = 0; seg_index < pNode->GetNumberOfSegments(); seg_index++)
129  {
130  if(pNode->GetSegment(seg_index)->GetOppositeNode(pNode)->IsCoincident(neighbour_location))
131  {
132  already_attached = true;
133  break;
134  }
135  }
136 
137  // Also ensure that the new location would not try to cross a vessel which is oriented diagonally
138  // TODO: Very slow, bottleneck
139 // if(already_attached or this->mpVesselNetwork->VesselCrossesLineSegment(neighbour_location, pNode->rGetLocation()))
140 // {
141 // continue;
142 // }
143 
144  if(already_attached)
145  {
146  continue;
147  }
148 
149  // Simple rule, equal probability for all directions
150  probability_of_moving[idx] = this->mMovementProbability * SimulationTime::Instance()->GetTimeStep();
151  }
152  return probability_of_moving;
153 }
154 
155 template<unsigned DIM>
156 std::vector<int> CellPopulationMigrationRule<DIM>::GetIndices(const std::vector<boost::shared_ptr<VesselNode<DIM> > >& rNodes)
157 {
158  if(!this->mpGrid)
159  {
160  EXCEPTION("A regular grid is required for this type of migration rule.");
161  }
162 
163  if(!this->mpVesselNetwork)
164  {
165  EXCEPTION("A vessel network is required for this type of migration rule.");
166  }
167 
168  if(!this->mpCellPopulation)
169  {
170  EXCEPTION("A cell population is required for this type of migration rule.");
171  }
172 
173  mPointCellMap = this->mpGrid->GetPointCellMap();
174 
175  // Set up the output indices vector
176  std::vector<int> indices(rNodes.size(), -1);
177 
178  // Get the point-node map from the regular grid
179  std::vector<std::vector<boost::shared_ptr<VesselNode<DIM> > > > point_node_map = this->mpGrid->GetPointNodeMap();
180 
181  // Get the neighbour data from the regular grid
182  std::vector<std::vector<unsigned> > neighbour_indices = this->mpGrid->GetNeighbourData();
183 
184  // Loop over all nodes, if they can move set the index
185  for(unsigned idx = 0; idx < rNodes.size(); idx++)
186  {
187  // Get the grid index of the node
188  unsigned grid_index = this->mpGrid->GetNearestGridIndex(rNodes[idx]->rGetLocation());
189 
190  // Get the probability of moving into each of the neighbour sites
191  std::vector<double> probability_of_moving = GetNeighbourMovementProbabilities(rNodes[idx], neighbour_indices[grid_index], grid_index);
192 
193  // Get the index of the neighbour to move into
194  double sum = std::fabs(std::accumulate(probability_of_moving.begin(), probability_of_moving.end(), 0.0));
195  if(sum > 0.0)
196  {
197  indices[idx] = LatticeBasedMigrationRule<DIM>::GetNeighbourMovementIndex(probability_of_moving, neighbour_indices[grid_index]);
198  }
199  }
200  return indices;
201 }
202 
203 // Explicit instantiation
204 template class CellPopulationMigrationRule<2> ;
205 template class CellPopulationMigrationRule<3> ;
static boost::shared_ptr< CellPopulationMigrationRule< DIM > > Create()
double GetOccupyingVolumeFraction(boost::shared_ptr< AbstractCellMutationState > mutation_state)
virtual int GetNeighbourMovementIndex(std::vector< double > movementProbabilities, std::vector< unsigned > neighbourIndices)
virtual std::vector< double > GetNeighbourMovementProbabilities(boost::shared_ptr< VesselNode< DIM > > pNode, std::vector< unsigned > neighbourIndices, unsigned gridIndex)
boost::shared_ptr< RegularGrid< DIM > > mpGrid
virtual std::vector< int > GetIndices(const std::vector< boost::shared_ptr< VesselNode< DIM > > > &rNodes)
std::map< boost::shared_ptr< AbstractCellMutationState >, double > mVolumeFractionMap
boost::shared_ptr< AbstractCellPopulation< DIM > > mpCellPopulation
void SetVolumeFraction(boost::shared_ptr< AbstractCellMutationState > mutation_state, double volume_fraction)
boost::shared_ptr< VesselNetwork< DIM > > mpVesselNetwork
std::vector< std::vector< CellPtr > > mPointCellMap