Chaste Commit::9e4a273f0754a391514ab12ed9d4a38fc9933db2
NodeBasedCellPopulationWithParticles.cpp
1/*
2
3Copyright (c) 2005-2026, 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 "NodeBasedCellPopulationWithParticles.hpp"
37#include "VtkMeshWriter.hpp"
38
39template<unsigned DIM>
41 std::vector<CellPtr>& rCells,
42 const std::vector<unsigned> locationIndices,
43 bool deleteMesh)
44 : NodeBasedCellPopulation<DIM>(rMesh, rCells, locationIndices, deleteMesh, false)
45{
47 if (!locationIndices.empty())
48 {
49 // Create a set of node indices corresponding to particles
50 std::set<unsigned> node_indices;
51 std::set<unsigned> location_indices;
52 std::set<unsigned> particle_indices;
53
54 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = rMesh.GetNodeIteratorBegin();
55 node_iter != rMesh.GetNodeIteratorEnd();
56 ++node_iter)
57 {
58 node_indices.insert(node_iter->GetIndex());
59 }
60 for (unsigned i=0; i<locationIndices.size(); i++)
61 {
62 location_indices.insert(locationIndices[i]);
63 }
64
65 std::set_difference(node_indices.begin(), node_indices.end(),
66 location_indices.begin(), location_indices.end(),
67 std::inserter(particle_indices, particle_indices.begin()));
68
69 // This method finishes and then calls Validate()
70 SetParticles(particle_indices);
71 }
72 else
73 {
74 for (typename NodesOnlyMesh<DIM>::NodeIterator node_iter = rMesh.GetNodeIteratorBegin();
75 node_iter != rMesh.GetNodeIteratorEnd();
76 ++node_iter)
77 {
78 (*node_iter).SetIsParticle(false);
79 }
81 }
82}
83
84template<unsigned DIM>
89
90template<unsigned DIM>
92{
93 return this->GetNode(index)->IsParticle();
94}
95
96template<unsigned DIM>
98{
99 std::set<unsigned> particle_indices;
100
101 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
102 node_iter != this->mrMesh.GetNodeIteratorEnd();
103 ++node_iter)
104 {
105 if (node_iter->IsParticle())
106 {
107 particle_indices.insert(node_iter->GetIndex());
108 }
109 }
110
111 return particle_indices;
112}
113
114template<unsigned DIM>
115void NodeBasedCellPopulationWithParticles<DIM>::SetParticles(const std::set<unsigned>& rParticleIndices)
116{
117 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
118 node_iter != this->mrMesh.GetNodeIteratorEnd();
119 ++node_iter)
120 {
121 if (rParticleIndices.find(node_iter->GetIndex()) != rParticleIndices.end())
122 {
123 node_iter->SetIsParticle(true);
124 }
125 }
127}
128
129template<unsigned DIM>
133
134template<unsigned DIM>
135CellPtr NodeBasedCellPopulationWithParticles<DIM>::AddCell(CellPtr pNewCell, CellPtr pParentCell)
136{
137 assert(pNewCell);
138
139 // Add new cell to population
140 CellPtr p_created_cell = AbstractCentreBasedCellPopulation<DIM>::AddCell(pNewCell, pParentCell);
141 assert(p_created_cell == pNewCell);
142
143 // Then set the new cell radius in the NodesOnlyMesh
144 unsigned node_index = this->GetLocationIndexUsingCell(p_created_cell);
145 this->GetNode(node_index)->SetRadius(0.5);
146 this->GetNode(node_index)->SetIsParticle(false);
147
148 // Return pointer to new cell
149 return p_created_cell;
150}
151
152template<unsigned DIM>
154{
155 std::map<unsigned, bool> validated_nodes;
156 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
157 node_iter != this->mrMesh.GetNodeIteratorEnd();
158 ++node_iter)
159 {
160 validated_nodes[node_iter->GetIndex()] = node_iter->IsParticle();
161 }
162
163 // Look through all of the cells and record what node they are associated with.
164 for (typename AbstractCellPopulation<DIM>::Iterator cell_iter=this->Begin(); cell_iter!=this->End(); ++cell_iter)
165 {
166 unsigned node_index = this->GetLocationIndexUsingCell((*cell_iter));
167
168 // If the node attached to this cell is labelled as a particle, then throw an error
169 if (this->GetNode(node_index)->IsParticle())
170 {
171 EXCEPTION("Node " << node_index << " is labelled as a particle and has a cell attached");
172 }
173 validated_nodes[node_index] = true;
174 }
175
176 for (std::map<unsigned, bool>::iterator map_iter = validated_nodes.begin();
177 map_iter != validated_nodes.end();
178 map_iter++)
179 {
180 if (!map_iter->second)
181 {
182 EXCEPTION("Node " << map_iter->first << " does not appear to be a particle or has a cell associated with it");
183 }
184 }
185}
186
187template<unsigned DIM>
189{
190 for (typename AbstractMesh<DIM, DIM>::NodeIterator node_iter = this->rGetMesh().GetNodeIteratorBegin();
191 node_iter != this->rGetMesh().GetNodeIteratorEnd();
192 ++node_iter)
193 {
194 // If it isn't a particle then there might be cell writers attached
195 if (! this->IsParticle(node_iter->GetIndex()))
196 {
197 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
198 cell_writer_iter != this->mCellWriters.end();
199 ++cell_writer_iter)
200 {
201 CellPtr cell_from_node = this->GetCellUsingLocationIndex(node_iter->GetIndex());
202 this->AcceptCellWriter(*cell_writer_iter, cell_from_node);
203 }
204 }
205 }
206}
207
208template<unsigned DIM>
210{
211#ifdef CHASTE_VTK
212 // Store the present time as a string
213 std::stringstream time;
215
216 // Make sure the nodes are ordered contiguously in memory
217 NodeMap map(1 + this->mpNodesOnlyMesh->GetMaximumNodeIndex());
218 this->mpNodesOnlyMesh->ReMesh(map);
219
220 // Store the number of cells for which to output data to VTK
221 unsigned num_nodes = this->GetNumNodes();
222 std::vector<double> rank(num_nodes);
223 std::vector<double> particles(num_nodes);
224
225 unsigned num_cell_data_items = 0;
226 std::vector<std::string> cell_data_names;
227
228 // We assume that the first cell is representative of all cells
229 if (num_nodes > 0)
230 {
231 num_cell_data_items = this->Begin()->GetCellData()->GetNumItems();
232 cell_data_names = this->Begin()->GetCellData()->GetKeys();
233 }
234
235 std::vector<std::vector<double> > cell_data;
236 for (unsigned var=0; var<num_cell_data_items; var++)
237 {
238 std::vector<double> cell_data_var(num_nodes);
239 cell_data.push_back(cell_data_var);
240 }
241
242 // Create mesh writer for VTK output
243 VtkMeshWriter<DIM, DIM> mesh_writer(rDirectory, "results_"+time.str(), false);
244 mesh_writer.SetParallelFiles(*(this->mpNodesOnlyMesh));
245
246 // Iterate over any cell writers that are present
247 for (typename std::vector<boost::shared_ptr<AbstractCellWriter<DIM, DIM> > >::iterator cell_writer_iter = this->mCellWriters.begin();
248 cell_writer_iter != this->mCellWriters.end();
249 ++cell_writer_iter)
250 {
251 // Create vector to store VTK cell data
252 std::vector<double> vtk_cell_data(num_nodes);
253
254 // Loop over nodes
255 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
256 node_iter != this->mrMesh.GetNodeIteratorEnd();
257 ++node_iter)
258 {
259 unsigned node_index = node_iter->GetIndex();
260
261 // If this node is a particle (not a cell), then we set the 'dummy' VTK cell data for this to be -2.0...
262 if (this->IsParticle(node_index))
263 {
264 vtk_cell_data[node_index] = -2.0;
265 }
266 else
267 {
268 // ...otherwise we populate the vector of VTK cell data as usual
269 CellPtr p_cell = this->GetCellUsingLocationIndex(node_index);
270 vtk_cell_data[node_index] = (*cell_writer_iter)->GetCellDataForVtkOutput(p_cell, this);
271 }
272 }
273
274 mesh_writer.AddPointData((*cell_writer_iter)->GetVtkCellDataName(), vtk_cell_data);
275 }
276
277 // Loop over cells
278 for (typename AbstractCellPopulation<DIM>::Iterator cell_iter = this->Begin();
279 cell_iter != this->End();
280 ++cell_iter)
281 {
282 // Get the node index corresponding to this cell
283 unsigned global_index = this->GetLocationIndexUsingCell(*cell_iter);
284 unsigned node_index = this->rGetMesh().SolveNodeMapping(global_index);
285
286 for (unsigned var=0; var<num_cell_data_items; var++)
287 {
288 cell_data[var][node_index] = cell_iter->GetCellData()->GetItem(cell_data_names[var]);
289 }
290
291 rank[node_index] = (PetscTools::GetMyRank());
292 }
293
294 mesh_writer.AddPointData("Process rank", rank);
295
296 // Loop over nodes
297 for (typename AbstractMesh<DIM,DIM>::NodeIterator node_iter = this->mrMesh.GetNodeIteratorBegin();
298 node_iter != this->mrMesh.GetNodeIteratorEnd();
299 ++node_iter)
300 {
301 unsigned node_index = node_iter->GetIndex();
302 particles[node_index] = (double) (this->IsParticle(node_index));
303 }
304
305 mesh_writer.AddPointData("Non-particles", particles);
306
307 if (num_cell_data_items > 0)
308 {
309 for (unsigned var=0; var<cell_data.size(); var++)
310 {
311 mesh_writer.AddPointData(cell_data_names[var], cell_data[var]);
312 }
313 }
314
315 mesh_writer.WriteFilesUsingMesh(*(this->mpNodesOnlyMesh));
316
317 *(this->mpVtkMetaFile) << " <DataSet timestep=\"";
318 *(this->mpVtkMetaFile) << SimulationTime::Instance()->GetTimeStepsElapsed();
319 *(this->mpVtkMetaFile) << "\" group=\"\" part=\"0\" file=\"results_";
320 *(this->mpVtkMetaFile) << SimulationTime::Instance()->GetTimeStepsElapsed();
322 {
323 *(this->mpVtkMetaFile) << ".vtu\"/>\n";
324 }
325/* {
326 // Parallel vtu files .vtu -> .pvtu
327 *(this->mpVtkMetaFile) << ".pvtu\"/>\n";
328 }*/
329#endif //CHASTE_VTK
330}
331
332template<unsigned DIM>
334{
335 // Call method on direct parent class
337}
338
339// Explicit instantiation
343
344// Serialization for Boost >= 1.36
#define EXCEPTION(message)
#define EXCEPT_IF_NOT(test)
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell=CellPtr())
NodeIterator GetNodeIteratorEnd()
NodeIterator GetNodeIteratorBegin(bool skipDeletedNodes=true)
CellPtr AddCell(CellPtr pNewCell, CellPtr pParentCell)
virtual void WriteVtkResultsToFile(const std::string &rDirectory)
void SetParticles(const std::set< unsigned > &rParticleIndices)
NodeBasedCellPopulationWithParticles(NodesOnlyMesh< DIM > &rMesh, std::vector< CellPtr > &rCells, const std::vector< unsigned > locationIndices=std::vector< unsigned >(), bool deleteMesh=false)
void OutputCellPopulationParameters(out_stream &rParamsFile)
static bool IsSequential()
static unsigned GetMyRank()
static SimulationTime * Instance()
unsigned GetTimeStepsElapsed() const
void AddPointData(std::string name, std::vector< double > data)
void SetParallelFiles(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)