Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
OffLatticeSimulation.cpp
1/*
2
3Copyright (c) 2005-2024, 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 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;
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
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
157template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
158void 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)
164 (node_iter)->rGetModifiableLocation() = oldNodeLoctions[&(*node_iter)];
165 }
166}
167
168template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
169void OffLatticeSimulation<ELEMENT_DIM,SPACE_DIM>::ApplyBoundaries(std::map<Node<SPACE_DIM>*,c_vector<double, SPACE_DIM> > oldNodeLoctions)
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)
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.");
188 }
189}
190
191template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
193{
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 }
204
205template<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 }
221 mpNumericalMethod->SetCellPopulation(dynamic_cast<AbstractOffLatticeCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&(this->mrCellPopulation)));
222 mpNumericalMethod->SetForceCollection(&mForceCollection);
223 mpNumericalMethod->SetBoundaryConditions(&mBoundaryConditions);
224}
225
226template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
228{
229 // Loop over forces
230 *rParamsFile << "\n\t<Forces>\n";
231 for (typename std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM,SPACE_DIM> > >::iterator iter = mForceCollection.begin();
232 iter != mForceCollection.end();
233 ++iter)
234 {
235 // Output force details
236 (*iter)->OutputForceInfo(rParamsFile);
237 }
238 *rParamsFile << "\t</Forces>\n";
239
240 // Loop over cell population boundary conditions
241 *rParamsFile << "\n\t<CellPopulationBoundaryConditions>\n";
242 for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator iter = mBoundaryConditions.begin();
243 iter != mBoundaryConditions.end();
244 ++iter)
245 {
246 // Output cell boundary condition details
247 (*iter)->OutputCellPopulationBoundaryConditionInfo(rParamsFile);
248 }
249 *rParamsFile << "\t</CellPopulationBoundaryConditions>\n";
250
251 // Output numerical method details
252 *rParamsFile << "\n\t<NumericalMethod>\n";
253 mpNumericalMethod->OutputNumericalMethodInfo(rParamsFile);
254 *rParamsFile << "\t</NumericalMethod>\n";
255}
256
257template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
259{
260 // No new parameters to output, so just call method on direct parent class
262}
263
264// Explicit instantiation
265template class OffLatticeSimulation<1,1>;
266template class OffLatticeSimulation<1,2>;
267template class OffLatticeSimulation<2,2>;
268template class OffLatticeSimulation<1,3>;
269template class OffLatticeSimulation<2,3>;
270template class OffLatticeSimulation<3,3>;
271
272// 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
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 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()