Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
CellPopulationAdjacencyMatrixWriter.cpp
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#include "CellPopulationAdjacencyMatrixWriter.hpp"
37
38#include "AbstractCellPopulation.hpp"
39#include "MeshBasedCellPopulation.hpp"
40#include "CaBasedCellPopulation.hpp"
41#include "NodeBasedCellPopulation.hpp"
42#include "PottsBasedCellPopulation.hpp"
43#include "VertexBasedCellPopulation.hpp"
44#include "ImmersedBoundaryCellPopulation.hpp"
45
46#include "CellLabel.hpp"
47
48template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
53
54template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
56{
57 // Make sure the cell population is updated
59 pCellPopulation->Update();
60
61 unsigned num_cells = pCellPopulation->GetNumRealCells();
62
63 // Store a map between cells numbered 1 to n and location indices
64 std::map<unsigned,unsigned> local_cell_id_location_index_map;
65
66 unsigned local_cell_id = 0;
67 for (typename AbstractCellPopulation<SPACE_DIM, SPACE_DIM>::Iterator cell_iter = pCellPopulation->Begin();
68 cell_iter != pCellPopulation->End();
69 ++cell_iter)
70 {
71 local_cell_id_location_index_map[pCellPopulation->GetLocationIndexUsingCell(*cell_iter)] = local_cell_id;
72 local_cell_id++;
73 }
74 assert(local_cell_id == num_cells);
75
76 // Iterate over cells and calculate the adjacency matrix (stored as a long vector)
77 std::vector<double> adjacency_matrix(num_cells*num_cells);
78 for (unsigned i=0; i<num_cells*num_cells; i++)
79 {
80 adjacency_matrix[i] = 0;
81 }
82
83 for (typename AbstractCellPopulation<SPACE_DIM, SPACE_DIM>::Iterator cell_iter = pCellPopulation->Begin();
84 cell_iter != pCellPopulation->End();
85 ++cell_iter)
86 {
87 // Determine whether this cell is labelled
88 bool cell_is_labelled = cell_iter->template HasCellProperty<CellLabel>();
89
90 // Get the location index corresponding to this cell
91 unsigned index = pCellPopulation->GetLocationIndexUsingCell(*cell_iter);
92
93 // Get the set of neighbouring location indices
94 std::set<unsigned> neighbour_indices = pCellPopulation->GetNeighbouringLocationIndices(*cell_iter);
95
96 // If this cell has any neighbours (as defined by mesh/population/interaction distance)...
97 if (!neighbour_indices.empty())
98 {
99 unsigned local_cell_index = local_cell_id_location_index_map[index];
100
101 for (std::set<unsigned>::iterator neighbour_iter = neighbour_indices.begin();
102 neighbour_iter != neighbour_indices.end();
103 ++neighbour_iter)
104 {
105 // If both cell_iter and p_neighbour_cell are not labelled, then set type_of_link to 1
106 unsigned type_of_link = 1;
107
108 // Determine whether this neighbour is labelled
109 CellPtr p_neighbour_cell = pCellPopulation->GetCellUsingLocationIndex(*neighbour_iter);
110 bool neighbour_is_labelled = p_neighbour_cell->template HasCellProperty<CellLabel>();
111
112 if (cell_is_labelled != neighbour_is_labelled)
113 {
114 // Here cell_iter is labelled but p_neighbour_cell is not, or vice versa, so set type_of_link to 3
115 type_of_link = 3;
116 }
117 else if (cell_is_labelled)
118 {
119 // Here both cell_iter and p_neighbour_cell are labelled, so set type_of_link to 2
120 type_of_link = 2;
121 }
122
123 unsigned local_neighbour_index = local_cell_id_location_index_map[*neighbour_iter];
124 adjacency_matrix[local_cell_index + num_cells*local_neighbour_index] = type_of_link;
125 adjacency_matrix[num_cells*local_cell_index + local_neighbour_index] = type_of_link;
126 }
127 }
128 }
129
130 // Output the number of cells and the elements of the adjacency matrix
131 *this->mpOutStream << num_cells << "\t";
132 for (unsigned i=0; i<num_cells*num_cells; i++)
133 {
134 *this->mpOutStream << adjacency_matrix[i] << "\t";
135 }
136}
137
138template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
140{
141 // Make sure the cell population is updated
143 pCellPopulation->Update();
144
145 unsigned num_cells = pCellPopulation->GetNumRealCells();
146
147 // Store a map between cells numbered 1 to n and location indices
148 std::map<unsigned,unsigned> local_cell_id_location_index_map;
149
150 unsigned local_cell_id = 0;
151 for (typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator cell_iter = pCellPopulation->Begin();
152 cell_iter != pCellPopulation->End();
153 ++cell_iter)
154 {
155 local_cell_id_location_index_map[pCellPopulation->GetLocationIndexUsingCell(*cell_iter)] = local_cell_id;
156 local_cell_id++;
157 }
158 assert(local_cell_id == num_cells);
159
160 // Iterate over cells and calculate the adjacency matrix (stored as a long vector)
161 std::vector<double> adjacency_matrix(num_cells*num_cells);
162 for (unsigned i=0; i<num_cells*num_cells; i++)
163 {
164 adjacency_matrix[i] = 0;
165 }
166
167 for (typename AbstractCellPopulation<ELEMENT_DIM, SPACE_DIM>::Iterator cell_iter = pCellPopulation->Begin();
168 cell_iter != pCellPopulation->End();
169 ++cell_iter)
170 {
171 // Determine whether this cell is labelled
172 bool cell_is_labelled = cell_iter->template HasCellProperty<CellLabel>();
173
174 // Get the location index corresponding to this cell
175 unsigned index = pCellPopulation->GetLocationIndexUsingCell(*cell_iter);
176
177 // Get the set of neighbouring location indices
178 std::set<unsigned> neighbour_indices = pCellPopulation->GetNeighbouringLocationIndices(*cell_iter);
179
180 // If this cell has any neighbours (as defined by mesh/population/interaction distance)...
181 if (!neighbour_indices.empty())
182 {
183 unsigned local_cell_index = local_cell_id_location_index_map[index];
184
185 for (std::set<unsigned>::iterator neighbour_iter = neighbour_indices.begin();
186 neighbour_iter != neighbour_indices.end();
187 ++neighbour_iter)
188 {
189 // If both cell_iter and p_neighbour_cell are not labelled, then set type_of_link to 1
190 unsigned type_of_link = 1;
191
192 // Determine whether this neighbour is labelled
193 CellPtr p_neighbour_cell = pCellPopulation->GetCellUsingLocationIndex(*neighbour_iter);
194 bool neighbour_is_labelled = p_neighbour_cell->template HasCellProperty<CellLabel>();
195
196 if (cell_is_labelled != neighbour_is_labelled)
197 {
198 // Here cell_iter is labelled but p_neighbour_cell is not, or vice versa, so set type_of_link to 3
199 type_of_link = 3;
200 }
201 else if (cell_is_labelled)
202 {
203 // Here both cell_iter and p_neighbour_cell are labelled, so set type_of_link to 2
204 type_of_link = 2;
205 }
206
207 unsigned local_neighbour_index = local_cell_id_location_index_map[*neighbour_iter];
208 adjacency_matrix[local_cell_index + num_cells*local_neighbour_index] = type_of_link;
209 adjacency_matrix[num_cells*local_cell_index + local_neighbour_index] = type_of_link;
210 }
211 }
212 }
213
214 // Output the number of cells and the elements of the adjacency matrix
215 *this->mpOutStream << num_cells << "\t";
216 for (unsigned i=0; i<num_cells*num_cells; i++)
217 {
218 *this->mpOutStream << adjacency_matrix[i] << "\t";
219 }
220}
221
222template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
226
227template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
229{
230 VisitAnyPopulation(pCellPopulation);
231}
232
233template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
235{
236 VisitAnyPopulation(pCellPopulation);
237}
238
239template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
241{
242 VisitAnyPopulation(pCellPopulation);
243}
244
245template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
247{
248 VisitAnyPopulation(pCellPopulation);
249}
250
251// Explicit instantiation
258
260// Declare identifier for the serializer
#define EXPORT_TEMPLATE_CLASS_ALL_DIMS(CLASS)
unsigned GetLocationIndexUsingCell(CellPtr pCell)
virtual std::set< unsigned > GetNeighbouringLocationIndices(CellPtr pCell)=0
virtual void Update(bool hasHadBirthsOrDeaths=true)=0
virtual CellPtr GetCellUsingLocationIndex(unsigned index)
virtual std::set< unsigned > GetNeighbouringLocationIndices(CellPtr pCell)
void VisitAnyPopulation(AbstractCellPopulation< SPACE_DIM, SPACE_DIM > *pCellPopulation)
virtual void Visit(MeshBasedCellPopulation< ELEMENT_DIM, SPACE_DIM > *pCellPopulation)
virtual void Update(bool hasHadBirthsOrDeaths=true)