Chaste  Release::2018.1
ParallelCellsGenerator.hpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 // Implementation
80 template<class CELL_CYCLE_MODEL, unsigned DIM>
81 void ParallelCellsGenerator<CELL_CYCLE_MODEL, DIM>::GenerateParallelCells(std::string archivePath, std::vector<CellPtr>& cells, NodesOnlyMesh<DIM>& mesh, boost::shared_ptr<AbstractCellProperty> pCellProliferativeType)
82 {
83  // Get a bounding box for the archived nodes
84  c_vector<double, 2*DIM> base_bounding_box = GetArchiveBoundingBox(archivePath);
85 
86  // Add small factor to make sure the box is big enough to contain the initial population.
87  for (unsigned i=0; i<DIM; i++)
88  {
89  base_bounding_box[2*i] -= 1e-14;
90  base_bounding_box[2*i + 1] += 1e-14;
91  }
92 
93  // Construct box collection of NodesOnlyMesh
94  mesh.SetInitialBoxCollection(base_bounding_box, mesh.GetMaximumInteractionDistance());
95 
96  unsigned node_index = 0;
97 
98  std::ifstream infile(archivePath.c_str(), std::ios::in);
99 
100  std::string line;
101  std::getline(infile, line); // Get first line which is ignored as it is a header.
102 
103  while (std::getline(infile, line))
104  {
105  // Get line in archive file.
106  std::istringstream iss(line);
107 
108  // Extract a location.
109  c_vector<double, DIM> location;
110  for (unsigned k=0; k<DIM; k++)
111  {
112  iss >> location[k];
113  }
114 
115  // Add it to the mesh and make an appropriate cell if it is owned.
116  if (mesh.IsOwned(location))
117  {
118  // Make a node at this point.
119  Node<DIM>* p_node = new Node<DIM>(node_index, location); // Memory is managed by NodesOnlyMesh.
120  mesh.AddNode(p_node);
121 
122  // Make cell
123  CELL_CYCLE_MODEL* p_cell_cycle_model = new CELL_CYCLE_MODEL;
124  p_cell_cycle_model->SetDimension(DIM);
125 
126  // Wild type hard-coded for now.
127  boost::shared_ptr<AbstractCellProperty> p_state(CellPropertyRegistry::Instance()->Get<WildTypeCellMutationState>());
128  CellPtr p_cell(new Cell(p_state, p_cell_cycle_model));
129  p_cell->SetCellProliferativeType(pCellProliferativeType);
130  cells.push_back(p_cell);
131 
132  node_index++;
133  }
134  }
135 
136  infile.close();
137 
138  // Tidy up the mesh.
139  NodeMap map(mesh.GetMaximumNodeIndex()+1);
140  mesh.ReMesh(map);
141 }
142 
143 template<class CELL_CYCLE_MODEL, unsigned DIM>
144 c_vector<double, 2*DIM> ParallelCellsGenerator<CELL_CYCLE_MODEL, DIM>::GetArchiveBoundingBox(std::string archivePath)
145 {
146  c_vector<double, 2*DIM> bounding_box;
147  for (unsigned i=0; i<DIM; i++)
148  {
149  bounding_box[2*i] = DBL_MAX;
150  bounding_box[2*i + 1] = -DBL_MAX;
151  }
152 
153  std::ifstream infile(archivePath.c_str());
154 
155  std::string line;
156  std::getline(infile, line);
157 
158  std::istringstream iss(line);
159 
160  // Check file dimensions and template dimensions match.
161  unsigned num_lines;
162  unsigned file_dimension;
163 
164  iss >> num_lines >> file_dimension;
165 
166  if (file_dimension != DIM)
167  {
168  EXCEPTION("Space dimension of ParallelCellsGenerator and archive file do not match");
169  }
170 
171  while (std::getline(infile, line))
172  {
173  std::stringstream new_iss(line);
174  for (unsigned i=0; i<DIM; i++)
175  {
176  double point;
177  new_iss >> point;
178  bounding_box[2*i] = (point < bounding_box[2*i]) ? point : bounding_box[2*i];
179  bounding_box[2*i+1] = (point > bounding_box[2*i+1]) ? point : bounding_box[2*i+1];
180  }
181  }
182 
183  infile.close();
184 
185  return bounding_box;
186 }
187 #endif /* PARALLELCELLSGENERATOR_HPP_ */
Definition: Cell.hpp:89
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)