Chaste  Release::2017.1
OffLatticeSimulation.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 "OffLatticeSimulation.hpp"
37 
38 #include <boost/make_shared.hpp>
39 
40 #include "CellBasedEventHandler.hpp"
41 #include "ForwardEulerNumericalMethod.hpp"
42 #include "StepSizeException.hpp"
43 
44 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
46  bool deleteCellPopulationInDestructor,
47  bool initialiseCells)
48  : AbstractCellBasedSimulation<ELEMENT_DIM,SPACE_DIM>(rCellPopulation, deleteCellPopulationInDestructor, initialiseCells)
49 {
50  if (!dynamic_cast<AbstractOffLatticeCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation))
51  {
52  EXCEPTION("OffLatticeSimulations require a subclass of AbstractOffLatticeCellPopulation.");
53  }
54 }
55 
56 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
58 {
59  mForceCollection.push_back(pForce);
60 }
61 
62 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
64 {
65  mForceCollection.clear();
66 }
67 
68 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
70 {
71  mBoundaryConditions.push_back(pBoundaryCondition);
72 }
73 
74 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
76 {
77  mBoundaryConditions.clear();
78 }
79 
80 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
82 {
83  mpNumericalMethod = pNumericalMethod;
84 }
85 
86 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
87 const boost::shared_ptr<AbstractNumericalMethod<ELEMENT_DIM, SPACE_DIM> > OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::GetNumericalMethod() const
88 {
89  return mpNumericalMethod;
90 }
91 
92 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
93 const std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM, SPACE_DIM> > >& OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::rGetForceCollection() const
94 {
95  return mForceCollection;
96 }
97 
98 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
100 {
101  CellBasedEventHandler::BeginEvent(CellBasedEventHandler::POSITION);
102 
103  double time_advanced_so_far = 0;
104  double target_time_step = this->mDt;
105  double present_time_step = this->mDt;
106 
107  while (time_advanced_so_far < target_time_step)
108  {
109  // Store the initial node positions (these may be needed when applying boundary conditions)
110  std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > old_node_locations;
111 
112  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = this->mrCellPopulation.rGetMesh().GetNodeIteratorBegin();
113  node_iter != this->mrCellPopulation.rGetMesh().GetNodeIteratorEnd();
114  ++node_iter)
115  {
116  old_node_locations[&(*node_iter)] = (node_iter)->rGetLocation();
117  }
118 
119  // Try to update node positions according to the numerical method
120  try
121  {
122  mpNumericalMethod->UpdateAllNodePositions(present_time_step);
123  ApplyBoundaries(old_node_locations);
124 
125  // Successful time step! Update time_advanced_so_far
126  time_advanced_so_far += present_time_step;
127 
128  // If using adaptive timestep, then increase the present_time_step (by 1% for now)
129  if (mpNumericalMethod->HasAdaptiveTimestep())
130  {
132  double timestep_increase = 0.01;
133  present_time_step = std::min((1+timestep_increase)*present_time_step, target_time_step - time_advanced_so_far);
134  }
135 
136  }
137  catch (StepSizeException& e)
138  {
139  // Detects if a node has travelled too far in a single time step
140  if (mpNumericalMethod->HasAdaptiveTimestep())
141  {
142  // If adaptivity is switched on, revert node locations and choose a suitably smaller time step
143  RevertToOldLocations(old_node_locations);
144  present_time_step = std::min(e.GetSuggestedNewStep(), target_time_step - time_advanced_so_far);
145  }
146  else
147  {
148  // If adaptivity is switched off, terminate with an error
149  EXCEPTION(e.what());
150  }
151  }
152  }
153 
154  CellBasedEventHandler::EndEvent(CellBasedEventHandler::POSITION);
155 }
156 
157 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
158 void OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::RevertToOldLocations(std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > oldNodeLoctions)
159 {
160  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = this->mrCellPopulation.rGetMesh().GetNodeIteratorBegin();
161  node_iter != this->mrCellPopulation.rGetMesh().GetNodeIteratorEnd();
162  ++node_iter)
163  {
164  (node_iter)->rGetModifiableLocation() = oldNodeLoctions[&(*node_iter)];
165  }
166 }
167 
168 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
169 void OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::ApplyBoundaries(std::map<Node<SPACE_DIM>*,c_vector<double, SPACE_DIM> > oldNodeLoctions)
170 {
171  // Apply any boundary conditions
172  for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator bcs_iter = mBoundaryConditions.begin();
173  bcs_iter != mBoundaryConditions.end();
174  ++bcs_iter)
175  {
176  (*bcs_iter)->ImposeBoundaryCondition(oldNodeLoctions);
177  }
178 
179  // Verify that each boundary condition is now satisfied
180  for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator bcs_iter = mBoundaryConditions.begin();
181  bcs_iter != mBoundaryConditions.end();
182  ++bcs_iter)
183  {
184  if (!((*bcs_iter)->VerifyBoundaryCondition()))
185  {
186  EXCEPTION("The cell population boundary conditions are incompatible.");
187  }
188  }
189 }
190 
191 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
193 {
194  if (PetscTools::AmMaster())
195  {
196  for (unsigned i=0; i<this->mForceCollection.size(); i++)
197  {
198  this->mForceCollection[i]->WriteDataToVisualizerSetupFile(this->mpVizSetupFile);
199  }
200 
201  this->mrCellPopulation.WriteDataToVisualizerSetupFile(this->mpVizSetupFile);
202  }
203 }
204 
205 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
207 {
208  // Clear all forces
209  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = this->mrCellPopulation.rGetMesh().GetNodeIteratorBegin();
210  node_iter != this->mrCellPopulation.rGetMesh().GetNodeIteratorEnd();
211  ++node_iter)
212  {
213  node_iter->ClearAppliedForce();
214  }
215 
216  // Use a forward Euler method by default, unless a numerical method has been specified already
217  if (mpNumericalMethod == nullptr)
218  {
219  mpNumericalMethod = boost::make_shared<ForwardEulerNumericalMethod<ELEMENT_DIM, SPACE_DIM> >();
220  }
222  mpNumericalMethod->SetForceCollection(&mForceCollection);
223 }
224 
225 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
227 {
228  // Loop over forces
229  *rParamsFile << "\n\t<Forces>\n";
230  for (typename std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM,SPACE_DIM> > >::iterator iter = mForceCollection.begin();
231  iter != mForceCollection.end();
232  ++iter)
233  {
234  // Output force details
235  (*iter)->OutputForceInfo(rParamsFile);
236  }
237  *rParamsFile << "\t</Forces>\n";
238 
239  // Loop over cell population boundary conditions
240  *rParamsFile << "\n\t<CellPopulationBoundaryConditions>\n";
241  for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator iter = mBoundaryConditions.begin();
242  iter != mBoundaryConditions.end();
243  ++iter)
244  {
245  // Output cell boundary condition details
246  (*iter)->OutputCellPopulationBoundaryConditionInfo(rParamsFile);
247  }
248  *rParamsFile << "\t</CellPopulationBoundaryConditions>\n";
249 
250  // Output numerical method details
251  *rParamsFile << "\n\t<NumericalMethod>\n";
252  mpNumericalMethod->OutputNumericalMethodInfo(rParamsFile);
253  *rParamsFile << "\t</NumericalMethod>\n";
254 }
255 
256 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
258 {
259  // No new parameters to output, so just call method on direct parent class
261 }
262 
263 // Explicit instantiation
264 template class OffLatticeSimulation<1,1>;
265 template class OffLatticeSimulation<1,2>;
266 template class OffLatticeSimulation<2,2>;
267 template class OffLatticeSimulation<1,3>;
268 template class OffLatticeSimulation<2,3>;
269 template class OffLatticeSimulation<3,3>;
270 
271 // Serialization for Boost >= 1.36
boost::shared_ptr< AbstractNumericalMethod< ELEMENT_DIM, SPACE_DIM > > mpNumericalMethod
const boost::shared_ptr< AbstractNumericalMethod< ELEMENT_DIM, SPACE_DIM > > GetNumericalMethod() const
virtual const char * what() const
void OutputAdditionalSimulationSetup(out_stream &rParamsFile)
Definition: Node.hpp:58
virtual void WriteVisualizerSetupFile()
#define EXCEPTION(message)
Definition: Exception.hpp:143
static bool AmMaster()
Definition: PetscTools.cpp:120
const std::vector< boost::shared_ptr< AbstractForce< ELEMENT_DIM, SPACE_DIM > > > & rGetForceCollection() const
std::vector< boost::shared_ptr< AbstractCellPopulationBoundaryCondition< ELEMENT_DIM, SPACE_DIM > > > mBoundaryConditions
void SetNumericalMethod(boost::shared_ptr< AbstractNumericalMethod< ELEMENT_DIM, SPACE_DIM > > pNumericalMethod)
void ApplyBoundaries(std::map< Node< SPACE_DIM > *, c_vector< double, SPACE_DIM > > oldNodeLoctions)
void AddForce(boost::shared_ptr< AbstractForce< ELEMENT_DIM, SPACE_DIM > > pForce)
void RemoveAllCellPopulationBoundaryConditions()
virtual void OutputSimulationParameters(out_stream &rParamsFile)=0
void RevertToOldLocations(std::map< Node< SPACE_DIM > *, c_vector< double, SPACE_DIM > > oldNodeLoctions)
#define EXPORT_TEMPLATE_CLASS_ALL_DIMS(CLASS)
std::vector< boost::shared_ptr< AbstractForce< ELEMENT_DIM, SPACE_DIM > > > mForceCollection
OffLatticeSimulation(AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > &rCellPopulation, bool deleteCellPopulationInDestructor=false, bool initialiseCells=true)
virtual void OutputSimulationParameters(out_stream &rParamsFile)
virtual void UpdateCellLocationsAndTopology()
void AddCellPopulationBoundaryCondition(boost::shared_ptr< AbstractCellPopulationBoundaryCondition< ELEMENT_DIM, SPACE_DIM > > pBoundaryCondition)
AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > & mrCellPopulation