Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
ParallelCellsGenerator.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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
49template<class CELL_CYCLE_MODEL, unsigned DIM>
51{
52 friend class TestParallelCellsGenerator; // For testing.
53
54private:
55
62 c_vector<double, 2*DIM> GetArchiveBoundingBox(std::string archivePath);
63
64public:
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
80template<class CELL_CYCLE_MODEL, unsigned DIM>
81void 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
143template<class CELL_CYCLE_MODEL, unsigned DIM>
144c_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_ */
#define EXCEPTION(message)
static CellPropertyRegistry * Instance()
Definition Cell.hpp:92
Definition Node.hpp:59
c_vector< double, 2 *DIM > GetArchiveBoundingBox(std::string archivePath)
void GenerateParallelCells(std::string archivePath, std::vector< CellPtr > &cells, NodesOnlyMesh< DIM > &mesh, boost::shared_ptr< AbstractCellProperty > pCellProliferativeType=boost::shared_ptr< AbstractCellProperty >())