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