Chaste  Release::2018.1
AbstractCentreBasedCellPopulation.cpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 "AbstractCentreBasedCellPopulation.hpp"
37 #include "RandomDirectionCentreBasedDivisionRule.hpp"
38 #include "RandomNumberGenerator.hpp"
39 #include "StepSizeException.hpp"
40 #include "WildTypeCellMutationState.hpp"
41 
42 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
44  std::vector<CellPtr>& rCells,
45  const std::vector<unsigned> locationIndices)
46  : AbstractOffLatticeCellPopulation<ELEMENT_DIM, SPACE_DIM>(rMesh, rCells, locationIndices),
47  mMeinekeDivisionSeparation(0.3) // educated guess
48 {
49  // If no location indices are specified, associate with nodes from the mesh.
50  std::list<CellPtr>::iterator it = this->mCells.begin();
52 
53  for (unsigned i=0; it != this->mCells.end(); ++it, ++i, ++node_iter)
54  {
55  unsigned index = locationIndices.empty() ? node_iter->GetIndex() : locationIndices[i]; // assume that the ordering matches
57  }
58 
60 }
61 
62 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
64  : AbstractOffLatticeCellPopulation<ELEMENT_DIM, SPACE_DIM>(rMesh),
65  mMeinekeDivisionSeparation(0.3) // educated guess
66 {
67 }
68 
69 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
71 {
72  return GetNodeCorrespondingToCell(pCell)->rGetLocation();
73 }
74 
75 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
77 {
78  unsigned index = this->GetLocationIndexUsingCell(pCell);
79  return this->GetNode(index);
80 }
81 
82 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
84  unsigned pdeNodeIndex,
85  std::string& rVariableName,
86  bool dirichletBoundaryConditionApplies,
87  double dirichletBoundaryValue)
88 {
89  CellPtr p_cell = this->GetCellUsingLocationIndex(pdeNodeIndex);
90  double value = p_cell->GetCellData()->GetItem(rVariableName);
91 
92  return value;
93 }
94 
95 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
96 CellPtr AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>::AddCell(CellPtr pNewCell, CellPtr pParentCell)
97 {
98  // Calculate the locations of the two daughter cells
99  std::pair<c_vector<double, SPACE_DIM>, c_vector<double, SPACE_DIM> > positions = mpCentreBasedDivisionRule->CalculateCellDivisionVector(pParentCell, *this);
100 
101  c_vector<double, SPACE_DIM> parent_position = positions.first;
102  c_vector<double, SPACE_DIM> daughter_position = positions.second;
103 
104  // Set the parent cell to use this location
105  ChastePoint<SPACE_DIM> parent_point(parent_position);
106  unsigned node_index = this->GetLocationIndexUsingCell(pParentCell);
107  this->SetNode(node_index, parent_point);
108 
109  // Create a new node
110  Node<SPACE_DIM>* p_new_node = new Node<SPACE_DIM>(this->GetNumNodes(), daughter_position, false); // never on boundary
111 
112  // Clear the applied force on the new node, in case velocity is ouptut on the same timestep as this cell's division
113  p_new_node->ClearAppliedForce();
114 
115  // Copy any node attributes from the parent node
116  if (this->GetNode(node_index)->HasNodeAttributes())
117  {
118  p_new_node->rGetNodeAttributes() = this->GetNode(node_index)->rGetNodeAttributes();
119  }
120 
121  unsigned new_node_index = this->AddNode(p_new_node); // use copy constructor so it doesn't matter that new_node goes out of scope
122 
123  // Update cells vector
124  this->mCells.push_back(pNewCell);
125 
126  // Update mappings between cells and location indices
127  this->SetCellUsingLocationIndex(new_node_index, pNewCell);
128  this->mCellLocationMap[pNewCell.get()] = new_node_index;
129 
130  return pNewCell;
131 }
132 
133 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
134 std::pair<CellPtr,CellPtr> AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>::CreateCellPair(CellPtr pCell1, CellPtr pCell2)
135 {
136  assert(pCell1);
137  assert(pCell2);
138 
139  std::pair<CellPtr,CellPtr> cell_pair;
140 
141  if (pCell1->GetCellId() < pCell2->GetCellId())
142  {
143  cell_pair.first = pCell1;
144  cell_pair.second = pCell2;
145  }
146  else
147  {
148  cell_pair.first = pCell2;
149  cell_pair.second = pCell1;
150  }
151  return cell_pair;
152 }
153 
154 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
155 bool AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>::IsMarkedSpring(const std::pair<CellPtr,CellPtr>& rCellPair)
156 {
157  // the pair should be ordered like this (CreateCellPair will ensure this)
158  assert(rCellPair.first->GetCellId() < rCellPair.second->GetCellId());
159 
160  return mMarkedSprings.find(rCellPair) != mMarkedSprings.end();
161 }
162 
163 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
165 {
166  // the pair should be ordered like this (CreateCellPair will ensure this)
167  assert(rCellPair.first->GetCellId() < rCellPair.second->GetCellId());
168 
169  mMarkedSprings.insert(rCellPair);
170 }
171 
172 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
174 {
175  // the pair should be ordered like this (CreateCellPair will ensure this)
176  assert(rCellPair.first->GetCellId() < rCellPair.second->GetCellId());
177 
178  mMarkedSprings.erase(rCellPair);
179 }
180 
181 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
183 {
184  return GetNodeCorrespondingToCell(pCell)->IsDeleted();
185 }
186 
187 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
189 {
190  unsigned node_index = this->GetLocationIndexUsingCell(pCell);
191  return this->GetNeighbouringNodeIndices(node_index);
192 }
193 
194 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
195 void AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>::CheckForStepSizeException(unsigned nodeIndex, c_vector<double,SPACE_DIM>& rDisplacement, double dt)
196 {
197  double length = norm_2(rDisplacement);
198 
199  if ((length > this->mAbsoluteMovementThreshold) && (!this->IsGhostNode(nodeIndex)) && (!this->IsParticle(nodeIndex)))
200  {
201  std::ostringstream message;
202  message << "Cells are moving by " << length;
203  message << ", which is more than the AbsoluteMovementThreshold: use a smaller timestep to avoid this exception.";
204 
205  // Suggest a net time step that will give a movement smaller than the movement threshold
206  double new_step = 0.95*dt*(this->mAbsoluteMovementThreshold/length);
207 
208  throw StepSizeException(new_step, message.str(), true); // terminate
209  }
210 }
211 
212 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
214 {
215  if (this->IsGhostNode(nodeIndex) || this->IsParticle(nodeIndex))
216  {
217  return this->GetDampingConstantNormal();
218  }
219  else
220  {
221  CellPtr p_cell = this->GetCellUsingLocationIndex(nodeIndex);
222  if (p_cell->GetMutationState()->IsType<WildTypeCellMutationState>())
223  {
224  return this->GetDampingConstantNormal();
225  }
226  else
227  {
228  return this->GetDampingConstantMutant();
229  }
230  }
231 }
232 
233 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
235 {
236  return false;
237 }
238 
239 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
241 {
242  return false;
243 }
244 
245 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
247 {
248  return mMeinekeDivisionSeparation;
249 }
250 
251 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
253 {
254  assert(divisionSeparation <= 1.0);
255  assert(divisionSeparation >= 0.0);
256  mMeinekeDivisionSeparation = divisionSeparation;
257 }
258 
259 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
261 {
262  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = this->rGetMesh().GetNodeIteratorBegin();
263  node_iter != this->rGetMesh().GetNodeIteratorEnd();
264  ++node_iter)
265  {
266  for (typename std::vector<boost::shared_ptr<AbstractCellWriter<ELEMENT_DIM, SPACE_DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
267  cell_writer_iter != this->mCellWriters.end();
268  ++cell_writer_iter)
269  {
270  CellPtr cell_from_node = this->GetCellUsingLocationIndex(node_iter->GetIndex());
271  this->AcceptCellWriter(*cell_writer_iter, cell_from_node);
272  }
273  }
274 }
275 
276 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
277 boost::shared_ptr<AbstractCentreBasedDivisionRule<ELEMENT_DIM, SPACE_DIM> > AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>::GetCentreBasedDivisionRule()
278 {
279  return mpCentreBasedDivisionRule;
280 }
281 
282 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
284 {
285  mpCentreBasedDivisionRule = pCentreBasedDivisionRule;
286 }
287 
288 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
290 {
291  *rParamsFile << "\t\t<MeinekeDivisionSeparation>" << mMeinekeDivisionSeparation << "</MeinekeDivisionSeparation>\n";
292 
293  // Add the division rule parameters
294  *rParamsFile << "\t\t<CentreBasedDivisionRule>\n";
295  mpCentreBasedDivisionRule->OutputCellCentreBasedDivisionRuleInfo(rParamsFile);
296  *rParamsFile << "\t\t</CentreBasedDivisionRule>\n";
297 
298  // Call method on direct parent class
300 }
301 
302 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
304 {
305  return 1.0/120.0;
306 }
307 
308 // Explicit instantiation
boost::shared_ptr< AbstractCentreBasedDivisionRule< ELEMENT_DIM, SPACE_DIM > > mpCentreBasedDivisionRule
void UnmarkSpring(std::pair< CellPtr, CellPtr > &rCellPair)
Definition: Node.hpp:58
virtual double GetCellDataItemAtPdeNode(unsigned pdeNodeIndex, std::string &rVariableName, bool dirichletBoundaryConditionApplies=false, double dirichletBoundaryValue=0.0)
std::vector< double > & rGetNodeAttributes()
Definition: Node.cpp:178
std::pair< CellPtr, CellPtr > CreateCellPair(CellPtr pCell1, CellPtr pCell2)
void ClearAppliedForce()
Definition: Node.cpp:216
virtual void OutputCellPopulationParameters(out_stream &rParamsFile)
virtual std::set< unsigned > GetNeighbouringLocationIndices(CellPtr pCell)
AbstractCentreBasedCellPopulation(AbstractMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
Node< SPACE_DIM > * GetNodeCorrespondingToCell(CellPtr pCell)
boost::shared_ptr< AbstractCentreBasedDivisionRule< ELEMENT_DIM, SPACE_DIM > > GetCentreBasedDivisionRule()
CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell=CellPtr())
NodeIterator GetNodeIteratorBegin(bool skipDeletedNodes=true)
void MarkSpring(std::pair< CellPtr, CellPtr > &rCellPair)
virtual void OutputCellPopulationParameters(out_stream &rParamsFile)
bool IsMarkedSpring(const std::pair< CellPtr, CellPtr > &rCellPair)
virtual void AddCellUsingLocationIndex(unsigned index, CellPtr pCell)
void SetMeinekeDivisionSeparation(double divisionSeparation)
virtual double GetDampingConstant(unsigned nodeIndex)
c_vector< double, SPACE_DIM > GetLocationOfCellCentre(CellPtr pCell)
virtual void CheckForStepSizeException(unsigned nodeIndex, c_vector< double, SPACE_DIM > &rDisplacement, double dt)
void SetCentreBasedDivisionRule(boost::shared_ptr< AbstractCentreBasedDivisionRule< ELEMENT_DIM, SPACE_DIM > > pCentreBasedDivisionRule)