Chaste Release::3.1
NodeBasedCellPopulationWithBuskeUpdate.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 #include "NodeBasedCellPopulationWithBuskeUpdate.hpp"
00036 
00037 #include "ReplicatableVector.hpp"
00038 #include "OdeLinearSystemSolver.hpp"
00039 
00040 template<unsigned DIM>
00041 NodeBasedCellPopulationWithBuskeUpdate<DIM>::NodeBasedCellPopulationWithBuskeUpdate(NodesOnlyMesh<DIM>& rMesh,
00042                                       std::vector<CellPtr>& rCells,
00043                                       const std::vector<unsigned> locationIndices,
00044                                       bool deleteMesh)
00045     : NodeBasedCellPopulation<DIM>(rMesh, rCells, locationIndices, deleteMesh)
00046 {
00047 }
00048 
00049 template<unsigned DIM>
00050 NodeBasedCellPopulationWithBuskeUpdate<DIM>::NodeBasedCellPopulationWithBuskeUpdate(NodesOnlyMesh<DIM>& rMesh)
00051     : NodeBasedCellPopulation<DIM>(rMesh)
00052 {
00053     // No Validate() because the cells are not associated with the cell population yet in archiving
00054 }
00055 
00056 template<unsigned DIM>
00057 void NodeBasedCellPopulationWithBuskeUpdate<DIM>::UpdateNodeLocations(const std::vector< c_vector<double, DIM> >& rNodeForces, double dt)
00058 {
00059     // Declare solver and give the size of the system and timestep
00060     unsigned system_size = rNodeForces.size()*DIM;
00061 
00062     OdeLinearSystemSolver solver(system_size, dt);
00063 
00064     // Set up the matrix
00065     Mat& r_matrix = solver.rGetLhsMatrix();
00066 
00067     // Initial condition
00068     Vec initial_condition = PetscTools::CreateAndSetVec(system_size, 0.0);
00069 
00070     // Then an rGetForceVector for RHS
00071     Vec& r_vector = solver.rGetForceVector();
00072 
00073     // Iterate over all nodes associated with real cells to construct the matrix A.
00074     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->Begin();
00075          cell_iter != this->End();
00076          ++cell_iter)
00077     {
00078         // Get index of node associated with cell
00079         unsigned node_index = this->GetLocationIndexUsingCell((*cell_iter));
00080 
00081         // Get the location of this node
00082         c_vector<double, DIM> node_i_location = this->GetNode(node_index)->rGetLocation();
00083 
00084         // Get the radius of this cell
00085         double radius_of_cell_i = this->rGetMesh().GetCellRadius(node_index);
00086 
00087         // Get damping constant for node
00088         double damping_const = this->GetDampingConstant(node_index);
00089 
00090         // loop over neighbours to add contribution
00091 
00092         // Get the set of node indices corresponding to this cell's neighbours
00093         std::set<unsigned> neighbouring_node_indices = this->GetNeighbouringNodeIndices(node_index);
00094 
00095         for (std::set<unsigned>::iterator iter = neighbouring_node_indices.begin();
00096              iter != neighbouring_node_indices.end();
00097              ++iter)
00098         {
00099             unsigned neighbour_node_index = *iter;
00100 
00101             // Calculate Aij
00102             double Aij = 0.0;
00103 
00104             // Get the location of this node
00105             c_vector<double, DIM> node_j_location = this->GetNode(neighbour_node_index)->rGetLocation();
00106 
00107             // Get the unit vector parallel to the line joining the two nodes (assuming no periodicities etc.)
00108             c_vector<double, DIM> unit_vector = node_j_location - node_i_location;
00109 
00110             // Calculate the distance between the two nodes
00111             double dij = norm_2(unit_vector);
00112 
00113             unit_vector /= dij;
00114 
00115             // Get the radius of the cell corresponding to this node
00116             double radius_of_cell_j = this->rGetMesh().GetCellRadius(neighbour_node_index);
00117 
00118             if (dij < radius_of_cell_i + radius_of_cell_j)
00119             {
00120                 // ...then compute the adhesion force and add it to the vector of forces...
00121                 double xij = 0.5*(radius_of_cell_i*radius_of_cell_i - radius_of_cell_j*radius_of_cell_j + dij*dij)/dij;
00122 
00123                 Aij = M_PI*(radius_of_cell_i*radius_of_cell_i - xij*xij);
00124 
00125                 // This is contribution from the sum term in (A7)
00126                 for (unsigned i=0; i<DIM; i++)
00127                 {
00128                     PetscMatTools::AddToElement(r_matrix, DIM*neighbour_node_index+i, DIM*neighbour_node_index+i, -damping_const*Aij);
00129                     PetscMatTools::AddToElement(r_matrix, DIM*node_index+i, DIM*node_index+i, damping_const*Aij);
00130                 }
00131             }
00132         }
00133 
00134         // This is the standard contribution (i.e. not in the sum) in (A7)
00135         for (unsigned i=0; i<DIM; i++)
00136         {
00137             PetscMatTools::AddToElement(r_matrix, DIM*node_index+i, DIM*node_index+i, damping_const);
00138         }
00139 
00140         // Add current positions to initial_conditions and RHS vector
00141         c_vector<double, DIM> current_location = this->GetNode(node_index)->rGetLocation();
00142         c_vector<double, DIM> forces = rNodeForces[node_index];
00143 
00144         for (unsigned i=0; i<DIM; i++)
00145         {
00146             PetscVecTools::SetElement(initial_condition, DIM*node_index+i, current_location(i));
00147             PetscVecTools::SetElement(r_vector, DIM*node_index+i, forces(i));
00148         }
00149     }
00150     PetscMatTools::Finalise(r_matrix);
00151 
00152     solver.SetInitialConditionVector(initial_condition);
00153 
00154     // Solve to get solution at next timestep
00155     Vec soln_next_timestep = solver.SolveOneTimeStep();
00156 
00157     ReplicatableVector soln_next_timestep_repl(soln_next_timestep);
00158 
00159     // Iterate over all nodes associated with real cells to update the node locations
00160     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->Begin();
00161          cell_iter != this->End();
00162          ++cell_iter)
00163     {
00164         // Get index of node associated with cell
00165         unsigned node_index = this->GetLocationIndexUsingCell((*cell_iter));
00166 
00167         c_vector<double, DIM> new_node_location;
00168 
00169         // Get new node location
00170         for (unsigned i=0; i<DIM; i++)
00171         {
00172             new_node_location(i) = soln_next_timestep_repl[DIM*node_index+i];
00173         }
00174 
00175         // Create ChastePoint for new node location
00176         ChastePoint<DIM> new_point(new_node_location);
00177 
00178         // Move the node
00179         this->SetNode(node_index, new_point);
00180     }
00181 
00182     // Tidy up
00183     PetscTools::Destroy(initial_condition);
00184 }
00185 
00186 template<unsigned DIM>
00187 void NodeBasedCellPopulationWithBuskeUpdate<DIM>::OutputCellPopulationParameters(out_stream& rParamsFile)
00188 {
00189     // Currently no specific parameters to output all come from parent classes
00190 
00191     // Call method on direct parent class
00192     NodeBasedCellPopulation<DIM>::OutputCellPopulationParameters(rParamsFile);
00193 }
00194 
00195 
00196 
00198 // Explicit instantiation
00200 
00201 template class NodeBasedCellPopulationWithBuskeUpdate<1>;
00202 template class NodeBasedCellPopulationWithBuskeUpdate<2>;
00203 template class NodeBasedCellPopulationWithBuskeUpdate<3>;
00204 
00205 // Serialization for Boost >= 1.36
00206 #include "SerializationExportWrapperForCpp.hpp"
00207 EXPORT_TEMPLATE_CLASS_SAME_DIMS(NodeBasedCellPopulationWithBuskeUpdate)