NodeBasedCellPopulationWithBuskeUpdate.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 #include "NodeBasedCellPopulationWithBuskeUpdate.hpp"
00029 
00030 #include "ReplicatableVector.hpp"
00031 #include "OdeLinearSystemSolver.hpp"
00032 
00033 template<unsigned DIM>
00034 NodeBasedCellPopulationWithBuskeUpdate<DIM>::NodeBasedCellPopulationWithBuskeUpdate(NodesOnlyMesh<DIM>& rMesh,
00035                                       std::vector<CellPtr>& rCells,
00036                                       const std::vector<unsigned> locationIndices,
00037                                       bool deleteMesh)
00038     : NodeBasedCellPopulation<DIM>(rMesh, rCells, locationIndices, deleteMesh)
00039 {
00040 }
00041 
00042 template<unsigned DIM>
00043 NodeBasedCellPopulationWithBuskeUpdate<DIM>::NodeBasedCellPopulationWithBuskeUpdate(NodesOnlyMesh<DIM>& rMesh)
00044     : NodeBasedCellPopulation<DIM>(rMesh)
00045 {
00046     // No Validate() because the cells are not associated with the cell population yet in archiving
00047 }
00048 
00049 template<unsigned DIM>
00050 void NodeBasedCellPopulationWithBuskeUpdate<DIM>::UpdateNodeLocations(const std::vector< c_vector<double, DIM> >& rNodeForces, double dt)
00051 {
00052     // Declare solver and give the size of the system and timestep
00053     unsigned system_size = rNodeForces.size()*DIM;
00054 
00055     OdeLinearSystemSolver solver(system_size, dt);
00056 
00057     // Set up the matrix
00058     Mat& r_matrix = solver.rGetLhsMatrix();
00059 
00060     // Initial condition
00061     Vec initial_condition = PetscTools::CreateAndSetVec(system_size, 0.0);
00062 
00063     // Then an rGetForceVector for RHS
00064     Vec& r_vector = solver.rGetForceVector();
00065 
00066     // Iterate over all nodes associated with real cells to construct the matrix A.
00067     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->Begin();
00068          cell_iter != this->End();
00069          ++cell_iter)
00070     {
00071         // Get index of node associated with cell
00072         unsigned node_index = this->mCellLocationMap[(*cell_iter).get()];
00073 
00074         // Get the location of this node
00075         c_vector<double, DIM> node_i_location = this->GetNode(node_index)->rGetLocation();
00076 
00077         // Get the radius of this cell
00078         double radius_of_cell_i = this->rGetMesh().GetCellRadius(node_index);
00079 
00080         // Get damping constant for node
00081         double damping_const = this->GetDampingConstant(node_index);
00082 
00083         // loop over neighbours to add contribution
00084 
00085         // Get the set of node indices corresponding to this cell's neighbours
00086         std::set<unsigned> neighbouring_node_indices = this->GetNeighbouringNodeIndices(node_index);
00087 
00088         for (std::set<unsigned>::iterator iter = neighbouring_node_indices.begin();
00089              iter != neighbouring_node_indices.end();
00090              ++iter)
00091         {
00092             unsigned neighbour_node_index = *iter;
00093 
00094             // Calculate Aij
00095             double Aij = 0.0;
00096 
00097             // Get the location of this node
00098             c_vector<double, DIM> node_j_location = this->GetNode(neighbour_node_index)->rGetLocation();
00099 
00100             // Get the unit vector parallel to the line joining the two nodes (assuming no periodicities etc.)
00101             c_vector<double, DIM> unit_vector = node_j_location - node_i_location;
00102 
00103             // Calculate the distance between the two nodes
00104             double dij = norm_2(unit_vector);
00105 
00106             unit_vector /= dij;
00107 
00108             // Get the radius of the cell corresponding to this node
00109             double radius_of_cell_j = this->rGetMesh().GetCellRadius(neighbour_node_index);
00110 
00111             if (dij < radius_of_cell_i + radius_of_cell_j)
00112             {
00113                 // ...then compute the adhesion force and add it to the vector of forces...
00114                 double xij = 0.5*(radius_of_cell_i*radius_of_cell_i - radius_of_cell_j*radius_of_cell_j + dij*dij)/dij;
00115 
00116                 Aij = M_PI*(radius_of_cell_i*radius_of_cell_i - xij*xij);
00117 
00118                 // This is contribution from the sum term in (A7)
00119                 for (unsigned i=0; i<DIM; i++)
00120                 {
00121                     PetscMatTools::AddToElement(r_matrix, DIM*neighbour_node_index+i, DIM*neighbour_node_index+i, -damping_const*Aij);
00122                     PetscMatTools::AddToElement(r_matrix, DIM*node_index+i, DIM*node_index+i, damping_const*Aij);
00123                 }
00124             }
00125         }
00126 
00127         // This is the standard contribution (i.e. not in the sum) in (A7)
00128         for (unsigned i=0; i<DIM; i++)
00129         {
00130             PetscMatTools::AddToElement(r_matrix, DIM*node_index+i, DIM*node_index+i, damping_const);
00131         }
00132 
00133         // Add current positions to initial_conditions and RHS vector
00134         c_vector<double, DIM> current_location = this->GetNode(node_index)->rGetLocation();
00135         c_vector<double, DIM> forces = rNodeForces[node_index];
00136 
00137         for (unsigned i=0; i<DIM; i++)
00138         {
00139             PetscVecTools::SetElement(initial_condition, DIM*node_index+i, current_location(i));
00140             PetscVecTools::SetElement(r_vector, DIM*node_index+i, forces(i));
00141         }
00142     }
00143     PetscMatTools::Finalise(r_matrix);
00144 
00145     solver.SetInitialConditionVector(initial_condition);
00146 
00147     // Solve to get solution at next timestep
00148     Vec soln_next_timestep = solver.SolveOneTimeStep();
00149 
00150     ReplicatableVector soln_next_timestep_repl(soln_next_timestep);
00151 
00152     // Iterate over all nodes associated with real cells to update the node locations
00153     for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->Begin();
00154          cell_iter != this->End();
00155          ++cell_iter)
00156     {
00157         // Get index of node associated with cell
00158         unsigned node_index = this->mCellLocationMap[(*cell_iter).get()];
00159 
00160         c_vector<double, DIM> new_node_location;
00161 
00162         // Get new node location
00163         for (unsigned i=0; i<DIM; i++)
00164         {
00165             new_node_location(i) = soln_next_timestep_repl[DIM*node_index+i];
00166         }
00167 
00168         // Create ChastePoint for new node location
00169         ChastePoint<DIM> new_point(new_node_location);
00170 
00171         // Move the node
00172         this->SetNode(node_index, new_point);
00173     }
00174 
00175     // Tidy up
00176     VecDestroy(initial_condition);
00177 }
00178 
00179 template<unsigned DIM>
00180 void NodeBasedCellPopulationWithBuskeUpdate<DIM>::OutputCellPopulationParameters(out_stream& rParamsFile)
00181 {
00182     // Currently no specific parameters to output all come from parent classes
00183 
00184     // Call method on direct parent class
00185     NodeBasedCellPopulation<DIM>::OutputCellPopulationParameters(rParamsFile);
00186 }
00187 
00188 
00189 
00191 // Explicit instantiation
00193 
00194 template class NodeBasedCellPopulationWithBuskeUpdate<1>;
00195 template class NodeBasedCellPopulationWithBuskeUpdate<2>;
00196 template class NodeBasedCellPopulationWithBuskeUpdate<3>;
00197 
00198 // Serialization for Boost >= 1.36
00199 #include "SerializationExportWrapperForCpp.hpp"
00200 EXPORT_TEMPLATE_CLASS_SAME_DIMS(NodeBasedCellPopulationWithBuskeUpdate)
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3