Chaste Commit::9e4a273f0754a391514ab12ed9d4a38fc9933db2
AbstractNumericalMethod.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 "AbstractNumericalMethod.hpp"
37#include "StepSizeException.hpp"
38#include "Warnings.hpp"
39#include "NodeBasedCellPopulationWithBuskeUpdate.hpp"
40#include "ImmersedBoundaryCellPopulation.hpp"
41#include "MeshBasedCellPopulationWithGhostNodes.hpp"
42#include "CellBasedEventHandler.hpp"
43
44template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
46 : mpCellPopulation(nullptr),
47 mpForceCollection(nullptr),
48 mUseAdaptiveTimestep(false),
49 mGhostNodeForcesEnabled(true)
50{
51 // mpCellPopulation, mpForceCollection and mpBoundaryConditions are initialized by the OffLatticeSimulation constructor
52}
53
54template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
58
59template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
61{
62 mpCellPopulation = pPopulation;
63
64 // Cell populations whose UpdateNodeLocations() is meaningful (rather than NEVER_REACHED)
65 // manage their own node position updates, and must be paired with NoNumericalMethod; every
66 // other population must be paired with a numerical method that does its own integration.
67 bool population_requires_delegation =
68 (dynamic_cast<NodeBasedCellPopulationWithBuskeUpdate<SPACE_DIM>*>(mpCellPopulation) != nullptr) ||
69 (dynamic_cast<ImmersedBoundaryCellPopulation<SPACE_DIM>*>(mpCellPopulation) != nullptr);
70
71 if (population_requires_delegation != this->DelegatesToPopulation())
72 {
73 EXCEPTION("NoNumericalMethod must be used if and only if the cell population manages its own "
74 "node position updates (currently NodeBasedCellPopulationWithBuskeUpdate and "
75 "ImmersedBoundaryCellPopulation).");
76 }
77
78 if (dynamic_cast<MeshBasedCellPopulationWithGhostNodes<SPACE_DIM>*>(mpCellPopulation))
79 {
80 mGhostNodeForcesEnabled = true;
81 }
82 else
83 {
84 mGhostNodeForcesEnabled = false;
85 }
86}
87
88template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
90{
91 mpForceCollection = pForces;
92}
93
94template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
96{
97 mpBoundaryConditions = pBoundaryConditions;
98}
99
100template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
102{
103 mUseAdaptiveTimestep = useAdaptiveTimestep;
104}
106template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
108{
109 return mUseAdaptiveTimestep;
110}
112template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
113std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::SaveCurrentNodeLocations()
114{
115 std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > node_locations;
116
117 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
118 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
119 ++node_iter)
120 {
121 node_locations[&(*node_iter)] = (node_iter)->rGetLocation();
122 }
124 return node_locations;
125}
126
127template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
128void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::ImposeBoundaryConditions(std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> >& rOldNodeLocations)
129{
130 // Apply any boundary conditions
131 for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator bcs_iter = mpBoundaryConditions->begin();
132 bcs_iter != mpBoundaryConditions->end();
133 ++bcs_iter)
134 {
135 (*bcs_iter)->ImposeBoundaryCondition(rOldNodeLocations);
136 }
137}
138
139template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
141{
142 CellBasedEventHandler::BeginEvent(CellBasedEventHandler::FORCE);
143
144 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
145 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd(); ++node_iter)
146 {
147 node_iter->ClearAppliedForce();
148 }
149
150 for (typename std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM, SPACE_DIM> > >::iterator iter = mpForceCollection->begin();
151 iter != mpForceCollection->end(); ++iter)
153 (*iter)->AddForceContribution(*mpCellPopulation);
154 }
155
162 if (mGhostNodeForcesEnabled)
163 {
164 dynamic_cast<MeshBasedCellPopulationWithGhostNodes<SPACE_DIM>*>(mpCellPopulation)->ApplyGhostForces();
165 }
166
167 // Store applied forces in a vector
168 std::vector<c_vector<double, SPACE_DIM> > forces_as_vector;
169 forces_as_vector.reserve(mpCellPopulation->GetNumNodes());
170
171 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
172 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd(); ++node_iter)
173 {
174 double damping = mpCellPopulation->GetDampingConstant(node_iter->GetIndex());
175 forces_as_vector.push_back(node_iter->rGetAppliedForce()/damping);
176 }
177
178 CellBasedEventHandler::EndEvent(CellBasedEventHandler::FORCE);
179
180 return forces_as_vector;
181}
182
183template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
186 std::vector<c_vector<double, SPACE_DIM> > current_locations;
187 current_locations.reserve(mpCellPopulation->GetNumNodes());
188
189 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
190 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
191 ++node_iter)
192 {
193 current_locations.push_back(node_iter->rGetLocation());
194 }
195
196 return current_locations;
197}
198
199template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
200void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::SafeNodePositionUpdate( unsigned nodeIndex, c_vector<double, SPACE_DIM> newPosition)
201{
202 ChastePoint<SPACE_DIM> new_point(newPosition);
203 mpCellPopulation->SetNode(nodeIndex, new_point);
204}
205
206template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
207void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::DetectStepSizeExceptions(unsigned nodeIndex, c_vector<double,SPACE_DIM>& displacement, double dt)
208{
209 try
210 {
211 mpCellPopulation->CheckForStepSizeException(nodeIndex, displacement, dt);
212 }
213 catch (StepSizeException& e)
214 {
215 if (!(e.IsTerminal()) && (mUseAdaptiveTimestep==false))
216 {
217 /*
218 * If adaptivity is turned off but the simulation can continue, just produce a warning.
219 * Only the case for vertex-based cell populations, which can alter node displacement directly
220 * to avoid cell rearrangement problems.
221 */
222 WARN_ONCE_ONLY(e.what());
223 }
224 else
225 {
226 throw e;
227 }
228 }
229}
230
231template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
236
237template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
239{
240 std::string numerical_method_type = GetIdentifier();
241
242 *rParamsFile << "\t\t<" << numerical_method_type << ">\n";
243 OutputNumericalMethodParameters(rParamsFile);
244 *rParamsFile << "\t\t</" << numerical_method_type << ">\n";
245}
246
247template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
249{
250 *rParamsFile << "\t\t\t<UseAdaptiveTimestep>" << mUseAdaptiveTimestep << "</UseAdaptiveTimestep> \n";
251 *rParamsFile << "\t\t\t<DelegatesToPopulation>" << this->DelegatesToPopulation() << "</DelegatesToPopulation> \n";
252 *rParamsFile << "\t\t\t<GhostNodeForcesEnabled>" << mGhostNodeForcesEnabled << "</GhostNodeForcesEnabled> \n";
253}
254
255// Explicit instantiation
256template class AbstractNumericalMethod<1,1>;
257template class AbstractNumericalMethod<1,2>;
258template class AbstractNumericalMethod<2,2>;
259template class AbstractNumericalMethod<1,3>;
260template class AbstractNumericalMethod<2,3>;
261template class AbstractNumericalMethod<3,3>;
#define EXCEPTION(message)
void SetForceCollection(std::vector< boost::shared_ptr< AbstractForce< ELEMENT_DIM, SPACE_DIM > > > *pForces)
std::map< Node< SPACE_DIM > *, c_vector< double, SPACE_DIM > > SaveCurrentNodeLocations()
void SetCellPopulation(AbstractOffLatticeCellPopulation< ELEMENT_DIM, SPACE_DIM > *pPopulation)
void SafeNodePositionUpdate(unsigned nodeIndex, c_vector< double, SPACE_DIM > newPosition)
void ImposeBoundaryConditions(std::map< Node< SPACE_DIM > *, c_vector< double, SPACE_DIM > > &rOldNodeLocations)
void OutputNumericalMethodInfo(out_stream &rParamsFile)
void DetectStepSizeExceptions(unsigned nodeIndex, c_vector< double, SPACE_DIM > &displacement, double dt)
virtual void SetUseAdaptiveTimestep(bool useAdaptiveTimestep)
void SetBoundaryConditions(std::vector< boost::shared_ptr< AbstractCellPopulationBoundaryCondition< ELEMENT_DIM, SPACE_DIM > > > *pBoundaryConditions)
std::vector< c_vector< double, SPACE_DIM > > ComputeForcesIncludingDamping()
std::vector< c_vector< double, SPACE_DIM > > SaveCurrentLocations()
virtual void OutputNumericalMethodParameters(out_stream &rParamsFile)
Definition Node.hpp:59