Chaste  Release::3.4
AbstractTwoBodyInteractionForce.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 "AbstractTwoBodyInteractionForce.hpp"
37 #include "IsNan.hpp"
38 
39 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
41  : AbstractForce<ELEMENT_DIM,SPACE_DIM>(),
42  mUseCutOffLength(false),
43  mMechanicsCutOffLength(DBL_MAX)
44 {
45 }
46 
47 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
49 {
50  return mUseCutOffLength;
51 }
52 
53 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
55 {
56  assert(cutOffLength > 0.0);
57  mUseCutOffLength = true;
58  mMechanicsCutOffLength = cutOffLength;
59 }
60 
61 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
63 {
64  return mMechanicsCutOffLength;
65 }
66 
67 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
69 {
70  // Throw an exception message if not using a subclass of AbstractCentreBasedCellPopulation
71  if (dynamic_cast<AbstractCentreBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation) == NULL)
72  {
73  EXCEPTION("Subclasses of AbstractTwoBodyInteractionForce are to be used with subclasses of AbstractCentreBasedCellPopulation only");
74  }
75 
77  if (bool(dynamic_cast<MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation)))
78  {
79  MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>* p_static_cast_cell_population = static_cast<MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation);
80 
81  // Iterate over all springs and add force contributions
82  for (typename MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>::SpringIterator spring_iterator = p_static_cast_cell_population->SpringsBegin();
83  spring_iterator != p_static_cast_cell_population->SpringsEnd();
84  ++spring_iterator)
85  {
86  unsigned nodeA_global_index = spring_iterator.GetNodeA()->GetIndex();
87  unsigned nodeB_global_index = spring_iterator.GetNodeB()->GetIndex();
88 
89  // Calculate the force between nodes
90  c_vector<double, SPACE_DIM> force = CalculateForceBetweenNodes(nodeA_global_index, nodeB_global_index, rCellPopulation);
91 
92  // Add the force contribution to each node
93  c_vector<double, SPACE_DIM> negative_force = -1.0*force;
94  spring_iterator.GetNodeB()->AddAppliedForceContribution(negative_force);
95  spring_iterator.GetNodeA()->AddAppliedForceContribution(force);
96  }
97  }
98  else // This is a NodeBasedCellPopulation
99  {
101 
102  std::vector< std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > >& r_node_pairs = p_static_cast_cell_population->rGetNodePairs();
103 
104  for (typename std::vector< std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > >::iterator iter = r_node_pairs.begin();
105  iter != r_node_pairs.end();
106  iter++)
107  {
108  std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > pair = *iter;
109 
110  unsigned node_a_index = pair.first->GetIndex();
111  unsigned node_b_index = pair.second->GetIndex();
112 
113  // Calculate the force between nodes
114  c_vector<double, SPACE_DIM> force = CalculateForceBetweenNodes(node_a_index, node_b_index, rCellPopulation);
115  for (unsigned j=0; j<SPACE_DIM; j++)
116  {
117  assert(!std::isnan(force[j]));
118  }
119 
120  // Add the force contribution to each node
121  c_vector<double, SPACE_DIM> negative_force = -1.0*force;
122  pair.first->AddAppliedForceContribution(force);
123  pair.second->AddAppliedForceContribution(negative_force);
124  }
125  }
126 }
127 
128 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
130 {
131  *rParamsFile << "\t\t\t<UseCutOffLength>" << mUseCutOffLength << "</UseCutOffLength>\n";
132  *rParamsFile << "\t\t\t<CutOffLength>" << mMechanicsCutOffLength << "</CutOffLength>\n";
133 
134  // Call method on direct parent class
136 }
137 
138 // Explicit instantiation
virtual void OutputForceParameters(out_stream &rParamsFile)
Definition: Node.hpp:58
#define EXCEPTION(message)
Definition: Exception.hpp:143
void AddForceContribution(AbstractCellPopulation< ELEMENT_DIM, SPACE_DIM > &rCellPopulation)
virtual std::vector< std::pair< Node< SPACE_DIM > *, Node< SPACE_DIM > * > > & rGetNodePairs()=0
unsigned GetIndex() const
Definition: Node.cpp:159
virtual void OutputForceParameters(out_stream &rParamsFile)=0