Chaste Release::3.1
SphereGeometryBoundaryCondition.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 "SphereGeometryBoundaryCondition.hpp"
00037 #include "NodeBasedCellPopulation.hpp"
00038 
00039 template<unsigned DIM>
00040 SphereGeometryBoundaryCondition<DIM>::SphereGeometryBoundaryCondition(AbstractCellPopulation<DIM>* pCellPopulation,
00041                                                                       c_vector<double, DIM> centre,
00042                                                                       double radius,
00043                                                                       double distance)
00044     : AbstractCellPopulationBoundaryCondition<DIM>(pCellPopulation),
00045       mCentreOfSphere(centre),
00046       mRadiusOfSphere(radius),
00047       mMaximumDistance(distance)
00048 {
00049     assert(mRadiusOfSphere > 0.0);
00050     assert(mMaximumDistance > 0.0);
00051 
00052     if (dynamic_cast<NodeBasedCellPopulation<DIM>*>(this->mpCellPopulation) == NULL)
00053     {
00054         EXCEPTION("A NodeBasedCellPopulation must be used with this boundary condition object.");
00055     }
00056     if (DIM == 1)
00057     {
00058         EXCEPTION("This boundary condition is not implemented in 1D.");
00059     }
00060 }
00061 
00062 template<unsigned DIM>
00063 const c_vector<double, DIM>& SphereGeometryBoundaryCondition<DIM>::rGetCentreOfSphere() const
00064 {
00065     return mCentreOfSphere;
00066 }
00067 
00068 template<unsigned DIM>
00069 double SphereGeometryBoundaryCondition<DIM>::GetRadiusOfSphere() const
00070 {
00071     return mRadiusOfSphere;
00072 }
00073 
00074 template<unsigned DIM>
00075 void SphereGeometryBoundaryCondition<DIM>::ImposeBoundaryCondition(const std::vector<c_vector<double, DIM> >& rOldLocations)
00076 {
00077     // Iterate over the cell population
00078     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
00079          cell_iter != this->mpCellPopulation->End();
00080          ++cell_iter)
00081     {
00082         // Find the radial distance between this cell and the surface of the sphere
00083         c_vector<double,DIM> cell_location = this->mpCellPopulation->GetLocationOfCellCentre(*cell_iter);
00084         double radius = norm_2(cell_location - mCentreOfSphere);
00085 
00086         // If the cell is too far from the surface of the sphere...
00087         if (fabs(radius - mRadiusOfSphere) > mMaximumDistance)
00088         {
00089             // ...move the cell back onto the surface of the sphere
00090             c_vector<double, DIM> location_on_sphere =
00091                 mCentreOfSphere + mRadiusOfSphere*(cell_location - mCentreOfSphere)/radius;
00092 
00093             unsigned node_index = this->mpCellPopulation->GetLocationIndexUsingCell(*cell_iter);
00094             Node<DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
00095 
00096             p_node->rGetModifiableLocation() = location_on_sphere;
00097         }
00098     }
00099 }
00100 
00101 template<unsigned DIM>
00102 bool SphereGeometryBoundaryCondition<DIM>::VerifyBoundaryCondition()
00103 {
00104     bool condition_satisfied = true;
00105 
00106     // Iterate over the cell population
00107     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
00108          cell_iter != this->mpCellPopulation->End();
00109          ++cell_iter)
00110     {
00111         // Find the radial distance between this cell and the surface of the sphere
00112         c_vector<double,DIM> cell_location = this->mpCellPopulation->GetLocationOfCellCentre(*cell_iter);
00113         double radius = norm_2(cell_location - mCentreOfSphere);
00114 
00115         // If the cell is too far from the surface of the sphere...
00116         if (fabs(radius - mRadiusOfSphere) > mMaximumDistance)
00117         {
00118             // ...then the boundary condition is not satisfied
00119             condition_satisfied = false;
00120             break;
00121         }
00122     }
00123     return condition_satisfied;
00124 }
00125 
00126 template<unsigned DIM>
00127 void SphereGeometryBoundaryCondition<DIM>::OutputCellPopulationBoundaryConditionParameters(out_stream& rParamsFile)
00128 {
00129     *rParamsFile << "\t\t\t<CentreOfSphere>";
00130     for (unsigned index=0; index != DIM-1U; index++) // Note: inequality avoids testing index < 0U when DIM=1
00131     {
00132         *rParamsFile << mCentreOfSphere[index] << ",";
00133     }
00134     *rParamsFile << mCentreOfSphere[DIM-1] << "</CentreOfSphere>\n";
00135 
00136     *rParamsFile << "\t\t\t<RadiusOfSphere>" << mRadiusOfSphere << "</RadiusOfSphere>\n";
00137     *rParamsFile << "\t\t\t<MaximumDistance>" << mMaximumDistance << "</MaximumDistance>\n";
00138 
00139     // Call method on direct parent class
00140     AbstractCellPopulationBoundaryCondition<DIM>::OutputCellPopulationBoundaryConditionParameters(rParamsFile);
00141 }
00142 
00144 // Explicit instantiation
00146 
00147 template class SphereGeometryBoundaryCondition<1>;
00148 template class SphereGeometryBoundaryCondition<2>;
00149 template class SphereGeometryBoundaryCondition<3>;
00150 
00151 // Serialization for Boost >= 1.36
00152 #include "SerializationExportWrapperForCpp.hpp"
00153 EXPORT_TEMPLATE_CLASS_SAME_DIMS(SphereGeometryBoundaryCondition)