Chaste Commit::9e4a273f0754a391514ab12ed9d4a38fc9933db2
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
98template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
100{
101 mMaxAdaptiveTimeSteps = maxAdaptiveTimeStep;
102}
103
104template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
106{
107 return mMaxAdaptiveTimeSteps;
108}
109
110template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
112{
113 CellBasedEventHandler::BeginEvent(CellBasedEventHandler::POSITION);
115 double time_advanced_so_far = 0;
116 double target_time_step = this->mDt;
117 double present_time_step = this->mDt;
118
119 unsigned adaptive_timer = 0;
120 while (time_advanced_so_far < target_time_step)
121 {
122 // Store the initial node positions (these may be needed when applying boundary conditions)
123 std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > old_node_locations;
124
125 for (auto node_iter = this->mrCellPopulation.rGetMesh().GetNodeIteratorBegin();
126 node_iter != this->mrCellPopulation.rGetMesh().GetNodeIteratorEnd();
127 ++node_iter)
128 {
129 old_node_locations[&(*node_iter)] = (node_iter)->rGetLocation();
130 }
131
132 // Try to update node positions according to the numerical method
133 try
135 mpNumericalMethod->UpdateAllNodePositions(present_time_step);
136 ApplyBoundaries(old_node_locations);
137
138 // Successful time step: update time_advanced_so_far and reset the adaptive failure counter
139 time_advanced_so_far += present_time_step;
140 adaptive_timer = 0;
141 }
142 catch (StepSizeException& e)
143 {
144 // Detects if a node has travelled too far in a single time step
145 if (mpNumericalMethod->HasAdaptiveTimestep() && adaptive_timer < mMaxAdaptiveTimeSteps)
146 {
147 // If adaptivity is switched on, revert node locations and choose a suitably smaller time step
148 RevertToOldLocations(old_node_locations);
149 present_time_step = std::min(e.GetSuggestedNewStep(), target_time_step - time_advanced_so_far);
150 adaptive_timer += 1;
151 }
152 else
153 {
154 // If adaptivity is switched off or we have reached the accepted number of adaptive attempts, terminate with an error
155 EXCEPTION(e.what());
156 }
157 }
158 }
159
160 CellBasedEventHandler::EndEvent(CellBasedEventHandler::POSITION);
162
163template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
164void OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::RevertToOldLocations(std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > oldNodeLoctions)
165{
166 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = this->mrCellPopulation.rGetMesh().GetNodeIteratorBegin();
167 node_iter != this->mrCellPopulation.rGetMesh().GetNodeIteratorEnd();
168 ++node_iter)
169 {
170 (node_iter)->rGetModifiableLocation() = oldNodeLoctions[&(*node_iter)];
171 }
172}
174template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
175void OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::ApplyBoundaries(std::map<Node<SPACE_DIM>*,c_vector<double, SPACE_DIM> > oldNodeLoctions)
176{
177 // Apply any boundary conditions
178 for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator bcs_iter = mBoundaryConditions.begin();
179 bcs_iter != mBoundaryConditions.end();
180 ++bcs_iter)
181 {
182 (*bcs_iter)->ImposeBoundaryCondition(oldNodeLoctions);
183 }
184
185 // Verify that each boundary condition is now satisfied
186 for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator bcs_iter = mBoundaryConditions.begin();
187 bcs_iter != mBoundaryConditions.end();
188 ++bcs_iter)
189 {
190 if (!((*bcs_iter)->VerifyBoundaryCondition()))
191 {
192 EXCEPTION("The cell population boundary conditions are incompatible.");
193 }
194 }
195}
196
197template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
199{
201 {
202 for (unsigned i=0; i<this->mForceCollection.size(); i++)
203 {
204 this->mForceCollection[i]->WriteDataToVisualizerSetupFile(this->mpVizSetupFile);
205 }
206
207 this->mrCellPopulation.WriteDataToVisualizerSetupFile(this->mpVizSetupFile);
208 }
209}
210
211template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
213{
214 // Clear all forces
215 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = this->mrCellPopulation.rGetMesh().GetNodeIteratorBegin();
216 node_iter != this->mrCellPopulation.rGetMesh().GetNodeIteratorEnd();
217 ++node_iter)
219 node_iter->ClearAppliedForce();
220 }
221
222 // Use a forward Euler method by default, unless a numerical method has been specified already
223 if (mpNumericalMethod == nullptr)
224 {
225 mpNumericalMethod = boost::make_shared<ForwardEulerNumericalMethod<ELEMENT_DIM, SPACE_DIM> >();
226 }
227 mpNumericalMethod->SetCellPopulation(dynamic_cast<AbstractOffLatticeCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&(this->mrCellPopulation)));
228 mpNumericalMethod->SetForceCollection(&mForceCollection);
229 mpNumericalMethod->SetBoundaryConditions(&mBoundaryConditions);
230}
231
232template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
234{
235 // Loop over forces
236 *rParamsFile << "\n\t<Forces>\n";
237 for (typename std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM,SPACE_DIM> > >::iterator iter = mForceCollection.begin();
238 iter != mForceCollection.end();
239 ++iter)
240 {
241 // Output force details
242 (*iter)->OutputForceInfo(rParamsFile);
243 }
244 *rParamsFile << "\t</Forces>\n";
245
246 // Loop over cell population boundary conditions
247 *rParamsFile << "\n\t<CellPopulationBoundaryConditions>\n";
248 for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator iter = mBoundaryConditions.begin();
249 iter != mBoundaryConditions.end();
250 ++iter)
251 {
252 // Output cell boundary condition details
253 (*iter)->OutputCellPopulationBoundaryConditionInfo(rParamsFile);
254 }
255 *rParamsFile << "\t</CellPopulationBoundaryConditions>\n";
256
257 // Output numerical method details
258 *rParamsFile << "\n\t<NumericalMethod>\n";
259 mpNumericalMethod->OutputNumericalMethodInfo(rParamsFile);
260 *rParamsFile << "\t</NumericalMethod>\n";
261}
262
263template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
265{
266 // No new parameters to output, so just call method on direct parent class
268}
269
270// Explicit instantiation
271template class OffLatticeSimulation<1,1>;
272template class OffLatticeSimulation<1,2>;
273template class OffLatticeSimulation<2,2>;
274template class OffLatticeSimulation<1,3>;
275template class OffLatticeSimulation<2,3>;
276template class OffLatticeSimulation<3,3>;
277
278// 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)
void SetMaxAdaptiveTimeStep(unsigned maxAdaptiveTimeStep)
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 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()