Chaste  Release::3.4
ParallelCellsGenerator.hpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef PARALLELCELLSGENERATOR_HPP_
37 #define PARALLELCELLSGENERATOR_HPP_
38 
39 #include <vector>
40 #include "Cell.hpp"
41 #include "WildTypeCellMutationState.hpp"
42 #include "AbstractCellProperty.hpp"
43 #include "NodesOnlyMesh.hpp"
44 
49 template<class CELL_CYCLE_MODEL, unsigned DIM>
51 {
52  friend class TestParallelCellsGenerator; // For testing.
53 
54 private:
55 
62  c_vector<double, 2*DIM> GetArchiveBoundingBox(std::string archivePath);
63 
64 public:
65 
73  void GenerateParallelCells( std::string archivePath,
74  std::vector<CellPtr>& cells,
76  boost::shared_ptr<AbstractCellProperty> pCellProliferativeType=boost::shared_ptr<AbstractCellProperty>());
77 
78 };
79 
80 // Implementation
81 template<class CELL_CYCLE_MODEL, unsigned DIM>
82 void ParallelCellsGenerator<CELL_CYCLE_MODEL, DIM>::GenerateParallelCells(std::string archivePath, std::vector<CellPtr>& cells, NodesOnlyMesh<DIM>& mesh, boost::shared_ptr<AbstractCellProperty> pCellProliferativeType)
83 {
84  // Get a bounding box for the archived nodes
85  c_vector<double, 2*DIM> base_bounding_box = GetArchiveBoundingBox(archivePath);
86 
87  // Add small factor to make sure the box is big enough to contain the initial population.
88  for (unsigned i=0; i<DIM; i++)
89  {
90  base_bounding_box[2*i] -= 1e-14;
91  base_bounding_box[2*i + 1] += 1e-14;
92  }
93 
94  // Construct box collection of NodesOnlyMesh
95  mesh.SetInitialBoxCollection(base_bounding_box, mesh.GetMaximumInteractionDistance());
96 
97  unsigned node_index = 0;
98 
99  std::ifstream infile(archivePath.c_str(), std::ios::in);
100 
101  std::string line;
102  std::getline(infile, line); // Get first line which is ignored as it is a header.
103 
104  while (std::getline(infile, line))
105  {
106  // Get line in archive file.
107  std::istringstream iss(line);
108 
109  // Extract a location.
110  c_vector<double, DIM> location;
111  for (unsigned k=0; k<DIM; k++)
112  {
113  iss >> location[k];
114  }
115 
116  // Add it to the mesh and make an appropriate cell if it is owned.
117  if (mesh.IsOwned(location))
118  {
119  // Make a node at this point.
120  Node<DIM>* p_node = new Node<DIM>(node_index, location); // Memory is managed by NodesOnlyMesh.
121  mesh.AddNode(p_node);
122 
123  // Make cell
124  CELL_CYCLE_MODEL* p_cell_cycle_model = new CELL_CYCLE_MODEL;
125  p_cell_cycle_model->SetDimension(DIM);
126 
127  // Wild type hard-coded for now.
128  boost::shared_ptr<AbstractCellProperty> p_state(CellPropertyRegistry::Instance()->Get<WildTypeCellMutationState>());
129  CellPtr p_cell(new Cell(p_state, p_cell_cycle_model));
130  p_cell->SetCellProliferativeType(pCellProliferativeType);
131  cells.push_back(p_cell);
132 
133  node_index++;
134  }
135  }
136 
137  infile.close();
138 
139  // Tidy up the mesh.
140  NodeMap map(mesh.GetMaximumNodeIndex()+1);
141  mesh.ReMesh(map);
142 }
143 
144 template<class CELL_CYCLE_MODEL, unsigned DIM>
145 c_vector<double, 2*DIM> ParallelCellsGenerator<CELL_CYCLE_MODEL, DIM>::GetArchiveBoundingBox(std::string archivePath)
146 {
147  c_vector<double, 2*DIM> bounding_box;
148  for (unsigned i=0; i<DIM; i++)
149  {
150  bounding_box[2*i] = DBL_MAX;
151  bounding_box[2*i + 1] = -DBL_MAX;
152  }
153 
154  std::ifstream infile(archivePath.c_str());
155 
156  std::string line;
157  std::getline(infile, line);
158 
159  std::istringstream iss(line);
160 
161  // Check file dimensions and template dimensions match.
162  unsigned num_lines;
163  unsigned file_dimension;
164 
165  iss >> num_lines >> file_dimension;
166 
167  if (file_dimension != DIM)
168  {
169  EXCEPTION("Space dimension of ParallelCellsGenerator and archive file do not match");
170  }
171 
172  while (std::getline(infile, line))
173  {
174  std::stringstream new_iss(line);
175  for (unsigned i=0; i<DIM; i++)
176  {
177  double point;
178  new_iss >> point;
179  bounding_box[2*i] = (point < bounding_box[2*i]) ? point : bounding_box[2*i];
180  bounding_box[2*i+1] = (point > bounding_box[2*i+1]) ? point : bounding_box[2*i+1];
181  }
182  }
183 
184  infile.close();
185 
186  return bounding_box;
187 }
188 #endif /* PARALLELCELLSGENERATOR_HPP_ */
Definition: Cell.hpp:77
virtual unsigned GetMaximumNodeIndex()
Definition: Node.hpp:58
#define EXCEPTION(message)
Definition: Exception.hpp:143
void ReMesh(NodeMap &rMap)
static CellPropertyRegistry * Instance()
double GetMaximumInteractionDistance()
void SetInitialBoxCollection(const c_vector< double, 2 *SPACE_DIM > domainSize, double maxInteractionDistance)
unsigned AddNode(Node< SPACE_DIM > *pNewNode)
bool IsOwned(c_vector< double, SPACE_DIM > &location)
void GenerateParallelCells(std::string archivePath, std::vector< CellPtr > &cells, NodesOnlyMesh< DIM > &mesh, boost::shared_ptr< AbstractCellProperty > pCellProliferativeType=boost::shared_ptr< AbstractCellProperty >())
c_vector< double, 2 *DIM > GetArchiveBoundingBox(std::string archivePath)