Chaste  Release::2017.1
AbstractNumericalMethod.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 
44 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
46  : mpCellPopulation(nullptr),
47  mpForceCollection(nullptr),
48  mUseAdaptiveTimestep(false),
49  mUseUpdateNodeLocation(false),
50  mGhostNodeForcesEnabled(true)
51 {
52  // mpCellPopulation and mpForceCollection are initialized by the OffLatticeSimulation constructor
53 }
54 
55 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
57 {
58 }
59 
60 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
62 {
63  mpCellPopulation = pPopulation;
64 
65  // Set other member variables according to the type of the cell population
67  {
69  WARNING("Non-Euler steppers are not yet implemented for NodeBasedCellPopulationWithBuskeUpdate");
70  }
71 
73  {
75  }
76  else
77  {
79  }
80 }
81 
82 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
84 {
85  mpForceCollection = pForces;
86 }
87 
88 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
90 {
91  mUseAdaptiveTimestep = useAdaptiveTimestep;
92 }
93 
94 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
96 {
97  return mUseAdaptiveTimestep;
98 }
99 
100 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
102 {
103  CellBasedEventHandler::BeginEvent(CellBasedEventHandler::FORCE);
104 
105  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
106  node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd(); ++node_iter)
107  {
108  node_iter->ClearAppliedForce();
109  }
110 
111  for (typename std::vector<boost::shared_ptr<AbstractForce<ELEMENT_DIM, SPACE_DIM> > >::iterator iter = mpForceCollection->begin();
112  iter != mpForceCollection->end(); ++iter)
113  {
114  (*iter)->AddForceContribution(*mpCellPopulation);
115  }
116 
124  {
125  dynamic_cast<MeshBasedCellPopulationWithGhostNodes<SPACE_DIM>*>(mpCellPopulation)->ApplyGhostForces();
126  }
127 
128  // Store applied forces in a vector
129  std::vector<c_vector<double, SPACE_DIM> > forces_as_vector;
130  forces_as_vector.reserve(mpCellPopulation->GetNumNodes());
131 
132  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
133  node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd(); ++node_iter)
134  {
135  double damping = mpCellPopulation->GetDampingConstant(node_iter->GetIndex());
136  forces_as_vector.push_back(node_iter->rGetAppliedForce()/damping);
137  }
138 
139  CellBasedEventHandler::EndEvent(CellBasedEventHandler::FORCE);
140 
141  return forces_as_vector;
142 }
143 
144 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
146 {
147  std::vector<c_vector<double, SPACE_DIM> > current_locations;
148  current_locations.reserve(mpCellPopulation->GetNumNodes());
149 
150  for (typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::NodeIterator node_iter = mpCellPopulation->rGetMesh().GetNodeIteratorBegin();
151  node_iter != mpCellPopulation->rGetMesh().GetNodeIteratorEnd();
152  ++node_iter)
153  {
154  current_locations.push_back(node_iter->rGetLocation());
155  }
156 
157  return current_locations;
158 }
159 
160 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
161 void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::SafeNodePositionUpdate( unsigned nodeIndex, c_vector<double, SPACE_DIM> newPosition)
162 {
163  ChastePoint<SPACE_DIM> new_point(newPosition);
164  mpCellPopulation->SetNode(nodeIndex, new_point);
165 }
166 
167 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
168 void AbstractNumericalMethod<ELEMENT_DIM,SPACE_DIM>::DetectStepSizeExceptions(unsigned nodeIndex, c_vector<double,SPACE_DIM>& displacement, double dt)
169 {
170  try
171  {
172  mpCellPopulation->CheckForStepSizeException(nodeIndex, displacement, dt);
173  }
174  catch (StepSizeException& e)
175  {
176  if (!(e.IsTerminal()) && (mUseAdaptiveTimestep==false))
177  {
178  /*
179  * If adaptivity is turned off but the simulation can continue, just produce a warning.
180  * Only the case for vertex-based cell populations, which can alter node displacement directly
181  * to avoid cell rearrangement problems.
182  */
183  WARN_ONCE_ONLY(e.what());
184  }
185  else
186  {
187  throw e;
188  }
189  }
190 }
191 
192 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
194 {
195  mUseUpdateNodeLocation = useUpdateNodeLocation;
196 }
197 
198 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
200 {
201  return mUseUpdateNodeLocation;
202 }
203 
204 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
206 {
207  std::string numerical_method_type = GetIdentifier();
208 
209  *rParamsFile << "\t\t<" << numerical_method_type << ">\n";
210  OutputNumericalMethodParameters(rParamsFile);
211  *rParamsFile << "\t\t</" << numerical_method_type << ">\n";
212 }
213 
214 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
216 {
217  *rParamsFile << "\t\t\t<UseAdaptiveTimestep>" << mUseAdaptiveTimestep << "</UseAdaptiveTimestep> \n";
218  *rParamsFile << "\t\t\t<UseUpdateNodeLocation>" << mUseUpdateNodeLocation << "</UseUpdateNodeLocation> \n";
219  *rParamsFile << "\t\t\t<GhostNodeForcesEnabled>" << mGhostNodeForcesEnabled << "</GhostNodeForcesEnabled> \n";
220 }
221 
222 // Explicit instantiation
223 template class AbstractNumericalMethod<1,1>;
224 template class AbstractNumericalMethod<1,2>;
225 template class AbstractNumericalMethod<2,2>;
226 template class AbstractNumericalMethod<1,3>;
227 template class AbstractNumericalMethod<2,3>;
228 template class AbstractNumericalMethod<3,3>;
void SafeNodePositionUpdate(unsigned nodeIndex, c_vector< double, SPACE_DIM > newPosition)
virtual const char * what() const
void SetForceCollection(std::vector< boost::shared_ptr< AbstractForce< ELEMENT_DIM, SPACE_DIM > > > *pForces)
std::vector< boost::shared_ptr< AbstractForce< ELEMENT_DIM, SPACE_DIM > > > * mpForceCollection
AbstractOffLatticeCellPopulation< ELEMENT_DIM, SPACE_DIM > * mpCellPopulation
virtual void OutputNumericalMethodParameters(out_stream &rParamsFile)
void OutputNumericalMethodInfo(out_stream &rParamsFile)
void SetUseUpdateNodeLocation(bool useUpdateNodeLocation)
void SetUseAdaptiveTimestep(bool useAdaptiveTimestep)
void SetCellPopulation(AbstractOffLatticeCellPopulation< ELEMENT_DIM, SPACE_DIM > *pPopulation)
std::vector< c_vector< double, SPACE_DIM > > SaveCurrentLocations()
void DetectStepSizeExceptions(unsigned nodeIndex, c_vector< double, SPACE_DIM > &displacement, double dt)
std::string GetIdentifier() const
std::vector< c_vector< double, SPACE_DIM > > ComputeForcesIncludingDamping()