Chaste Release::3.1
MeshBasedCellPopulation.hpp
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 #ifndef MESHBASEDCELLPOPULATION_HPP_
00037 #define MESHBASEDCELLPOPULATION_HPP_
00038 
00039 #include <map>
00040 #include "AbstractCentreBasedCellPopulation.hpp"
00041 #include "MutableMesh.hpp"
00042 #include "VertexMesh.hpp"
00043 #include "TrianglesMeshReader.hpp"
00044 
00045 #include "ChasteSerialization.hpp"
00046 #include <boost/serialization/base_object.hpp>
00047 #include <boost/serialization/set.hpp>
00048 #include <boost/serialization/vector.hpp>
00049 
00050 
00058 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
00059 class MeshBasedCellPopulation : public AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>
00060 {
00061     friend class TestMeshBasedCellPopulation;
00062 private:
00064     friend class boost::serialization::access;
00076     template<class Archive>
00077     void serialize(Archive & archive, const unsigned int version)
00078     {
00079         archive & boost::serialization::base_object<AbstractCentreBasedCellPopulation<ELEMENT_DIM, SPACE_DIM> >(*this);
00080 
00081         /*
00082          * In its current form the code does not allow the direct serialization
00083          * of the VertexMesh class, so instead we delete mpVoronoiTessellation.
00084          */
00085         delete mpVoronoiTessellation;
00086         mpVoronoiTessellation = NULL;
00087 
00088         archive & mSpringRestLengths;
00089         archive & mUseAreaBasedDampingConstant;
00090         archive & mAreaBasedDampingConstantParameter;
00091         archive & mOutputVoronoiData;
00092         archive & mOutputCellPopulationVolumes;
00093         archive & mWriteVtkAsPoints;
00094         archive & mHasVariableRestLength;
00095 
00096 
00097         this->Validate();
00098     }
00099 
00100 protected:
00101 #define COVERAGE_IGNORE // Avoid prototypes being treated as code by gcov
00102 
00118     VertexMesh<ELEMENT_DIM, SPACE_DIM>* mpVoronoiTessellation;
00119 
00121     MutableMesh<ELEMENT_DIM, SPACE_DIM>* mpMutableMesh;
00122 
00127     bool mDeleteMesh;
00128 
00132     std::map<std::pair<unsigned,unsigned>, double> mSpringRestLengths;
00133 
00135     out_stream mpVizElementsFile;
00136 
00138     out_stream mpVoronoiFile;
00139 
00141     out_stream mpCellPopulationVolumesFile;
00142 
00144     bool mUseAreaBasedDampingConstant;
00145 
00147     double mAreaBasedDampingConstantParameter;
00148 
00150     bool mOutputVoronoiData;
00151 
00153     bool mOutputCellPopulationVolumes;
00154 
00156     bool mWriteVtkAsPoints;
00157 
00159     bool mHasVariableRestLength;
00160 
00162     std::set< std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > > mNodePairs;
00163 
00164 #undef COVERAGE_IGNORE // Avoid prototypes being treated as code by gcov
00165 
00171     virtual void UpdateGhostNodesAfterReMesh(NodeMap& rMap);
00172 
00177     virtual void Validate();
00178 
00179 public:
00180 #define COVERAGE_IGNORE // Avoid prototypes being treated as code by gcov
00181 
00193     MeshBasedCellPopulation(MutableMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00194                     std::vector<CellPtr>& rCells,
00195                     const std::vector<unsigned> locationIndices=std::vector<unsigned>(),
00196                     bool deleteMesh=false,
00197                     bool validate=true);
00198 
00204     MeshBasedCellPopulation(MutableMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00205 
00209     virtual ~MeshBasedCellPopulation();
00210 
00214     MutableMesh<ELEMENT_DIM, SPACE_DIM>& rGetMesh();
00215 
00219     const MutableMesh<ELEMENT_DIM, SPACE_DIM>& rGetMesh() const;
00220 
00222     bool UseAreaBasedDampingConstant();
00223 
00229     void SetOutputVoronoiData(bool outputVoronoiData);
00230 
00239     unsigned AddNode(Node<SPACE_DIM>* pNewNode);
00240 
00249     void SetNode(unsigned nodeIndex, ChastePoint<SPACE_DIM>& rNewLocation);
00250 
00258     double GetDampingConstant(unsigned nodeIndex);
00259 
00265     void SetAreaBasedDampingConstant(bool useAreaBasedDampingConstant);
00266 
00279     virtual unsigned RemoveDeadCells();
00280 
00293     virtual CellPtr AddCell(CellPtr pNewCell, const c_vector<double,SPACE_DIM>& rCellDivisionVector, CellPtr pParentCell);
00294 
00301     void CreateOutputFiles(const std::string& rDirectory, bool cleanOutputDirectory);
00302 
00306     void CloseOutputFiles();
00307 
00311     void WriteResultsToFiles();
00312 
00320     virtual void Update(bool hasHadBirthsOrDeaths=true);
00321 
00327     void TessellateIfNeeded();
00328 
00336     Node<SPACE_DIM>* GetNode(unsigned index);
00337 
00343     unsigned GetNumNodes();
00344 
00348     virtual void WriteVtkResultsToFile();
00349 
00355     void WriteVoronoiResultsToFile();
00356 
00361     void WriteCellPopulationVolumeResultsToFile();
00362 
00366     void WriteCellVolumeResultsToFile();
00367 
00373     double GetVolumeOfCell(CellPtr pCell);
00374 
00378     void CreateVoronoiTessellation();
00379 
00383     VertexMesh<ELEMENT_DIM, SPACE_DIM>* GetVoronoiTessellation();
00384 
00397     double GetVolumeOfVoronoiElement(unsigned index);
00398 
00409     double GetSurfaceAreaOfVoronoiElement(unsigned index);
00410 
00422     double GetVoronoiEdgeLength(unsigned index1, unsigned index2);
00423 
00433     double GetWidth(const unsigned& rDimension);
00434 
00440     class SpringIterator
00441     {
00442     public:
00443 
00447         Node<SPACE_DIM>* GetNodeA();
00448 
00452         Node<SPACE_DIM>* GetNodeB();
00453 
00457         CellPtr GetCellA();
00458 
00462         CellPtr GetCellB();
00463 
00469         bool operator!=(const MeshBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>::SpringIterator& rOther);
00470 
00474         SpringIterator& operator++();
00475 
00482         SpringIterator(MeshBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>& rCellPopulation, typename MutableMesh<ELEMENT_DIM, SPACE_DIM>::EdgeIterator edgeIter);
00483 
00484     private:
00485 
00487         std::set<std::set<unsigned> > mSpringsVisited;
00488 
00490         MeshBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>& mrCellPopulation;
00491 
00493         typename MutableMesh<ELEMENT_DIM, SPACE_DIM>::EdgeIterator mEdgeIter;
00494     };
00495 
00499     SpringIterator SpringsBegin();
00500 
00504     SpringIterator SpringsEnd();
00505 
00509     void CheckCellPointers();
00510 
00514     double GetAreaBasedDampingConstantParameter();
00515 
00521     void SetAreaBasedDampingConstantParameter(double areaBasedDampingConstantParameter);
00522 
00526     bool GetOutputVoronoiData();
00527 
00531     bool GetOutputCellPopulationVolumes();
00532 
00538     void SetOutputCellPopulationVolumes(bool outputCellPopulationVolumes);
00539 
00545     std::set< std::pair<Node<SPACE_DIM>*, Node<SPACE_DIM>* > >& rGetNodePairs();
00546 
00552     void OutputCellPopulationParameters(out_stream& rParamsFile);
00553 
00559     void SetWriteVtkAsPoints(bool writeVtkAsPoints);
00560 
00564     bool GetWriteVtkAsPoints();
00565 
00572     std::set<unsigned> GetNeighbouringNodeIndices(unsigned index);
00573 
00577     void CalculateRestLengths();
00578 
00585     double GetRestLength(unsigned indexA, unsigned indexB);
00586 };
00587 #undef COVERAGE_IGNORE // Avoid prototypes being treated as code by gcov
00588 
00589 #include "SerializationExportWrapper.hpp"
00590 EXPORT_TEMPLATE_CLASS_ALL_DIMS(MeshBasedCellPopulation)
00591 
00592 namespace boost
00593 {
00594 namespace serialization
00595 {
00599 template<class Archive, unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00600 inline void save_construct_data(
00601     Archive & ar, const MeshBasedCellPopulation<ELEMENT_DIM, SPACE_DIM> * t, const BOOST_PFTO unsigned int file_version)
00602 {
00603     // Save data required to construct instance
00604     const MutableMesh<ELEMENT_DIM, SPACE_DIM>* p_mesh = &(t->rGetMesh());
00605     ar & p_mesh;
00606 }
00607 
00612 template<class Archive,  unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00613 inline void load_construct_data(
00614     Archive & ar, MeshBasedCellPopulation<ELEMENT_DIM, SPACE_DIM> * t, const unsigned int file_version)
00615 {
00616     // Retrieve data from archive required to construct new instance
00617     MutableMesh<ELEMENT_DIM, SPACE_DIM>* p_mesh;
00618     ar >> p_mesh;
00619 
00620     // Invoke inplace constructor to initialise instance
00621     ::new(t)MeshBasedCellPopulation<ELEMENT_DIM, SPACE_DIM>(*p_mesh);
00622 }
00623 }
00624 } // namespace ...
00625 
00626 #endif /*MESHBASEDCELLPOPULATION_HPP_*/