ParallelCellsGenerator.hpp

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 #ifndef PARALLELCELLSGENERATOR_HPP_
00037 #define PARALLELCELLSGENERATOR_HPP_
00038 
00039 #include <vector>
00040 #include "Cell.hpp"
00041 #include "WildTypeCellMutationState.hpp"
00042 #include "AbstractCellProperty.hpp"
00043 #include "NodesOnlyMesh.hpp"
00044 
00049 template<class CELL_CYCLE_MODEL, unsigned DIM>
00050 class ParallelCellsGenerator
00051 {
00052     friend class TestParallelCellsGenerator;    // For testing.
00053 
00054 private:
00055 
00062     c_vector<double, 2*DIM> GetArchiveBoundingBox(std::string archivePath);
00063 
00064 public:
00065 
00073     void GenerateParallelCells( std::string archivePath,
00074                                 std::vector<CellPtr>& cells,
00075                                 NodesOnlyMesh<DIM>& mesh,
00076                                 boost::shared_ptr<AbstractCellProperty> pCellProliferativeType=boost::shared_ptr<AbstractCellProperty>());
00077 
00078 };
00079 
00080 // Implementation
00081 template<class CELL_CYCLE_MODEL, unsigned DIM>
00082 void ParallelCellsGenerator<CELL_CYCLE_MODEL, DIM>::GenerateParallelCells(std::string archivePath, std::vector<CellPtr>& cells, NodesOnlyMesh<DIM>& mesh, boost::shared_ptr<AbstractCellProperty> pCellProliferativeType)
00083 {
00084     // Get a bounding box for the archived nodes
00085     c_vector<double, 2*DIM> base_bounding_box = GetArchiveBoundingBox(archivePath);
00086 
00087     // Add small factor to make sure the box is big enough to contain the initial population.
00088     for (unsigned i=0; i<DIM; i++)
00089     {
00090         base_bounding_box[2*i] -= 1e-14;
00091         base_bounding_box[2*i + 1] += 1e-14;
00092     }
00093 
00094     // Construct box collection of NodesOnlyMesh
00095     mesh.SetInitialBoxCollection(base_bounding_box, mesh.GetMaximumInteractionDistance());
00096 
00097     unsigned node_index = 0;
00098 
00099     std::ifstream infile(archivePath.c_str(), std::ios::in);
00100 
00101     std::string line;
00102     std::getline(infile, line);    // Get first line which is ignored as it is a header.
00103 
00104     while (std::getline(infile, line))
00105     {
00106         // Get line in archive file.
00107         std::istringstream iss(line);
00108 
00109         // Extract a location.
00110         c_vector<double, DIM> location;
00111         for (unsigned k=0; k<DIM; k++)
00112         {
00113             iss >> location[k];
00114         }
00115 
00116         // Add it to the mesh and make an appropriate cell if it is owned.
00117         if (mesh.IsOwned(location))
00118         {
00119             // Make a node at this point.
00120             Node<DIM>* p_node = new Node<DIM>(node_index, location);    // Memory is managed by NodesOnlyMesh.
00121             mesh.AddNode(p_node);
00122 
00123             // Make cell
00124             CELL_CYCLE_MODEL* p_cell_cycle_model = new CELL_CYCLE_MODEL;
00125             p_cell_cycle_model->SetDimension(DIM);
00126 
00127             // Wild type hard-coded for now.
00128             boost::shared_ptr<AbstractCellProperty> p_state(CellPropertyRegistry::Instance()->Get<WildTypeCellMutationState>());
00129             CellPtr p_cell(new Cell(p_state, p_cell_cycle_model));
00130             p_cell->SetCellProliferativeType(pCellProliferativeType);
00131             cells.push_back(p_cell);
00132 
00133             node_index++;
00134         }
00135     }
00136 
00137     infile.close();
00138 
00139     // Tidy up the mesh.
00140     NodeMap map(mesh.GetMaximumNodeIndex()+1);
00141     mesh.ReMesh(map);
00142 }
00143 
00144 template<class CELL_CYCLE_MODEL, unsigned DIM>
00145 c_vector<double, 2*DIM> ParallelCellsGenerator<CELL_CYCLE_MODEL, DIM>::GetArchiveBoundingBox(std::string archivePath)
00146 {
00147     c_vector<double, 2*DIM> bounding_box;
00148     for (unsigned i=0; i<DIM; i++)
00149     {
00150         bounding_box[2*i] = DBL_MAX;
00151         bounding_box[2*i + 1] = -DBL_MAX;
00152     }
00153 
00154     std::ifstream infile(archivePath.c_str());
00155 
00156     std::string line;
00157     std::getline(infile, line);
00158 
00159     std::istringstream iss(line);
00160 
00161     // Check file dimensions and template dimensions match.
00162     unsigned num_lines;
00163     unsigned file_dimension;
00164 
00165     iss >> num_lines >> file_dimension;
00166 
00167     if (file_dimension != DIM)
00168     {
00169         EXCEPTION("Space dimension of ParallelCellsGenerator and archive file do not match");
00170     }
00171 
00172     while (std::getline(infile, line))
00173     {
00174         std::stringstream new_iss(line);
00175         for (unsigned i=0; i<DIM; i++)
00176         {
00177             double point;
00178             new_iss >> point;
00179             bounding_box[2*i] = (point < bounding_box[2*i]) ? point : bounding_box[2*i];
00180             bounding_box[2*i+1] = (point > bounding_box[2*i+1]) ? point : bounding_box[2*i+1];
00181         }
00182     }
00183 
00184     infile.close();
00185 
00186     return bounding_box;
00187 }
00188 #endif /* PARALLELCELLSGENERATOR_HPP_ */

Generated by  doxygen 1.6.2