PlaneBoundaryCondition.cpp

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

Generated by  doxygen 1.6.2