Chaste Release::3.1
AbstractTwoBodyInteractionForce.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "AbstractTwoBodyInteractionForce.hpp"
00037 
00038 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 AbstractTwoBodyInteractionForce<ELEMENT_DIM,SPACE_DIM>::AbstractTwoBodyInteractionForce()
00040    : AbstractForce<ELEMENT_DIM,SPACE_DIM>(),
00041      mUseCutOffLength(false),
00042      mMechanicsCutOffLength(DBL_MAX)
00043 {
00044 }
00045 
00046 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00047 bool AbstractTwoBodyInteractionForce<ELEMENT_DIM,SPACE_DIM>::GetUseCutOffLength()
00048 {
00049     return mUseCutOffLength;
00050 }
00051 
00052 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00053 void AbstractTwoBodyInteractionForce<ELEMENT_DIM,SPACE_DIM>::SetCutOffLength(double cutOffLength)
00054 {
00055     assert(cutOffLength > 0.0);
00056     mUseCutOffLength = true;
00057     mMechanicsCutOffLength = cutOffLength;
00058 }
00059 
00060 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00061 double AbstractTwoBodyInteractionForce<ELEMENT_DIM,SPACE_DIM>::GetCutOffLength()
00062 {
00063     return mMechanicsCutOffLength;
00064 }
00065 
00066 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00067 void AbstractTwoBodyInteractionForce<ELEMENT_DIM,SPACE_DIM>::AddForceContribution(std::vector<c_vector<double, SPACE_DIM> >& rForces,
00068                                                                 AbstractCellPopulation<ELEMENT_DIM,SPACE_DIM>& rCellPopulation)
00069 {
00070     // Throw an exception message if not using a subclass of AbstractCentreBasedCellPopulation
00071     if (dynamic_cast<AbstractCentreBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation) == NULL)
00072     {
00073         EXCEPTION("Subclasses of AbstractTwoBodyInteractionForce are to be used with subclasses of AbstractCentreBasedCellPopulation only");
00074     }
00075 
00077     if (dynamic_cast<MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation))
00078     {
00079         MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>* p_static_cast_cell_population = static_cast<MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation);
00080 
00081         // Iterate over all springs and add force contributions
00082         for (typename MeshBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>::SpringIterator spring_iterator = p_static_cast_cell_population->SpringsBegin();
00083              spring_iterator != p_static_cast_cell_population->SpringsEnd();
00084              ++spring_iterator)
00085         {
00086             unsigned nodeA_global_index = spring_iterator.GetNodeA()->GetIndex();
00087             unsigned nodeB_global_index = spring_iterator.GetNodeB()->GetIndex();
00088 
00089             // Calculate the force between nodes
00090             c_vector<double, SPACE_DIM> force = CalculateForceBetweenNodes(nodeA_global_index, nodeB_global_index, rCellPopulation);
00091 
00092             // Add the force contribution to each node
00093             rForces[nodeB_global_index] -= force;
00094             rForces[nodeA_global_index] += force;
00095         }
00096     }
00097     else// This is a NodeBasedCellPopulation
00098     {
00099 
00100         AbstractCentreBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>* p_static_cast_cell_population = static_cast<AbstractCentreBasedCellPopulation<ELEMENT_DIM,SPACE_DIM>*>(&rCellPopulation);
00101 
00102         std::set< std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > >& r_node_pairs = p_static_cast_cell_population->rGetNodePairs();
00103 
00104         for (typename std::set< std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > >::iterator iter = r_node_pairs.begin();
00105             iter != r_node_pairs.end();
00106             iter++)
00107         {
00108             std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > pair = *iter;
00109 
00110             unsigned node_a_index = pair.first->GetIndex();
00111             unsigned node_b_index = pair.second->GetIndex();
00112 
00113             // Calculate the force between nodes
00114             c_vector<double, SPACE_DIM> force = CalculateForceBetweenNodes(node_a_index, node_b_index, rCellPopulation);
00115             for (unsigned j=0; j<SPACE_DIM; j++)
00116             {
00117                 assert(!std::isnan(force[j]));
00118             }
00119 
00120             // Add the force contribution to each node
00121             rForces[node_a_index] += force;
00122             rForces[node_b_index] -= force;
00123         }
00124     }
00125 }
00126 
00127 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00128 void AbstractTwoBodyInteractionForce<ELEMENT_DIM,SPACE_DIM>::OutputForceParameters(out_stream& rParamsFile)
00129 {
00130     *rParamsFile << "\t\t\t<UseCutOffLength>" << mUseCutOffLength << "</UseCutOffLength>\n";
00131     *rParamsFile << "\t\t\t<CutOffLength>" << mMechanicsCutOffLength << "</CutOffLength>\n";
00132 
00133     // Call method on direct parent class
00134     AbstractForce<ELEMENT_DIM,SPACE_DIM>::OutputForceParameters(rParamsFile);
00135 }
00136 
00138 // Explicit instantiation
00140 
00141 template class AbstractTwoBodyInteractionForce<1,1>;
00142 template class AbstractTwoBodyInteractionForce<1,2>;
00143 template class AbstractTwoBodyInteractionForce<2,2>;
00144 template class AbstractTwoBodyInteractionForce<1,3>;
00145 template class AbstractTwoBodyInteractionForce<2,3>;
00146 template class AbstractTwoBodyInteractionForce<3,3>;