Chaste Release::3.1
PlaneBoundaryCondition.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 "PlaneBoundaryCondition.hpp"
00037 #include "AbstractCentreBasedCellPopulation.hpp"
00038 #include "VertexBasedCellPopulation.hpp"
00039 
00040 template<unsigned DIM>
00041 PlaneBoundaryCondition<DIM>::PlaneBoundaryCondition(AbstractCellPopulation<DIM>* pCellPopulation,
00042                                                     c_vector<double, DIM> point,
00043                                                     c_vector<double, DIM> normal)
00044         : AbstractCellPopulationBoundaryCondition<DIM>(pCellPopulation),
00045           mPointOnPlane(point)
00046 {
00047     assert(norm_2(normal) > 0.0);
00048     mNormalToPlane = normal/norm_2(normal);
00049 }
00050 
00051 template<unsigned DIM>
00052 const c_vector<double, DIM>& PlaneBoundaryCondition<DIM>::rGetPointOnPlane() const
00053 {
00054     return mPointOnPlane;
00055 }
00056 
00057 template<unsigned DIM>
00058 const c_vector<double, DIM>& PlaneBoundaryCondition<DIM>::rGetNormalToPlane() const
00059 {
00060     return mNormalToPlane;
00061 }
00062 
00063 template<unsigned DIM>
00064 void PlaneBoundaryCondition<DIM>::ImposeBoundaryCondition(const std::vector< c_vector<double, DIM> >& rOldLocations)
00065 {
00067     if (dynamic_cast<AbstractOffLatticeCellPopulation<DIM>*>(this->mpCellPopulation)==NULL)
00068     {
00069         EXCEPTION("PlaneBoundaryCondition requires a subclass of AbstractOffLatticeCellPopulation.");
00070     }
00071 
00072     assert((dynamic_cast<AbstractCentreBasedCellPopulation<DIM>*>(this->mpCellPopulation))
00073             || (dynamic_cast<VertexBasedCellPopulation<DIM>*>(this->mpCellPopulation)) );
00074 
00075     if (DIM != 1)
00076     {
00077         if (dynamic_cast<AbstractCentreBasedCellPopulation<DIM>*>(this->mpCellPopulation))
00078         {
00079             for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
00080                  cell_iter != this->mpCellPopulation->End();
00081                  ++cell_iter)
00082             {
00083                 c_vector<double, DIM> cell_location = this->mpCellPopulation->GetLocationOfCellCentre(*cell_iter);
00084 
00085                 unsigned node_index = this->mpCellPopulation->GetLocationIndexUsingCell(*cell_iter);
00086                 Node<DIM>* p_node = this->mpCellPopulation->GetNode(node_index);
00087 
00088                 double signed_distance = inner_prod(cell_location - mPointOnPlane, mNormalToPlane);
00089                 if ( signed_distance > 0.0)
00090                 {
00091 
00092                     // For the closest point on the plane we travel from cell_location the signed_distance in the direction of -mNormalToPlane
00093                     c_vector<double, DIM> nearest_point = cell_location - signed_distance*mNormalToPlane;
00094                     p_node->rGetModifiableLocation() = nearest_point;
00095                 }
00096             }
00097         }
00098         else
00099         {
00100             assert(dynamic_cast<VertexBasedCellPopulation<DIM>*>(this->mpCellPopulation));
00101 
00102             VertexBasedCellPopulation<DIM>* pStaticCastCellPopulation = static_cast<VertexBasedCellPopulation<DIM>*>(this->mpCellPopulation);
00103 
00104             // Iterate over all nodes and update their positions according to the boundary conditions
00105             unsigned num_nodes = pStaticCastCellPopulation->GetNumNodes();
00106             for (unsigned node_index=0; node_index<num_nodes; node_index++)
00107             {
00108                 Node<DIM>* p_node = pStaticCastCellPopulation->GetNode(node_index);
00109                 c_vector<double, DIM> node_location = p_node->rGetLocation();
00110 
00111                 double signed_distance = inner_prod(node_location - mPointOnPlane, mNormalToPlane);
00112                 if (signed_distance > 0.0)
00113                 {
00114                     // For the closest point on the plane we travel from cell_location the signed_distance in the direction of -mNormalToPlane
00115                     c_vector<double, DIM> nearest_point = node_location - signed_distance*mNormalToPlane;
00116                     p_node->rGetModifiableLocation() = nearest_point;
00117                 }
00118             }
00119         }
00120     }
00121     else
00122     {
00123         // DIM == 1
00124         NEVER_REACHED;
00125         //PlaneBoundaryCondition::ImposeBoundaryCondition is not implemented in 1D
00126     }
00127 }
00128 
00129 template<unsigned DIM>
00130 bool PlaneBoundaryCondition<DIM>::VerifyBoundaryCondition()
00131 {
00132     bool condition_satisfied = true;
00133 
00134     if (DIM == 1)
00135     {
00136         EXCEPTION("PlaneBoundaryCondition is not implemented in 1D");
00137     }
00138     else
00139     {
00140         for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->mpCellPopulation->Begin();
00141              cell_iter != this->mpCellPopulation->End();
00142              ++cell_iter)
00143         {
00144             c_vector<double, DIM> cell_location = this->mpCellPopulation->GetLocationOfCellCentre(*cell_iter);
00145 
00146             if (inner_prod(cell_location - mPointOnPlane, mNormalToPlane) > 0.0)
00147             {
00148                 condition_satisfied = false;
00149                 break;
00150             }
00151         }
00152     }
00153 
00154     return condition_satisfied;
00155 }
00156 
00157 template<unsigned DIM>
00158 void PlaneBoundaryCondition<DIM>::OutputCellPopulationBoundaryConditionParameters(out_stream& rParamsFile)
00159 {
00160     *rParamsFile << "\t\t\t<PointOnPlane>";
00161     for (unsigned index=0; index != DIM-1U; index++) // Note: inequality avoids testing index < 0U when DIM=1
00162     {
00163         *rParamsFile << mPointOnPlane[index] << ",";
00164     }
00165     *rParamsFile << mPointOnPlane[DIM-1] << "</PointOnPlane>\n";
00166 
00167     *rParamsFile << "\t\t\t<NormalToPlane>";
00168      for (unsigned index=0; index != DIM-1U; index++) // Note: inequality avoids testing index < 0U when DIM=1
00169      {
00170          *rParamsFile << mNormalToPlane[index] << ",";
00171      }
00172      *rParamsFile << mNormalToPlane[DIM-1] << "</NormalToPlane>\n";
00173 
00174     // Call method on direct parent class
00175     AbstractCellPopulationBoundaryCondition<DIM>::OutputCellPopulationBoundaryConditionParameters(rParamsFile);
00176 }
00177 
00179 // Explicit instantiation
00181 
00182 template class PlaneBoundaryCondition<1>;
00183 template class PlaneBoundaryCondition<2>;
00184 template class PlaneBoundaryCondition<3>;
00185 
00186 // Serialization for Boost >= 1.36
00187 #include "SerializationExportWrapperForCpp.hpp"
00188 EXPORT_TEMPLATE_CLASS_SAME_DIMS(PlaneBoundaryCondition)