Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
AbstractNumericalMethod.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 "AbstractNumericalMethod.hpp"
37#include "StepSizeException.hpp"
38#include "Warnings.hpp"
39#include "AbstractCentreBasedCellPopulation.hpp"
40#include "NodeBasedCellPopulationWithBuskeUpdate.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 mUseUpdateNodeLocation(false),
50 mGhostNodeForcesEnabled(true)
51{
52 // mpCellPopulation, mpForceCollection and mpBoundaryConditions are initialized by the OffLatticeSimulation constructor
53}
54
55template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
59
60template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
62{
63 mpCellPopulation = pPopulation;
64
65 // Set other member variables according to the type of the cell population
66 if (dynamic_cast<NodeBasedCellPopulationWithBuskeUpdate<SPACE_DIM>*>(mpCellPopulation))
67 {
68 mUseUpdateNodeLocation = true;
69 WARNING("Non-Euler steppers are not yet implemented for NodeBasedCellPopulationWithBuskeUpdate");
70 }
71
72 if (dynamic_cast<MeshBasedCellPopulationWithGhostNodes<SPACE_DIM>*>(mpCellPopulation))
73 {
74 mGhostNodeForcesEnabled = true;
75 }
76 else
77 {
78 mGhostNodeForcesEnabled = false;
79 }
80}
81
82template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
84{
85 mpForceCollection = pForces;
86}
87
88template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
90{
91 mpBoundaryConditions = pBoundaryConditions;
92}
93
94template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
96{
97 mUseAdaptiveTimestep = useAdaptiveTimestep;
98}
99
100template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
102{
103 return mUseAdaptiveTimestep;
104}
105
106template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
107std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::SaveCurrentNodeLocations()
108{
109 std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> > node_locations;
110
111 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
112 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
113 ++node_iter)
114 {
115 node_locations[&(*node_iter)] = (node_iter)->rGetLocation();
116 }
118 return node_locations;
119}
120
121template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
122void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::ImposeBoundaryConditions(std::map<Node<SPACE_DIM>*, c_vector<double, SPACE_DIM> >& rOldNodeLocations)
124 // Apply any boundary conditions
125 for (typename std::vector<boost::shared_ptr<AbstractCellPopulationBoundaryCondition<ELEMENT_DIM,SPACE_DIM> > >::iterator bcs_iter = mpBoundaryConditions->begin();
126 bcs_iter != mpBoundaryConditions->end();
127 ++bcs_iter)
128 {
129 (*bcs_iter)->ImposeBoundaryCondition(rOldNodeLocations);
130 }
131}
132
133template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
136 CellBasedEventHandler::BeginEvent(CellBasedEventHandler::FORCE);
137
138 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
139 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd(); ++node_iter)
140 {
141 node_iter->ClearAppliedForce();
142 }
144 for (typename std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM, SPACE_DIM> > >::iterator iter = mpForceCollection->begin();
145 iter != mpForceCollection->end(); ++iter)
146 {
147 (*iter)->AddForceContribution(*mpCellPopulation);
148 }
149
156 if (mGhostNodeForcesEnabled)
157 {
158 dynamic_cast<MeshBasedCellPopulationWithGhostNodes<SPACE_DIM>*>(mpCellPopulation)->ApplyGhostForces();
159 }
160
161 // Store applied forces in a vector
162 std::vector<c_vector<double, SPACE_DIM> > forces_as_vector;
163 forces_as_vector.reserve(mpCellPopulation->GetNumNodes());
165 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
166 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd(); ++node_iter)
167 {
168 double damping = mpCellPopulation->GetDampingConstant(node_iter->GetIndex());
169 forces_as_vector.push_back(node_iter->rGetAppliedForce()/damping);
170 }
171
172 CellBasedEventHandler::EndEvent(CellBasedEventHandler::FORCE);
173
174 return forces_as_vector;
175}
177template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
179{
180 std::vector<c_vector<double, SPACE_DIM> > current_locations;
181 current_locations.reserve(mpCellPopulation->GetNumNodes());
182
183 for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
184 node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
185 ++node_iter)
186 {
187 current_locations.push_back(node_iter->rGetLocation());
188 }
189
190 return current_locations;
191}
192
193template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
194void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::SafeNodePositionUpdate( unsigned nodeIndex, c_vector<double, SPACE_DIM> newPosition)
195{
196 ChastePoint<SPACE_DIM> new_point(newPosition);
197 mpCellPopulation->SetNode(nodeIndex, new_point);
198}
199
200template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
201void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::DetectStepSizeExceptions(unsigned nodeIndex, c_vector<double,SPACE_DIM>& displacement, double dt)
202{
203 try
205 mpCellPopulation->CheckForStepSizeException(nodeIndex, displacement, dt);
206 }
207 catch (StepSizeException& e)
208 {
209 if (!(e.IsTerminal()) && (mUseAdaptiveTimestep==false))
210 {
211 /*
212 * If adaptivity is turned off but the simulation can continue, just produce a warning.
213 * Only the case for vertex-based cell populations, which can alter node displacement directly
214 * to avoid cell rearrangement problems.
215 */
216 WARN_ONCE_ONLY(e.what());
217 }
218 else
219 {
220 throw e;
221 }
222 }
223}
224
225template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
227{
228 mUseUpdateNodeLocation = useUpdateNodeLocation;
229}
230
231template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
234 return mUseUpdateNodeLocation;
235}
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<UseUpdateNodeLocation>" << mUseUpdateNodeLocation << "</UseUpdateNodeLocation> \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>;
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)
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()
void SetUseUpdateNodeLocation(bool useUpdateNodeLocation)
std::vector< c_vector< double, SPACE_DIM > > SaveCurrentLocations()
virtual void OutputNumericalMethodParameters(out_stream &rParamsFile)
Definition Node.hpp:59