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